From 803e4e7a002a97ddf5e0d0db42a5e96bd2881c7a Mon Sep 17 00:00:00 2001 From: John Manuel Derecho Date: Thu, 23 May 2024 00:04:53 +0800 Subject: [PATCH 1/4] webflow dynamic recipient --- lib/digitaltolk/webflow_service.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/digitaltolk/webflow_service.rb b/lib/digitaltolk/webflow_service.rb index c68d8a5d84449..ac46ff18b4ff4 100644 --- a/lib/digitaltolk/webflow_service.rb +++ b/lib/digitaltolk/webflow_service.rb @@ -1,8 +1,6 @@ class Digitaltolk::WebflowService attr_accessor :params - INFO_EMAIL = 'info@digitaltolk.se'.freeze - def initialize(params) @params = params end @@ -38,7 +36,7 @@ def process_webflow_submission end def emails - [INFO_EMAIL] + form_data['recipients'].to_s.split(',') end def webflow_params From 76c8b95e6fe9e1144df4895f5a91060158ecd387 Mon Sep 17 00:00:00 2001 From: John Manuel Derecho Date: Thu, 23 May 2024 23:37:50 +0800 Subject: [PATCH 2/4] remove temporary deploy check --- .github/workflows/deploy_check.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy_check.yml b/.github/workflows/deploy_check.yml index 7fda2b1a446c0..0560bb98e4ec8 100644 --- a/.github/workflows/deploy_check.yml +++ b/.github/workflows/deploy_check.yml @@ -3,8 +3,8 @@ ## deployment url will be of the form chatwoot-pr-.herokuapp.com name: Deploy Check -on: - pull_request: +# on: +# pull_request: jobs: deployment_check: From 8b7062c510a3a6a0629e1e0c51ae17893a6ce379 Mon Sep 17 00:00:00 2001 From: John Manuel Derecho Date: Fri, 24 May 2024 03:33:42 +0800 Subject: [PATCH 3/4] webflow update --- spec/lib/digitaltolk/webflow_service_spec.rb | 81 ++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 spec/lib/digitaltolk/webflow_service_spec.rb diff --git a/spec/lib/digitaltolk/webflow_service_spec.rb b/spec/lib/digitaltolk/webflow_service_spec.rb new file mode 100644 index 0000000000000..2f5f21eed2045 --- /dev/null +++ b/spec/lib/digitaltolk/webflow_service_spec.rb @@ -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': 'test@test.com', + '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) { 'info@example.com' } + + 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) { 'info@example.com, info2@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).to have_received(:perform_in).twice + end + end +end From 84c1660292c8f68fd3b99f0d085d6d74c5a40c26 Mon Sep 17 00:00:00 2001 From: John Manuel Derecho Date: Fri, 24 May 2024 03:34:17 +0800 Subject: [PATCH 4/4] webflow update --- lib/digitaltolk/webflow_service.rb | 82 +++++++++--------------------- 1 file changed, 23 insertions(+), 59 deletions(-) diff --git a/lib/digitaltolk/webflow_service.rb b/lib/digitaltolk/webflow_service.rb index ac46ff18b4ff4..47ba4253eb516 100644 --- a/lib/digitaltolk/webflow_service.rb +++ b/lib/digitaltolk/webflow_service.rb @@ -1,32 +1,41 @@ class Digitaltolk::WebflowService - attr_accessor :params + 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}" @@ -36,7 +45,7 @@ def process_webflow_submission end def emails - form_data['recipients'].to_s.split(',') + form_data['recipient'].to_s.split(',').map(&:strip) end def webflow_params @@ -76,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": "jmanuel.derecho@gmail.com", - # "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": "jmanuel.derecho@gmail.com", - # "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