fix: send api
This commit is contained in:
14
server/src/messages/Send.ts
Normal file
14
server/src/messages/Send.ts
Normal file
@@ -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
|
||||
){}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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' });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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"]
|
||||
|
||||
Reference in New Issue
Block a user