Skip to content

Commit

Permalink
updated notification function to use switch case for comments, likes …
Browse files Browse the repository at this point in the history
…and post. Refractor createPost, like and addComment endpoint to actual use the new notification added
  • Loading branch information
lokytech5 committed Sep 27, 2024
1 parent f4b8d47 commit 58bad5f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 14 deletions.
65 changes: 54 additions & 11 deletions backend/services/post-api/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { v1 as uuidv1 } from 'uuid';
import { BatchGetCommand, BatchWriteCommand, GetCommand, PutCommand, QueryCommand, ScanCommand } from "@aws-sdk/lib-dynamodb";
import { buildResponse } from "../../../lib/responseUtils";
import { sendNotification } from "./notificationUtils";
import { getUserDetails } from "./userUtils";

const POSTS_TABLE = process.env.POSTS_TABLE;
const USERS_TABLE = process.env.USERS_TABLE_NAME;
Expand Down Expand Up @@ -61,19 +62,10 @@ export const createPost = async (req: Request, res: Response) => {
};

try {
const usercommand = new GetCommand(userParams);
const userData = await docClient.send(usercommand);

const user = userData.Item;

if(!user) {
return res.status(404).json({error: "User not found "});
}
const user = await getUserDetails(userId);

const { email, fullName } = user;
console.log(user);



const postCommand = new PutCommand(postParams);
await docClient.send(postCommand);

Expand All @@ -86,6 +78,7 @@ export const createPost = async (req: Request, res: Response) => {
userEmail: email,
userName: fullName,
postContent: content,
notificationType: "post_created"
});

res.status(201).json(buildResponse(postParams.Item));
Expand Down Expand Up @@ -271,6 +264,31 @@ export const addComment = async (req: Request, res: Response) => {
try {
const command = new PutCommand(params);
await docClient.send(command);

const postParams = {
TableName: process.env.POSTS_TABLE!,
Key: { PostID: postId }
};

const postCommand = new GetCommand(postParams);
const postData = await docClient.send(postCommand);
const post = postData.Item;

if(!post) {
return res.status(404).json({ error: "Post not found" });
}

const user = await getUserDetails(post.UserID);

const { email , fullName } = user;

await sendNotification({
userEmail: email,
userName: fullName,
postContent: `Your poost with ID: ${postId} has received a new comment: "${content}"`,
notificationType: "post_commented"
});

res.status(201).json(buildResponse(params.Item));
} catch (error) {
if (error instanceof Error) {
Expand Down Expand Up @@ -325,6 +343,31 @@ export const likePost = async (req: Request, res: Response) => {
try {
const command = new PutCommand(params);
await docClient.send(command);

const postParams = {
TableName: process.env.POSTS_TABLE!,
Key: { PostID: postId }
};

const postCommand = new GetCommand(postParams);
const postData = await docClient.send(postCommand);
const post = postData.Item;

if(!post){
return res.status(404).json({ error: "Post not found" });
}

const user = await getUserDetails(post.UserID);

const { email, fullName } = user;

await sendNotification({
userEmail: email,
userName: fullName,
postContent: `Your post with ID: ${postId} has been liked.`,
notificationType: "post_liked",
})

res.status(201).json(buildResponse(params.Item));
} catch (error) {
if (error instanceof Error) {
Expand Down
30 changes: 27 additions & 3 deletions backend/services/post-api/src/notificationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,36 @@ import { sendEmail } from "../../../lib/sendEmailUtils";


export const sendNotification = async (event: any) => {
const { userEmail, userName, postContent } = event;
const { userEmail, userName, postContent, notificationType } = event;

let subject = " ";
let body = " ";

switch(notificationType) {
case 'post_created':
subject = "Notification: Your post was created";
body = `Hello ${userName}, \n\nYour post "${postContent}" has been successfully created. `;
break;

case 'post_liked':
subject = "Notification: Your post was liked";
body = `Hello ${userName}, \n\nYour post "${postContent}" has been liked. `;
break;

case 'post_commented':
subject = "Notification: New comment on your post";
body = `Hello ${userName}, \n\nYour post "${postContent}" has received a new comment. `;
break;

default:
subject = "Notification";
body = `Hello ${userName}, \n\nYou have a new notification about your post: "${postContent}". `;
}

const emailParams = {
toAddresses: [userEmail],
subject: "Notification: Your post was created",
body:`Hello ${userName},\n\nYour post "${postContent}" has been successfully created.`,
subject: subject,
body: body,
source: "lokosman5@hotmail.com",
};

Expand Down

0 comments on commit 58bad5f

Please sign in to comment.