aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/services/LinkedPatientService.scala
blob: 8fc1662d6ae5ecb2ca40229ba37d3e1b90c1584c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package xyz.driver.pdsuidomain.services

import xyz.driver.pdsuicommon.domain._
import xyz.driver.pdsuicommon.error.DomainError
import xyz.driver.pdsuicommon.logging._
import xyz.driver.pdsuidomain.entities.{LinkedPatient, Patient, Trial}

import scala.concurrent.Future

object LinkedPatientService {

  trait DefaultTrialNotFoundError {
    def userMessage: String = "Trial not found"
  }

  trait DefaultPatientNotFoundError {
    def userMessage: String = "Patient not found"
  }

  final case class RichLinkedPatient(email: Email,
                                     name: String,
                                     patientId: UuidId[Patient],
                                     trialId: StringId[Trial]) {
    def toLinkedPatient(user: User) = LinkedPatient(
      userId = user.id,
      patientId = patientId,
      trialId = trialId
    )
  }

  object RichLinkedPatient {
    implicit def toPhiString(x: RichLinkedPatient): PhiString = {
      import x._
      phi"RichLinkedPatient(email=${Unsafe(email)}, patientId=$patientId, trialId=$trialId)"
    }
  }

  sealed trait CreateReply
  object CreateReply {
    type Error = CreateReply with DomainError

    /**
      * @param createdUser None if a user was created before
      */
    final case class Created(x: RichLinkedPatient, createdUser: Option[User]) extends CreateReply

    case object PatientNotFoundError
      extends CreateReply with DefaultPatientNotFoundError with DomainError.NotFoundError

    case object TrialNotFoundError
      extends CreateReply with DefaultPatientNotFoundError with DomainError.NotFoundError

    final case class CommonError(userMessage: String) extends CreateReply with DomainError
  }
}

trait LinkedPatientService {

  import LinkedPatientService._

  def create(entity: RichLinkedPatient): Future[CreateReply]
}