aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/formats/json/export/ApiExportPatientLabelEvidence.scala
blob: d14176295799a902f075fea775f14335adf48dcb (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
package xyz.driver.pdsuidomain.formats.json.export

import play.api.libs.functional.syntax._
import play.api.libs.json._
import xyz.driver.pdsuicommon.domain.{FuzzyValue, LongId}
import xyz.driver.pdsuidomain.entities.export.patient.ExportPatientLabelEvidence

final case class ApiExportPatientLabelEvidence(evidenceId: String,
                                               labelValue: String,
                                               evidenceText: String,
                                               document: ApiExportPatientLabelEvidenceDocument) {

  def toDomain = ExportPatientLabelEvidence(
    id = LongId(this.evidenceId.toLong),
    value = FuzzyValue.fromString
      .applyOrElse(this.labelValue, (s: String) => throw new NoSuchElementException(s"Unknown fuzzy value $s")),
    evidenceText = this.evidenceText,
    document = this.document.toDomain
  )

}

object ApiExportPatientLabelEvidence {

  implicit val format: Format[ApiExportPatientLabelEvidence] = (
    (JsPath \ "evidenceId").format[String] and
      (JsPath \ "labelValue").format[String](Writes[String](x => JsString(x.toUpperCase))) and
      (JsPath \ "evidenceText").format[String] and
      (JsPath \ "document").format[ApiExportPatientLabelEvidenceDocument]
  )(ApiExportPatientLabelEvidence.apply, unlift(ApiExportPatientLabelEvidence.unapply))

  def fromDomain(evidence: ExportPatientLabelEvidence) = ApiExportPatientLabelEvidence(
    evidenceId = evidence.id.toString,
    labelValue = FuzzyValue.valueToString(evidence.value),
    evidenceText = evidence.evidenceText,
    document = ApiExportPatientLabelEvidenceDocument.fromDomain(evidence.document)
  )
}