-
Notifications
You must be signed in to change notification settings - Fork 479
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
Create a wrapper around the Mailjet API #57580
Merged
bethanyaconnor
merged 11 commits into
staging
from
bethany/lifecycle-marketing/mailjet-wrapper
Apr 11, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9ba3d34
Add test and constants file
bethanyaconnor 34d9b17
Update constants
bethanyaconnor 8c8820b
Wrap Mailjet calls with rescue and Honeybadger notifications
bethanyaconnor 540cb59
Update contants for staging and to be from Hadi
bethanyaconnor d5ae00d
Merge remote-tracking branch 'origin/staging' into bethany/lifecycle-…
bethanyaconnor 85a4fdc
Merge remote-tracking branch 'origin/staging' into bethany/lifecycle-…
bethanyaconnor 32125a0
PR feedback: syntax
bethanyaconnor ade5644
Push date format conversion into create_contact method
bethanyaconnor ebad19e
Refactor create_contact
bethanyaconnor fe6573c
Restrict sending in test and move fetching contacts into send_templat…
bethanyaconnor e26e532
Clarify that Mailjet shouldn't be used in test
bethanyaconnor File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
require 'mailjet' | ||
require 'honeybadger/ruby' | ||
require_relative './shared_constants/mailjet_constants' | ||
|
||
module MailJet | ||
include MailJetConstants | ||
|
||
API_KEY = CDO.try(:mailjet_api_key).freeze | ||
SECRET_KEY = CDO.try(:mailjet_secret_key).freeze | ||
|
||
# We use MailJet when the following are true: | ||
# - The use_mailjet DCDO is true | ||
# - We are not in the test environment | ||
# - We have the required secrets set | ||
def self.enabled? | ||
DCDO.get('use_mailjet', false) && | ||
!Rails.env.test? && | ||
API_KEY.present? && | ||
SECRET_KEY.present? | ||
end | ||
|
||
def self.subaccount | ||
case Rails.env | ||
when 'production' | ||
'production' | ||
when 'staging' | ||
'staging' | ||
when 'test' | ||
# We don't want to send real emails in test | ||
'' | ||
else | ||
'development' | ||
end | ||
end | ||
|
||
def self.create_contact_and_send_welcome_email(user) | ||
return unless enabled? | ||
|
||
return unless user&.id.present? | ||
return unless user.teacher? | ||
|
||
create_contact(user.email, user.name, user.created_at.to_datetime) | ||
send_template_email( | ||
user.email, | ||
user.name, | ||
EMAILS[:welcome] | ||
) | ||
end | ||
|
||
def self.create_contact(email, name, sign_up_date) | ||
return unless enabled? | ||
|
||
Mailjet.configure do |config| | ||
config.api_key = API_KEY | ||
config.secret_key = SECRET_KEY | ||
config.api_version = "v3" | ||
end | ||
|
||
contact = Mailjet::Contactdata.find(email) | ||
|
||
if contact&.id.nil? | ||
Mailjet::Contact.create( | ||
is_excluded_from_campaigns: true, | ||
email: email, | ||
name: name | ||
) | ||
contact = Mailjet::Contactdata.find(email) | ||
end | ||
|
||
sign_up_date_rfc3339 = sign_up_date.rfc3339 | ||
contact.update_attributes( | ||
data: [ | ||
{ | ||
name: 'sign_up_date', | ||
value: sign_up_date_rfc3339 | ||
} | ||
] | ||
) | ||
end | ||
|
||
def self.send_template_email(to_email, to_name, email_config) | ||
return unless enabled? | ||
|
||
Mailjet.configure do |config| | ||
config.api_key = API_KEY | ||
config.secret_key = SECRET_KEY | ||
config.api_version = "v3.1" | ||
end | ||
|
||
from_address = email_config[:from_address] | ||
from_name = email_config[:from_name] | ||
template_id = email_config[:template_id][subaccount.to_sym] | ||
|
||
Mailjet::Send.create(messages: | ||
[{ | ||
From: { | ||
Email: from_address, | ||
Name: from_name | ||
}, | ||
To: [ | ||
{ | ||
Email: to_email, | ||
Name: to_name | ||
} | ||
], | ||
TemplateID: template_id, | ||
TemplateLanguage: true, | ||
}] | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module MailJetConstants | ||
EMAILS = { | ||
welcome: { | ||
template_id: { | ||
production: 5_421_128, | ||
staging: 5_826_411, | ||
development: 5_808_078 | ||
}, | ||
from_address: 'hadi_partovi@code.org', | ||
from_name: 'Hadi Partovi', | ||
} | ||
}.freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
require_relative '../test_helper' | ||
require 'cdo/mailjet' | ||
|
||
class MailJetTest < Minitest::Test | ||
def setup | ||
MailJet.stubs(:enabled?).returns(true) | ||
end | ||
|
||
def test_create_contact | ||
email = 'fake.email@email.com' | ||
name = 'Fake Name' | ||
|
||
Mailjet::Contact.expects(:create).with(is_excluded_from_campaigns: true, email: email, name: name) | ||
|
||
mock_contact = mock('Mailjet::Contactdata') | ||
Mailjet::Contactdata.stubs(:find).with(email).returns(nil).then.returns(mock_contact) | ||
|
||
time = Time.now.to_datetime | ||
mock_contact.expects(:update_attributes).with(data: [{name: 'sign_up_date', value: time.rfc3339}]) | ||
|
||
MailJet.create_contact(email, name, time) | ||
end | ||
|
||
def test_create_contact_with_existing_contact | ||
email = 'fake.email@email.com' | ||
name = 'Fake Name' | ||
|
||
mock_existing_contact = mock('Mailjet::Contactdata') | ||
mock_existing_contact.stubs(:id).returns(123) | ||
Mailjet::Contactdata.expects(:find).with(email).returns(mock_existing_contact) | ||
|
||
Mailjet::Contact.expects(:create).never | ||
|
||
time = Time.now.to_datetime | ||
mock_existing_contact.expects(:update_attributes).with(data: [{name: 'sign_up_date', value: time.rfc3339}]) | ||
|
||
MailJet.create_contact(email, name, time) | ||
end | ||
|
||
def test_send_template_email | ||
to_email = 'fake.email@email.com' | ||
to_name = 'Fake Name' | ||
from_address = 'test@code.org' | ||
from_name = 'Test Name' | ||
template_id = 123 | ||
|
||
MailJet.stubs(:subaccount).returns('unit_test') | ||
|
||
email_config = { | ||
from_address: from_address, | ||
from_name: from_name, | ||
template_id: { | ||
unit_test: template_id | ||
} | ||
} | ||
|
||
Mailjet::Send.expects(:create).with do |params| | ||
messages = params[:messages] | ||
messages.length == 1 && | ||
messages[0][:From][:Email] == from_address && | ||
messages[0][:From][:Name] == from_name && | ||
messages[0][:To].length == 1 && | ||
messages[0][:To][0][:Email] == to_email && | ||
messages[0][:To][0][:Name] == to_name && | ||
messages[0][:TemplateID] == template_id | ||
end | ||
|
||
MailJet.send_template_email(to_email, to_name, email_config) | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would there be value in enforcing that the template_id be found in
MailJetConstants
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean to push the fetching of the template ID down to this function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was just noticing that there's nothing to force future developers to use to use the template listing instead of hardcoding template ID's elsewhere in the codebase. Once we have examples of using the wrapper, the convention might be more clear though.