aboutsummaryrefslogtreecommitdiff
path: root/src/controller/friend.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/controller/friend.ts')
-rw-r--r--src/controller/friend.ts110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/controller/friend.ts b/src/controller/friend.ts
new file mode 100644
index 0000000..e98f1e6
--- /dev/null
+++ b/src/controller/friend.ts
@@ -0,0 +1,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
+}