From 5d276d31bf9429b756684a2d15e2f96609784881 Mon Sep 17 00:00:00 2001 From: conganhhcmus Date: Sat, 16 Mar 2024 10:08:52 +0700 Subject: [PATCH] feat: update transaction api --- src/controllers/auth.ts | 8 ++++++++ src/repositories/users.ts | 2 +- src/routers/auth.ts | 4 +++- src/services/auth.ts | 14 ++++++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/controllers/auth.ts b/src/controllers/auth.ts index a32959a..cefd612 100644 --- a/src/controllers/auth.ts +++ b/src/controllers/auth.ts @@ -37,3 +37,11 @@ export const resetPassword = async (req: Request, res: Response) => { // res.cookie(REFRESH_TOKEN_KEY, result.refreshToken); return res.status(200).json(result); }; + +export const fetchInfo = async (req: Request, res: Response) => { + const payload = req['identity'] as UserJwtPayload; + const result = await auth.fetchInfo(payload); + // res.cookie(TOKEN_KEY, result.token); + // res.cookie(REFRESH_TOKEN_KEY, result.refreshToken); + return res.status(200).json(result.token); +}; diff --git a/src/repositories/users.ts b/src/repositories/users.ts index 59b8d93..f20e4de 100644 --- a/src/repositories/users.ts +++ b/src/repositories/users.ts @@ -26,4 +26,4 @@ export const deleteUserById = (id: string) => UserModel.findByIdAndDelete(id); export const updateUserById = (id: string, values: Record) => UserModel.findByIdAndUpdate(id, values, { new: true }); export const updateWalletById = (id: string, amount: number) => - UserModel.findOneAndUpdate({ _id: new Types.ObjectId(id) }, { $set: { wallet: amount } }, { new: true }); + UserModel.findOneAndUpdate({ _id: new Types.ObjectId(id) }, { $inc: { wallet: amount } }, { new: true }); diff --git a/src/routers/auth.ts b/src/routers/auth.ts index 4038f6f..ddb4ba4 100644 --- a/src/routers/auth.ts +++ b/src/routers/auth.ts @@ -1,5 +1,5 @@ import { Router } from 'express'; -import { login, register, resetPassword, resetToken } from '@/controllers/auth'; +import { fetchInfo, login, register, resetPassword, resetToken } from '@/controllers/auth'; import { verifyAccessToken, verifyRefreshToken } from '@/middlewares/authToken'; import { isAdminOrOwner } from '@/middlewares/usersValidation'; @@ -139,4 +139,6 @@ export default (router: Router) => { * - accessToken: [] */ router.post('/reset-password/:id', verifyAccessToken, isAdminOrOwner, resetPassword); + + router.get('/fetch-info', verifyAccessToken, fetchInfo); }; diff --git a/src/services/auth.ts b/src/services/auth.ts index 1e38068..8db90b4 100644 --- a/src/services/auth.ts +++ b/src/services/auth.ts @@ -88,3 +88,17 @@ export const resetPassword = async (data: User, newPassword: string): Promise<{ return { token, refreshToken }; }; + +export const fetchInfo = async (payload: UserJwtPayload): Promise<{ token: string; refreshToken: string }> => { + const user = await getUserByUserName(payload.username); + let token = ''; + const refreshToken = ''; + if (!user) { + throw new BadRequestError(USER_NOT_FOUND); + } + if (payload.wallet !== user.wallet) { + const { password, ...payloadNew } = user.toObject(); + token = createToken(payloadNew); + } + return { token, refreshToken }; +};