/* Financer - Pocket Money Tracker * Copyright (C) 2025 Vidhu Kant Sharma * 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 . */ 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 "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 _ = 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 { 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); } } export { friendRequest, addFriend, removeFriend, listFriends }