aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/patientlabel.scala
blob: e29b9fdafc185dfbb98f55185117764a9953f154 (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
63
64
65
66
package xyz.driver.pdsuidomain.formats.json.sprayformats

import spray.json._
import xyz.driver.pdsuicommon.domain.FuzzyValue
import xyz.driver.pdsuidomain.entities._

object patientlabel {
  import DefaultJsonProtocol._
  import common._

  def applyUpdateToPatientLabel(json: JsValue, orig: PatientLabel): PatientLabel = json match {
    case JsObject(fields) =>
      val primaryValue = if (fields.contains("primaryValue")) {
        fields
          .get("primaryValue")
          .map(_.convertTo[FuzzyValue])
      } else orig.primaryValue

      val verifiedPrimaryValue = if (fields.contains("verifiedPrimaryValue")) {
        fields
          .get("verifiedPrimaryValue")
          .map(_.convertTo[FuzzyValue])
      } else orig.verifiedPrimaryValue

      orig.copy(
        primaryValue = primaryValue,
        verifiedPrimaryValue = verifiedPrimaryValue
      )

    case _ => deserializationError(s"Expected Json Object as PatientLabel, but got $json")
  }

  implicit val patientLabelWriter: JsonWriter[(PatientLabel, Boolean)] = new JsonWriter[(PatientLabel, Boolean)] {
    override def write(obj: (PatientLabel, Boolean)): JsValue = {
      val patientLabel = obj._1
      val isVerified   = obj._2
      JsObject(
        "id"                   -> patientLabel.id.toJson,
        "labelId"              -> patientLabel.labelId.toJson,
        "primaryValue"         -> patientLabel.primaryValue.toJson,
        "verifiedPrimaryValue" -> patientLabel.verifiedPrimaryValue.toJson,
        "score"                -> patientLabel.score.toJson,
        "isImplicitMatch"      -> patientLabel.isImplicitMatch.toJson,
        "isVisible"            -> patientLabel.isVisible.toJson,
        "isVerified"           -> isVerified.toJson
      )
    }
  }

  implicit val patientLabelEvidenceWriter: JsonWriter[PatientLabelEvidenceView] =
    new JsonWriter[PatientLabelEvidenceView] {
      override def write(evidence: PatientLabelEvidenceView): JsValue =
        JsObject(
          "id"           -> evidence.id.toJson,
          "value"        -> evidence.value.toJson,
          "evidenceText" -> evidence.evidenceText.toJson,
          "documentId"   -> evidence.documentId.toJson,
          "evidenceId"   -> evidence.evidenceId.toJson,
          "reportId"     -> evidence.isImplicitMatch.toJson,
          "documentType" -> evidence.documentType.toJson,
          "date"         -> evidence.date.toJson,
          "providerType" -> evidence.providerType.toJson
        )
    }

}