diff --git a/aws/backend/db/query.ts b/aws/backend/db/query.ts index 026b560..bd44701 100644 --- a/aws/backend/db/query.ts +++ b/aws/backend/db/query.ts @@ -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") @@ -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, @@ -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 @@ -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) diff --git a/aws/backend/lambda/NotifyUsers.ts b/aws/backend/lambda/NotifyUsers.ts index 549b503..e29ffaa 100644 --- a/aws/backend/lambda/NotifyUsers.ts +++ b/aws/backend/lambda/NotifyUsers.ts @@ -1,6 +1,5 @@ -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 @@ -8,7 +7,7 @@ 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() @@ -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: { diff --git a/aws/backend/lambda/SendEmail.ts b/aws/backend/lambda/SendEmail.ts index 17eb494..6d3c080 100644 --- a/aws/backend/lambda/SendEmail.ts +++ b/aws/backend/lambda/SendEmail.ts @@ -1,7 +1,24 @@ import { APIGatewayProxyResult, SQSEvent } from "aws-lambda" +import { fetchuEmailserByUuid, fetchPostById } from "../db/query" export const handler = async (event: SQSEvent): Promise => { - 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 diff --git a/aws/events/SendEmail.json b/aws/events/SendEmail.json new file mode 100644 index 0000000..fee3e94 --- /dev/null +++ b/aws/events/SendEmail.json @@ -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" + } + ] +} \ No newline at end of file diff --git a/aws/lib/stack.ts b/aws/lib/stack.ts index 367f658..d7d0131 100644 --- a/aws/lib/stack.ts +++ b/aws/lib/stack.ts @@ -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)