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

Discount expiry notifier - segment and save only successes and failures #2673

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions handlers/discount-expiry-notifier/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"dependencies": {
"@aws-sdk/client-s3": "3.735.0",
"@aws-sdk/client-sqs": "3.734.0",
"@google-cloud/bigquery": "^7.9.1",
"aws-sdk": "^2.1692.0",
"google-auth-library": "^9.15.0",
Expand Down
19 changes: 15 additions & 4 deletions handlers/discount-expiry-notifier/src/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,25 @@ export const runQuery = async (
return [
{
firstName: 'David',
nextPaymentDate: '2025-02-28',
paymentAmount: 12,
nextPaymentDate: '2025-02-27',
paymentAmount: 1.23,
paymentCurrency: 'GBP',
paymentFrequency: 'Month',
productName: 'Supporter Plus',
sfContactId: '0039E00001HiIGlQAN',
subName: 'A-S00814342', // Active sub in dev sandbox
workEmail: 'david.pepper@guardian.co.uk',
subName: 'A-S00886188', // Active sub in dev sandbox
workEmail: 'david.pepper+david@guardian.co.uk',
},
{
firstName: 'Mary',
nextPaymentDate: '2025-02-29',
paymentAmount: 4.56,
paymentCurrency: 'USD',
paymentFrequency: 'Month',
productName: 'Supporter Plus',
sfContactId: '0039E00001HwhM9QAJ',
subName: 'A-S00886159', // Active subscription in dev sandbox
workEmail: 'graham.hopgood@guardian.co.uk',
},
];
};
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const getQuery = (discountExpiresOnDate: string): string =>
sub.is_latest_version = TRUE AND
sub.status = 'Active' AND
DATE_ADD(charge.effective_start_date, INTERVAL charge.up_to_periods MONTH) = '${discountExpiresOnDate}' AND
sub.name = 'A-S02287430'
sub.name = 'xxxxx'
)
SELECT
STRING_AGG(DISTINCT firstName) as firstName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export const handler = async (event: {
const payload = {
...{
To: {
Address: 'david.pepper@guardian.co.uk', // hard code this during testing
Address: event.workEmail,
ContactAttributes: {
SubscriberAttributes: {
EmailAddress: 'david.pepper@guardian.co.uk', // hard code this during testing
EmailAddress: event.workEmail,
paymentAmount: `${currencySymbol}${event.paymentAmount}`,
first_name: event.firstName,
date_of_payment: formatDate(event.nextPaymentDate),
Expand All @@ -33,12 +33,27 @@ export const handler = async (event: {
},
SfContactId: event.sfContactId,
};
try {
const response = await sendEmail(stageFromEnvironment(), payload);

const emailSend = await sendEmail(stageFromEnvironment(), payload);

console.log('emailSend:', emailSend);

return emailSend;
return {
detail: event,
emailSendAttempt: {
status: 'success',
payload,
response,
},
};
} catch (error) {
return {
detail: event,
emailSendAttempt: {
status: 'error',
payload,
response: error as string,
},
};
}
};

function formatDate(inputDate: string): string {
Expand Down
139 changes: 116 additions & 23 deletions handlers/discount-expiry-notifier/src/handlers/saveResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,7 @@ import { getIfDefined } from '@modules/nullAndUndefined';
import { uploadFileToS3 } from '../s3';

//todo add a type check on the input event
export const handler = async (event: {
discountExpiresOnDate: string;
expiringDiscountsToProcess: Array<{
firstName: string;
nextPaymentDate: string;
paymentAmount: number;
paymentCurrency: string;
paymentFrequency: string;
productName: string;
sfContactId: string;
subName: string;
workEmail: string;
}>;
expiringDiscountProcessingAttempts: Array<{
status: string;
}>;
}) => {
export const handler = async (event: DiscountProcessingEvent) => {
const bucketName = getIfDefined<string>(
process.env.S3_BUCKET,
'S3_BUCKET environment variable not set',
Expand All @@ -28,18 +12,127 @@ export const handler = async (event: {
event.discountExpiresOnDate,
'event.discountExpiresOnDate variable not set',
);

const executionDateTime = new Date().toISOString();
const { discountProcessingAttempts } = event;

const filePath = `${discountExpiresOnDate}/${executionDateTime}`;
const successes = discountProcessingAttempts.filter(
(attempt) => attempt.emailSendAttempt.status === 'success',
);
const successesFilePath = generateFilePath(
discountExpiresOnDate,
executionDateTime,
'successes',
);
const saveSuccessesAttempt = await uploadFileToS3({
bucketName,
filePath: successesFilePath,
content: JSON.stringify(successes, null, 2),
});

await uploadFileToS3({
const failures = discountProcessingAttempts.filter(
(attempt) => attempt.emailSendAttempt.status === 'error',
);
const failuresFilePath = generateFilePath(
discountExpiresOnDate,
executionDateTime,
'failures',
);
const saveFailuresAttempt = await uploadFileToS3({
bucketName,
filePath,
content: JSON.stringify(event),
filePath: failuresFilePath,
content: JSON.stringify(failures, null, 2),
});

return {
filePath,
successesFilePath: successesFilePath,
saveSuccessesAttempt,
failuresFilePath: failuresFilePath,
saveFailuresAttempt,
};
};

function generateFilePath(
discountExpiresOnDate: string,
executionDateTime: string,
status: 'successes' | 'failures',
) {
return `${discountExpiresOnDate}/${status}/${executionDateTime}`;
}

type DiscountProcessingEvent = {
discountExpiresOnDate: string;
expiringDiscountsToProcess: ExpiringDiscount[];
discountProcessingAttempts: DiscountProcessingAttempt[];
};

type ExpiringDiscount = {
firstName: string;
nextPaymentDate: string;
paymentAmount: number;
paymentCurrency: string;
paymentFrequency: string;
productName: string;
sfContactId: string;
subName: string;
workEmail: string;
};

type DiscountProcessingAttempt = {
detail: DiscountDetail;
emailSendAttempt: EmailSendAttempt;
};

type DiscountDetail = ExpiringDiscount & {
status: string;
};

type EmailSendAttempt = {
status: 'success' | 'error';
payload: EmailPayload;
response: EmailResponse;
};

type EmailPayload = {
To: EmailRecipient;
DataExtensionName: string;
SfContactId: string;
};

type EmailRecipient = {
Address: string;
ContactAttributes: {
SubscriberAttributes: SubscriberAttributes;
};
};

type SubscriberAttributes = {
EmailAddress: string;
paymentAmount: string;
first_name: string;
date_of_payment: string;
paymentFrequency: string;
};

type EmailResponse = SuccessResponse | ErrorResponse;

type SuccessResponse = {
$metadata: Metadata;
MD5OfMessageBody: string;
MessageId: string;
};

type ErrorResponse = {
name: string;
$fault: string;
$metadata: Metadata;
__type: string;
Code: string;
Type: string;
};

type Metadata = {
httpStatusCode: number;
requestId: string;
attempts: number;
totalRetryDelay: number;
};
21 changes: 12 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.