This commit is contained in:
Pitu 2021-06-20 11:51:01 +09:00
parent bc5ca732b0
commit a634cefb0c
2 changed files with 69 additions and 0 deletions

View File

@ -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();
});
};

View File

@ -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);
};