lib/lifen/flow.rb in lifen-0.2.0 vs lib/lifen/flow.rb in lifen-0.2.1

- old
+ new

@@ -5,17 +5,39 @@ attribute :title, String attribute :user, Lifen::User attribute :active_users, Array[Lifen::User] def create - json = client.post("central/api/chats", {title: title}) + params = {title: title} - flow = self.class.new(json.first) + 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 @@ -25,9 +47,12 @@ 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 \ No newline at end of file