From 7f3326c4a5046323094a81d01781b95f9f8f498a Mon Sep 17 00:00:00 2001 From: eneller Date: Wed, 18 Mar 2026 10:27:26 +0100 Subject: [PATCH] fix: send api --- server/src/messages/Send.ts | 14 ++++++++++++++ server/src/model/transaction.ts | 3 +++ server/src/routes/send.ts | 32 ++++++++++++++++++-------------- server/src/util/auth.ts | 1 + tsconfig.json | 3 ++- 5 files changed, 38 insertions(+), 15 deletions(-) create mode 100644 server/src/messages/Send.ts diff --git a/server/src/messages/Send.ts b/server/src/messages/Send.ts new file mode 100644 index 0000000..1c94fbc --- /dev/null +++ b/server/src/messages/Send.ts @@ -0,0 +1,14 @@ +export class SendRequest{ + constructor( + public recipientID: string, + public amount: number, + public reference: string + ){} +} + +export class SendResponse{ + constructor( + public balance: number, + public message: string + ){} +} \ No newline at end of file diff --git a/server/src/model/transaction.ts b/server/src/model/transaction.ts index 56228da..5a84480 100644 --- a/server/src/model/transaction.ts +++ b/server/src/model/transaction.ts @@ -5,6 +5,9 @@ import User from './user'; export default class Transaction extends Model{ @Column declare amount: number; + + @Column + declare reference: string; @Column @ForeignKey(()=> User) diff --git a/server/src/routes/send.ts b/server/src/routes/send.ts index 8dc283d..4af957f 100644 --- a/server/src/routes/send.ts +++ b/server/src/routes/send.ts @@ -1,41 +1,45 @@ -import express from 'express'; +import express from 'express'; import { logger } from '../util/logging'; import { requireAuth } from '../util/auth'; import User from '../model/user'; import { db } from '../util/db'; import Transaction from '../model/transaction'; +import { SendRequest, SendResponse} from '../messages/Send'; const router = express.Router(); -router.get('/:recipientID', requireAuth, async (req, res) => { +router.post('/', requireAuth, async (req, res) => { try { - let sender = res.locals.user as User; - let amount = Number(req.query.amount); - let recipient = await User.findOne({where: {userID: req.params.recipientID}}) - if ( amount <= 0) { + const sender = res.locals.user as User; + const data : SendRequest = req.body; + const recipient = await User.findOne({where: {userID: data.recipientID}}) + if ( Number(data.amount) <= 0) { + // TODO return SendResponse here and everywhere else in this file return res.status(400).json({ error: 'Invalid transfer amount' }); } if (!recipient) { return res.status(404).json({ error: 'Recipient not found' }); } - if (sender.balance < amount){ - res.status(400).json({error: 'Insufficient balance'}) + if (Number(sender.balance) < Number(data.amount)){ + logger.error(`Insufficient balance: ${sender.balance} < ${data.amount}`) + return res.status(402).json({error: 'Insufficient balance'}) } await db.transaction(async (t) =>{ - await sender.decrement({balance: amount}); - await recipient.increment({balance: amount}); + await sender.decrement({balance: data.amount}); + await recipient.increment({balance: data.amount}); await Transaction.create({ - amount: amount, + amount: data.amount, senderID: sender.userID, - receiverID: recipient.userID + receiverID: recipient.userID, + reference: data.reference }); }) - res.status(200).json({balance: sender.balance, amount: amount}); + return res.status(200).json({balance: sender.balance, amount: data.amount}); } catch (err) { logger.error('Failed to commit transaction:', err); - res.status(500).json({ error: 'Failed to commit transaction' }); + return res.status(500).json({ error: 'Failed to commit transaction' }); } }); diff --git a/server/src/util/auth.ts b/server/src/util/auth.ts index 99f2870..118bab2 100644 --- a/server/src/util/auth.ts +++ b/server/src/util/auth.ts @@ -30,6 +30,7 @@ async function requireAuth(req: Request, res: Response, next: NextFunction) { if (!user) { return res.status(401).json({ error: 'Unauthorized: User not found' }); } + //TODO extend req instead of using res.locals res.locals.user = user; next(); } catch (err) { diff --git a/tsconfig.json b/tsconfig.json index 5bead01..024f663 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,7 +10,8 @@ "module": "es2022", "lib": ["es2022", "dom"], "paths": { - "@model/*": ["server/src/model/*"] + "@model/*": ["server/src/model/*"], + "@message/*": ["server/src/messages/*"], } }, "exclude": ["node_modules"]