-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #208 from mattzcarey/feat/lambda-welcome-email
feat: add welcome email to add user lambda
- Loading branch information
Showing
11 changed files
with
142 additions
and
67 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
testRegex: '/src/.*.test.ts', | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import * as dotenv from 'dotenv'; | ||
|
||
dotenv.config({ path: './services/tests/.env' }); | ||
|
||
describe("CloudFlare email worker health test", () => { | ||
test("Should return 200 after successfully hitting the CloudFlare health endpoint", async() => { | ||
const res = await fetch(process.env.CLOUDFLARE_WORKER_HEALTH_URL ?? "", { | ||
method: "GET", | ||
}); | ||
|
||
expect(res.status).toEqual(200); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
testRegex: '/services/tests/.*.integ.test.ts', | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { DynamoDBStreamEvent } from "aws-lambda"; | ||
import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; | ||
import { PutCommand, DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; | ||
import fetch from "node-fetch"; | ||
import { getVariableFromSSM } from "../../../core/functions/helpers/getVariable"; | ||
import { Config } from "sst/node/config"; | ||
|
||
const client = new DynamoDBClient({}); | ||
const docClient = DynamoDBDocumentClient.from(client); | ||
|
||
const postEmail = async(email: string, name: string) => { | ||
const url = await getVariableFromSSM(process.env.CLOUDFLARE_WORKER_URL_NAME) ?? ""; | ||
return await fetch(url.concat("api/email"), { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"Authorization": await getVariableFromSSM( | ||
process.env.CLOUDFLARE_WORKER_TOKEN_NAME ?? "" | ||
), | ||
}, | ||
body: JSON.stringify({ | ||
"to": {"email": email, "name": name }, | ||
"from": { "email": "[email protected]", "name": "Matt from Orion Tools" }, | ||
"subject": "Welcome to Code Review GPT", | ||
"html": "<p>Thanks for signing up for Orion tools. We aim to make the best AI powered dev tools. We hope you enjoy using our code review product. <br/>Here is a <a href=\"https://github.com/mattzcarey/code-review-gpt\">link</a> to the repo, give it a star. Here is a <a href=\"https://join.slack.com/t/orion-tools/shared_invite/zt-20x79nfgm-UGIHK1uWGQ59JQTpODYDwg\">link</a> to our slack community.</p>" | ||
}), | ||
}); | ||
}; | ||
|
||
export const main = async (event: DynamoDBStreamEvent) => { | ||
if (event.Records == null) { | ||
return Promise.resolve({ | ||
statusCode: 400, | ||
body: "The request does not contain a any dynamodb records as expected.", | ||
}); | ||
} | ||
|
||
try { | ||
if (event.Records[0].dynamodb?.NewImage) { | ||
const record = event.Records[0].dynamodb?.NewImage; | ||
const userId = record.id['S']; | ||
const name = record.name['S']; | ||
const email = record.email['S']; | ||
const pictureUrl = record.image['S']; | ||
|
||
if (userId === undefined || name === undefined || email === undefined || pictureUrl === undefined) { | ||
return Promise.resolve({ | ||
statusCode: 400, | ||
body: "The request record does not contain the expected data.", | ||
}); | ||
} | ||
|
||
const res = await postEmail(email, name); | ||
if (!res.ok) { | ||
console.error("Failed to send welcome email due to this status code: ", res.status); | ||
} | ||
|
||
const command = new PutCommand({ | ||
TableName: `${process.env.SST_STAGE}-crgpt-data`, | ||
Item: { | ||
PK: `USERID#${userId}`, | ||
SK: "ROOT", | ||
userId: userId, | ||
name: name, | ||
email: email, | ||
pictureUrl: pictureUrl, | ||
}, | ||
}); | ||
await docClient.send(command); | ||
|
||
return Promise.resolve({ | ||
statusCode: 200, | ||
body: "Successfully added new user to the user db", | ||
}); | ||
} else { | ||
return Promise.resolve({ | ||
statusCode: 400, | ||
body: "Dynamodb record did not contain any new users", | ||
}); | ||
} | ||
} catch (err) { | ||
console.error(err); | ||
|
||
return Promise.resolve({ | ||
statusCode: 500, | ||
body: "Error when updating user.", | ||
}); | ||
} | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
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