import { AggregateID, IdResponse, RpcExceptionCode, RpcValidationPipe, } from '@mobicoop/ddd-library'; import { Controller, UsePipes } from '@nestjs/common'; import { QueryBus } from '@nestjs/cqrs'; import { GrpcMethod, RpcException } from '@nestjs/microservices'; import { ValidateAuthenticationRequestDto } from './dtos/validate-authentication.request.dto'; import { ValidateAuthenticationQuery } from '@modules/authentication/core/application/queries/validate-authentication/validate-authentication.query'; @UsePipes( new RpcValidationPipe({ whitelist: true, forbidUnknownValues: false, }), ) @Controller() export class ValidateAuthenticationGrpcController { constructor(private readonly queryBus: QueryBus) {} @GrpcMethod('AuthenticationService', 'Validate') async validate(data: ValidateAuthenticationRequestDto): Promise { try { const aggregateID: AggregateID = await this.queryBus.execute( new ValidateAuthenticationQuery(data.name, data.password), ); return new IdResponse(aggregateID); } catch (error: any) { throw new RpcException({ code: RpcExceptionCode.PERMISSION_DENIED, message: 'Permission denied', }); } } }