Created
March 2, 2022 20:14
-
-
Save kabootit/23b4f54051d2fd3b7f83db050bada19a to your computer and use it in GitHub Desktop.
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
require 'json' | |
require 'aws-sdk-dynamodb' | |
require 'mail' | |
require 'aws-sdk-ses' | |
def respond(event:, context:) | |
event['Records'].each do |record| | |
ses_client = Aws::SES::Client.new(region: 'us-west-2') | |
db_client = Aws::DynamoDB::Client.new(region: 'us-west-2') | |
state = get_state(record) | |
status = get_status(record) | |
destination_email = get_email(record) | |
next unless destination_email | |
user_settings = get_user_settings(db_client, destination_email)[:items].last | |
case state | |
when 6 | |
send_email( | |
destination_email: destination_email, | |
body: 'We just created a Purchase Order on your behalf!', | |
subject: 'Congratulations! New Purchase Order Created!', | |
ses_client: ses_client | |
) if can_send_success?(user_settings) | |
when 7 | |
send_email( | |
destination_email: destination_email, | |
body: "A purchase order could not be created because #{status}", | |
subject: 'Purchase Order Not created.', | |
ses_client: ses_client | |
) if can_send_failure?(user_settings) | |
when 8 | |
send_email( | |
destination_email: destination_email, | |
body: 'A Purchase Order could not be created because APM Help timed out trying to communicate with Appfolio.', | |
subject: 'Purchase Order Not created.', | |
ses_client: ses_client | |
) if can_send_failure?(user_settings) | |
when 9 | |
send_email( | |
destination_email: destination_email, | |
body: 'A Purchase Order could not be created because we detected that you may have already sent this receipt before!', | |
subject: 'Purchase Order Not created.', | |
ses_client: ses_client | |
) if can_send_failure?(user_settings) | |
else | |
puts "record: #{record}" | |
end | |
end | |
end | |
def can_send_failure?(settings) | |
settings[:receive_email_on_failure] | |
end | |
def can_send_success?(settings) | |
settings[:receive_email_on_success] | |
end | |
def get_user_settings(client, user_email) | |
begin | |
all_settings = client.query({ | |
table_name: ENV['USER_TABLE_NAME'], | |
key_condition_expression: '#email = :email', | |
expression_attribute_names: { | |
'#email' => 'email' | |
}, | |
expression_attribute_values: { | |
':email' => user_email | |
} | |
}) | |
settings = all_settings[:items].last | |
rescue => e | |
default_settings | |
end | |
format_settings(settings) | |
end | |
def default_settings | |
{ | |
receive_email_on_failure: false, | |
receive_email_on_success: false | |
} | |
end | |
def format_settings(settings) | |
if settings.any? | |
{ | |
receive_email_on_failure: settings['receive_email_on_failure'], | |
receive_email_on_success: settings['receive_email_on_success'] | |
} | |
else | |
default_settings | |
end | |
end | |
def get_state(record) | |
message(record)['current_state'].values.last.to_i | |
end | |
def get_status(record) | |
message(record)['history'].values.flatten.last.values.last['message'].values.last | |
end | |
def get_email(record) | |
message(record)['email'].values.last | |
rescue StandardError | |
nil | |
end | |
def message(record) | |
JSON.parse(record.dig('Sns', 'Message'))['data'] | |
end | |
def send_email(destination_email:, body:, subject:, ses_client:) | |
ses_client.send_email( | |
destination: { | |
to_addresses: [ | |
destination_email | |
] | |
}, | |
message: { | |
body: { | |
text: { | |
charset: 'UTF-8', | |
data: body | |
} | |
}, | |
subject: { | |
charset: 'UTF-8', | |
data: subject | |
} | |
}, | |
source: '[email protected]' | |
) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment