Files
docuseal/app/models/webhook_url.rb
T

63 lines
1.5 KiB
Ruby
Raw Normal View History

2024-07-14 21:38:24 +03:00
# frozen_string_literal: true
# == Schema Information
#
# Table name: webhook_urls
#
2026-05-06 11:45:35 +03:00
# id :bigint not null, primary key
# events :text not null
# hmac_secret :text not null
# secret :text not null
# sha1 :string not null
# url :text not null
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint not null
2024-07-14 21:38:24 +03:00
#
# Indexes
#
# index_webhook_urls_on_account_id (account_id)
# index_webhook_urls_on_sha1 (sha1)
#
# Foreign Keys
#
# fk_rails_... (account_id => accounts.id)
#
class WebhookUrl < ApplicationRecord
EVENTS = %w[
form.viewed
form.started
form.completed
form.declined
submission.created
2025-03-10 18:27:35 +02:00
submission.completed
submission.expired
submission.archived
2025-03-10 18:27:35 +02:00
template.created
template.updated
2026-03-18 08:46:19 +02:00
template.archived
].freeze
2024-07-14 21:38:24 +03:00
belongs_to :account
2025-07-04 21:11:08 +03:00
has_many :webhook_events, dependent: nil
2024-07-14 21:38:24 +03:00
attribute :events, :string, default: -> { %w[form.viewed form.started form.completed form.declined] }
2024-11-01 15:35:02 +02:00
attribute :secret, :string, default: -> { {} }
2024-07-14 21:38:24 +03:00
serialize :events, coder: JSON
serialize :secret, coder: JSON
2024-07-14 21:38:24 +03:00
before_validation :set_sha1
2026-05-06 11:45:35 +03:00
before_validation :set_hmac_secret
2024-07-14 21:38:24 +03:00
2026-05-06 11:45:35 +03:00
encrypts :url, :secret, :hmac_secret
2024-07-14 21:38:24 +03:00
def set_sha1
self.sha1 = Digest::SHA1.hexdigest(url)
end
2026-05-06 11:45:35 +03:00
def set_hmac_secret
self.hmac_secret ||= WebhookUrls::Signatures.generate_secret
end
2024-07-14 21:38:24 +03:00
end