# typed: false # frozen_string_literal: true class AppController < ApplicationController include Authable include Constants::PlugApp include PathParameter::AppParameters include BodyParameter::AppParameters before_action :from__app_? # Inbound message from ${App} def webhook # Error if necesary parameters from ${App} are missing return bad_request unless has_inbound__app__params? response = YettoService.get_plug_installation(pparam_organization_id, pparam_inbox_id, pparam_plug_installation_id) # Error if Yetto is down return service_unavailable(response) if response.unavailable? plug_installation = response.parse_json_body installed_on_inbox = plug_installation.fetch("installed_on_inbox", {}) organization = installed_on_inbox.fetch("organization", {}) # Bail if the organization is not active return forbidden unless organization.fetch("status", "") == "active" plug_id = plug_installation.fetch("plug", {}).fetch("id", "") inbox_id = installed_on_inbox.fetch("id", "") organization_id = organization.fetch("id", "") return bad_request if plug_id.blank? || inbox_id.blank? # Send the message to Yetto update_data = { type: "create_message", inbox: { id: inbox_id }, organization: { id: organization_id }, payload: { creator: { id: plug_id, }, message: { title: title, text_content: text_body, is_public: true, author: { name: from_email, }, attachments: bparam_attachments, metadata: { cc_addresses: cc_addresses, postmark_message_id: bparam_message_id, email_message_id: email_message_id, }, }, }, } UpdateYettoJob.perform_later(update_data) created end def process_inbound no_content end end