lib/wolf_core/application/integrations/webhooks_operations.rb



module WolfCore
  module Integrations
    module WebhooksOperations
      def get_payload(event_type:, params:)
        return if event_type == 'SubscriptionConfirmation'
        params = JSON.parse(params['Message'] || '{}')
        params['payload'] = JSON.parse(params['payload'] || '{}')
      end

      def get_event_type(params:)
        params['Type']
      end

      def get_event_name(params:)
        message = JSON.parse(params['Message'] || {})
        message['event_name']
      end

      def validate_user(event_type:, params:)
        return if event_type == 'SubscriptionConfirmation'

        user_id = params.dig('invoker', 'user_id').to_s
        return unless user_id == ENV['WOLF_ADMIN_ID'].to_s

        raise_service_error("Ignoring event from user id #{user_id}")
      end

      def subscription_confirmation_request(url:)
        response = safe_http_get(
          url: url,
          error_message: "Can not confirm subscription",
        )
        response
      end

      def build_body_from_params(params:, id_key:)
        params.slice(
          'event_name', 'payload'
        ).merge({ id_key => params['object_id'] })
      end
    end
  end
end