diff --git a/app/commands/user/bootcamp/subscribe_to_onboarding_emails.rb b/app/commands/user/bootcamp/subscribe_to_onboarding_emails.rb index 75a8e9da40..7f60c8b619 100644 --- a/app/commands/user/bootcamp/subscribe_to_onboarding_emails.rb +++ b/app/commands/user/bootcamp/subscribe_to_onboarding_emails.rb @@ -10,6 +10,7 @@ class SubscribeToOnboardingEmails def call return unless bootcamp_data.enrolled? + return User::Bootcamp::SubscribeToOnboardingEmails.(duplicate) if duplicate new_sub = add_subscriber! add_to_form! if new_sub @@ -41,13 +42,21 @@ def add_subscriber! def add_to_form! RestClient.post( - "https://api.kit.com//v4/forms/#{BOOTCAMP_FORM_ID}/subscribers", + "https://api.kit.com/v4/forms/#{BOOTCAMP_FORM_ID}/subscribers", { "email_address": bootcamp_data.email }, HEADERS ) end + def duplicate + User::BootcampData. + where(email: bootcamp_data.email). + where.not(id: bootcamp_data.id). + paid. + first + end + HEADERS = { 'Accept': 'application/json', 'X-Kit-Api-Key': Exercism.secrets.key_api4_key diff --git a/app/models/user/bootcamp_data.rb b/app/models/user/bootcamp_data.rb index 5d64cd4ff5..b9913c938d 100644 --- a/app/models/user/bootcamp_data.rb +++ b/app/models/user/bootcamp_data.rb @@ -8,7 +8,7 @@ class User::BootcampData < ApplicationRecord scope :not_enrolled, -> { where(enrolled_at: nil) } scope :not_paid, -> { where(paid_at: nil) } - after_save do + after_save_commit do User::Bootcamp::SubscribeToOnboardingEmails.defer(self) end