Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Webflow dynamic recipient #38

Merged
merged 5 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 23 additions & 61 deletions lib/digitaltolk/webflow_service.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
class Digitaltolk::WebflowService
attr_accessor :params

INFO_EMAIL = '[email protected]'.freeze
attr_accessor :params, :errors

def initialize(params)
@params = params
@errors = []
end

def perform
unless form_submission?
Rails.logger.error "Error: webflow trigger type not supported '#{trigger_event}'"
return
if valid?
process_webflow_submission
else
Rails.logger.error @errors.join(',')
end

process_webflow_submission
rescue StandardError => e
errmsg = ['webflow_error', e.message.to_s, e.backtrace.first.to_s].join(',')
Rails.logger.error errmsg
@errors << ['webflow_error', e.message.to_s, e.backtrace.first.to_s].join(',')
Rails.logger.error @errors.join(',')
end

private

def process_webflow_submission
if from_email.blank?
Rails.logger.warn 'blank from email'
return
end
def valid?
@errors << "Error: webflow trigger type not supported '#{trigger_event}'" unless form_submission?

@errors << 'blank from email' if from_email.blank?

@errors.blank?
end

def valid_email?(email)
regex = /\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/
email.match?(regex)
end

def process_webflow_submission
emails.each_with_index do |recipient_email, index|
next if recipient_email.blank?
next unless valid_email?(recipient_email)

second_interval = (5 * (index + 1))
Rails.logger.warn "Sent webform email for #{from_email}"
Expand All @@ -38,7 +45,7 @@ def process_webflow_submission
end

def emails
[INFO_EMAIL]
form_data['recipient'].to_s.split(',').map(&:strip)
end

def webflow_params
Expand Down Expand Up @@ -78,49 +85,4 @@ def trigger_event
def form_submission?
trigger_event.to_s.casecmp('form_submission').zero?
end

# def test_webhook_data
# data = {
# "params": {
# "triggerType": "form_submission",
# "payload": {
# "name": "Info kontakt",
# "siteId": "6476e981f6a8eff9068d9af2",
# "data": {
# "Name-kom": "Test JM",
# "Email-kom": "[email protected]",
# "phone-kom": "",
# "Bok.nr.": "#3364311",
# "Subject": "Mejl för tolk",
# "Meddelande": "Hej! Har bokat tolk men ser inte tolkens mejladress någonstans. Behöver den för att skicka video länk."
# },
# "submittedAt": "2024-02-01T15:35:45.897Z",
# "id": "65bbba510f02b427c7024707",
# "formId": "64dd32123cda9a590ffbf718"
# },
# "controller": "webhooks/webflow",
# "action": "process_payload",
# "webflow": {
# "triggerType": "form_submission",
# "payload": {
# "name": "Info kontakt",
# "siteId": "6476e981f6a8eff9068d9af2",
# "data": {
# "Name-kom": "Test JM",
# "Email-kom": "[email protected]",
# "phone-kom": "",
# "Bok.nr.": "#3364311",
# "Subject": "Mejl för tolk",
# "Meddelande": "Hej! Har bokat tolk men ser inte tolkens mejladress någonstans. Behöver den för att skicka video länk."
# },
# "submittedAt": "2024-02-01T15:35:45.897Z",
# "id": "65bbba510f02b427c7024707",
# "formId": "64dd32123cda9a590ffbf718"
# }
# }
# }
# }

# ActionController::Parameters.new(data)
# end
end
81 changes: 81 additions & 0 deletions spec/lib/digitaltolk/webflow_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require 'rails_helper'

describe Digitaltolk::WebflowService do
let(:service) { described_class.new(params) }

let(:params) do
ActionController::Parameters.new(webhook_data)
end

let(:webhook_data) do
{
'params': {
'webflow': {
'triggerType': 'form_submission',
'payload': {
'name': 'Info kontakt',
'siteId': '6476e981f6a8eff9068d9af2',
'data': {
'Name-kom': 'Test JM',
'Email-kom': '[email protected]',
'phone-kom': '',
'Bok. nr.': '#3364311',
'Subject': 'Mejl för tolk',
'Meddelande': 'Hej! Har bokat tolk men ser inte tolkens mejladress någonstans. Behöver den för att skicka video länk.',
'recipient': recipient
}
}
}
}
}
end

let(:recipient) { '[email protected]' }

before do
service.perform
end

it 'has no errors' do
service.perform
expect(service.errors).to eq([])
end

it 'triggers digitaltolk email worker' do
worker_double = instance_double(DigitaltolkEmailWorker)
allow(DigitaltolkEmailWorker).to receive(:perform_in).and_return(worker_double)
allow(worker_double).to receive(:perform)

service.perform

expect(DigitaltolkEmailWorker).to have_received(:perform_in)
end

context 'with invalid email' do
let(:recipient) { 'example@@example.com' }

it 'does not trigger email worker' do
worker_double = instance_double(DigitaltolkEmailWorker)
allow(DigitaltolkEmailWorker).to receive(:perform_in).and_return(worker_double)
allow(worker_double).to receive(:perform)

service.perform

expect(DigitaltolkEmailWorker).not_to have_received(:perform_in)
end
end

context 'with multiple email' do
let(:recipient) { '[email protected], [email protected]' }

it 'does not trigger email worker' do
worker_double = instance_double(DigitaltolkEmailWorker)
allow(DigitaltolkEmailWorker).to receive(:perform_in).and_return(worker_double)
allow(worker_double).to receive(:perform)

service.perform

expect(DigitaltolkEmailWorker).to have_received(:perform_in).twice
end
end
end
Loading