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

Send denied application email with SES #72

Open
wants to merge 3 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
43 changes: 43 additions & 0 deletions apps/backend/src/lambdas/applicationUpdate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { SESClient, SendEmailCommand } from '@aws-sdk/client-ses';

const sesClient = new SESClient({ region: process.env.AWS_REGION });

exports.handler = async (event: any) => {
const { firstName, userEmail } = event;

try {
// Send email using SES
const emailParams = {
Destination: {
ToAddresses: [userEmail],
},
Message: {
Body: {
Text: {
Data: `Hi ${firstName},\n\nWe regret to inform you that your application has not been approved at this time. Please contact the Office of Green Infrastructure if you have any questions or concerns about your application.\n\nSincerely,\nOffice of Green Infrastructure`,
},
},
Subject: {
Data: 'Green Infrastructure Volunteer Application Update',
},
},
Source: process.env.SES_SOURCE_EMAIL,
};

const sendEmailCommand = new SendEmailCommand(emailParams);
await sesClient.send(sendEmailCommand);

return {
statusCode: 200,
body: JSON.stringify({ message: 'Email sent successfully' }),
};
} catch (error) {
console.error('Error sending email:', error);
return {
statusCode: 500,
body: JSON.stringify({
message: `Error sending email: ${error.message}`,
}),
};
}
};
2 changes: 1 addition & 1 deletion apps/backend/src/user/user.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type UserInputModel = {
zipCode: {S: string},
birthDate: {S: string},
role: {S: string},
siteIds: {S: string},
siteIds: {L: {N: string}[] },
status: {S: string}
};
export class EditUserModel {
Expand Down
35 changes: 23 additions & 12 deletions apps/backend/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { UserModel, EditUserModel } from "./user.model";
import { DynamoDbService } from "../dynamodb";
import { UserInputModel, UserStatus, Role } from "./user.model";
import { NewUserInput } from "../dtos/newUserDTO";
import { LambdaClient, InvokeCommand } from '@aws-sdk/client-lambda';

@Injectable()
export class UserService {

private readonly tableName = 'gibostonUsers';
constructor(private readonly dynamoDbService: DynamoDbService) {}


private readonly lambdaClient: LambdaClient = new LambdaClient({ region: process.env.AWS_REGION });
constructor(private readonly dynamoDbService: DynamoDbService) { }

/**
* Gets a user's information based on the user's id.
Expand Down Expand Up @@ -76,8 +76,6 @@ export class UserService {

const originalUser = await this.getUser(userId);



if (model.firstName) {
commands.push(`firstName = :firstName`);
expressionAttributeValues[":firstName"] = { S: model.firstName };
Expand Down Expand Up @@ -112,13 +110,25 @@ export class UserService {
if (!siteCheckResult) {
return {statusCode: 400, message: "Site does not exist"};
}
}
}

if (model.status) {
if (model.status) {
commands.push(`#s = :status`);
expressionAttributeValues[":status"] = { S: model.status };
expressionAttributeNames["#s"] = "status"; // Define the alias
}
expressionAttributeNames["#s"] = "status";
if (model.status === UserStatus.DENIED) {
// Send email if status is denied
const lamdaParams = {
FunctionName: 'giSendEmail',
Payload: JSON.stringify({
firstName: model.firstName ? model.firstName : originalUser.firstName,
userEmail: originalUser.email
}),
}
const command = new InvokeCommand(lamdaParams);
await this.lambdaClient.send(command);
}
}


// Make sure commands aren't empty
Expand Down Expand Up @@ -177,8 +187,9 @@ export class UserService {
* @returns the user's information as a UserModel object
*/
private mapDynamoDBItemToUserModel(objectId: number, data?: {[key: string]: any}): UserModel {

const siteIds = data["siteIds"].L.map(item => Number(item.N)) ?? [];
const siteIds = Array.isArray(data["siteIds"]?.L)
? data["siteIds"].L.map(item => Number(item.N))
: [];


return {
Expand All @@ -205,7 +216,7 @@ export class UserService {
zipCode: {S: input.zipCode},
birthDate: {S: input.birthDate},
role: {S: role},
siteIds: {S: "null"},
siteIds: { L: [] },
status: {S: UserStatus.PENDING}
};
}
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"private": true,
"dependencies": {
"@aws-sdk/client-dynamodb": "^3.319.0",
"@aws-sdk/client-lambda": "^3.738.0",
"@aws-sdk/client-ses": "^3.738.0",
"@aws-sdk/util-dynamodb": "^3.319.0",
"@blueprintjs/core": "^4.18.0",
"@blueprintjs/table": "^4.10.1",
Expand Down
Loading
Loading