wip
This commit is contained in:
parent
bc5ca732b0
commit
a634cefb0c
|
@ -0,0 +1,32 @@
|
|||
import { Request, Response, NextFunction } from 'express';
|
||||
import prisma from '../structures/database';
|
||||
import type { RequestWithUser } from './auth';
|
||||
|
||||
export default (req: RequestWithUser, res: Response, next: NextFunction) => {
|
||||
// if (this.options.adminOnly && !user.isAdmin) { return res.status(401).json({ message: 'Invalid authorization' }); }
|
||||
if (!req.headers.authorization) return res.status(401).json({ message: 'No authorization header provided' });
|
||||
|
||||
const token = req.headers.authorization.split(' ')[1];
|
||||
if (!token) return res.status(401).json({ message: 'No authorization header provided' });
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
||||
JWT.verify(token, process.env.secret ?? '', async (error, decoded) => {
|
||||
if (error) return res.status(401).json({ message: 'Invalid token' });
|
||||
const id = (decoded as Decoded | undefined)?.sub ?? null;
|
||||
if (!id) return res.status(401).json({ message: 'Invalid authorization' });
|
||||
|
||||
const user = await prisma.users.findFirst({
|
||||
where: {
|
||||
id
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
username: true
|
||||
}
|
||||
});
|
||||
|
||||
if (!user) return res.status(401).json({ message: 'Invalid authorization' });
|
||||
req.user = user;
|
||||
next();
|
||||
});
|
||||
};
|
|
@ -0,0 +1,37 @@
|
|||
import { Response } from 'express';
|
||||
import prisma from '../../../../structures/database';
|
||||
import type { RequestWithUser } from '../../../../middlewares/auth';
|
||||
|
||||
export const middlewares = ['auth'];
|
||||
|
||||
export const run = async (req: RequestWithUser, res: Response) => {
|
||||
if (!req.body) return res.status(400).json({ message: 'No body provided' });
|
||||
const { coinId, amount, fiatId, noCostTransaction, purchasePrice, label, feePrice, purchaseDate }: {
|
||||
coinId: number;
|
||||
amount: number;
|
||||
fiatId: number;
|
||||
noCostTransaction: boolean;
|
||||
purchasePrice: number;
|
||||
label: string;
|
||||
feePrice: number;
|
||||
purchaseDate: string;
|
||||
} = req.body;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
if (!coinId || !amount || noCostTransaction === null || noCostTransaction === undefined) return res.sendStatus(400);
|
||||
|
||||
await prisma.wallet.create({
|
||||
data: {
|
||||
userId: req.user.id,
|
||||
coinId,
|
||||
amount,
|
||||
fiatId,
|
||||
purchasePrice,
|
||||
label,
|
||||
feePrice,
|
||||
paidPrice: amount * purchasePrice,
|
||||
purchaseDate
|
||||
}
|
||||
});
|
||||
|
||||
return res.sendStatus(200);
|
||||
};
|
Loading…
Reference in New Issue