diff --git a/app/models/webhook_endpoint.rb b/app/models/webhook_endpoint.rb index 8626e11b93..4c5d45dcef 100644 --- a/app/models/webhook_endpoint.rb +++ b/app/models/webhook_endpoint.rb @@ -2,5 +2,8 @@ # Records a webhook url to send notifications to class WebhookEndpoint < ApplicationRecord + WEBHOOK_TYPES = %w(order_cycle_opened payment_status_changed).freeze + validates :url, presence: true + validates :webhook_type, presence: true, inclusion: { in: WEBHOOK_TYPES } end diff --git a/db/migrate/20251124043324_add_type_to_webhook_endpoints.rb b/db/migrate/20251124043324_add_type_to_webhook_endpoints.rb new file mode 100644 index 0000000000..d4dd6238f7 --- /dev/null +++ b/db/migrate/20251124043324_add_type_to_webhook_endpoints.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +class AddTypeToWebhookEndpoints < ActiveRecord::Migration[7.1] + def up + # Using "order_cycle_opened" as default will update existing record + change_table(:webhook_endpoints, bulk: true) do |t| + t.column :webhook_type, :string, limit: 255, null: false, default: "order_cycle_opened" + end + # Drop the default value + change_column_default :webhook_endpoints, :webhook_type, nil + end + + def down + remove_column :webhook_endpoints, :webhook_type + end +end diff --git a/db/schema.rb b/db/schema.rb index acbc0d48cd..9ebfe7a970 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1143,6 +1143,7 @@ ActiveRecord::Schema[7.1].define(version: 2025_11_26_005628) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.bigint "user_id", default: 0, null: false + t.string "webhook_type", limit: 255, null: false t.index ["user_id"], name: "index_webhook_endpoints_on_user_id" end diff --git a/spec/models/webhook_endpoint_spec.rb b/spec/models/webhook_endpoint_spec.rb index 2aba887175..30b3ab4395 100644 --- a/spec/models/webhook_endpoint_spec.rb +++ b/spec/models/webhook_endpoint_spec.rb @@ -5,5 +5,9 @@ require 'spec_helper' RSpec.describe WebhookEndpoint do describe "validations" do it { is_expected.to validate_presence_of(:url) } + it { + is_expected.to validate_inclusion_of(:webhook_type) + .in_array(%w(order_cycle_opened payment_status_changed)) + } end end