matcher/src/modules/ad/interface/grpc-controllers/match.grpc-controller.ts

56 lines
2.0 KiB
TypeScript

import { RpcValidationPipe } from '@mobicoop/ddd-library';
import { AD_ROUTE_PROVIDER } from '@modules/ad/ad.di-tokens';
import { GeorouterPort } from '@modules/ad/core/application/ports/georouter.port';
import { MatchQuery } from '@modules/ad/core/application/queries/match/match.query';
import { MatchingResult } from '@modules/ad/core/application/queries/match/match.query-handler';
import { MatchEntity } from '@modules/ad/core/domain/match.entity';
import { MatchMapper } from '@modules/ad/match.mapper';
import { CacheInterceptor, CacheKey } from '@nestjs/cache-manager';
import {
Controller,
Inject,
UseFilters,
UseInterceptors,
UsePipes,
} from '@nestjs/common';
import { QueryBus } from '@nestjs/cqrs';
import { GrpcMethod } from '@nestjs/microservices';
import { LogCauseExceptionFilter } from '@src/log-cause.exception-filter';
import { MatchingPaginatedResponseDto } from '../dtos/matching.paginated.response.dto';
import { MatchRequestDto } from './dtos/match.request.dto';
@UseFilters(LogCauseExceptionFilter)
@UsePipes(
new RpcValidationPipe({
whitelist: false,
forbidUnknownValues: false,
}),
)
@Controller()
export class MatchGrpcController {
constructor(
private readonly queryBus: QueryBus,
@Inject(AD_ROUTE_PROVIDER)
private readonly routeProvider: GeorouterPort,
private readonly matchMapper: MatchMapper,
) {}
@CacheKey('MatcherServiceMatch')
@UseInterceptors(CacheInterceptor)
@GrpcMethod('MatcherService', 'Match')
async match(data: MatchRequestDto): Promise<MatchingPaginatedResponseDto> {
const matchingResult: MatchingResult = await this.queryBus.execute(
new MatchQuery(data, this.routeProvider),
);
return new MatchingPaginatedResponseDto({
id: matchingResult.id,
data: matchingResult.matches.map((match: MatchEntity) =>
this.matchMapper.toResponse(match),
),
page: matchingResult.page,
perPage: matchingResult.perPage,
total: matchingResult.total,
});
}
}