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

chore: add success email as last step of treasury generation #459

Merged
merged 3 commits into from
Oct 1, 2024
Merged
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
17 changes: 17 additions & 0 deletions api/src/services/uploads/uploads.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
SubrecipientLambdaPayload,
ProjectLambdaPayload,
CreateArchiveLambdaPayload,
EmailLambdaPayload,
} from './uploads'
import type { StandardScenario } from './uploads.scenarios'

Expand Down Expand Up @@ -336,16 +337,32 @@ describe('treasury report', () => {
},
},
}
const emailPayload: EmailLambdaPayload = {
email: {
organization: {
id: mockOrganization.id,
preferences: {
current_reporting_period_id: mockReportingPeriod.id,
},
},
user: {
email: mockUser.email,
id: mockUser.id,
},
},
}

const input = JSON.stringify({
'1A': {},
'1B': {},
'1C': {},
Subrecipient: {},
zip: {},
email: {},
...projectPayload,
...subrecipientPayload,
...zipPayload,
...emailPayload,
})
const result = await sendTreasuryReport()

Expand Down
31 changes: 31 additions & 0 deletions api/src/services/uploads/uploads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ type InfoForSubrecipient = {
type InfoForArchive = {
organization: OrganizationObj
}
type InfoForEmail = {
organization: OrganizationObj
user: UserObj
}
/*
This type should be similar to the following python class:
class ProjectLambdaPayload(BaseModel):
Expand All @@ -204,6 +208,7 @@ export type SubrecipientLambdaPayload = Record<
InfoForSubrecipient
>
export type CreateArchiveLambdaPayload = Record<'zip', InfoForArchive>
export type EmailLambdaPayload = Record<'email', InfoForEmail>

export const getUploadsByExpenditureCategory = async (
organization: Organization,
Expand Down Expand Up @@ -343,6 +348,27 @@ export const getCreateArchiveLambdaPayload = async (
}
}

export const getEmailLambdaPayload = async (
organization: Organization,
user: CurrentUser
): Promise<EmailLambdaPayload> => {
return {
email: {
organization: {
id: organization.id,
preferences: {
current_reporting_period_id:
organization.preferences['current_reporting_period_id'],
},
},
user: {
email: user.email,
id: user.id,
},
},
}
}

export const sendTreasuryReport: MutationResolvers['sendTreasuryReport'] =
async () => {
try {
Expand All @@ -363,15 +389,20 @@ export const sendTreasuryReport: MutationResolvers['sendTreasuryReport'] =
const createArchiveLambdaPayload: CreateArchiveLambdaPayload =
await getCreateArchiveLambdaPayload(organization)

const emailLambdaPayload: EmailLambdaPayload =
await getEmailLambdaPayload(organization, context.currentUser)

const input = {
'1A': {},
'1B': {},
'1C': {},
Subrecipient: {},
zip: {},
email: {},
...projectLambdaPayload,
...subrecipientLambdaPayload,
...createArchiveLambdaPayload,
...emailLambdaPayload,
}

await startStepFunctionExecution(
Expand Down
22 changes: 22 additions & 0 deletions terraform/treasury_generation_step_function.tf
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ module "treasury_generation_step_function" {
},
"CreateTreasuryZipfile" : {
"Type" : "Task",
"Next" : "SendSuccessEmail",
"Resource" : "arn:aws:states:::lambda:invoke",
"Parameters" : {
"FunctionName" : module.lambda_function-cpfCreateArchive.lambda_function_arn,
Expand All @@ -141,6 +142,27 @@ module "treasury_generation_step_function" {
"BackoffRate" : 2
}
],
}
"SendSuccessEmail" : {
"Type" : "Task",
"Resource" : "arn:aws:states:::lambda:invoke",
"Parameters" : {
"FunctionName" : module.lambda_function-email-presigned-url.lambda_function_arn,
"Payload.$" : "$$.Execution.Input.email"
},
"Retry" : [
{
"ErrorEquals" : [
"Lambda.ServiceException",
"Lambda.AWSLambdaException",
"Lambda.SdkClientException",
"Lambda.TooManyRequestsException"
],
"IntervalSeconds" : 1,
"MaxAttempts" : 3,
"BackoffRate" : 2
}
],
"End" : true
}
}
Expand Down
Loading