module Lifen class User include Virtus.model(finalize: false) attribute :token, "Lifen::Token" attribute :status, "Lifen::Status" attribute :settings, "Lifen::Settings" attribute :channels, ["Lifen::Channel"] attribute :uuid, String attribute :email, String attribute :last_name, String attribute :first_name, String attribute :profile_picture_url, String attribute :rpps, String @@create_lock = Mutex.new def flows Lifen::Flows.new(user: self).all end def create(persisted_lifen_uuid: ->(user) {}, save_to_db: ->(user) {}) params = {emailAddress: email, lastName: last_name, firstName: first_name, profilePicUrl: profile_picture_url} @@create_lock.synchronize do exisiting_uuid = persisted_lifen_uuid.call(self) if exisiting_uuid.nil? json = application_client.post("authentication/api/register/third_party", params) self.uuid = json["accountUuid"] else self.uuid = exisiting_uuid end save_to_db.call(self) end end def self.find(uuid) json = application_client.get("docbook/api/thirdParty/person/#{uuid}") json[:uuid] = json["uuid"] json[:email] = json["emailAddress"] json[:first_name] = json["firstName"] json[:last_name] = json["lastName"] json[:profile_picture_url] = json["profilePicUrl"] new(json) end def reload self.class.find(uuid) end def save params = {emailAddress: email, lastName: last_name, firstName: first_name, profilePicUrl: profile_picture_url} params[:uuid] = uuid json = application_client.put("docbook/api/thirdParty/person/", params) self.email = json["emailAddress"] self.first_name = json["firstName"] self.last_name = json["lastName"] self.profile_picture_url = json["profilePicUrl"] self end def unread_messages status.unread end def token @token ||= Lifen::Token.new(user: self) end def token=(token) token.user = self @token = token end def status @status ||= Lifen::Status.new(user: self).reload end def settings @settings ||= Lifen::Settings.new(user: self).reload end def client UserAuthenticatedClient.new(token) end # def self.where(params) # json = application_client.get("fhir/Practitioner/", params) # users = [] # users << new(user_json) # users # end private def application_client @application_client ||= AppAuthenticatedClient.new end def self.application_client @application_client ||= AppAuthenticatedClient.new end end end