From 9e30b4102896c40789bdf356293340e1bec1dbfb Mon Sep 17 00:00:00 2001 From: Xiao Yijun Date: Fri, 18 Feb 2022 17:02:08 +0800 Subject: [PATCH] feat(core): delete user and update user password api (#248) --- packages/core/src/routes/admin-user.ts | 60 +++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 5 deletions(-) diff --git a/packages/core/src/routes/admin-user.ts b/packages/core/src/routes/admin-user.ts index 792c89da3..d30f53e6e 100644 --- a/packages/core/src/routes/admin-user.ts +++ b/packages/core/src/routes/admin-user.ts @@ -10,6 +10,7 @@ import koaPagination from '@/middleware/koa-pagination'; import { findRolesByRoleNames } from '@/queries/roles'; import { clearUserCustomDataById, + deleteUserById, findAllUsers, findTotalNumberOfUsers, findUserById, @@ -39,7 +40,7 @@ export default function adminUserRoutes(router: T) { router.get( '/users/:userId', koaGuard({ - params: object({ userId: string().min(1) }), + params: object({ userId: string() }), }), async (ctx, next) => { const { @@ -97,7 +98,7 @@ export default function adminUserRoutes(router: T) { router.patch( '/users/:userId', koaGuard({ - params: object({ userId: string().min(1) }), + params: object({ userId: string() }), body: object({ name: string().min(3).optional(), avatar: string().url().optional(), @@ -122,10 +123,59 @@ export default function adminUserRoutes(router: T) { } ); + router.patch( + '/users/:userId/password', + koaGuard({ + params: object({ userId: string() }), + body: object({ password: string().min(6) }), + }), + async (ctx, next) => { + const { + params: { userId }, + body: { password }, + } = ctx.guard; + + await findUserById(userId); + + const { passwordEncryptionSalt, passwordEncrypted, passwordEncryptionMethod } = + encryptUserPassword(userId, password); + + const user = await updateUserById(userId, { + passwordEncrypted, + passwordEncryptionMethod, + passwordEncryptionSalt, + }); + + ctx.body = pick(user, ...userInfoSelectFields); + + return next(); + } + ); + + router.delete( + '/users/:userId', + koaGuard({ + params: object({ userId: string() }), + }), + async (ctx, next) => { + const { + params: { userId }, + } = ctx.guard; + + await findUserById(userId); + + await deleteUserById(userId); + + ctx.status = 204; + + return next(); + } + ); + router.patch( '/users/:userId/roleNames', koaGuard({ - params: object({ userId: string().min(1) }), + params: object({ userId: string() }), body: object({ roleNames: string().array() }), }), async (ctx, next) => { @@ -159,7 +209,7 @@ export default function adminUserRoutes(router: T) { router.patch( '/users/:userId/custom-data', koaGuard({ - params: object({ userId: string().min(1) }), + params: object({ userId: string() }), body: object({ customData: customDataGuard }), }), async (ctx, next) => { @@ -183,7 +233,7 @@ export default function adminUserRoutes(router: T) { router.delete( '/users/:userId/custom-data', koaGuard({ - params: object({ userId: string().min(1) }), + params: object({ userId: string() }), }), async (ctx, next) => { const {