mirror of
https://gitlab.com/mobicoop/v3/service/matcher.git
synced 2025-12-31 08:22:41 +00:00
refactor to ddh, first commit
This commit is contained in:
82
old/modules/ad/adapters/primaries/ad-messager.service.ts
Normal file
82
old/modules/ad/adapters/primaries/ad-messager.service.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { Controller, Inject } from '@nestjs/common';
|
||||
import { CommandBus } from '@nestjs/cqrs';
|
||||
import { CreateAdCommand } from '../../commands/create-ad.command';
|
||||
import { CreateAdRequest } from '../../domain/dtos/create-ad.request';
|
||||
import { validateOrReject } from 'class-validator';
|
||||
import { plainToInstance } from 'class-transformer';
|
||||
import { DatabaseException } from 'src/modules/database/exceptions/database.exception';
|
||||
import { ExceptionCode } from 'src/modules/utils/exception-code.enum';
|
||||
import { IPublishMessage } from 'src/interfaces/message-publisher';
|
||||
import { MESSAGE_PUBLISHER } from 'src/app.constants';
|
||||
import { RabbitSubscribe } from '@mobicoop/message-broker-module';
|
||||
|
||||
@Controller()
|
||||
export class AdMessagerService {
|
||||
constructor(
|
||||
@Inject(MESSAGE_PUBLISHER)
|
||||
private readonly messagePublisher: IPublishMessage,
|
||||
private readonly commandBus: CommandBus,
|
||||
) {}
|
||||
|
||||
@RabbitSubscribe({
|
||||
name: 'adCreated',
|
||||
})
|
||||
async adCreatedHandler(message: string): Promise<void> {
|
||||
let createAdRequest: CreateAdRequest;
|
||||
// parse message to request instance
|
||||
try {
|
||||
createAdRequest = plainToInstance(CreateAdRequest, JSON.parse(message));
|
||||
// validate instance
|
||||
await validateOrReject(createAdRequest);
|
||||
// validate nested objects (fixes direct nested validation bug)
|
||||
for (const waypoint of createAdRequest.addresses) {
|
||||
try {
|
||||
await validateOrReject(waypoint);
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
this.messagePublisher.publish(
|
||||
'matcher.ad.crit',
|
||||
JSON.stringify({
|
||||
message: `Can't validate message : ${message}`,
|
||||
error: e,
|
||||
}),
|
||||
);
|
||||
}
|
||||
try {
|
||||
await this.commandBus.execute(new CreateAdCommand(createAdRequest));
|
||||
} catch (e) {
|
||||
if (e instanceof DatabaseException) {
|
||||
if (e.message.includes('already exists')) {
|
||||
this.messagePublisher.publish(
|
||||
'matcher.ad.crit',
|
||||
JSON.stringify({
|
||||
code: ExceptionCode.ALREADY_EXISTS,
|
||||
message: 'Already exists',
|
||||
uuid: createAdRequest.uuid,
|
||||
}),
|
||||
);
|
||||
}
|
||||
if (e.message.includes("Can't reach database server")) {
|
||||
this.messagePublisher.publish(
|
||||
'matcher.ad.crit',
|
||||
JSON.stringify({
|
||||
code: ExceptionCode.UNAVAILABLE,
|
||||
message: 'Database server unavailable',
|
||||
uuid: createAdRequest.uuid,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
this.messagePublisher.publish(
|
||||
'logging.matcher.ad.crit',
|
||||
JSON.stringify({
|
||||
message,
|
||||
error: e,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
129
old/modules/ad/adapters/secondaries/ad.repository.ts
Normal file
129
old/modules/ad/adapters/secondaries/ad.repository.ts
Normal file
@@ -0,0 +1,129 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { DatabaseRepository } from '../../../database/domain/database.repository';
|
||||
import { Ad } from '../../domain/entities/ad';
|
||||
import { DatabaseException } from '../../../database/exceptions/database.exception';
|
||||
|
||||
@Injectable()
|
||||
export class AdRepository extends DatabaseRepository<Ad> {
|
||||
protected model = 'ad';
|
||||
|
||||
createAd = async (ad: Partial<Ad>): Promise<Ad> => {
|
||||
try {
|
||||
const affectedRowNumber = await this.createWithFields(
|
||||
this.createFields(ad),
|
||||
);
|
||||
if (affectedRowNumber == 1) {
|
||||
return this.findOneByUuid(ad.uuid);
|
||||
}
|
||||
throw new DatabaseException();
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
private createFields = (ad: Partial<Ad>): Partial<AdFields> => ({
|
||||
uuid: `'${ad.uuid}'`,
|
||||
userUuid: `'${ad.userUuid}'`,
|
||||
driver: ad.driver ? 'true' : 'false',
|
||||
passenger: ad.passenger ? 'true' : 'false',
|
||||
frequency: `'${ad.frequency}'`,
|
||||
fromDate: `'${ad.fromDate.getFullYear()}-${
|
||||
ad.fromDate.getMonth() + 1
|
||||
}-${ad.fromDate.getDate()}'`,
|
||||
toDate: `'${ad.toDate.getFullYear()}-${
|
||||
ad.toDate.getMonth() + 1
|
||||
}-${ad.toDate.getDate()}'`,
|
||||
monTime: ad.monTime
|
||||
? `'${ad.monTime.getFullYear()}-${
|
||||
ad.monTime.getMonth() + 1
|
||||
}-${ad.monTime.getDate()}T${ad.monTime.getHours()}:${ad.monTime.getMinutes()}Z'`
|
||||
: 'NULL',
|
||||
tueTime: ad.tueTime
|
||||
? `'${ad.tueTime.getFullYear()}-${
|
||||
ad.tueTime.getMonth() + 1
|
||||
}-${ad.tueTime.getDate()}T${ad.tueTime.getHours()}:${ad.tueTime.getMinutes()}Z'`
|
||||
: 'NULL',
|
||||
wedTime: ad.wedTime
|
||||
? `'${ad.wedTime.getFullYear()}-${
|
||||
ad.wedTime.getMonth() + 1
|
||||
}-${ad.wedTime.getDate()}T${ad.wedTime.getHours()}:${ad.wedTime.getMinutes()}Z'`
|
||||
: 'NULL',
|
||||
thuTime: ad.thuTime
|
||||
? `'${ad.thuTime.getFullYear()}-${
|
||||
ad.thuTime.getMonth() + 1
|
||||
}-${ad.thuTime.getDate()}T${ad.thuTime.getHours()}:${ad.thuTime.getMinutes()}Z'`
|
||||
: 'NULL',
|
||||
friTime: ad.friTime
|
||||
? `'${ad.friTime.getFullYear()}-${
|
||||
ad.friTime.getMonth() + 1
|
||||
}-${ad.friTime.getDate()}T${ad.friTime.getHours()}:${ad.friTime.getMinutes()}Z'`
|
||||
: 'NULL',
|
||||
satTime: ad.satTime
|
||||
? `'${ad.satTime.getFullYear()}-${
|
||||
ad.satTime.getMonth() + 1
|
||||
}-${ad.satTime.getDate()}T${ad.satTime.getHours()}:${ad.satTime.getMinutes()}Z'`
|
||||
: 'NULL',
|
||||
sunTime: ad.sunTime
|
||||
? `'${ad.sunTime.getFullYear()}-${
|
||||
ad.sunTime.getMonth() + 1
|
||||
}-${ad.sunTime.getDate()}T${ad.sunTime.getHours()}:${ad.sunTime.getMinutes()}Z'`
|
||||
: 'NULL',
|
||||
monMargin: ad.monMargin,
|
||||
tueMargin: ad.tueMargin,
|
||||
wedMargin: ad.wedMargin,
|
||||
thuMargin: ad.thuMargin,
|
||||
friMargin: ad.friMargin,
|
||||
satMargin: ad.satMargin,
|
||||
sunMargin: ad.sunMargin,
|
||||
fwdAzimuth: ad.fwdAzimuth,
|
||||
backAzimuth: ad.backAzimuth,
|
||||
driverDuration: ad.driverDuration ?? 'NULL',
|
||||
driverDistance: ad.driverDistance ?? 'NULL',
|
||||
passengerDuration: ad.passengerDuration ?? 'NULL',
|
||||
passengerDistance: ad.passengerDistance ?? 'NULL',
|
||||
waypoints: ad.waypoints,
|
||||
direction: ad.direction,
|
||||
seatsDriver: ad.seatsDriver,
|
||||
seatsPassenger: ad.seatsPassenger,
|
||||
seatsUsed: ad.seatsUsed ?? 0,
|
||||
strict: ad.strict,
|
||||
});
|
||||
}
|
||||
|
||||
type AdFields = {
|
||||
uuid: string;
|
||||
userUuid: string;
|
||||
driver: string;
|
||||
passenger: string;
|
||||
frequency: string;
|
||||
fromDate: string;
|
||||
toDate: string;
|
||||
monTime: string;
|
||||
tueTime: string;
|
||||
wedTime: string;
|
||||
thuTime: string;
|
||||
friTime: string;
|
||||
satTime: string;
|
||||
sunTime: string;
|
||||
monMargin: number;
|
||||
tueMargin: number;
|
||||
wedMargin: number;
|
||||
thuMargin: number;
|
||||
friMargin: number;
|
||||
satMargin: number;
|
||||
sunMargin: number;
|
||||
driverDuration?: number | 'NULL';
|
||||
driverDistance?: number | 'NULL';
|
||||
passengerDuration?: number | 'NULL';
|
||||
passengerDistance?: number | 'NULL';
|
||||
waypoints: string;
|
||||
direction: string;
|
||||
fwdAzimuth: number;
|
||||
backAzimuth: number;
|
||||
seatsDriver?: number;
|
||||
seatsPassenger?: number;
|
||||
seatsUsed?: number;
|
||||
strict: boolean;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { DefaultParams } from '../../domain/types/default-params.type';
|
||||
import { IProvideParams } from '../../domain/interfaces/params-provider.interface';
|
||||
|
||||
@Injectable()
|
||||
export class DefaultParamsProvider implements IProvideParams {
|
||||
constructor(private readonly configService: ConfigService) {}
|
||||
|
||||
getParams = (): DefaultParams => {
|
||||
return {
|
||||
DEFAULT_TIMEZONE: this.configService.get('DEFAULT_TIMEZONE'),
|
||||
GEOROUTER_TYPE: this.configService.get('GEOROUTER_TYPE'),
|
||||
GEOROUTER_URL: this.configService.get('GEOROUTER_URL'),
|
||||
};
|
||||
};
|
||||
}
|
||||
16
old/modules/ad/adapters/secondaries/message-publisher.ts
Normal file
16
old/modules/ad/adapters/secondaries/message-publisher.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { MESSAGE_BROKER_PUBLISHER } from '../../../../app.constants';
|
||||
import { MessageBrokerPublisher } from '@mobicoop/message-broker-module';
|
||||
import { IPublishMessage } from '../../../../interfaces/message-publisher';
|
||||
|
||||
@Injectable()
|
||||
export class MessagePublisher implements IPublishMessage {
|
||||
constructor(
|
||||
@Inject(MESSAGE_BROKER_PUBLISHER)
|
||||
private readonly messageBrokerPublisher: MessageBrokerPublisher,
|
||||
) {}
|
||||
|
||||
publish = (routingKey: string, message: string): void => {
|
||||
this.messageBrokerPublisher.publish(routingKey, message);
|
||||
};
|
||||
}
|
||||
11
old/modules/ad/adapters/secondaries/timezone-finder.ts
Normal file
11
old/modules/ad/adapters/secondaries/timezone-finder.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { GeoTimezoneFinder } from '../../../geography/adapters/secondaries/geo-timezone-finder';
|
||||
import { IFindTimezone } from '../../../geography/domain/interfaces/timezone-finder.interface';
|
||||
|
||||
@Injectable()
|
||||
export class TimezoneFinder implements IFindTimezone {
|
||||
constructor(private readonly geoTimezoneFinder: GeoTimezoneFinder) {}
|
||||
|
||||
timezones = (lon: number, lat: number): string[] =>
|
||||
this.geoTimezoneFinder.timezones(lon, lat);
|
||||
}
|
||||
Reference in New Issue
Block a user