diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2025-04-15 13:01:38 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2025-04-15 13:01:38 +0530 |
commit | 9c911e79c18d09224b90546ef29c2e9986e4cc0c (patch) | |
tree | 5de085d2c61a8e606caeb787dd391b3de5477fc4 | |
parent | 549f75c9e271ca9b8b8f919ee996526b31e659a8 (diff) |
Added functionality to delete a transaction
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | src/controller/transaction.ts | 22 | ||||
-rw-r--r-- | src/service/transaction.ts | 24 |
3 files changed, 41 insertions, 7 deletions
diff --git a/package.json b/package.json index eab1f15..24c152a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "financer", - "version": "0.0.2", + "version": "0.0.3", "description": "Pocket Money Tracker", "main": "dist/index.js", "scripts": { diff --git a/src/controller/transaction.ts b/src/controller/transaction.ts index b5fcf99..6c818d6 100644 --- a/src/controller/transaction.ts +++ b/src/controller/transaction.ts @@ -16,7 +16,7 @@ */ import { Request, Response } from "express"; -import { create, read } from "../service/transaction"; +import { create, read, del } from "../service/transaction"; const addNew = async (req: Request, res: Response) => { try { @@ -42,11 +42,23 @@ const get = async (req: Request, res: Response) => { const remove = async (req: Request, res: Response) => { try { - // TODO: check ownership before deletion - res.status(200).json({ "message": "work in progress" }) + // @ts-ignore + await del(req.userId, req.body.id) + res.status(200).json({ "message": "success" }) } catch(error) { - res.status(500).json({ error: "Internal Server Error" }); - console.error(error); + // @ts-ignore + switch (error.message) { + case "403": + res.status(403).json({ error: "Not Authorized To Do This Task" }); + break; + case "404": + res.status(404).json({ error: "Not Found" }); + break; + default: + res.status(500).json({ error: "Internal Server Error" }); + console.error(error); + break; + } } } diff --git a/src/service/transaction.ts b/src/service/transaction.ts index 29a9faa..8f1c99d 100644 --- a/src/service/transaction.ts +++ b/src/service/transaction.ts @@ -42,7 +42,29 @@ const read = async (userId: Number, type: String) => { }) } +// TODO: ig collaborated transactions would need more logic +const del = async (userId: Number, transactionId: String) => { + const t = await prisma.transaction.findUnique({ + // @ts-ignore + where: { id: transactionId } + }) + + if (t) { + if (t.userId == userId) { + return prisma.transaction.delete({ + // @ts-ignore + where: { id: transactionId } + }) + } else { + throw new Error("403") + } + } else { + throw new Error("404") + } +} + export { create, - read + read, + del } |