diff options
Diffstat (limited to 'src/controller')
-rw-r--r-- | src/controller/friend.ts | 38 |
1 files changed, 33 insertions, 5 deletions
diff --git a/src/controller/friend.ts b/src/controller/friend.ts index a73d3d1..5d76350 100644 --- a/src/controller/friend.ts +++ b/src/controller/friend.ts @@ -17,8 +17,9 @@ import { Request, Response } from "express"; import { getFriendToken } from "../util/auth"; -import { friend, unfriend, list } from "../service/friend"; +import { friend, unfriend, list, get } from "../service/friend"; import jwt from "jsonwebtoken"; +import {readById} from "../service/user"; const friendRequest = async (req: Request, res: Response) => { try { @@ -55,12 +56,21 @@ const addFriend = async (req: Request, res: Response) => { } else { try { // @ts-ignore - const _ = await friend(claims.userId, req.userId); - res.status(200).json({ message: "success" }); + const f = await friend(claims.userId, req.userId); + // @ts-ignore + delete f["password"]; + // @ts-ignore + delete f["refreshTokenVersion"]; + // @ts-ignore + delete f["createdAt"]; + // @ts-ignore + delete f["updatedAt"]; + + res.status(200).json({ friend: f }); } catch (error) { // @ts-ignore if (error.code == "P2002") { - res.status(409).json({ error: "Already friends" }); + res.status(409).json({error: "Already friends"}); } else { res.status(500).json({ error: "Internal Server Error" }); console.error(error); @@ -107,9 +117,27 @@ const listFriends = async (req: Request, res: Response) => { } } +const getFriend = async (req: Request, res: Response) => { + try { + // @ts-ignore + const friend = await get(req.userId, parseInt(req.params.friendId)) // TODO: handle non int value passed (bad req) + // @ts-ignore + res.status(200).json({ friend: friend }); + } catch(error) { + // @ts-ignore + if (error.message == "forbidden") { + res.status(403).json({ error: "You are not allowed to view this profile." }); + } else { + res.status(500).json({ error: "Internal Server Error" }); + console.error(error); + } + } +} + export { friendRequest, addFriend, removeFriend, - listFriends + listFriends, + getFriend } |