module Lifen class Practitioner < Base attribute :channels, [Lifen::Channel] attribute :uuid, String attribute :last_name, String attribute :first_name, String attribute :rpps, String def fhir_payload { reference: "Practitioner/#{uuid}" } end def self.find_by_rpps(rpps) json = application_client.get("fhir/Practitioner/?identifier=#{rpps}") raise "Practitioner not found" if Array(json["entry"]).size != 1 user_json = Array(json["entry"]).first.fetch("resource") { {} } user_json[:uuid] = user_json["id"] user = new(user_json) Array(user_json["telecom"]).each do |telecom_json| user.channels << Lifen::Channel.from_json(telecom_json, "telecom") end Array(user_json["address"]).each do |address_json| user.channels << Lifen::Channel.from_json(address_json, "address") end user end def create_address(params) filtered_params = {"resourceType" => "Practitioner"} address = { "line": Array(params[:lines]), "city": params[:city], "postalCode": params[:postal_code], "country": params[:country] } filtered_params[params[:type]] = address json = application_client.post("fhir/Practitioner/#{uuid}/$add-address", filtered_params) channel = Channel.new(uuid: json["issue"][0]["id"], type: params[:type], value: "#{Array(params[:lines]).join(", ")}, #{params[:postal_code]} #{params[:city]}") self.channels << channel channel end def self.from_json(json) reference = json["reference"] uuid = reference.gsub("Practitioner/", "") new(uuid: uuid) end def create_telecom(params) filtered_params = {"resourceType" => "Practitioner"} telecom = { "system": params[:system], "value": params[:value] } filtered_params[params[:type]] = telecom json = application_client.post("fhir/Practitioner/#{uuid}/$add-telecom", filtered_params) channel = Channel.new(uuid: json["issue"][0]["id"], type: params[:type], value: params[:value]) self.channels << channel channel end private def application_client @application_client ||= AppAuthenticatedClient.new end def self.application_client @application_client ||= AppAuthenticatedClient.new end end end