aboutsummaryrefslogtreecommitdiff
path: root/src/controller/friend.ts
blob: e98f1e693371498a76b945c7b0a5b5f2999e5ea4 (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
/* 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 } from "../service/friend";
import jwt from "jsonwebtoken";

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 "jwt expired":
                        res.status(401).json({ error: "Request Expired" });
                        break;
                    case "invalid signature":
                        res.status(401).json({ error: "Request Invalid" });
                        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 _ = await friend(claims.userId, req.userId);
                        res.status(200).json({ message: "success" });
                    } catch (error) {
                        // @ts-ignore
                        if (error.code == "P2002") {
                            res.status(409).json({ error: "Already friends" });
                        } else {
                            console.error(error);
                            res.status(500).json({ error: "Internal Server Error" });
                        }
                    }
                }
            }
        });
    } catch(error) {
        res.status(500).json({ error: "Internal Server Error" });
        console.error(error);
    }
}

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" });
        } 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(200).json({ friends: friends });
    } catch(error) {
        res.status(500).json({ error: "Internal Server Error" });
        console.error(error);
    }
}

export {
    friendRequest,
    addFriend,
    removeFriend,
    listFriends
}