From 85c98c6d3e998bdb761fc6c36032fac426cfaf0d Mon Sep 17 00:00:00 2001 From: David Cook Date: Fri, 16 Sep 2022 15:16:27 +1000 Subject: [PATCH] 2. Add model WebhookEndpoint [migration] This will store the URL for each user that wants a notification. We probably don't need URL validation (it's not done on Enterprise for example). It could be validated by browser input, and anyway will be validated if the webhook actually works or not. Inspired by Keygen: https://keygen.sh/blog/how-to-build-a-webhook-system-in-rails-using-sidekiq/ --- app/models/webhook_endpoint.rb | 6 ++++++ db/migrate/20221028051650_create_webhook_endpoints.rb | 11 +++++++++++ db/schema.rb | 6 ++++++ spec/models/webhook_endpoint_spec.rb | 9 +++++++++ 4 files changed, 32 insertions(+) create mode 100644 app/models/webhook_endpoint.rb create mode 100644 db/migrate/20221028051650_create_webhook_endpoints.rb create mode 100644 spec/models/webhook_endpoint_spec.rb diff --git a/app/models/webhook_endpoint.rb b/app/models/webhook_endpoint.rb new file mode 100644 index 0000000000..8626e11b93 --- /dev/null +++ b/app/models/webhook_endpoint.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +# Records a webhook url to send notifications to +class WebhookEndpoint < ApplicationRecord + validates :url, presence: true +end diff --git a/db/migrate/20221028051650_create_webhook_endpoints.rb b/db/migrate/20221028051650_create_webhook_endpoints.rb new file mode 100644 index 0000000000..034d1141d2 --- /dev/null +++ b/db/migrate/20221028051650_create_webhook_endpoints.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class CreateWebhookEndpoints < ActiveRecord::Migration[6.1] + def change + create_table :webhook_endpoints do |t| + t.string :url, null: false + + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 77975612f5..d28ee2ba80 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1189,6 +1189,12 @@ ActiveRecord::Schema.define(version: 2023_02_13_160135) do t.index ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id" end + create_table "webhook_endpoints", force: :cascade do |t| + t.string "url", null: false + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + end + add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" add_foreign_key "adjustment_metadata", "enterprises", name: "adjustment_metadata_enterprise_id_fk" diff --git a/spec/models/webhook_endpoint_spec.rb b/spec/models/webhook_endpoint_spec.rb new file mode 100644 index 0000000000..5f1d630f09 --- /dev/null +++ b/spec/models/webhook_endpoint_spec.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe WebhookEndpoint, type: :model do + describe "validations" do + it { is_expected.to validate_presence_of(:url) } + end +end