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

[treasury-report] Update Send Treasury Report by email button to only send the email and not trigger the step function #516

Closed
wants to merge 18 commits into from
Closed
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
3 changes: 3 additions & 0 deletions .env.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ AWS_DEFAULT_REGION=us-west-2
AWS_ACCESS_KEY_ID=test
AWS_SECRET_ACCESS_KEY=test

# Localstack environment defaults (needed for SQS routing)
LOCALSTACK_HOSTNAME=localhost

# Datadog environment defaults (note: RUM vars are not secret)
DD_ENABLED=false
DD_ENV=sandbox
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ terraform/.terraform
.tool-versions

localstack/volume/
volume/

# Python virtual environments
.venv/
Expand All @@ -165,3 +166,6 @@ python/dist/
# Generated Type files
web/types/graphql.d.ts
api/types/graphql.d.ts

# Generated zips for local testing
scripts/local_treasury_file_email/*.zip
4 changes: 1 addition & 3 deletions api/src/lib/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,7 @@ function getSQSClient() {
let sqs: SQSClient
if (process.env.LOCALSTACK_HOSTNAME) {
console.log('------------ USING LOCALSTACK FOR SQS ------------')
const endpoint = `http://${process.env.LOCALSTACK_HOSTNAME}:${
process.env.EDGE_PORT || 4566
}`
const endpoint = `http://sqs.us-west-2.localhost.localstack.cloud:4566/000000000000/treasury-email-queue`
sqs = new SQSClient({ endpoint, region: process.env.AWS_DEFAULT_REGION })
} else {
sqs = new SQSClient()
Expand Down
128 changes: 15 additions & 113 deletions api/src/services/uploads/uploads.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
deleteUploadFile,
s3UploadFilePutSignedUrl,
getSignedUrl,
startStepFunctionExecution,
sendSqsMessage,
} from 'src/lib/aws'
import { db } from 'src/lib/db'
import { logger } from 'src/lib/logger'
Expand All @@ -23,9 +23,6 @@ import {
getUploadsByExpenditureCategory,
getValidUploadsInCurrentPeriod,
sendTreasuryReport,
SubrecipientLambdaPayload,
ProjectLambdaPayload,
CreateArchiveLambdaPayload,
EmailLambdaPayload,
} from './uploads'
import type { StandardScenario } from './uploads.scenarios'
Expand All @@ -42,7 +39,7 @@ jest.mock('src/lib/aws', () => ({
deleteUploadFile: jest.fn(),
s3UploadFilePutSignedUrl: jest.fn(),
getSignedUrl: jest.fn(),
startStepFunctionExecution: jest.fn(),
sendSqsMessage: jest.fn(),
}))
jest.mock('uuid', () => ({
v4: () => '00000000-0000-0000-0000-000000000000',
Expand Down Expand Up @@ -379,6 +376,8 @@ describe('treasury report', () => {
beforeEach(() => {
jest.resetAllMocks()
process.env.TREASURY_STEP_FUNCTION_ARN = 'test-arn'
process.env.TREASURY_EMAIL_SQS_URL =
'https://sqs.us-east-1.amazon.com/fake_aws_account_key/fake_queue'
})

scenario(
Expand All @@ -387,124 +386,27 @@ describe('treasury report', () => {
mockCurrentUser(scenario.user.one)
const mockOrganization = scenario.organization.one
const mockReportingPeriod = scenario.reportingPeriod.one
const mockUpload = scenario.upload.two
const mockUser = scenario.user.one

const projectPayload: ProjectLambdaPayload = {
'1A': {
organization: {
id: mockOrganization.id,
preferences: {
current_reporting_period_id: mockReportingPeriod.id,
},
},
user: {
email: mockUser.email,
id: mockUser.id,
},
outputTemplateId: mockReportingPeriod.outputTemplateId,
uploadsToAdd: {
[mockUpload.agencyId]: {
objectKey: `uploads/${mockOrganization.id}/${mockUpload.agencyId}/${mockReportingPeriod.id}/${mockUpload.id}/${mockUpload.filename}`,
createdAt: mockUpload.createdAt,
filename: mockUpload.filename,
},
},
uploadsToRemove: {},
ProjectType: '1A',
},
'1B': {
organization: {
id: mockOrganization.id,
preferences: {
current_reporting_period_id: mockReportingPeriod.id,
},
},
user: {
email: mockUser.email,
id: mockUser.id,
},
outputTemplateId: mockReportingPeriod.outputTemplateId,
uploadsToAdd: {},
uploadsToRemove: {},
ProjectType: '1B',
},
'1C': {
organization: {
id: mockOrganization.id,
preferences: {
current_reporting_period_id: mockReportingPeriod.id,
},
},
user: {
email: mockUser.email,
id: mockUser.id,
},
outputTemplateId: mockReportingPeriod.outputTemplateId,
uploadsToAdd: {},
uploadsToRemove: {},
ProjectType: '1C',
},
}
const subrecipientPayload: SubrecipientLambdaPayload = {
Subrecipient: {
organization: {
id: mockOrganization.id,
preferences: {
current_reporting_period_id: mockReportingPeriod.id,
},
},
user: {
email: mockUser.email,
id: mockUser.id,
},
outputTemplateId: mockReportingPeriod.outputTemplateId,
},
}

const zipPayload: CreateArchiveLambdaPayload = {
zip: {
organization: {
id: mockOrganization.id,
preferences: {
current_reporting_period_id: mockReportingPeriod.id,
},
},
},
}
const emailPayload: EmailLambdaPayload = {
email: {
organization: {
id: mockOrganization.id,
preferences: {
current_reporting_period_id: mockReportingPeriod.id,
},
},
user: {
email: mockUser.email,
id: mockUser.id,
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 input = emailPayload
const result = await sendTreasuryReport()

expect(result).toBe(true)
expect(startStepFunctionExecution).toHaveBeenCalledWith(
'test-arn',
`Force-kick-off-00000000-0000-0000-0000-000000000000`,
expect(sendSqsMessage).toHaveBeenCalledWith(
'https://sqs.us-east-1.amazon.com/fake_aws_account_key/fake_queue',
input
)
}
Expand Down
60 changes: 14 additions & 46 deletions api/src/services/uploads/uploads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
MutationResolvers,
UploadRelationResolvers,
} from 'types/graphql'
import { v4 as uuidv4 } from 'uuid'

import { RedwoodError } from '@redwoodjs/api'

Expand All @@ -16,7 +15,7 @@ import {
s3UploadFilePutSignedUrl,
getSignedUrl,
getS3UploadFileKey,
startStepFunctionExecution,
sendSqsMessage,
} from 'src/lib/aws'
import { ROLES } from 'src/lib/constants'
import { db } from 'src/lib/db'
Expand Down Expand Up @@ -212,7 +211,7 @@ export type SubrecipientLambdaPayload = Record<
InfoForSubrecipient
>
export type CreateArchiveLambdaPayload = Record<'zip', InfoForArchive>
export type EmailLambdaPayload = Record<'email', InfoForEmail>
export type EmailLambdaPayload = InfoForEmail

export const getUploadsByExpenditureCategory = async (
organization: Organization,
Expand Down Expand Up @@ -348,19 +347,17 @@ export const getEmailLambdaPayload = async (
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,
organization: {
id: organization.id,
preferences: {
current_reporting_period_id:
organization.preferences['current_reporting_period_id'],
},
},
user: {
email: user.email,
id: user.id,
},
}
}

Expand All @@ -370,41 +367,12 @@ export const sendTreasuryReport: MutationResolvers['sendTreasuryReport'] =
const organization = await db.organization.findFirst({
where: { id: context.currentUser.agency.organizationId },
})
const reportingPeriod = await db.reportingPeriod.findFirst({
where: { id: organization.preferences['current_reporting_period_id'] },
})
const projectLambdaPayload: ProjectLambdaPayload =
await getUploadsByExpenditureCategory(organization, reportingPeriod)
const subrecipientLambdaPayload: SubrecipientLambdaPayload =
await getSubrecipientLambdaPayload(
organization,
context.currentUser,
reportingPeriod
)
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(
process.env.TREASURY_STEP_FUNCTION_ARN,
`Force-kick-off-${uuidv4()}`,
JSON.stringify(input)
)
const input = emailLambdaPayload

await sendSqsMessage(process.env.TREASURY_EMAIL_SQS_URL, input)
return true
} catch (error) {
logger.error(error, 'Error sending Treasury Report')
Expand Down
14 changes: 13 additions & 1 deletion docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ services:
- db
networks:
- redwood
- ls
dns:
# Set the DNS server to be the LocalStack container
- 10.0.2.20
environment:
- DATABASE_URL=postgresql://redwood:redwood@db:5432/redwood
- TEST_DATABASE_URL=postgresql://redwood:redwood@db:5432/redwood_test
Expand Down Expand Up @@ -91,7 +95,10 @@ services:
- DOCKER_HOST=unix:///var/run/docker.sock
- AWS_DEFAULT_REGION=${AWS_REGION:-us-west-2}
networks:
- redwood
redwood:
ls:
# Set the container IP address in the 10.0.2.0/24 subnet
ipv4_address: 10.0.2.20
volumes:
- "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
Expand All @@ -100,6 +107,11 @@ services:
networks:
redwood:
driver: bridge
ls:
ipam:
config:
# Specify the subnet range for IP address allocation
- subnet: 10.0.2.0/24
Comment on lines +110 to +114
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed for proper SQS message routing in the local development environment: localstack/localstack#10590 (comment)


volumes:
node_modules:
Expand Down
1 change: 1 addition & 0 deletions localstack/entrypoint/init-aws.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ for email in "${VALID_EMAILS[@]}"; do
done

awslocal s3api create-bucket --bucket cpf-reporter --region us-west-2 --create-bucket-configuration '{"LocationConstraint": "us-west-2"}'
awslocal sqs create-queue --queue-name treasury-email-queue
Loading
Loading