feat: api send
This commit is contained in:
@@ -3,6 +3,7 @@ import cors from "cors";
|
|||||||
import cookieParser from "cookie-parser";
|
import cookieParser from "cookie-parser";
|
||||||
import transactionsRouter from './routes/transactions';
|
import transactionsRouter from './routes/transactions';
|
||||||
import authRouter from './routes/auth';
|
import authRouter from './routes/auth';
|
||||||
|
import sendRouter from './routes/send';
|
||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
dotenv.config({quiet: true});
|
dotenv.config({quiet: true});
|
||||||
import { db, testConnection } from "./util/db";
|
import { db, testConnection } from "./util/db";
|
||||||
@@ -20,6 +21,7 @@ app.get("/api/health", (req: Request, res: Response) => {
|
|||||||
|
|
||||||
app.use('/api/auth', authRouter);
|
app.use('/api/auth', authRouter);
|
||||||
app.use('/api/transactions', transactionsRouter);
|
app.use('/api/transactions', transactionsRouter);
|
||||||
|
app.use('/api/send', sendRouter);
|
||||||
|
|
||||||
const PORT: number = parseInt(process.env.FM_PORT as string) || 3000;
|
const PORT: number = parseInt(process.env.FM_PORT as string) || 3000;
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
import { Table, Column, Model, CreatedAt, DataType} from 'sequelize-typescript';
|
import { Table, Column, Model, CreatedAt, DataType, Scopes} from 'sequelize-typescript';
|
||||||
|
|
||||||
|
@Scopes(() => ({
|
||||||
|
withoutPassword: {
|
||||||
|
attributes: { exclude: ['password'] }
|
||||||
|
}
|
||||||
|
}))
|
||||||
@Table
|
@Table
|
||||||
export default class User extends Model{
|
export default class User extends Model{
|
||||||
|
|
||||||
|
|||||||
42
server/src/routes/send.ts
Normal file
42
server/src/routes/send.ts
Normal 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;
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { logger } from '../util/logging';
|
import { logger } from '../util/logging';
|
||||||
import Transaction from '../model/transaction';
|
import Transaction from '../model/transaction';
|
||||||
|
import { requireAuth } from '../util/auth';
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
router.get('/', async (req, res) => {
|
router.get('/', requireAuth, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const transactions = await Transaction.findAll({ limit: 10 });
|
const transactions = await Transaction.findAll({ limit: 10 });
|
||||||
res.json(transactions);
|
res.json(transactions);
|
||||||
|
|||||||
Reference in New Issue
Block a user