From 98b26f2ef7ab77d74ef0c3ceeb58ce8e08fc0920 Mon Sep 17 00:00:00 2001 From: LuongDangDoanh Date: Sun, 30 Jun 2024 18:58:58 +0700 Subject: [PATCH] feat:block-unblock-account --- src/modules/user/user.controllers.ts | 47 ++++++++++++++++++++++++++++ src/modules/user/user.messages.ts | 5 ++- src/modules/user/user.middlewares.ts | 8 +++-- src/modules/user/user.requests.ts | 7 +++++ src/modules/user/user.routes.ts | 10 ++++++ src/modules/user/user.schema.ts | 12 +++++++ src/modules/user/user.services.ts | 46 +++++++++++++++++++++++++-- 7 files changed, 129 insertions(+), 6 deletions(-) diff --git a/src/modules/user/user.controllers.ts b/src/modules/user/user.controllers.ts index abde693..898c6be 100644 --- a/src/modules/user/user.controllers.ts +++ b/src/modules/user/user.controllers.ts @@ -9,6 +9,7 @@ import decrypt, { encrypt } from '~/utils/crypto' import { OTP_MESSAGES } from '../otp/otp.messages' import { USER_MESSAGES } from './user.messages' import { + BlockBody, LoginRequestBody, LogoutReqBody, RefreshTokenReqBody, @@ -19,6 +20,7 @@ import { } from './user.requests' import User from './user.schema' import usersService from './user.services' +import databaseService from '~/database/database.services' export const registerController = async ( req: Request, @@ -253,3 +255,48 @@ export const refreshTokenController = async ( data: { access_token } }) } + +export const blockAccountController = async ( + req: Request, + res: Response +) => { + console.log(req.cookies['refresh_token']) + await usersService.logout(req.cookies['refresh_token']) + res.clearCookie('refresh_token') + try { + const reason = req.body.description_reason + const picture_image_prove = req.body.picture_image_prove + const timeBlock = new Date() + const user_id = req.body._id + await usersService.blockAccount( + user_id, + reason, + picture_image_prove, + timeBlock + ) + return res.json({ + message: USER_MESSAGES.USER_HAS_BEEN_BLOCKED + }) + } catch (error) { + return res.status(400).json({ + message: 'Bad request' + }) + } +} + +export const unblockAccountController = async ( + req: Request, + res: Response +) => { + try { + const user_id = req.body._id + await usersService.unblockAccount(user_id) + return res.json({ + message: USER_MESSAGES.USER_UNBLOCK_SUCCESSFULLY + }) + } catch (error) { + return res.status(400).json({ + message: 'Bad request' + }) + } +} diff --git a/src/modules/user/user.messages.ts b/src/modules/user/user.messages.ts index dce6e7c..0d7d335 100644 --- a/src/modules/user/user.messages.ts +++ b/src/modules/user/user.messages.ts @@ -106,5 +106,8 @@ export const USER_MESSAGES = { //token REFRESH_TOKEN_IS_REQUIRED: 'Refresh token is required', - OTP_IS_INCORRECT: 'OTP is incorrect' + OTP_IS_INCORRECT: 'OTP is incorrect', + // block + USER_HAS_BEEN_BLOCKED: 'user has been blocked', + USER_UNBLOCK_SUCCESSFULLY: 'user unblock successfully' } as const diff --git a/src/modules/user/user.middlewares.ts b/src/modules/user/user.middlewares.ts index 8d1d6f8..a1338a6 100644 --- a/src/modules/user/user.middlewares.ts +++ b/src/modules/user/user.middlewares.ts @@ -17,7 +17,7 @@ import { isValidPhoneNumberForCountry, validate } from '~/utils/validation' import { OTP_STATUS } from '../otp/otp.enum' import { OTP_MESSAGES } from '../otp/otp.messages' import otpService from '../otp/otp.services' -import { NoticeUser, UserVerifyStatus } from './user.enum' +import { NoticeUser, Subscription, UserVerifyStatus } from './user.enum' import { LoginRequestBody, TokenPayload } from './user.requests' import usersService from './user.services' import { StatusCodes } from 'http-status-codes' @@ -283,7 +283,8 @@ export const loginValidator = validate( if ( user.notice === NoticeUser.Banned || - user.reasonBanned !== '' + user.reasonBanned !== '' || + user.block === Subscription.True ) { throw new ErrorWithStatus({ message: USER_MESSAGES.ACCOUNT_IS_BANNED, @@ -368,7 +369,8 @@ export const loginValidator = validate( if ( user.notice === NoticeUser.Banned || - user.reasonBanned !== '' + user.reasonBanned !== '' || + user.block === Subscription.True ) { throw new ErrorWithStatus({ message: USER_MESSAGES.ACCOUNT_IS_BANNED, diff --git a/src/modules/user/user.requests.ts b/src/modules/user/user.requests.ts index b5266cc..6b45d5d 100644 --- a/src/modules/user/user.requests.ts +++ b/src/modules/user/user.requests.ts @@ -1,5 +1,6 @@ import { JwtPayload } from 'jsonwebtoken' import { Subscription, TokenType, UserVerifyStatus } from './user.enum' +import { ObjectId } from 'mongodb' export interface RegisterReqBody { email_phone: string @@ -76,3 +77,9 @@ export type LogoutReqBody = { export type RefreshTokenReqBody = { refresh_token: string } +export interface BlockBody { + _id: ObjectId + description_reason?: string + picture_image_prove?: string + refresh_token: string +} diff --git a/src/modules/user/user.routes.ts b/src/modules/user/user.routes.ts index b4be4fe..7cadf95 100644 --- a/src/modules/user/user.routes.ts +++ b/src/modules/user/user.routes.ts @@ -3,6 +3,7 @@ import { Router } from 'express' import { cronJobFake } from '~/utils/cronJobFake' import { wrapAsync } from '~/utils/handler' import { + blockAccountController, changePasswordController, forgotPasswordController, getMeController, @@ -13,6 +14,7 @@ import { resetPasswordController, searchAccountController, sendVerifyAccountOTPController, + unblockAccountController, updateMeController, verifyAccountController, verifyForgotPasswordTokenController @@ -231,4 +233,12 @@ usersRouter.post( wrapAsync(refreshTokenController) ) +usersRouter.post( + '/block', + accessTokenValidator, + refreshTokenCookieValidator, + wrapAsync(blockAccountController) +) + +usersRouter.post('/unblock', wrapAsync(unblockAccountController)) export default usersRouter diff --git a/src/modules/user/user.schema.ts b/src/modules/user/user.schema.ts index cc8e9d0..d6ae0b7 100644 --- a/src/modules/user/user.schema.ts +++ b/src/modules/user/user.schema.ts @@ -19,6 +19,10 @@ interface UserType { updated_at?: Date status?: UserVerifyStatus notice?: NoticeUser + block?: Subscription + reasonBlocked?: string + picture_image_prove?: string + block_time?: Date wrongPasswordTimes?: number reasonBanned?: string avatar_url?: string @@ -38,6 +42,10 @@ export default class User { updated_at: Date status: UserVerifyStatus notice: NoticeUser + block: Subscription + reasonBlocked?: string + picture_image_prove?: string + block_time: Date wrongPasswordTimes?: number reasonBanned?: string avatar_url: string @@ -56,6 +64,10 @@ export default class User { this.updated_at = user.updated_at || date this.status = user.status || UserVerifyStatus.Unverified this.notice = NoticeUser.Active + this.block = Subscription.False + this.reasonBlocked = user.reasonBlocked || '' + this.picture_image_prove = user.picture_image_prove || '' + this.block_time = user.block_time || date this.wrongPasswordTimes = 0 this.reasonBanned = user.reasonBanned || '' this.avatar_url = user.avatar_url || '' diff --git a/src/modules/user/user.services.ts b/src/modules/user/user.services.ts index 853eb7c..9b0dfb0 100644 --- a/src/modules/user/user.services.ts +++ b/src/modules/user/user.services.ts @@ -1,5 +1,5 @@ import 'dotenv/config' -import { capitalize, omit } from 'lodash' +import { capitalize, isNull, omit } from 'lodash' import { ObjectId } from 'mongodb' import otpGenerator from 'otp-generator' import databaseService from '~/database/database.services' @@ -9,7 +9,13 @@ import { signToken, verifyToken } from '~/utils/jwt' import { OTP_KIND } from '../otp/otp.enum' import otpService from '../otp/otp.services' import RefreshToken from '../refreshToken/refreshToken.schema' -import { NoticeUser, TokenType, UserRole, UserVerifyStatus } from './user.enum' +import { + NoticeUser, + Subscription, + TokenType, + UserRole, + UserVerifyStatus +} from './user.enum' import { LogoutReqBody, RegisterOauthReqBody, @@ -356,6 +362,42 @@ class UsersService { return true } + async blockAccount( + user_id: ObjectId, + re?: string, + pi?: string, + time?: Date + ) { + await databaseService.users.updateMany( + { _id: new ObjectId(user_id) }, + { + $set: { + block: Subscription.True, + reasonBlocked: re, + picture_image_prove: pi, + block_time: time + } + } + ) + + return true + } + + async unblockAccount(user_id: ObjectId) { + await databaseService.users.updateMany( + { _id: new ObjectId(user_id) }, + { + $set: { + block: Subscription.False, + reasonBlocked: '', + picture_image_prove: '' + } + } + ) + + return true + } + async updateMe({ user_id, payload