# frozen_string_literal: true module Qismo module Resources module RoomResource # # List rooms # # @param params [Hash] # # @return [Array] # def list(params = {}) resp = Qismo.client.get("/api/v2/customer_rooms", params) body = resp.http_body customer_rooms = body.dig("data", "customer_rooms") rooms = [] customer_rooms.each do |cr| rooms.append(Room.new(cr)) end rooms end # # Get room by id # # @param id [Integer] # # @return [Room] # def get(id) resp = Qismo.client.get("/api/v2/customer_rooms/#{id}") body = resp.http_body room = body.dig("data", "customer_room") if room == {} raise NotFound.new( "Room with id #{id} is not found", http_code: 404, http_headers: resp.http_headers, http_body: body, http_raw_body: resp.http_raw_body ) end Room.new(room) end # # Assign an agent to a room # # @param room_id [String] # @param agent_id [Integer] # @param options [Hash] # # @return [Agent] # def assign_agent(room_id, agent_id, options = {}) options[:room_id] = room_id.to_s options[:agent_id] = agent_id resp = Qismo.client.post("/api/v1/admin/service/assign_agent", options) body = resp.http_body added_agent = body.dig("data", "added_agent") Agent.new(added_agent) end # # List agents who can be assigned to a room # # @param options [Hash] # @option options [Integer, String] :room_id Required # @option options [Integer, String] :limit Optional. Default: 10. Max: 25 # @option options [String] :cursor_before # @option options [String] :cursor_after # @option options [String] :search Filter agents by name or email # # @return [Array] # def other_agents(options = {}) validate_existence_of!(:room_id) resp = Qismo.client.call(:get, "/api/v2/admin/service/other_agents", params: options) body = resp.http_body qismo_agents = body.dig("data", "agents") agents = [] qismo_agents.each do |qismo_agent| agents.append(Agent.new(qismo_agent)) end agents end alias_method :eligible_agents, :other_agents # # Resolve room # # @param options [Hash] # @option options room_id [String] # @option options notes [String] # @option options last_comment_id [Integer] # @option options is_send_email [TruClass, FalseClass] # @option options extras optional [Hash] # # @return [Response] # def resolve(options = {}) validate_existence_of!(:room_id, on: options) fetch_last_comment_id = lambda do resp = Qismo.client.raw_request(:get, "https://api.qiscus.com/api/v2.1/rest/load_comments", { headers: { qiscus_sdk_app_id: Qismo.client.app_id, qiscus_sdk_secret: Qismo.client.secret_key }, params: { room_id: options[:room_id], limit: 1 }, }) body = resp.http_body if body.empty? raise BadRequest.new( "There is no message in room #{options[:room_id]}", http_code: 200, http_headers: resp.http_headers, http_body: resp.http_body, http_raw_body: resp.http_raw_body ) end id = body.dig("results", "comments", 0, "id") if id.nil? raise BadRequest.new( "There is no message in room #{options[:room_id]}", http_code: 200, http_headers: resp.http_headers, http_body: resp.http_body, http_raw_body: resp.http_raw_body ) end id end if falsy?(options[:last_comment_id]) options[:last_comment_id] = fetch_last_comment_id.call end validate_existence_of!(:last_comment_id, :notes, :is_send_email, :extras, on: options) options[:room_id] = options[:room_id].to_i Qismo.client.post(:post, "/api/v1/admin/service/mark_as_resolved", options) end # # Activate bot in a room # # @param id [Integer] Room id # # @return [Response] # def activate_bot(id) Qismo.client.post("/bot/#{id}/activate", is_active: true) end # # Deactivate bot in a room # # @param id [Integer] Room id # # @return [Response] # def deactivate_bot(id) Qismo.client.post("/bot/#{id}/activate", is_active: true) end end end end