From 85dfe7faf4bd8732527a0fdca5e85562451445bc Mon Sep 17 00:00:00 2001 From: Muhammed Mustafa Date: Tue, 19 Sep 2023 07:45:19 +0000 Subject: [PATCH] fix(nocohub): Added profile update api and also to swagger --- .../src/controllers/users/users.controller.ts | 17 ++++++++++--- packages/nocodb/src/models/User.ts | 5 ++++ packages/nocodb/src/schema/swagger.json | 24 +++++++++++++++++++ .../src/services/users/users.service.ts | 16 +++++++++++++ 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/packages/nocodb/src/controllers/users/users.controller.ts b/packages/nocodb/src/controllers/users/users.controller.ts index 71a5918b93..a9b476375c 100644 --- a/packages/nocodb/src/controllers/users/users.controller.ts +++ b/packages/nocodb/src/controllers/users/users.controller.ts @@ -1,10 +1,8 @@ import { Body, Controller, - Get, HttpCode, - Param, - Post, + Patch, Request, Response, UseGuards, @@ -12,6 +10,7 @@ import { import { ConfigService } from '@nestjs/config'; import type { AppConfig } from '~/interface/config'; +import { GlobalGuard } from '~/guards/global/global.guard'; import { AppHooksService } from '~/services/app-hooks/app-hooks.service'; import { UsersService } from '~/services/users/users.service'; @@ -23,4 +22,16 @@ export class UsersController { protected readonly appHooksService: AppHooksService, protected readonly config: ConfigService, ) {} + + @Patch(['/api/v1/user/profile']) + @UseGuards(GlobalGuard) + @HttpCode(200) + async update(@Body() body, @Request() req, @Response() res) { + res.json( + await this.usersService.profileUpdate({ + id: req.user.id, + params: body, + }), + ); + } } diff --git a/packages/nocodb/src/models/User.ts b/packages/nocodb/src/models/User.ts index d8e4eafd0f..c4f608972f 100644 --- a/packages/nocodb/src/models/User.ts +++ b/packages/nocodb/src/models/User.ts @@ -25,6 +25,9 @@ export default class User implements UserType { roles?: string; token_version?: string; + display_name?: string; + avatar?: string; + constructor(data: User) { Object.assign(this, data); } @@ -80,6 +83,8 @@ export default class User implements UserType { 'email_verified', 'roles', 'token_version', + 'display_name', + 'avatar', ]); if (updateObj.email) { diff --git a/packages/nocodb/src/schema/swagger.json b/packages/nocodb/src/schema/swagger.json index 97c5ba6b98..68fe037d75 100644 --- a/packages/nocodb/src/schema/swagger.json +++ b/packages/nocodb/src/schema/swagger.json @@ -64,6 +64,30 @@ } ], "paths": { + "/api/v1/user/profile": { + "patch": { + "summary": "Update User Profile", + "operationId": "user-profile-update", + "responses": { + "200": { + "$ref": "#/components/schemas/User" + } + }, + "tags": [ + "User profile" + ], + "description": "Update User Profile", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/User" + } + } + } + } + } + }, "/api/v1/auth/user/signup": { "post": { "summary": "Signup", diff --git a/packages/nocodb/src/services/users/users.service.ts b/packages/nocodb/src/services/users/users.service.ts index 3e614703c9..56fcb9c4a8 100644 --- a/packages/nocodb/src/services/users/users.service.ts +++ b/packages/nocodb/src/services/users/users.service.ts @@ -25,6 +25,7 @@ import { randomTokenString } from '~/helpers/stringHelpers'; import NcPluginMgrv2 from '~/helpers/NcPluginMgrv2'; import { NcError } from '~/helpers/catchError'; import { ProjectsService } from '~/services/projects.service'; +import { extractProps } from '~/helpers/extractProps'; @Injectable() export class UsersService { @@ -70,6 +71,21 @@ export class UsersService { }); } + async profileUpdate({ + id, + params, + }: { + id: number; + params: { + display_name?: string; + avatar?: string; + }; + }) { + const updateObj = extractProps(params, ['display_name', 'avatar']); + + return await User.update(id, updateObj); + } + async registerNewUserIfAllowed({ email, salt,