Skip to content

Commit

Permalink
feat: update transaction api
Browse files Browse the repository at this point in the history
  • Loading branch information
conganhhcmus committed Mar 16, 2024
1 parent 775db14 commit 5d276d3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
2 changes: 1 addition & 1 deletion src/repositories/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ export const deleteUserById = (id: string) => UserModel.findByIdAndDelete(id);
export const updateUserById = (id: string, values: Record<string, any>) => 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 });
4 changes: 3 additions & 1 deletion src/routers/auth.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -139,4 +139,6 @@ export default (router: Router) => {
* - accessToken: []
*/
router.post('/reset-password/:id', verifyAccessToken, isAdminOrOwner, resetPassword);

router.get('/fetch-info', verifyAccessToken, fetchInfo);
};
14 changes: 14 additions & 0 deletions src/services/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
};

0 comments on commit 5d276d3

Please sign in to comment.