module Lifen class CommunicationRequest < Base attribute :uuid, String attribute :number_communications, Integer attribute :sender, Lifen::Practitioner attribute :recipients, [Lifen::Practitioner] attribute :category, Lifen::Category, default: Lifen::Category.new attribute :medium, [Lifen::Medium] attribute :patient, Lifen::Patient attribute :attachment, Lifen::Attachment attribute :binary, Lifen::Binary attribute :content_string, Lifen::ContentString def send json = application_client.post("fhir/CommunicationRequest", fhir_payload) self.uuid = json["id"] self.number_communications = Array(json["issue"]).length self end private def fhir_payload payload = { resourceType: "CommunicationRequest", sender: [ sender.fhir_payload ], recipient: recipients.map(&:fhir_payload), contained: [], medium: medium.map(&:fhir_payload) , category: [ category.fhir_payload], payload: [ document_content ] } if patient payload[:contained] << patient.fhir_payload payload[:subject] = [ {reference: "patient"} ] end if content_string payload[:payload] << content_string.fhir_payload end payload end def document_content if !attachment.nil? attachment.fhir_payload else binary.fhir_payload end end def application_client @application_client ||= AppAuthenticatedClient.new end end end