module Lifen class Flow < Base attribute :uuid, String attribute :title, String attribute :user, Lifen::User attribute :active_users, Array[Lifen::User] def create params = {title: title} users_to_attach = [] if !active_users.empty? users_to_attach = active_users params[:users] = active_users.map(&:uuid) end json = client.post("central/api/chats?rel=activeUsers", params) json_flow = json.first flow = self.class.new(json_flow) self.user = user self.uuid = flow.uuid self.active_users = [] Array(json_flow["activeUsers"]).each do |element| element[:first_name] = element["firstName"] element[:last_name] = element["lastName"] self.active_users << Lifen::User.new(element) end users_to_attach.each do |user| raise Lifen::Error, "User #{user.uuid} was not attached to this flow" if !active_users.map(&:uuid).include? user.uuid end self.title = flow.title self end def attach_users!(users) users = Array(users) params = users.map(&:uuid).compact.uniq json = client.post("central/api/chats/#{uuid}/attach_users?rel=activeUsers", params) Array(json["activeUsers"]).each do |element| element[:first_name] = element["firstName"] element[:last_name] = element["lastName"] self.active_users << Lifen::User.new(element) end users.each do |user| raise Lifen::Error, "User #{user.uuid} was not attached to this flow" if !active_users.map(&:uuid).include? user.uuid end self end def detach_users!(users) users = Array(users) params = users.map(&:uuid).compact.uniq json = client.post("central/api/chats/#{uuid}/detach_users?rel=activeUsers", params) self.active_users = [] Array(json["activeUsers"]).each do |element| self.active_users << Lifen::User.new(element) end users.each do |user| raise Lifen::Error, "User #{user.uuid} was not detached to this flow" if active_users.map(&:uuid).include? user.uuid end self end private def client @client ||= user.client end end end