aboutsummaryrefslogtreecommitdiff
path: root/src/controller/friend.ts
blob: 5d76350e017affc284607941870e68690f17e9b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* Financer - Pocket Money Tracker
 * Copyright (C) 2025  Vidhu Kant Sharma <vidhukant@vidhukant.com>

 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

import { Request, Response } from "express";
import { getFriendToken } from "../util/auth";
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 {
        // @ts-ignore
        res.status(200).json({ token: getFriendToken(req.userId) })
    } catch (error) {
        res.status(500).json({ error: "Internal Server Error" });
        console.error(error);
    }
}

const addFriend = async (req: Request, res: Response) => {
    try {
        // @ts-ignore
        jwt.verify(req.body.token as string, process.env.FRIEND_TOKEN_SECRET, async (err, claims) => {
            if (err) {
                // @ts-ignore
                switch (err.message) {
                    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)
                        break;
                }
            } else {
                // @ts-ignore
                if (claims.userId == req.userId) {
                    res.status(400).json({ message: "Attempted to become friends with self" });
                } else {
                    try {
                        // @ts-ignore
                        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"});
                        } else {
                            res.status(500).json({ error: "Internal Server Error" });
                            console.error(error);
                        }
                    }
                }
            }
        });
    } catch(error) {
        res.status(500).json({ error: "Internal Server Error" });
        console.error(error);
    }
}

const removeFriend = async (req: Request, res: Response) => {
    try {
        // @ts-ignore
        if (req.userId == req.body.friendId) {
            res.status(400).json({ message: "Attempted to unfriend self" });
        } else {
            // @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" });
        console.error(error);
    }
}

const listFriends = async (req: Request, res: Response) => {
    try {
        // @ts-ignore
        const friends = await list(req.userId)
        // @ts-ignore
        res.status(friends.length > 0 ? 200 : 201).json({ friends: friends });
    } catch(error) {
        res.status(500).json({ error: "Internal Server Error" });
        console.error(error);
    }
}

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,
    getFriend
}