Skip to content

Commit

Permalink
Add inbound webhooks model
Browse files Browse the repository at this point in the history
  • Loading branch information
ancorcruz committed Dec 17, 2024
1 parent ccf73e0 commit 6997b66
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
35 changes: 35 additions & 0 deletions app/models/inbound_webhook.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

class InboundWebhook < ApplicationRecord
belongs_to :organization

validates :event_type, :payload, :source, :status, presence: true

STATUSES = {pending: "pending"}

enum :status, STATUSES
end

# == Schema Information
#
# Table name: inbound_webhooks
#
# id :uuid not null, primary key
# code :string
# event_type :string not null
# payload :jsonb not null
# signature :string
# source :string not null
# status :string default("pending"), not null
# created_at :datetime not null
# updated_at :datetime not null
# organization_id :uuid not null
#
# Indexes
#
# index_inbound_webhooks_on_organization_id (organization_id)
#
# Foreign Keys
#
# fk_rails_... (organization_id => organizations.id)
#
17 changes: 17 additions & 0 deletions db/migrate/20241213182343_create_inbound_webhooks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

class CreateInboundWebhooks < ActiveRecord::Migration[7.1]
def change
create_table :inbound_webhooks, id: :uuid do |t|
t.string :source, null: false
t.string :event_type, null: false
t.jsonb :payload, null: false
t.string :status, null: false, default: 'pending'
t.belongs_to :organization, null: false, foreign_key: true, type: :uuid, index: true
t.string :code
t.string :signature

t.timestamps
end
end
end
14 changes: 14 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions spec/factories/inbound_webhooks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

FactoryBot.define do
factory :inbound_webhook do
organization

source { "stripe" }
event_type { "payment_intent.succeeded" }
status { "pending" }
code { "webhook-endpoint-code" }
signature { "MySignature" }

payload do
File.read(Rails.root.join('spec/fixtures/stripe/payment_intent_event.json'))
end
end
end
16 changes: 16 additions & 0 deletions spec/models/inbound_webhook_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe InboundWebhook, type: :model do
subject(:inbound_webhook) { build(:inbound_webhook) }

it { is_expected.to belong_to(:organization) }

it { is_expected.to validate_presence_of(:event_type) }
it { is_expected.to validate_presence_of(:payload) }
it { is_expected.to validate_presence_of(:source) }
it { is_expected.to validate_presence_of(:status) }

it { is_expected.to be_pending }
end

0 comments on commit 6997b66

Please sign in to comment.