From 85b5b4c8c722da059114fbfd4ba19ab15f334868 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Wed, 16 Apr 2025 12:27:03 +0530 Subject: Responding with HTTP 400 upon trying to unfriend self --- package.json | 2 +- src/controller/friend.ts | 23 ++++++++++++++--------- src/service/friend.ts | 2 -- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index eebdd81..fc5203c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "financer", - "version": "0.1.0", + "version": "0.1.1", "description": "Pocket Money Tracker", "main": "dist/index.js", "scripts": { diff --git a/src/controller/friend.ts b/src/controller/friend.ts index e98f1e6..a73d3d1 100644 --- a/src/controller/friend.ts +++ b/src/controller/friend.ts @@ -37,12 +37,12 @@ const addFriend = async (req: Request, res: Response) => { if (err) { // @ts-ignore switch (err.message) { - case "jwt expired": - res.status(401).json({ error: "Request Expired" }); - break; case "invalid signature": res.status(401).json({ error: "Request Invalid" }); break; + case "jwt expired": + res.status(410).json({ error: "Request Expired" }); + break; default: res.status(500).json({ error: "Internal Server Error" }); console.error(err) @@ -62,8 +62,8 @@ const addFriend = async (req: Request, res: Response) => { if (error.code == "P2002") { res.status(409).json({ error: "Already friends" }); } else { - console.error(error); res.status(500).json({ error: "Internal Server Error" }); + console.error(error); } } } @@ -78,11 +78,16 @@ const addFriend = async (req: Request, res: Response) => { const removeFriend = async (req: Request, res: Response) => { try { // @ts-ignore - const { count } = await unfriend(req.userId, req.body.friendId) - if (count > 0) { - res.status(200).json({ message: "success" }); + if (req.userId == req.body.friendId) { + res.status(400).json({ message: "Attempted to unfriend self" }); } else { - res.status(404).json({ error: "Friend not found" }); + // @ts-ignore + const { count } = await unfriend(req.userId, req.body.friendId) + if (count > 0) { + res.status(200).json({ message: "success" }); + } else { + res.status(404).json({ error: "Friend not found" }); + } } } catch(error) { res.status(500).json({ error: "Internal Server Error" }); @@ -95,7 +100,7 @@ const listFriends = async (req: Request, res: Response) => { // @ts-ignore const friends = await list(req.userId) // @ts-ignore - res.status(200).json({ friends: friends }); + res.status(friends.length > 0 ? 200 : 201).json({ friends: friends }); } catch(error) { res.status(500).json({ error: "Internal Server Error" }); console.error(error); diff --git a/src/service/friend.ts b/src/service/friend.ts index c7f4b6b..6986858 100644 --- a/src/service/friend.ts +++ b/src/service/friend.ts @@ -51,8 +51,6 @@ const list = async (userId: Number) => { include: { friend: true } }) - // TODO: check if no friends - // @ts-ignore return friendsRaw.map((x: any) => { const friend = x.friend; -- cgit v1.2.3