60 lines
2.1 KiB
TypeScript
60 lines
2.1 KiB
TypeScript
import { Mapper } from '@automapper/core';
|
|
import { InjectMapper } from '@automapper/nestjs';
|
|
import { Controller, UsePipes } from '@nestjs/common';
|
|
import { QueryBus } from '@nestjs/cqrs';
|
|
import { GrpcMethod, RpcException } from '@nestjs/microservices';
|
|
import { RpcValidationPipe } from '../../../utils/pipes/rpc.validation-pipe';
|
|
import { MatchRequest } from '../../domain/dtos/match.request';
|
|
import { ICollection } from '../../../database/interfaces/collection.interface';
|
|
import { MatchQuery } from '../../queries/match.query';
|
|
import { MatchPresenter } from '../secondaries/match.presenter';
|
|
import { DefaultParamsProvider } from '../secondaries/default-params.provider';
|
|
import { GeorouterCreator } from '../secondaries/georouter-creator';
|
|
import { Match } from '../../domain/entities/ecosystem/match';
|
|
import { GeoTimezoneFinder } from '../../../geography/adapters/secondaries/geo-timezone-finder';
|
|
import { TimeConverter } from '../secondaries/time-converter';
|
|
|
|
@UsePipes(
|
|
new RpcValidationPipe({
|
|
whitelist: false,
|
|
forbidUnknownValues: false,
|
|
}),
|
|
)
|
|
@Controller()
|
|
export class MatcherController {
|
|
constructor(
|
|
private readonly queryBus: QueryBus,
|
|
private readonly defaultParamsProvider: DefaultParamsProvider,
|
|
@InjectMapper() private readonly mapper: Mapper,
|
|
private readonly georouterCreator: GeorouterCreator,
|
|
private readonly timezoneFinder: GeoTimezoneFinder,
|
|
private readonly timeConverter: TimeConverter,
|
|
) {}
|
|
|
|
@GrpcMethod('MatcherService', 'Match')
|
|
async match(data: MatchRequest): Promise<ICollection<Match>> {
|
|
try {
|
|
const matchCollection = await this.queryBus.execute(
|
|
new MatchQuery(
|
|
data,
|
|
this.defaultParamsProvider.getParams(),
|
|
this.georouterCreator,
|
|
this.timezoneFinder,
|
|
this.timeConverter,
|
|
),
|
|
);
|
|
return Promise.resolve({
|
|
data: matchCollection.data.map((match: Match) =>
|
|
this.mapper.map(match, Match, MatchPresenter),
|
|
),
|
|
total: matchCollection.total,
|
|
});
|
|
} catch (e) {
|
|
throw new RpcException({
|
|
code: e.code,
|
|
message: e.message,
|
|
});
|
|
}
|
|
}
|
|
}
|