generated from lucien/api-template
add: added routes and improved frontend
This commit is contained in:
parent
37c96f5341
commit
94e4d5750f
15 changed files with 1039 additions and 172 deletions
|
@ -1,11 +1,11 @@
|
|||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { getConnection, getUserAccounts, getUserCards } = require('../libs/mysql');
|
||||
const { getConnection, getUserAccounts, getUserCards, getUserTransfers, setAccountBalance, getAccount, addTransfer } = require('../libs/mysql');
|
||||
const { checkAuth } = require('../libs/middlewares');
|
||||
|
||||
router.post('/', checkAuth, async (req, res) => {
|
||||
const user = req.user;
|
||||
res.send({ id: user.id, name: user.name, lastname: user.lastname, admin: user.admin });
|
||||
res.send(user);
|
||||
});
|
||||
|
||||
router.post('/accounts', checkAuth, async (req, res) => {
|
||||
|
@ -13,11 +13,7 @@ router.post('/accounts', checkAuth, async (req, res) => {
|
|||
const connection = await getConnection();
|
||||
const accounts = await getUserAccounts(connection, user.id);
|
||||
connection.end();
|
||||
|
||||
if (!accounts[0]) {
|
||||
return res.status(404).send({ error: 'No accounts found' });
|
||||
}
|
||||
res.send({ accounts: accounts });
|
||||
res.send(accounts);
|
||||
});
|
||||
|
||||
router.post('/cards', checkAuth, async (req, res) => {
|
||||
|
@ -25,11 +21,47 @@ router.post('/cards', checkAuth, async (req, res) => {
|
|||
const connection = await getConnection();
|
||||
const cards = await getUserCards(connection, user.id);
|
||||
connection.end();
|
||||
res.send(cards);
|
||||
});
|
||||
|
||||
if (!cards[0]) {
|
||||
return res.status(404).send({ error: 'No cards found' });
|
||||
router.post('/transfers', checkAuth, async (req, res) => {
|
||||
const user = req.user;
|
||||
const connection = await getConnection();
|
||||
const transfers = await getUserTransfers(connection, user.id);
|
||||
connection.end();
|
||||
res.send(transfers);
|
||||
});
|
||||
|
||||
router.post('/send-money', checkAuth, async (req, res) => {
|
||||
const user = req.user;
|
||||
const { account_from_id, account_to_id, amount, name } = req.body;
|
||||
|
||||
if (!account_from_id || !account_to_id || !amount || !name) {
|
||||
return res.status(400).send({ error: 'Missing required fields' });
|
||||
}
|
||||
res.send({ cards: cards });
|
||||
|
||||
const connection = await getConnection();
|
||||
const accountFrom = await getAccount(connection, account_from_id);
|
||||
const accountTo = await getAccount(connection, account_to_id);
|
||||
|
||||
if (!accountFrom[0] || !accountTo[0]) {
|
||||
return res.status(400).send({ error: 'Invalid account ID' });
|
||||
}
|
||||
|
||||
if (accountFrom[0].client_id !== user.id) {
|
||||
return res.status(403).send({ error: 'You are not authorized to send money from this account' });
|
||||
}
|
||||
|
||||
if (accountFrom[0].balance < amount) {
|
||||
return res.status(400).send({ error: 'Insufficient funds' });
|
||||
}
|
||||
|
||||
await setAccountBalance(connection, account_from_id, accountFrom[0].balance - amount);
|
||||
await setAccountBalance(connection, account_to_id, accountTo[0].balance + amount);
|
||||
await addTransfer(connection, account_from_id, account_to_id, name, amount);
|
||||
connection.end();
|
||||
|
||||
res.send({ message: 'Money sent successfully' });
|
||||
});
|
||||
|
||||
module.exports = router;
|
Loading…
Add table
Add a link
Reference in a new issue