Skip to content

Commit

Permalink
update NotifyUsers lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
haffi96 committed Sep 24, 2023
1 parent 6afcd4b commit 6b3b227
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 11 deletions.
16 changes: 11 additions & 5 deletions aws/backend/db/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export const fetchUserById = async (userId: number) => {
return await db.select({ userUuid: users.user_uuid }).from(users).where(eq(users.id, userId))
}

export const fetchuEmailserByUuid = async (userUuid: string) => {
const result = await db.select({ userEmail: users.email }).from(users).where(eq(users.user_uuid, userUuid))
const { userEmail } = result[0]
return userEmail
}

export const fetchUserSubscribedBlogs = async (userEmail: string) => {
const userQuery = db.select({ userId: users.id }).from(users).where(eq(users.email, userEmail)).as("userQuery")

Expand Down Expand Up @@ -102,7 +108,7 @@ export const createBlogPostEntry = async (newBlogPostParams: newBlogPostEntry) =

export const fetchNewPosts = async () => {
const dateDelta = new Date()
dateDelta.setDate(dateDelta.getDate() - 3)
dateDelta.setDate(dateDelta.getDate() - 7)
return await db.select({
blogId: blogPosts.blog_id,
postUuid: blogPosts.post_uuid,
Expand Down Expand Up @@ -166,7 +172,7 @@ export const createUserPostEntry = async (userId: number, postId: number) => {
}


export const fetchTodaysUsersToNotify = async () => {
export const fetchUsersToNotify = async () => {
const newPosts = await fetchNewPosts()

// Map of userUuids to postUuids
Expand Down Expand Up @@ -210,9 +216,9 @@ export const fetchTodaysUsersToNotify = async () => {
}


// fetchTodaysUsersToNotify().then((users) => {
// console.log(users)
// })
fetchUsersToNotify().then((users) => {
console.log(users)
})

// fetchAllPostsForUser({ userEmail: "haffimazhar96@gmail.com", offset: 0, limit: 10 }).then((posts) => {
// console.log(posts)
Expand Down
7 changes: 3 additions & 4 deletions aws/backend/lambda/NotifyUsers.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { fetchTodaysUsersToNotify } from "../db/query"
import { fetchUsersToNotify } from "../db/query"
import { SQSClient, SendMessageBatchCommand, SendMessageBatchCommandInput } from "@aws-sdk/client-sqs"
// import * as cdk from "aws-cdk-lib/core"
import { v4 as uuidv4 } from "uuid"

// SQS Confiig
const sqsClient = new SQSClient({ region: "eu-west-2" })

export const handler = async () => {
try {
const user_posts = await fetchTodaysUsersToNotify()
const user_posts = await fetchUsersToNotify()
const obj = Object.fromEntries(user_posts)

const messageId = uuidv4()
Expand All @@ -17,7 +16,7 @@ export const handler = async () => {
QueueUrl: process.env.SQS_QUEUE_URL,
Entries: Object.entries(obj).map((entry) => ({
Id: messageId,
MessageBody: JSON.stringify(entry),
MessageBody: JSON.stringify({ userId: entry[0], postsIds: entry[1] }),
DelaySeconds: 0,
MessageAttributes: {
key: {
Expand Down
19 changes: 18 additions & 1 deletion aws/backend/lambda/SendEmail.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import { APIGatewayProxyResult, SQSEvent } from "aws-lambda"
import { fetchuEmailserByUuid, fetchPostById } from "../db/query"

export const handler = async (event: SQSEvent): Promise<APIGatewayProxyResult> => {
console.log(event);
const eventBody = event.Records[0].body

// const eventBodyJson = JSON.parse(eventBody)
const user = eventBody[0]

const posts = eventBody[1]

console.log(user)
const userEmail = await fetchuEmailserByUuid(user)
console.log(userEmail)
console.log(posts)

// posts.forEach(async (post: string) => {
// const postRecord = await fetchPostById(post)
// console.log(postRecord)
// }
// )

try {
// fetch is available with Node.js 18
Expand Down
27 changes: 27 additions & 0 deletions aws/events/SendEmail.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"Records": [
{
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78",
"receiptHandle": "MessageReceiptHandle",
"body": [
"5c41c714-1d6b-43a6-8425-b6b2557f7dc8",
[
"6ab7a7c4-d2a6-4a23-a4b4-216e35ab5076",
"08e312a6-6fab-4c36-9033-f7b23a20ac4c",
"212d630e-31a7-4b31-b6af-f99d31542c42"
]
],
"attributes": {
"ApproximateReceiveCount": "1",
"SentTimestamp": "1523232000000",
"SenderId": "123456789012",
"ApproximateFirstReceiveTimestamp": "1523232000001"
},
"messageAttributes": {},
"md5OfBody": "71c0129f53bc10b50dc38f53533969e2",
"eventSource": "aws:sqs",
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
"awsRegion": "us-east-1"
}
]
}
2 changes: 1 addition & 1 deletion aws/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class TestStack extends cdk.Stack {
api.addIntegration("POST", "/subscribe", addUserSubscriptionLambda)

// Create an SQS event source for NotifyUsers Lambda to process messages from the queue
const eventSource = new lambdaEventSources.SqsEventSource(queue, { batchSize: 1 })
const eventSource = new lambdaEventSources.SqsEventSource(queue)
sendEmailLambda.addEventSource(eventSource)


Expand Down

0 comments on commit 6b3b227

Please sign in to comment.