feat: api send

This commit is contained in:
eneller
2026-03-18 09:18:27 +01:00
parent c6629234e1
commit 823d696d86
4 changed files with 52 additions and 2 deletions

42
server/src/routes/send.ts Normal file
View File

@@ -0,0 +1,42 @@
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';
const router = express.Router();
router.get('/:recipientID', 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) {
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'})
}
await db.transaction(async (t) =>{
await sender.decrement({balance: amount});
await recipient.increment({balance: amount});
await Transaction.create({
amount: amount,
senderID: sender.userID,
receiverID: recipient.userID
});
})
res.status(200).json({balance: sender.balance, amount: amount});
} catch (err) {
logger.error('Failed to commit transaction:', err);
res.status(500).json({ error: 'Failed to commit transaction' });
}
});
export default router;

View File

@@ -1,10 +1,11 @@
import express from 'express';
import { logger } from '../util/logging';
import Transaction from '../model/transaction';
import { requireAuth } from '../util/auth';
const router = express.Router();
router.get('/', async (req, res) => {
router.get('/', requireAuth, async (req, res) => {
try {
const transactions = await Transaction.findAll({ limit: 10 });
res.json(transactions);