aboutsummaryrefslogblamecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/formats/json/patient/trial/ApiPartialPatientCriterionList.scala
blob: b7616a1485bc5ca7d4f0263e49a2a820b510fda2 (plain) (tree)
1
2
3
4
5
6
7
8

                                                         
                                           



                                                         
                                            


                                                                                    

                                                                                  


                                                              
                                                                         








                                                                   


                                                                  

                                                                 


                         
                                                     
                                                                                         
 
package xyz.driver.pdsuidomain.formats.json.patient.trial

import xyz.driver.pdsuicommon.domain.LongId
import xyz.driver.pdsuidomain.entities.PatientCriterion
import play.api.data.validation.ValidationError
import play.api.libs.functional.syntax._
import play.api.libs.json.{Format, JsPath, Reads, Writes}
import xyz.driver.entities.labels.LabelValue
import xyz.driver.pdsuidomain.services.PatientCriterionService.DraftPatientCriterion

final case class ApiPartialPatientCriterionList(id: Long,
                                                eligibilityStatus: Option[String],
                                                isVerified: Option[Boolean]) {

  def toDomain: DraftPatientCriterion = DraftPatientCriterion(
    id = LongId[PatientCriterion](id),
    eligibilityStatus = eligibilityStatus.flatMap(LabelValue.fromString),
    isVerified = isVerified
  )
}

object ApiPartialPatientCriterionList {

  implicit val format: Format[ApiPartialPatientCriterionList] = (
    (JsPath \ "id").format[Long] and
      (JsPath \ "eligibilityStatus").formatNullable[String](Format(
        Reads
          .of[String]
          .filter(ValidationError("unknown eligibility status"))({
            case x if LabelValue.fromString(x).isDefined => true
            case _                                       => false
          }),
        Writes.of[String]
      )) and
      (JsPath \ "isVerified").formatNullable[Boolean]
  )(ApiPartialPatientCriterionList.apply, unlift(ApiPartialPatientCriterionList.unapply))
}