diff --git a/src/modules/users/users.controller.ts b/src/modules/users/users.controller.ts index eca2387..429f538 100644 --- a/src/modules/users/users.controller.ts +++ b/src/modules/users/users.controller.ts @@ -4,7 +4,9 @@ import type { CreateUserRequest, CreateUserResponse, GetMeRequest, - GetMeResponse + GetMeResponse, + PatchUserRequest, + PatchUserResponse } from '@teacinema/contracts/gen/users' import { UsersService } from './users.service' @@ -22,4 +24,9 @@ export class UsersController { public async getMe(data: GetMeRequest): Promise { return this.usersService.getMe(data) } + + @GrpcMethod('UsersService', 'PatchUser') + public async patch(data: PatchUserRequest): Promise { + return this.usersService.patchUser(data) + } } diff --git a/src/modules/users/users.repository.ts b/src/modules/users/users.repository.ts index 951e63f..5d5d298 100644 --- a/src/modules/users/users.repository.ts +++ b/src/modules/users/users.repository.ts @@ -11,7 +11,7 @@ export class UsersRepository { private readonly repository: Repository ) {} - public findById(id: string) { + public findById(id: UserEntity['id']) { return this.repository.findOne({ where: { id } }) } @@ -19,4 +19,10 @@ export class UsersRepository { const user = this.repository.create(data) return this.repository.save(user) } + + public async update(id: UserEntity['id'], data: Partial) { + await this.repository.update({ id }, data) + + return this.findById(id) + } } diff --git a/src/modules/users/users.service.ts b/src/modules/users/users.service.ts index 294c1d4..6f0522b 100644 --- a/src/modules/users/users.service.ts +++ b/src/modules/users/users.service.ts @@ -4,7 +4,9 @@ import { RpcStatus } from '@teacinema/common' import { CreateUserRequest, GetMeRequest, - GetMeResponse + GetMeResponse, + PatchUserRequest, + PatchUserResponse } from '@teacinema/contracts/gen/users' import { lastValueFrom } from 'rxjs' @@ -54,4 +56,25 @@ export class UsersService { } } } + + public async patchUser(data: PatchUserRequest): Promise { + const { name, userId } = data + + const user = await this.repository.findById(userId) + + if (!user) { + throw new RpcException({ + code: RpcStatus.NOT_FOUND, + details: 'User not found' + }) + } + + await this.repository.update(userId, { + ...(name !== undefined && { name }) + }) + + return { + ok: true + } + } } diff --git a/yarn.lock b/yarn.lock index e12a8a1..049a6a5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1020,7 +1020,15 @@ resolved "https://registry.yarnpkg.com/@nestjs/mapped-types/-/mapped-types-2.1.0.tgz#b9b536b7c3571567aa1d0223db8baa1a51505a19" integrity sha512-W+n+rM69XsFdwORF11UqJahn4J3xi4g/ZEOlJNL6KoW5ygWSmBB2p0S2BZ4FQeS/NDH72e6xIcu35SfJnE8bXw== -"@nestjs/microservices@^11.1.12", "@nestjs/microservices@^11.1.14": +"@nestjs/microservices@^11.1.12": + version "11.1.15" + resolved "https://registry.yarnpkg.com/@nestjs/microservices/-/microservices-11.1.15.tgz#e98d806d7bd813d0b0d3a1332fe70adb2b137623" + integrity sha512-9ee8AymMT/6HKNm10LRoYcUZKylu5K+FXRF7ynw+GlG4HXPVPlwst71wwNkbBHcqjJzfPhxEmC/j92sVeJURAg== + dependencies: + iterare "1.2.1" + tslib "2.8.1" + +"@nestjs/microservices@^11.1.14": version "11.1.14" resolved "https://registry.yarnpkg.com/@nestjs/microservices/-/microservices-11.1.14.tgz#8fa2de758ec2c768da7d993d79f772b699a507ff" integrity sha512-7x+QaX7351xJMYpjw4pqwoACXtSxuUQt4Prb7ZyqqsCawnBRrObPpZ/HIdpdKJPgo8vFfLJoAnkFJ+mQ6xz6Rw== @@ -1174,9 +1182,9 @@ integrity sha512-gMPQtWtlVQ5lEm4Z9eO/Iqd+5jyjsoHYR1HgD+JmkTai+Gj+Vz6UN4hNL2tso1mGIe6foNfYozbin2TEbBRzdg== "@teacinema/contracts@^1.1.2": - version "1.1.2" - resolved "https://git.ksv741.keenetic.pro/api/packages/teacinema/npm/%40teacinema%2Fcontracts/-/1.1.2/contracts-1.1.2.tgz#74e5ce7c95515983fd299332ef095a882dbe6665" - integrity sha512-LL9UqmjIX0C9kHVtyoAoNUrp0zk1u/JzNTfnU+gqTXHJhejHOSa3blVtQBO4cjTt+yYDaN89fjm5fRbL/V0dDw== + version "1.1.3" + resolved "https://git.ksv741.keenetic.pro/api/packages/teacinema/npm/%40teacinema%2Fcontracts/-/1.1.3/contracts-1.1.3.tgz#e08600a8dc09977dae35654d372e29e411029e33" + integrity sha512-kwcSh2YIxmoBJQ56L+tQpGj12ySwNwf9KRNcUqhPffLPO60PR5aiKA2eN2SfKc23TKp0YoSkdC9LWeor0a9Ysg== dependencies: "@nestjs/microservices" "^11.1.12" protoc "33.4.0"