Skip to content

Commit

Permalink
refactor em fix self notification (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunocorrea23 authored Jul 6, 2023
1 parent 264f828 commit 2d4e635
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions routes/item/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,34 @@ export const handler: Handlers<ItemPageData, State> = {
return new Response(null, { status: 400 });
}

const itemId = ctx.params.id;
const user = await getUserBySession(ctx.state.sessionId);
const item = await getItem(itemId);

if (item === null || user === null) {
return new Response(null, { status: 404 });
}

const comment: Comment = {
userId: user!.id,
itemId: ctx.params.id,
userId: user.id,
itemId: itemId,
text,
...newCommentProps(),
};
await createComment(comment);

const item = await getItem(comment.itemId);

const notification: Notification = {
userId: item!.userId,
type: "comment",
text: `${user!.login} commented on your post: ${item!.title}`,
originUrl: `/item/${ctx.params.id}`,
...newNotificationProps(),
};
await createNotification(notification);
if (item.userId !== user.id) {
const notification: Notification = {
userId: item.userId,
type: "comment",
text: `${user.login} commented on your post: ${item.title}`,
originUrl: `/item/${itemId}`,
...newNotificationProps(),
};
await createNotification(notification);
}

return redirect(`/item/${ctx.params.id}`);
return redirect(`/item/${itemId}`);
},
};

Expand Down

0 comments on commit 2d4e635

Please sign in to comment.