module Lifen class Communication < Base attribute :uuid, String attribute :sender, Lifen::User attribute :recipient, Lifen::User attribute :category, Lifen::Category, default: Lifen::Category.new attribute :channel, Lifen::Channel attribute :patient, Lifen::Patient attribute :attachment, Lifen::Attachment attribute :binary, Lifen::Binary attribute :content_string, Lifen::ContentString attribute :status, String attribute :sent_at, DateTime attribute :received_at, DateTime def send json = application_client.post("fhir/CommunicationRequest", fhir_payload) self.uuid = json["id"] self end def self.find(uuid) json = application_client.get("fhir/Communication/#{uuid}") resource_type = json["resourceType"] raise Error, "Invalid resourceType (#{resource_type})" if resource_type != "Communication" attributes = {} attributes[:uuid] = json["id"] attributes[:status] = json["status"] attributes[:sent_at] = json["sent"] attributes[:received_at] = json["received"] attributes[:sender] = User.from_json(json["sender"]) attributes[:recipient] = User.from_json(Array(json["recipient"]).first) new(attributes) end private def fhir_payload payload = { resourceType: "CommunicationRequest", sender: [ sender.fhir_payload ], recipient: [ recipient.fhir_payload ], contained: [ channel.fhir_payload(recipient) ], 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 def self.application_client @application_client ||= AppAuthenticatedClient.new end end end