From c1c356c30a1086d03199768afa40b0caabb6283a Mon Sep 17 00:00:00 2001 From: Xiao Yijun Date: Thu, 17 Feb 2022 14:10:26 +0800 Subject: [PATCH] feat(core): delete `users/:userid/custom-data` (#233) --- packages/core/src/queries/user.ts | 12 ++++++++++++ packages/core/src/routes/admin-user.ts | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/packages/core/src/queries/user.ts b/packages/core/src/queries/user.ts index e681e93ae..ef0e298ed 100644 --- a/packages/core/src/queries/user.ts +++ b/packages/core/src/queries/user.ts @@ -108,3 +108,15 @@ export const deleteUserById = async (id: string) => { throw new DeletionError(Users.table, id); } }; + +export const clearUserCustomDataById = async (id: string) => { + const { rowCount } = await pool.query(sql` + update ${table} + set ${fields.customData}='{}'::jsonb + where id=${id} + `); + + if (rowCount < 1) { + throw new DeletionError(Users.table, id); + } +}; diff --git a/packages/core/src/routes/admin-user.ts b/packages/core/src/routes/admin-user.ts index 1fbddbd98..5de5bc9e6 100644 --- a/packages/core/src/routes/admin-user.ts +++ b/packages/core/src/routes/admin-user.ts @@ -9,6 +9,7 @@ import koaGuard from '@/middleware/koa-guard'; import koaPagination from '@/middleware/koa-pagination'; import { findRolesByRoleNames } from '@/queries/roles'; import { + clearUserCustomDataById, findAllUsers, findTotalNumberOfUsers, findUserById, @@ -150,4 +151,24 @@ export default function adminUserRoutes(router: T) { return next(); } ); + + router.delete( + '/users/:userId/custom-data', + koaGuard({ + params: object({ userId: string().min(1) }), + }), + async (ctx, next) => { + const { + params: { userId }, + } = ctx.guard; + + await findUserById(userId); + + await clearUserCustomDataById(userId); + + ctx.status = 200; + + return next(); + } + ); }