diff options
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | src/controller/transaction.ts | 19 | ||||
-rw-r--r-- | src/route/transaction.ts | 4 | ||||
-rw-r--r-- | src/service/transaction.ts | 16 |
4 files changed, 38 insertions, 3 deletions
diff --git a/package.json b/package.json index fa2d502..c49b152 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "financer", - "version": "0.4.0", + "version": "0.5.0", "description": "Pocket Money Tracker", "main": "dist/index.js", "scripts": { diff --git a/src/controller/transaction.ts b/src/controller/transaction.ts index 6c818d6..9d4d6da 100644 --- a/src/controller/transaction.ts +++ b/src/controller/transaction.ts @@ -16,7 +16,7 @@ */ import { Request, Response } from "express"; -import { create, read, del } from "../service/transaction"; +import { create, read, readOne, del } from "../service/transaction"; const addNew = async (req: Request, res: Response) => { try { @@ -40,6 +40,22 @@ const get = async (req: Request, res: Response) => { } } +const getOne = async (req: Request, res: Response) => { + try { + // @ts-ignore + const transaction = await readOne(req.userId, parseInt(req.params.transactionId)); // TODO: handle non int value passed (bad req) + res.status(200).json({ transaction: transaction }); + } catch(error) { + // @ts-ignore + if (error.message == "forbidden") { + res.status(403).json({ error: "You are not allowed to view this transaction." }); + } else { + res.status(500).json({ error: "Internal Server Error" }); + console.error(error); + } + } +} + const remove = async (req: Request, res: Response) => { try { // @ts-ignore @@ -65,5 +81,6 @@ const remove = async (req: Request, res: Response) => { export { addNew, get, + getOne, remove } diff --git a/src/route/transaction.ts b/src/route/transaction.ts index d42f94b..c0893fc 100644 --- a/src/route/transaction.ts +++ b/src/route/transaction.ts @@ -16,11 +16,13 @@ */ import { Router } from "express"; -import { get, addNew, remove } from "../controller/transaction"; +import { get, getOne, addNew, remove } from "../controller/transaction"; +import {getFriend} from "../controller/friend"; const router: Router = Router(); router.get("/", get); +router.get("/:transactionId", getOne); router.post("/", addNew); router.delete("/", remove); diff --git a/src/service/transaction.ts b/src/service/transaction.ts index 8f1c99d..c3a2408 100644 --- a/src/service/transaction.ts +++ b/src/service/transaction.ts @@ -42,6 +42,21 @@ const read = async (userId: Number, type: String) => { }) } +const readOne = async (userId: Number, id: Number) => { + const transaction = await prisma.transaction.findUnique({ + // @ts-ignore + where: { id: id }, + }) + + // NOTE: this gon change a bit when there are also collaborators + // @ts-ignore + if (transaction.userId !== userId) { + throw new Error("forbidden") + } + + return transaction +} + // TODO: ig collaborated transactions would need more logic const del = async (userId: Number, transactionId: String) => { const t = await prisma.transaction.findUnique({ @@ -66,5 +81,6 @@ const del = async (userId: Number, transactionId: String) => { export { create, read, + readOne, del } |