From 3a583e81e48c5228edf0fc7cabe0729e8a1932c7 Mon Sep 17 00:00:00 2001 From: Xiao Yijun Date: Mon, 28 Feb 2022 13:18:17 +0800 Subject: [PATCH] fix(core): patch `/users/:userId` should not fail if only `name` or `avatar` is provided --- packages/core/src/routes/admin-user.test.ts | 19 +++++++++++++++++++ packages/core/src/routes/admin-user.ts | 5 ++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/core/src/routes/admin-user.test.ts b/packages/core/src/routes/admin-user.test.ts index 6a3cd0e21..e28da4d98 100644 --- a/packages/core/src/routes/admin-user.test.ts +++ b/packages/core/src/routes/admin-user.test.ts @@ -167,6 +167,25 @@ describe('adminUserRoutes', () => { }); }); + it('PATCH /users/:userId should updated with one field if the other is undefined', async () => { + const name = 'Micheal'; + + const updateNameResponse = await userRequest.patch('/users/foo').send({ name }); + expect(updateNameResponse.status).toEqual(200); + expect(updateNameResponse.body).toEqual({ + ...mockUserResponse, + name, + }); + + const avatar = 'https://www.miceal.png'; + const updateAvatarResponse = await userRequest.patch('/users/foo').send({ avatar }); + expect(updateAvatarResponse.status).toEqual(200); + expect(updateAvatarResponse.body).toEqual({ + ...mockUserResponse, + avatar, + }); + }); + it('PATCH /users/:userId throw with invalid input params', async () => { const name = 'Micheal'; const avatar = 'http://www.micheal.png'; diff --git a/packages/core/src/routes/admin-user.ts b/packages/core/src/routes/admin-user.ts index 13a7df04c..bd33c69d2 100644 --- a/packages/core/src/routes/admin-user.ts +++ b/packages/core/src/routes/admin-user.ts @@ -116,14 +116,13 @@ export default function adminUserRoutes(router: T) { async (ctx, next) => { const { params: { userId }, - body: { name, avatar }, + body, } = ctx.guard; await findUserById(userId); const user = await updateUserById(userId, { - name, - avatar, + ...body, }); ctx.body = pick(user, ...userInfoSelectFields);