feat: add get me
This commit is contained in:
parent
98643fee39
commit
03d4f38fb0
@ -4,6 +4,7 @@ import { PassportModule } from '@teacinema/passport'
|
||||
|
||||
import { AccountModule } from '../modules/account/account.module'
|
||||
import { AuthModule } from '../modules/auth/auth.module'
|
||||
import { UsersModule } from '../modules/users/users.module'
|
||||
|
||||
import { AppController } from './app.controller'
|
||||
import { AppService } from './app.service'
|
||||
@ -14,6 +15,7 @@ import { getPassportConfig } from './config'
|
||||
ConfigModule.forRoot({ isGlobal: true }),
|
||||
AuthModule,
|
||||
AccountModule,
|
||||
UsersModule,
|
||||
PassportModule.registerAsync({
|
||||
useFactory: getPassportConfig,
|
||||
inject: [ConfigService]
|
||||
|
||||
1
src/modules/users/dto/index.ts
Normal file
1
src/modules/users/dto/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './rs'
|
||||
21
src/modules/users/dto/rs/get-me.response.dto.ts
Normal file
21
src/modules/users/dto/rs/get-me.response.dto.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { ApiProperty } from '@nestjs/swagger'
|
||||
|
||||
export class GetMeResponseDto {
|
||||
@ApiProperty({ example: '123' })
|
||||
public id: string
|
||||
|
||||
@ApiProperty({ example: 'Ivan' })
|
||||
public name: string
|
||||
|
||||
@ApiProperty({ example: 'mail@mail.ru' })
|
||||
public email: string
|
||||
|
||||
@ApiProperty({ example: '+79999999999' })
|
||||
public phone: string
|
||||
|
||||
@ApiProperty({
|
||||
example:
|
||||
'https://t4.ftcdn.net/jpg/11/66/06/77/360_F_1166067709_2SooAuPWXp20XkGev7oOT7nuK1VThCsN.jpg'
|
||||
})
|
||||
public avatar: string
|
||||
}
|
||||
1
src/modules/users/dto/rs/index.ts
Normal file
1
src/modules/users/dto/rs/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './get-me.response.dto'
|
||||
27
src/modules/users/users.controller.ts
Normal file
27
src/modules/users/users.controller.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import { Controller, Get, HttpCode, HttpStatus } from '@nestjs/common'
|
||||
import { ApiBearerAuth, ApiOkResponse, ApiOperation } from '@nestjs/swagger'
|
||||
import { lastValueFrom } from 'rxjs'
|
||||
|
||||
import { CurrentUser, Protected } from '../../shared/decorators'
|
||||
|
||||
import { GetMeResponseDto } from './dto'
|
||||
import { UsersClientGrpc } from './users.grpc'
|
||||
|
||||
@Controller('users')
|
||||
export class UsersController {
|
||||
constructor(private readonly client: UsersClientGrpc) {}
|
||||
|
||||
@ApiOperation({
|
||||
summary: 'Get Me',
|
||||
description: 'Get current user'
|
||||
})
|
||||
@ApiBearerAuth()
|
||||
@ApiOkResponse({ type: GetMeResponseDto })
|
||||
@Protected()
|
||||
@Get('me')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async getMe(@CurrentUser() userId: string) {
|
||||
const { user } = await lastValueFrom(this.client.getMe({ id: userId }))
|
||||
return user
|
||||
}
|
||||
}
|
||||
22
src/modules/users/users.grpc.ts
Normal file
22
src/modules/users/users.grpc.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { Inject, Injectable, OnModuleInit } from '@nestjs/common'
|
||||
import type { ClientGrpc } from '@nestjs/microservices'
|
||||
import {
|
||||
GetMeRequest,
|
||||
UsersServiceClient
|
||||
} from '@teacinema/contracts/gen/users'
|
||||
|
||||
@Injectable()
|
||||
export class UsersClientGrpc implements OnModuleInit {
|
||||
private usersService: UsersServiceClient
|
||||
|
||||
constructor(@Inject('USERS_PACKAGE') private readonly client: ClientGrpc) {}
|
||||
|
||||
onModuleInit() {
|
||||
this.usersService =
|
||||
this.client.getService<UsersServiceClient>('UsersService')
|
||||
}
|
||||
|
||||
public getMe(request: GetMeRequest) {
|
||||
return this.usersService.getMe(request)
|
||||
}
|
||||
}
|
||||
30
src/modules/users/users.module.ts
Normal file
30
src/modules/users/users.module.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
import { ConfigService } from '@nestjs/config'
|
||||
import { ClientsModule, Transport } from '@nestjs/microservices'
|
||||
import { PROTO_PATHS } from '@teacinema/contracts'
|
||||
|
||||
import { UsersController } from './users.controller'
|
||||
import { UsersClientGrpc } from './users.grpc'
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ClientsModule.registerAsync([
|
||||
{
|
||||
name: 'USERS_PACKAGE',
|
||||
useFactory: (configService: ConfigService) => ({
|
||||
transport: Transport.GRPC,
|
||||
options: {
|
||||
package: 'users.v1',
|
||||
protoPath: PROTO_PATHS.USERS,
|
||||
url: configService.getOrThrow<string>('USERS_GRPC_URL')
|
||||
}
|
||||
}),
|
||||
inject: [ConfigService]
|
||||
}
|
||||
])
|
||||
],
|
||||
controllers: [UsersController],
|
||||
providers: [UsersClientGrpc],
|
||||
exports: [UsersClientGrpc]
|
||||
})
|
||||
export class UsersModule {}
|
||||
@ -1221,9 +1221,9 @@
|
||||
integrity sha512-q3DURJbSk3k8MNWFIYaSM4LEcBgPbWa+HJmBz/nzYT4kuYitJVSXxpZ97kr0Ea+81AZwAU3JhQKlkK0SBWUi0A==
|
||||
|
||||
"@teacinema/contracts@^1.0.0":
|
||||
version "1.0.9"
|
||||
resolved "https://git.ksv741.keenetic.pro/api/packages/teacinema/npm/%40teacinema%2Fcontracts/-/1.0.9/contracts-1.0.9.tgz#14b136d0c08552147b0de637a7af6d340d538527"
|
||||
integrity sha512-9BwWvXaDKgRcef6+1ep9vUDWBdOndqheeTX5mXK5OOvLXbLOlzYoqAUoT0ZwaRwOCD+Je/LDrqcdti6eoss71Q==
|
||||
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==
|
||||
dependencies:
|
||||
"@nestjs/microservices" "^11.1.12"
|
||||
protoc "33.4.0"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user