module Support
  module Services
    class OwsAbacusEvent
      include HTTParty

      base_uri DOMAINS[:ows_abacus_event]

      class << self
        def post_abacus_event(body)
          path = "/abacus-event"

          response = post(path, headers: { "Content-Type" => "application/json" }, body: body.to_json)

          verify_success(response)
          response
        end

        def generate_abacus_event(accounting_run_id)
          { event_date: DateTime.now.strftime("%Y-%m-%dT%H:%M:%S.%L%z"),
            event_name: "commit",
            target_id: accounting_run_id,
            target_type: "accounting_run" }
        end

        private

        def verify_success(response)
          return if response.code == 201
          fail(
            "request to ows-abacus-event did not succeed:"\
          " #{JSON.parse(response.body)}"
          )
        end
      end
    end
  end
end
