mirror of
https://gitlab.com/mobicoop/v3/service/matcher.git
synced 2026-01-01 13:52:40 +00:00
refactor to ddh, first commit
This commit is contained in:
5
src/modules/ad/ad.di-tokens.ts
Normal file
5
src/modules/ad/ad.di-tokens.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export const AD_MESSAGE_PUBLISHER = Symbol('AD_MESSAGE_PUBLISHER');
|
||||
export const PARAMS_PROVIDER = Symbol('PARAMS_PROVIDER');
|
||||
export const TIMEZONE_FINDER = Symbol('TIMEZONE_FINDER');
|
||||
export const GEOROUTER_CREATOR = Symbol('GEOROUTER_CREATOR');
|
||||
export const AD_REPOSITORY = Symbol('AD_REPOSITORY');
|
||||
111
src/modules/ad/ad.mapper.ts
Normal file
111
src/modules/ad/ad.mapper.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
import { Mapper } from '@mobicoop/ddd-library';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { AdEntity } from './core/domain/ad.entity';
|
||||
import {
|
||||
AdWriteModel,
|
||||
AdReadModel,
|
||||
ScheduleItemModel,
|
||||
} from './infrastructure/ad.repository';
|
||||
import { Frequency } from './core/domain/ad.types';
|
||||
import { v4 } from 'uuid';
|
||||
import { ScheduleItemProps } from './core/domain/value-objects/schedule-item.value-object';
|
||||
|
||||
/**
|
||||
* Mapper constructs objects that are used in different layers:
|
||||
* Record is an object that is stored in a database,
|
||||
* Entity is an object that is used in application domain layer,
|
||||
* and a ResponseDTO is an object returned to a user (usually as json).
|
||||
*/
|
||||
|
||||
@Injectable()
|
||||
export class AdMapper
|
||||
implements Mapper<AdEntity, AdReadModel, AdWriteModel, undefined>
|
||||
{
|
||||
toPersistence = (entity: AdEntity): AdWriteModel => {
|
||||
const copy = entity.getProps();
|
||||
const now = new Date();
|
||||
const record: AdWriteModel = {
|
||||
uuid: copy.id,
|
||||
userUuid: copy.userId,
|
||||
driver: copy.driver,
|
||||
passenger: copy.passenger,
|
||||
frequency: copy.frequency,
|
||||
fromDate: new Date(copy.fromDate),
|
||||
toDate: new Date(copy.toDate),
|
||||
schedule: {
|
||||
create: copy.schedule.map((scheduleItem: ScheduleItemProps) => ({
|
||||
uuid: v4(),
|
||||
day: scheduleItem.day,
|
||||
time: new Date(
|
||||
1970,
|
||||
0,
|
||||
1,
|
||||
parseInt(scheduleItem.time.split(':')[0]),
|
||||
parseInt(scheduleItem.time.split(':')[1]),
|
||||
),
|
||||
margin: scheduleItem.margin,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
})),
|
||||
},
|
||||
seatsProposed: copy.seatsProposed,
|
||||
seatsRequested: copy.seatsRequested,
|
||||
strict: copy.strict,
|
||||
driverDuration: copy.driverDuration,
|
||||
driverDistance: copy.driverDistance,
|
||||
passengerDuration: copy.passengerDuration,
|
||||
passengerDistance: copy.passengerDistance,
|
||||
waypoints: copy.waypoints,
|
||||
direction: copy.direction,
|
||||
fwdAzimuth: copy.fwdAzimuth,
|
||||
backAzimuth: copy.backAzimuth,
|
||||
createdAt: copy.createdAt,
|
||||
updatedAt: copy.updatedAt,
|
||||
};
|
||||
return record;
|
||||
};
|
||||
|
||||
toDomain = (record: AdReadModel): AdEntity => {
|
||||
const entity = new AdEntity({
|
||||
id: record.uuid,
|
||||
createdAt: new Date(record.createdAt),
|
||||
updatedAt: new Date(record.updatedAt),
|
||||
props: {
|
||||
userId: record.userUuid,
|
||||
driver: record.driver,
|
||||
passenger: record.passenger,
|
||||
frequency: Frequency[record.frequency],
|
||||
fromDate: record.fromDate.toISOString().split('T')[0],
|
||||
toDate: record.toDate.toISOString().split('T')[0],
|
||||
schedule: record.schedule.map((scheduleItem: ScheduleItemModel) => ({
|
||||
day: scheduleItem.day,
|
||||
time: `${scheduleItem.time
|
||||
.getUTCHours()
|
||||
.toString()
|
||||
.padStart(2, '0')}:${scheduleItem.time
|
||||
.getUTCMinutes()
|
||||
.toString()
|
||||
.padStart(2, '0')}`,
|
||||
margin: scheduleItem.margin,
|
||||
})),
|
||||
seatsProposed: record.seatsProposed,
|
||||
seatsRequested: record.seatsRequested,
|
||||
strict: record.strict,
|
||||
driverDuration: record.driverDuration,
|
||||
driverDistance: record.driverDistance,
|
||||
passengerDuration: record.passengerDuration,
|
||||
passengerDistance: record.passengerDistance,
|
||||
waypoints: record.waypoints,
|
||||
direction: record.direction,
|
||||
fwdAzimuth: record.fwdAzimuth,
|
||||
backAzimuth: record.backAzimuth,
|
||||
},
|
||||
});
|
||||
return entity;
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
toResponse = (entity: AdEntity): undefined => {
|
||||
return undefined;
|
||||
};
|
||||
}
|
||||
@@ -1,72 +1,61 @@
|
||||
import { RabbitMQModule } from '@golevelup/nestjs-rabbitmq';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { AdMessagerController } from './adapters/primaries/ad-messager.controller';
|
||||
import { AdProfile } from './mappers/ad.profile';
|
||||
import { CreateAdUseCase } from './domain/usecases/create-ad.usecase';
|
||||
import { AdRepository } from './adapters/secondaries/ad.repository';
|
||||
import { DatabaseModule } from '../database/database.module';
|
||||
import { Module, Provider } from '@nestjs/common';
|
||||
import { CqrsModule } from '@nestjs/cqrs';
|
||||
import { Messager } from './adapters/secondaries/messager';
|
||||
import { GeoTimezoneFinder } from '../geography/adapters/secondaries/geo-timezone-finder';
|
||||
import { DefaultParamsProvider } from './adapters/secondaries/default-params.provider';
|
||||
import { GeorouterCreator } from '../geography/adapters/secondaries/georouter-creator';
|
||||
import { GeographyModule } from '../geography/geography.module';
|
||||
import { HttpModule } from '@nestjs/axios';
|
||||
import { PostgresDirectionEncoder } from '../geography/adapters/secondaries/postgres-direction-encoder';
|
||||
import {
|
||||
AD_MESSAGE_PUBLISHER,
|
||||
AD_REPOSITORY,
|
||||
PARAMS_PROVIDER,
|
||||
TIMEZONE_FINDER,
|
||||
} from './ad.di-tokens';
|
||||
import { MessageBrokerPublisher } from '@mobicoop/message-broker-module';
|
||||
import { AdRepository } from './infrastructure/ad.repository';
|
||||
import { PrismaService } from './infrastructure/prisma.service';
|
||||
import { DefaultParamsProvider } from './infrastructure/default-params-provider';
|
||||
import { TimezoneFinder } from './infrastructure/timezone-finder';
|
||||
import { AdMapper } from './ad.mapper';
|
||||
|
||||
const mappers: Provider[] = [AdMapper];
|
||||
|
||||
const repositories: Provider[] = [
|
||||
{
|
||||
provide: AD_REPOSITORY,
|
||||
useClass: AdRepository,
|
||||
},
|
||||
];
|
||||
|
||||
const messagePublishers: Provider[] = [
|
||||
{
|
||||
provide: AD_MESSAGE_PUBLISHER,
|
||||
useExisting: MessageBrokerPublisher,
|
||||
},
|
||||
];
|
||||
const orms: Provider[] = [PrismaService];
|
||||
|
||||
const adapters: Provider[] = [
|
||||
{
|
||||
provide: PARAMS_PROVIDER,
|
||||
useClass: DefaultParamsProvider,
|
||||
},
|
||||
{
|
||||
provide: TIMEZONE_FINDER,
|
||||
useClass: TimezoneFinder,
|
||||
},
|
||||
];
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
GeographyModule,
|
||||
DatabaseModule,
|
||||
CqrsModule,
|
||||
HttpModule,
|
||||
RabbitMQModule.forRootAsync(RabbitMQModule, {
|
||||
imports: [ConfigModule],
|
||||
useFactory: async (configService: ConfigService) => ({
|
||||
exchanges: [
|
||||
{
|
||||
name: configService.get<string>('RMQ_EXCHANGE'),
|
||||
type: 'topic',
|
||||
},
|
||||
],
|
||||
handlers: {
|
||||
adCreated: {
|
||||
exchange: configService.get<string>('RMQ_EXCHANGE'),
|
||||
routingKey: 'ad.created',
|
||||
queue: 'matcher-ad-created',
|
||||
},
|
||||
},
|
||||
uri: configService.get<string>('RMQ_URI'),
|
||||
connectionInitOptions: { wait: false },
|
||||
enableControllerDiscovery: true,
|
||||
}),
|
||||
inject: [ConfigService],
|
||||
}),
|
||||
],
|
||||
controllers: [AdMessagerController],
|
||||
imports: [CqrsModule],
|
||||
providers: [
|
||||
{
|
||||
provide: 'ParamsProvider',
|
||||
useClass: DefaultParamsProvider,
|
||||
},
|
||||
{
|
||||
provide: 'GeorouterCreator',
|
||||
useClass: GeorouterCreator,
|
||||
},
|
||||
{
|
||||
provide: 'TimezoneFinder',
|
||||
useClass: GeoTimezoneFinder,
|
||||
},
|
||||
{
|
||||
provide: 'DirectionEncoder',
|
||||
useClass: PostgresDirectionEncoder,
|
||||
},
|
||||
AdProfile,
|
||||
Messager,
|
||||
AdRepository,
|
||||
CreateAdUseCase,
|
||||
...mappers,
|
||||
...repositories,
|
||||
...messagePublishers,
|
||||
...orms,
|
||||
...adapters,
|
||||
],
|
||||
exports: [
|
||||
PrismaService,
|
||||
AdMapper,
|
||||
AD_REPOSITORY,
|
||||
PARAMS_PROVIDER,
|
||||
TIMEZONE_FINDER,
|
||||
],
|
||||
exports: [],
|
||||
})
|
||||
export class AdModule {}
|
||||
|
||||
@@ -1,81 +0,0 @@
|
||||
import { RabbitSubscribe } from '@golevelup/nestjs-rabbitmq';
|
||||
import { Controller } from '@nestjs/common';
|
||||
import { CommandBus, QueryBus } from '@nestjs/cqrs';
|
||||
import { CreateAdCommand } from '../../commands/create-ad.command';
|
||||
import { CreateAdRequest } from '../../domain/dtos/create-ad.request';
|
||||
import { validateOrReject } from 'class-validator';
|
||||
import { Messager } from '../secondaries/messager';
|
||||
import { plainToInstance } from 'class-transformer';
|
||||
import { DatabaseException } from 'src/modules/database/exceptions/database.exception';
|
||||
import { ExceptionCode } from 'src/modules/utils/exception-code.enum';
|
||||
|
||||
@Controller()
|
||||
export class AdMessagerController {
|
||||
constructor(
|
||||
private readonly messager: Messager,
|
||||
private readonly commandBus: CommandBus,
|
||||
private readonly queryBus: QueryBus,
|
||||
) {}
|
||||
|
||||
@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.waypoints) {
|
||||
try {
|
||||
await validateOrReject(waypoint);
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
this.messager.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.messager.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.messager.publish(
|
||||
'matcher.ad.crit',
|
||||
JSON.stringify({
|
||||
code: ExceptionCode.UNAVAILABLE,
|
||||
message: 'Database server unavailable',
|
||||
uuid: createAdRequest.uuid,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
this.messager.publish(
|
||||
'logging.matcher.ad.crit',
|
||||
JSON.stringify({
|
||||
message,
|
||||
error: e,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
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;
|
||||
};
|
||||
@@ -1,17 +0,0 @@
|
||||
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'),
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export abstract class MessageBroker {
|
||||
exchange: string;
|
||||
|
||||
constructor(exchange: string) {
|
||||
this.exchange = exchange;
|
||||
}
|
||||
|
||||
abstract publish(routingKey: string, message: string): void;
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { MessageBroker } from './message-broker';
|
||||
|
||||
@Injectable()
|
||||
export class Messager extends MessageBroker {
|
||||
constructor(
|
||||
private readonly amqpConnection: AmqpConnection,
|
||||
configService: ConfigService,
|
||||
) {
|
||||
super(configService.get<string>('RMQ_EXCHANGE'));
|
||||
}
|
||||
|
||||
publish = (routingKey: string, message: string): void => {
|
||||
this.amqpConnection.publish(this.exchange, routingKey, message);
|
||||
};
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
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);
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
import { CreateAdRequest } from '../domain/dtos/create-ad.request';
|
||||
|
||||
export class CreateAdCommand {
|
||||
readonly createAdRequest: CreateAdRequest;
|
||||
|
||||
constructor(request: CreateAdRequest) {
|
||||
this.createAdRequest = request;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { RepositoryPort } from '@mobicoop/ddd-library';
|
||||
import { AdEntity } from '../../domain/ad.entity';
|
||||
|
||||
export type AdRepositoryPort = RepositoryPort<AdEntity>;
|
||||
@@ -0,0 +1,5 @@
|
||||
import { DefaultParams } from './default-params.type';
|
||||
|
||||
export interface DefaultParamsProviderPort {
|
||||
getParams(): DefaultParams;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export interface TimezoneFinderPort {
|
||||
timezones(lon: number, lat: number, defaultTimezone?: string): string[];
|
||||
}
|
||||
16
src/modules/ad/core/domain/ad.entity.ts
Normal file
16
src/modules/ad/core/domain/ad.entity.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { AggregateRoot, AggregateID } from '@mobicoop/ddd-library';
|
||||
import { AdProps, CreateAdProps } from './ad.types';
|
||||
|
||||
export class AdEntity extends AggregateRoot<AdProps> {
|
||||
protected readonly _id: AggregateID;
|
||||
|
||||
static create = (create: CreateAdProps): AdEntity => {
|
||||
const props: AdProps = { ...create };
|
||||
const ad = new AdEntity({ id: create.id, props });
|
||||
return ad;
|
||||
};
|
||||
|
||||
validate(): void {
|
||||
// entity business rules validation to protect it's invariant before saving entity to a database
|
||||
}
|
||||
}
|
||||
51
src/modules/ad/core/domain/ad.types.ts
Normal file
51
src/modules/ad/core/domain/ad.types.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { ScheduleItemProps } from './value-objects/schedule-item.value-object';
|
||||
|
||||
// All properties that an Ad has
|
||||
export interface AdProps {
|
||||
userId: string;
|
||||
driver: boolean;
|
||||
passenger: boolean;
|
||||
frequency: Frequency;
|
||||
fromDate: string;
|
||||
toDate: string;
|
||||
schedule: ScheduleItemProps[];
|
||||
seatsProposed: number;
|
||||
seatsRequested: number;
|
||||
strict: boolean;
|
||||
driverDuration: number;
|
||||
driverDistance: number;
|
||||
passengerDuration: number;
|
||||
passengerDistance: number;
|
||||
waypoints: string;
|
||||
direction: string;
|
||||
fwdAzimuth: number;
|
||||
backAzimuth: number;
|
||||
}
|
||||
|
||||
// Properties that are needed for an Ad creation
|
||||
export interface CreateAdProps {
|
||||
id: string;
|
||||
userId: string;
|
||||
driver: boolean;
|
||||
passenger: boolean;
|
||||
frequency: Frequency;
|
||||
fromDate: string;
|
||||
toDate: string;
|
||||
schedule: ScheduleItemProps[];
|
||||
seatsProposed: number;
|
||||
seatsRequested: number;
|
||||
strict: boolean;
|
||||
driverDuration: number;
|
||||
driverDistance: number;
|
||||
passengerDuration: number;
|
||||
passengerDistance: number;
|
||||
waypoints: string;
|
||||
direction: string;
|
||||
fwdAzimuth: number;
|
||||
backAzimuth: number;
|
||||
}
|
||||
|
||||
export enum Frequency {
|
||||
PUNCTUAL = 'PUNCTUAL',
|
||||
RECURRENT = 'RECURRENT',
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { ValueObject } from '@mobicoop/ddd-library';
|
||||
|
||||
/** Note:
|
||||
* Value Objects with multiple properties can contain
|
||||
* other Value Objects inside if needed.
|
||||
* */
|
||||
|
||||
export interface ScheduleItemProps {
|
||||
day?: number;
|
||||
time: string;
|
||||
margin?: number;
|
||||
}
|
||||
|
||||
export class ScheduleItem extends ValueObject<ScheduleItemProps> {
|
||||
get day(): number | undefined {
|
||||
return this.props.day;
|
||||
}
|
||||
|
||||
get time(): string {
|
||||
return this.props.time;
|
||||
}
|
||||
|
||||
get margin(): number | undefined {
|
||||
return this.props.margin;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
protected validate(props: ScheduleItemProps): void {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -1,140 +0,0 @@
|
||||
import { AutoMap } from '@automapper/classes';
|
||||
import {
|
||||
ArrayMinSize,
|
||||
IsArray,
|
||||
IsBoolean,
|
||||
IsDate,
|
||||
IsEnum,
|
||||
IsMilitaryTime,
|
||||
IsNotEmpty,
|
||||
IsNumber,
|
||||
IsOptional,
|
||||
IsString,
|
||||
} from 'class-validator';
|
||||
import { Frequency } from '../types/frequency.enum';
|
||||
import { Coordinate } from '../../../geography/domain/entities/coordinate';
|
||||
import { Type } from 'class-transformer';
|
||||
import { HasTruthyWith } from './has-truthy-with.validator';
|
||||
|
||||
export class CreateAdRequest {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@AutoMap()
|
||||
uuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@AutoMap()
|
||||
userUuid: string;
|
||||
|
||||
@HasTruthyWith('passenger', {
|
||||
message: 'A role (driver or passenger) must be set to true',
|
||||
})
|
||||
@IsBoolean()
|
||||
@AutoMap()
|
||||
driver: boolean;
|
||||
|
||||
@IsBoolean()
|
||||
@AutoMap()
|
||||
passenger: boolean;
|
||||
|
||||
@IsEnum(Frequency)
|
||||
@AutoMap()
|
||||
frequency: Frequency;
|
||||
|
||||
@Type(() => Date)
|
||||
@IsDate()
|
||||
@AutoMap()
|
||||
fromDate: Date;
|
||||
|
||||
@Type(() => Date)
|
||||
@IsDate()
|
||||
@AutoMap()
|
||||
toDate: Date;
|
||||
|
||||
@IsOptional()
|
||||
@IsMilitaryTime()
|
||||
@AutoMap()
|
||||
monTime?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsMilitaryTime()
|
||||
@AutoMap()
|
||||
tueTime?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsMilitaryTime()
|
||||
@AutoMap()
|
||||
wedTime?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsMilitaryTime()
|
||||
@AutoMap()
|
||||
thuTime?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsMilitaryTime()
|
||||
@AutoMap()
|
||||
friTime?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsMilitaryTime()
|
||||
@AutoMap()
|
||||
satTime?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsMilitaryTime()
|
||||
@AutoMap()
|
||||
sunTime?: string;
|
||||
|
||||
@IsNumber()
|
||||
@AutoMap()
|
||||
monMargin: number;
|
||||
|
||||
@IsNumber()
|
||||
@AutoMap()
|
||||
tueMargin: number;
|
||||
|
||||
@IsNumber()
|
||||
@AutoMap()
|
||||
wedMargin: number;
|
||||
|
||||
@IsNumber()
|
||||
@AutoMap()
|
||||
thuMargin: number;
|
||||
|
||||
@IsNumber()
|
||||
@AutoMap()
|
||||
friMargin: number;
|
||||
|
||||
@IsNumber()
|
||||
@AutoMap()
|
||||
satMargin: number;
|
||||
|
||||
@IsNumber()
|
||||
@AutoMap()
|
||||
sunMargin: number;
|
||||
|
||||
@Type(() => Coordinate)
|
||||
@IsArray()
|
||||
@ArrayMinSize(2)
|
||||
@AutoMap(() => [Coordinate])
|
||||
waypoints: Coordinate[];
|
||||
|
||||
@IsNumber()
|
||||
@AutoMap()
|
||||
seatsDriver: number;
|
||||
|
||||
@IsNumber()
|
||||
@AutoMap()
|
||||
seatsPassenger: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
@AutoMap()
|
||||
seatsUsed?: number;
|
||||
|
||||
@IsBoolean()
|
||||
@AutoMap()
|
||||
strict: boolean;
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
import {
|
||||
registerDecorator,
|
||||
ValidationOptions,
|
||||
ValidationArguments,
|
||||
} from 'class-validator';
|
||||
|
||||
export function HasTruthyWith(
|
||||
property: string,
|
||||
validationOptions?: ValidationOptions,
|
||||
) {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
return function (object: Object, propertyName: string) {
|
||||
registerDecorator({
|
||||
name: 'hasTruthyWith',
|
||||
target: object.constructor,
|
||||
propertyName: propertyName,
|
||||
constraints: [property],
|
||||
options: validationOptions,
|
||||
validator: {
|
||||
validate(value: any, args: ValidationArguments) {
|
||||
const [relatedPropertyName] = args.constraints;
|
||||
const relatedValue = (args.object as any)[relatedPropertyName];
|
||||
return (
|
||||
typeof value === 'boolean' &&
|
||||
typeof relatedValue === 'boolean' &&
|
||||
(value || relatedValue)
|
||||
); // you can return a Promise<boolean> here as well, if you want to make async validation
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
import { AutoMap } from '@automapper/classes';
|
||||
import { Frequency } from '../types/frequency.enum';
|
||||
|
||||
export class Ad {
|
||||
@AutoMap()
|
||||
uuid: string;
|
||||
|
||||
@AutoMap()
|
||||
userUuid: string;
|
||||
|
||||
@AutoMap()
|
||||
driver: boolean;
|
||||
|
||||
@AutoMap()
|
||||
passenger: boolean;
|
||||
|
||||
@AutoMap()
|
||||
frequency: Frequency;
|
||||
|
||||
@AutoMap()
|
||||
fromDate: Date;
|
||||
|
||||
@AutoMap()
|
||||
toDate: Date;
|
||||
|
||||
@AutoMap()
|
||||
monTime: Date;
|
||||
|
||||
@AutoMap()
|
||||
tueTime: Date;
|
||||
|
||||
@AutoMap()
|
||||
wedTime: Date;
|
||||
|
||||
@AutoMap()
|
||||
thuTime: Date;
|
||||
|
||||
@AutoMap()
|
||||
friTime: Date;
|
||||
|
||||
@AutoMap()
|
||||
satTime: Date;
|
||||
|
||||
@AutoMap()
|
||||
sunTime: Date;
|
||||
|
||||
@AutoMap()
|
||||
monMargin: number;
|
||||
|
||||
@AutoMap()
|
||||
tueMargin: number;
|
||||
|
||||
@AutoMap()
|
||||
wedMargin: number;
|
||||
|
||||
@AutoMap()
|
||||
thuMargin: number;
|
||||
|
||||
@AutoMap()
|
||||
friMargin: number;
|
||||
|
||||
@AutoMap()
|
||||
satMargin: number;
|
||||
|
||||
@AutoMap()
|
||||
sunMargin: number;
|
||||
|
||||
@AutoMap()
|
||||
driverDuration?: number;
|
||||
|
||||
@AutoMap()
|
||||
driverDistance?: number;
|
||||
|
||||
@AutoMap()
|
||||
passengerDuration?: number;
|
||||
|
||||
@AutoMap()
|
||||
passengerDistance?: number;
|
||||
|
||||
@AutoMap()
|
||||
waypoints: string;
|
||||
|
||||
@AutoMap()
|
||||
direction: string;
|
||||
|
||||
@AutoMap()
|
||||
fwdAzimuth: number;
|
||||
|
||||
@AutoMap()
|
||||
backAzimuth: number;
|
||||
|
||||
@AutoMap()
|
||||
seatsDriver: number;
|
||||
|
||||
@AutoMap()
|
||||
seatsPassenger: number;
|
||||
|
||||
@AutoMap()
|
||||
seatsUsed: number;
|
||||
|
||||
@AutoMap()
|
||||
strict: boolean;
|
||||
|
||||
@AutoMap()
|
||||
createdAt: Date;
|
||||
|
||||
@AutoMap()
|
||||
updatedAt: Date;
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
import { Coordinate } from '../../../geography/domain/entities/coordinate';
|
||||
import { Route } from '../../../geography/domain/entities/route';
|
||||
import { Role } from '../types/role.enum';
|
||||
import { IGeorouter } from '../../../geography/domain/interfaces/georouter.interface';
|
||||
import { Path } from '../../../geography/domain/types/path.type';
|
||||
import { GeorouterSettings } from '../../../geography/domain/types/georouter-settings.type';
|
||||
|
||||
export class Geography {
|
||||
private coordinates: Coordinate[];
|
||||
driverRoute: Route;
|
||||
passengerRoute: Route;
|
||||
|
||||
constructor(coordinates: Coordinate[]) {
|
||||
this.coordinates = coordinates;
|
||||
}
|
||||
|
||||
createRoutes = async (
|
||||
roles: Role[],
|
||||
georouter: IGeorouter,
|
||||
settings: GeorouterSettings,
|
||||
): Promise<void> => {
|
||||
const paths: Path[] = this.getPaths(roles);
|
||||
const routes = await georouter.route(paths, settings);
|
||||
if (routes.some((route) => route.key == RouteKey.COMMON)) {
|
||||
this.driverRoute = routes.find(
|
||||
(route) => route.key == RouteKey.COMMON,
|
||||
).route;
|
||||
this.passengerRoute = routes.find(
|
||||
(route) => route.key == RouteKey.COMMON,
|
||||
).route;
|
||||
} else {
|
||||
if (routes.some((route) => route.key == RouteKey.DRIVER)) {
|
||||
this.driverRoute = routes.find(
|
||||
(route) => route.key == RouteKey.DRIVER,
|
||||
).route;
|
||||
}
|
||||
if (routes.some((route) => route.key == RouteKey.PASSENGER)) {
|
||||
this.passengerRoute = routes.find(
|
||||
(route) => route.key == RouteKey.PASSENGER,
|
||||
).route;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private getPaths = (roles: Role[]): Path[] => {
|
||||
const paths: Path[] = [];
|
||||
if (roles.includes(Role.DRIVER) && roles.includes(Role.PASSENGER)) {
|
||||
if (this.coordinates.length == 2) {
|
||||
// 2 points => same route for driver and passenger
|
||||
const commonPath: Path = {
|
||||
key: RouteKey.COMMON,
|
||||
points: this.coordinates,
|
||||
};
|
||||
paths.push(commonPath);
|
||||
} else {
|
||||
const driverPath: Path = this.createDriverPath();
|
||||
const passengerPath: Path = this.createPassengerPath();
|
||||
paths.push(driverPath, passengerPath);
|
||||
}
|
||||
} else if (roles.includes(Role.DRIVER)) {
|
||||
const driverPath: Path = this.createDriverPath();
|
||||
paths.push(driverPath);
|
||||
} else if (roles.includes(Role.PASSENGER)) {
|
||||
const passengerPath: Path = this.createPassengerPath();
|
||||
paths.push(passengerPath);
|
||||
}
|
||||
return paths;
|
||||
};
|
||||
|
||||
private createDriverPath = (): Path => {
|
||||
return {
|
||||
key: RouteKey.DRIVER,
|
||||
points: this.coordinates,
|
||||
};
|
||||
};
|
||||
|
||||
private createPassengerPath = (): Path => {
|
||||
return {
|
||||
key: RouteKey.PASSENGER,
|
||||
points: [
|
||||
this.coordinates[0],
|
||||
this.coordinates[this.coordinates.length - 1],
|
||||
],
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
export enum RouteKey {
|
||||
COMMON = 'common',
|
||||
DRIVER = 'driver',
|
||||
PASSENGER = 'passenger',
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
import { DateTime, TimeZone } from 'timezonecomplete';
|
||||
|
||||
export class TimeConverter {
|
||||
static toUtcDatetime = (date: Date, time: string, timezone: string): Date => {
|
||||
try {
|
||||
if (!date || !time || !timezone) throw new Error();
|
||||
return new Date(
|
||||
new DateTime(
|
||||
`${date.toISOString().split('T')[0]}T${time}:00`,
|
||||
TimeZone.zone(timezone, false),
|
||||
)
|
||||
.convert(TimeZone.zone('UTC'))
|
||||
.toIsoString(),
|
||||
);
|
||||
} catch (e) {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
import { DefaultParams } from '../types/default-params.type';
|
||||
|
||||
export interface IProvideParams {
|
||||
getParams(): DefaultParams;
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
export enum Frequency {
|
||||
PUNCTUAL = 'PUNCTUAL',
|
||||
RECURRENT = 'RECURRENT',
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
export enum Role {
|
||||
DRIVER = 'DRIVER',
|
||||
PASSENGER = 'PASSENGER',
|
||||
}
|
||||
@@ -1,144 +0,0 @@
|
||||
import { CommandHandler } from '@nestjs/cqrs';
|
||||
import { CreateAdCommand } from '../../commands/create-ad.command';
|
||||
import { Ad } from '../entities/ad';
|
||||
import { AdRepository } from '../../adapters/secondaries/ad.repository';
|
||||
import { InjectMapper } from '@automapper/nestjs';
|
||||
import { Mapper } from '@automapper/core';
|
||||
import { CreateAdRequest } from '../dtos/create-ad.request';
|
||||
import { Inject } from '@nestjs/common';
|
||||
import { IProvideParams } from '../interfaces/params-provider.interface';
|
||||
import { ICreateGeorouter } from '../../../geography/domain/interfaces/georouter-creator.interface';
|
||||
import { IFindTimezone } from '../../../geography/domain/interfaces/timezone-finder.interface';
|
||||
import { IGeorouter } from '../../../geography/domain/interfaces/georouter.interface';
|
||||
import { DefaultParams } from '../types/default-params.type';
|
||||
import { Role } from '../types/role.enum';
|
||||
import { Geography } from '../entities/geography';
|
||||
import { IEncodeDirection } from '../../../geography/domain/interfaces/direction-encoder.interface';
|
||||
import { TimeConverter } from '../entities/time-converter';
|
||||
import { Coordinate } from '../../../geography/domain/entities/coordinate';
|
||||
|
||||
@CommandHandler(CreateAdCommand)
|
||||
export class CreateAdUseCase {
|
||||
private readonly georouter: IGeorouter;
|
||||
private readonly defaultParams: DefaultParams;
|
||||
private timezone: string;
|
||||
private roles: Role[];
|
||||
private geography: Geography;
|
||||
private ad: Ad;
|
||||
|
||||
constructor(
|
||||
@InjectMapper() private readonly mapper: Mapper,
|
||||
private readonly adRepository: AdRepository,
|
||||
@Inject('ParamsProvider')
|
||||
private readonly defaultParamsProvider: IProvideParams,
|
||||
@Inject('GeorouterCreator')
|
||||
private readonly georouterCreator: ICreateGeorouter,
|
||||
@Inject('TimezoneFinder')
|
||||
private readonly timezoneFinder: IFindTimezone,
|
||||
@Inject('DirectionEncoder')
|
||||
private readonly directionEncoder: IEncodeDirection,
|
||||
) {
|
||||
this.defaultParams = defaultParamsProvider.getParams();
|
||||
this.georouter = georouterCreator.create(
|
||||
this.defaultParams.GEOROUTER_TYPE,
|
||||
this.defaultParams.GEOROUTER_URL,
|
||||
);
|
||||
}
|
||||
|
||||
async execute(command: CreateAdCommand): Promise<Ad> {
|
||||
try {
|
||||
this.ad = this.mapper.map(command.createAdRequest, CreateAdRequest, Ad);
|
||||
this.setTimezone(command.createAdRequest.waypoints);
|
||||
this.setGeography(command.createAdRequest.waypoints);
|
||||
this.setRoles(command.createAdRequest);
|
||||
await this.geography.createRoutes(this.roles, this.georouter, {
|
||||
withDistance: false,
|
||||
withPoints: true,
|
||||
withTime: false,
|
||||
});
|
||||
this.setAdGeography(command);
|
||||
this.setAdSchedule(command);
|
||||
return await this.adRepository.createAd(this.ad);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
private setTimezone = (coordinates: Coordinate[]): void => {
|
||||
this.timezone = this.defaultParams.DEFAULT_TIMEZONE;
|
||||
try {
|
||||
const timezones = this.timezoneFinder.timezones(
|
||||
coordinates[0].lon,
|
||||
coordinates[0].lat,
|
||||
);
|
||||
if (timezones.length > 0) this.timezone = timezones[0];
|
||||
} catch (e) {}
|
||||
};
|
||||
|
||||
private setRoles = (createAdRequest: CreateAdRequest): void => {
|
||||
this.roles = [];
|
||||
if (createAdRequest.driver) this.roles.push(Role.DRIVER);
|
||||
if (createAdRequest.passenger) this.roles.push(Role.PASSENGER);
|
||||
};
|
||||
|
||||
private setGeography = (coordinates: Coordinate[]): void => {
|
||||
this.geography = new Geography(coordinates);
|
||||
};
|
||||
|
||||
private setAdGeography = (command: CreateAdCommand): void => {
|
||||
this.ad.driverDistance = this.geography.driverRoute?.distance;
|
||||
this.ad.driverDuration = this.geography.driverRoute?.duration;
|
||||
this.ad.passengerDistance = this.geography.passengerRoute?.distance;
|
||||
this.ad.passengerDuration = this.geography.passengerRoute?.duration;
|
||||
this.ad.fwdAzimuth = this.geography.driverRoute
|
||||
? this.geography.driverRoute.fwdAzimuth
|
||||
: this.geography.passengerRoute.fwdAzimuth;
|
||||
this.ad.backAzimuth = this.geography.driverRoute
|
||||
? this.geography.driverRoute.backAzimuth
|
||||
: this.geography.passengerRoute.backAzimuth;
|
||||
this.ad.waypoints = this.directionEncoder.encode(
|
||||
command.createAdRequest.waypoints,
|
||||
);
|
||||
this.ad.direction = this.geography.driverRoute
|
||||
? this.directionEncoder.encode(this.geography.driverRoute.points)
|
||||
: undefined;
|
||||
};
|
||||
|
||||
private setAdSchedule = (command: CreateAdCommand): void => {
|
||||
this.ad.monTime = TimeConverter.toUtcDatetime(
|
||||
this.ad.fromDate,
|
||||
command.createAdRequest.monTime,
|
||||
this.timezone,
|
||||
);
|
||||
this.ad.tueTime = TimeConverter.toUtcDatetime(
|
||||
this.ad.fromDate,
|
||||
command.createAdRequest.tueTime,
|
||||
this.timezone,
|
||||
);
|
||||
this.ad.wedTime = TimeConverter.toUtcDatetime(
|
||||
this.ad.fromDate,
|
||||
command.createAdRequest.wedTime,
|
||||
this.timezone,
|
||||
);
|
||||
this.ad.thuTime = TimeConverter.toUtcDatetime(
|
||||
this.ad.fromDate,
|
||||
command.createAdRequest.thuTime,
|
||||
this.timezone,
|
||||
);
|
||||
this.ad.friTime = TimeConverter.toUtcDatetime(
|
||||
this.ad.fromDate,
|
||||
command.createAdRequest.friTime,
|
||||
this.timezone,
|
||||
);
|
||||
this.ad.satTime = TimeConverter.toUtcDatetime(
|
||||
this.ad.fromDate,
|
||||
command.createAdRequest.satTime,
|
||||
this.timezone,
|
||||
);
|
||||
this.ad.sunTime = TimeConverter.toUtcDatetime(
|
||||
this.ad.fromDate,
|
||||
command.createAdRequest.sunTime,
|
||||
this.timezone,
|
||||
);
|
||||
};
|
||||
}
|
||||
83
src/modules/ad/infrastructure/ad.repository.ts
Normal file
83
src/modules/ad/infrastructure/ad.repository.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { Inject, Injectable, Logger } from '@nestjs/common';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { AdRepositoryPort } from '../core/application/ports/ad.repository.port';
|
||||
import {
|
||||
LoggerBase,
|
||||
MessagePublisherPort,
|
||||
PrismaRepositoryBase,
|
||||
} from '@mobicoop/ddd-library';
|
||||
import { PrismaService } from './prisma.service';
|
||||
import { AD_MESSAGE_PUBLISHER } from '../ad.di-tokens';
|
||||
import { AdEntity } from '../core/domain/ad.entity';
|
||||
import { AdMapper } from '../ad.mapper';
|
||||
|
||||
export type AdBaseModel = {
|
||||
uuid: string;
|
||||
userUuid: string;
|
||||
driver: boolean;
|
||||
passenger: boolean;
|
||||
frequency: string;
|
||||
fromDate: Date;
|
||||
toDate: Date;
|
||||
seatsProposed: number;
|
||||
seatsRequested: number;
|
||||
strict: boolean;
|
||||
driverDuration: number;
|
||||
driverDistance: number;
|
||||
passengerDuration: number;
|
||||
passengerDistance: number;
|
||||
waypoints: string;
|
||||
direction: string;
|
||||
fwdAzimuth: number;
|
||||
backAzimuth: number;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
};
|
||||
|
||||
export type AdReadModel = AdBaseModel & {
|
||||
schedule: ScheduleItemModel[];
|
||||
};
|
||||
|
||||
export type AdWriteModel = AdBaseModel & {
|
||||
schedule: {
|
||||
create: ScheduleItemModel[];
|
||||
};
|
||||
};
|
||||
|
||||
export type ScheduleItemModel = {
|
||||
uuid: string;
|
||||
day: number;
|
||||
time: Date;
|
||||
margin: number;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
};
|
||||
|
||||
/**
|
||||
* Repository is used for retrieving/saving domain entities
|
||||
* */
|
||||
@Injectable()
|
||||
export class AdRepository
|
||||
extends PrismaRepositoryBase<AdEntity, AdReadModel, AdWriteModel>
|
||||
implements AdRepositoryPort
|
||||
{
|
||||
constructor(
|
||||
prisma: PrismaService,
|
||||
mapper: AdMapper,
|
||||
eventEmitter: EventEmitter2,
|
||||
@Inject(AD_MESSAGE_PUBLISHER)
|
||||
protected readonly messagePublisher: MessagePublisherPort,
|
||||
) {
|
||||
super(
|
||||
prisma.ad,
|
||||
prisma,
|
||||
mapper,
|
||||
eventEmitter,
|
||||
new LoggerBase({
|
||||
logger: new Logger(AdRepository.name),
|
||||
domain: 'matcher',
|
||||
messagePublisher,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
14
src/modules/ad/infrastructure/default-params-provider.ts
Normal file
14
src/modules/ad/infrastructure/default-params-provider.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { DefaultParamsProviderPort } from '../core/application/ports/default-params-provider.port';
|
||||
import { DefaultParams } from '../core/application/ports/default-params.type';
|
||||
|
||||
@Injectable()
|
||||
export class DefaultParamsProvider implements DefaultParamsProviderPort {
|
||||
constructor(private readonly _configService: ConfigService) {}
|
||||
getParams = (): DefaultParams => ({
|
||||
GEOROUTER_TYPE: this._configService.get('GEOROUTER_TYPE'),
|
||||
GEOROUTER_URL: this._configService.get('GEOROUTER_URL'),
|
||||
DEFAULT_TIMEZONE: this._configService.get('DEFAULT_TIMEZONE'),
|
||||
});
|
||||
}
|
||||
15
src/modules/ad/infrastructure/prisma.service.ts
Normal file
15
src/modules/ad/infrastructure/prisma.service.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { INestApplication, Injectable, OnModuleInit } from '@nestjs/common';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
@Injectable()
|
||||
export class PrismaService extends PrismaClient implements OnModuleInit {
|
||||
async onModuleInit() {
|
||||
await this.$connect();
|
||||
}
|
||||
|
||||
async enableShutdownHooks(app: INestApplication) {
|
||||
this.$on('beforeExit', async () => {
|
||||
await app.close();
|
||||
});
|
||||
}
|
||||
}
|
||||
16
src/modules/ad/infrastructure/timezone-finder.ts
Normal file
16
src/modules/ad/infrastructure/timezone-finder.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { find } from 'geo-tz';
|
||||
import { TimezoneFinderPort } from '../core/application/ports/timezone-finder.port';
|
||||
|
||||
@Injectable()
|
||||
export class TimezoneFinder implements TimezoneFinderPort {
|
||||
timezones = (
|
||||
lon: number,
|
||||
lat: number,
|
||||
defaultTimezone?: string,
|
||||
): string[] => {
|
||||
const foundTimezones = find(lat, lon);
|
||||
if (defaultTimezone && foundTimezones.length == 0) return [defaultTimezone];
|
||||
return foundTimezones;
|
||||
};
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
import { createMap, Mapper } from '@automapper/core';
|
||||
import { AutomapperProfile, InjectMapper } from '@automapper/nestjs';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Ad } from '../domain/entities/ad';
|
||||
import { CreateAdRequest } from '../domain/dtos/create-ad.request';
|
||||
|
||||
@Injectable()
|
||||
export class AdProfile extends AutomapperProfile {
|
||||
constructor(@InjectMapper() mapper: Mapper) {
|
||||
super(mapper);
|
||||
}
|
||||
|
||||
override get profile() {
|
||||
return (mapper: any) => {
|
||||
createMap(mapper, CreateAdRequest, Ad);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,402 +0,0 @@
|
||||
import { TestingModule, Test } from '@nestjs/testing';
|
||||
import { DatabaseModule } from '../../../database/database.module';
|
||||
import { PrismaService } from '../../../database/adapters/secondaries/prisma.service';
|
||||
import { AdRepository } from '../../adapters/secondaries/ad.repository';
|
||||
import { Ad } from '../../domain/entities/ad';
|
||||
import { Frequency } from '../../domain/types/frequency.enum';
|
||||
|
||||
describe('AdRepository', () => {
|
||||
let prismaService: PrismaService;
|
||||
let adRepository: AdRepository;
|
||||
|
||||
const baseUuid = {
|
||||
uuid: 'be459a29-7a41-4c0b-b371-abe90bfb6f00',
|
||||
};
|
||||
const baseUserUuid = {
|
||||
userUuid: '4e52b54d-a729-4dbd-9283-f84a11bb2200',
|
||||
};
|
||||
const driverAd = {
|
||||
driver: 'true',
|
||||
passenger: 'false',
|
||||
fwdAzimuth: 0,
|
||||
backAzimuth: 180,
|
||||
waypoints: "'LINESTRING(6 47,6.1 47.1,6.2 47.2)'",
|
||||
direction: "'LINESTRING(6 47,6.05 47.05,6.1 47.1,6.15 47.15,6.2 47.2)'",
|
||||
seatsDriver: 3,
|
||||
seatsPassenger: 1,
|
||||
seatsUsed: 0,
|
||||
strict: 'false',
|
||||
};
|
||||
const passengerAd = {
|
||||
driver: 'false',
|
||||
passenger: 'true',
|
||||
fwdAzimuth: 0,
|
||||
backAzimuth: 180,
|
||||
waypoints: "'LINESTRING(6 47,6.2 47.2)'",
|
||||
direction: "'LINESTRING(6 47,6.05 47.05,6.15 47.15,6.2 47.2)'",
|
||||
seatsDriver: 3,
|
||||
seatsPassenger: 1,
|
||||
seatsUsed: 0,
|
||||
strict: 'false',
|
||||
};
|
||||
const driverAndPassengerAd = {
|
||||
driver: 'true',
|
||||
passenger: 'true',
|
||||
fwdAzimuth: 0,
|
||||
backAzimuth: 180,
|
||||
waypoints: "'LINESTRING(6 47,6.1 47.1,6.2 47.2)'",
|
||||
direction: "'LINESTRING(6 47,6.05 47.05,6.1 47.1,6.15 47.15,6.2 47.2)'",
|
||||
seatsDriver: 3,
|
||||
seatsPassenger: 1,
|
||||
seatsUsed: 0,
|
||||
strict: 'false',
|
||||
};
|
||||
const punctualAd = {
|
||||
frequency: `'PUNCTUAL'`,
|
||||
fromDate: `'2023-01-01'`,
|
||||
toDate: `'2023-01-01'`,
|
||||
monTime: 'NULL',
|
||||
tueTime: 'NULL',
|
||||
wedTime: 'NULL',
|
||||
thuTime: 'NULL',
|
||||
friTime: 'NULL',
|
||||
satTime: 'NULL',
|
||||
sunTime: `'2023-01-01T07:00Z'`,
|
||||
monMargin: 900,
|
||||
tueMargin: 900,
|
||||
wedMargin: 900,
|
||||
thuMargin: 900,
|
||||
friMargin: 900,
|
||||
satMargin: 900,
|
||||
sunMargin: 900,
|
||||
};
|
||||
const recurrentAd = {
|
||||
frequency: `'RECURRENT'`,
|
||||
fromDate: `'2023-01-01'`,
|
||||
toDate: `'2023-12-31'`,
|
||||
monTime: `'2023-01-01T07:00Z'`,
|
||||
tueTime: `'2023-01-01T07:00Z'`,
|
||||
wedTime: `'2023-01-01T07:00Z'`,
|
||||
thuTime: `'2023-01-01T07:00Z'`,
|
||||
friTime: `'2023-01-01T07:00Z'`,
|
||||
satTime: 'NULL',
|
||||
sunTime: 'NULL',
|
||||
monMargin: 900,
|
||||
tueMargin: 900,
|
||||
wedMargin: 900,
|
||||
thuMargin: 900,
|
||||
friMargin: 900,
|
||||
satMargin: 900,
|
||||
sunMargin: 900,
|
||||
};
|
||||
|
||||
const createPunctualDriverAds = async (nbToCreate = 10) => {
|
||||
const adToCreate = {
|
||||
...baseUuid,
|
||||
...baseUserUuid,
|
||||
...driverAd,
|
||||
...punctualAd,
|
||||
};
|
||||
for (let i = 0; i < nbToCreate; i++) {
|
||||
adToCreate.uuid = `'${baseUuid.uuid.slice(0, -2)}${i
|
||||
.toString(16)
|
||||
.padStart(2, '0')}'`;
|
||||
adToCreate.userUuid = `'${baseUserUuid.userUuid.slice(0, -2)}${i
|
||||
.toString(16)
|
||||
.padStart(2, '0')}'`;
|
||||
await executeInsertCommand(adToCreate);
|
||||
}
|
||||
};
|
||||
|
||||
const createRecurrentDriverAds = async (nbToCreate = 10) => {
|
||||
const adToCreate = {
|
||||
...baseUuid,
|
||||
...baseUserUuid,
|
||||
...driverAd,
|
||||
...recurrentAd,
|
||||
};
|
||||
for (let i = 0; i < nbToCreate; i++) {
|
||||
adToCreate.uuid = `'${baseUuid.uuid.slice(0, -2)}${i
|
||||
.toString(16)
|
||||
.padStart(2, '0')}'`;
|
||||
adToCreate.userUuid = `'${baseUserUuid.userUuid.slice(0, -2)}${i
|
||||
.toString(16)
|
||||
.padStart(2, '0')}'`;
|
||||
await executeInsertCommand(adToCreate);
|
||||
}
|
||||
};
|
||||
|
||||
const createPunctualPassengerAds = async (nbToCreate = 10) => {
|
||||
const adToCreate = {
|
||||
...baseUuid,
|
||||
...baseUserUuid,
|
||||
...passengerAd,
|
||||
...punctualAd,
|
||||
};
|
||||
for (let i = 0; i < nbToCreate; i++) {
|
||||
adToCreate.uuid = `'${baseUuid.uuid.slice(0, -2)}${i
|
||||
.toString(16)
|
||||
.padStart(2, '0')}'`;
|
||||
adToCreate.userUuid = `'${baseUserUuid.userUuid.slice(0, -2)}${i
|
||||
.toString(16)
|
||||
.padStart(2, '0')}'`;
|
||||
await executeInsertCommand(adToCreate);
|
||||
}
|
||||
};
|
||||
|
||||
const createRecurrentPassengerAds = async (nbToCreate = 10) => {
|
||||
const adToCreate = {
|
||||
...baseUuid,
|
||||
...baseUserUuid,
|
||||
...passengerAd,
|
||||
...recurrentAd,
|
||||
};
|
||||
for (let i = 0; i < nbToCreate; i++) {
|
||||
adToCreate.uuid = `'${baseUuid.uuid.slice(0, -2)}${i
|
||||
.toString(16)
|
||||
.padStart(2, '0')}'`;
|
||||
adToCreate.userUuid = `'${baseUserUuid.userUuid.slice(0, -2)}${i
|
||||
.toString(16)
|
||||
.padStart(2, '0')}'`;
|
||||
await executeInsertCommand(adToCreate);
|
||||
}
|
||||
};
|
||||
|
||||
const createPunctualDriverPassengerAds = async (nbToCreate = 10) => {
|
||||
const adToCreate = {
|
||||
...baseUuid,
|
||||
...baseUserUuid,
|
||||
...driverAndPassengerAd,
|
||||
...punctualAd,
|
||||
};
|
||||
for (let i = 0; i < nbToCreate; i++) {
|
||||
adToCreate.uuid = `'${baseUuid.uuid.slice(0, -2)}${i
|
||||
.toString(16)
|
||||
.padStart(2, '0')}'`;
|
||||
adToCreate.userUuid = `'${baseUserUuid.userUuid.slice(0, -2)}${i
|
||||
.toString(16)
|
||||
.padStart(2, '0')}'`;
|
||||
await executeInsertCommand(adToCreate);
|
||||
}
|
||||
};
|
||||
|
||||
const createRecurrentDriverPassengerAds = async (nbToCreate = 10) => {
|
||||
const adToCreate = {
|
||||
...baseUuid,
|
||||
...baseUserUuid,
|
||||
...driverAndPassengerAd,
|
||||
...recurrentAd,
|
||||
};
|
||||
for (let i = 0; i < nbToCreate; i++) {
|
||||
adToCreate.uuid = `'${baseUuid.uuid.slice(0, -2)}${i
|
||||
.toString(16)
|
||||
.padStart(2, '0')}'`;
|
||||
adToCreate.userUuid = `'${baseUserUuid.userUuid.slice(0, -2)}${i
|
||||
.toString(16)
|
||||
.padStart(2, '0')}'`;
|
||||
await executeInsertCommand(adToCreate);
|
||||
}
|
||||
};
|
||||
|
||||
const executeInsertCommand = async (object: any) => {
|
||||
const command = `INSERT INTO ad ("${Object.keys(object).join(
|
||||
'","',
|
||||
)}") VALUES (${Object.values(object).join(',')})`;
|
||||
await prismaService.$executeRawUnsafe(command);
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
imports: [DatabaseModule],
|
||||
providers: [AdRepository, PrismaService],
|
||||
}).compile();
|
||||
|
||||
prismaService = module.get<PrismaService>(PrismaService);
|
||||
adRepository = module.get<AdRepository>(AdRepository);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await prismaService.$disconnect();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
await prismaService.ad.deleteMany();
|
||||
});
|
||||
|
||||
describe('findAll', () => {
|
||||
it('should return an empty data array', async () => {
|
||||
const res = await adRepository.findAll();
|
||||
expect(res).toEqual({
|
||||
data: [],
|
||||
total: 0,
|
||||
});
|
||||
});
|
||||
|
||||
describe('drivers', () => {
|
||||
it('should return a data array with 8 punctual driver ads', async () => {
|
||||
await createPunctualDriverAds(8);
|
||||
const ads = await adRepository.findAll();
|
||||
expect(ads.data.length).toBe(8);
|
||||
expect(ads.total).toBe(8);
|
||||
expect(ads.data[0].driver).toBeTruthy();
|
||||
expect(ads.data[0].passenger).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should return a data array limited to 10 punctual driver ads', async () => {
|
||||
await createPunctualDriverAds(20);
|
||||
const ads = await adRepository.findAll();
|
||||
expect(ads.data.length).toBe(10);
|
||||
expect(ads.total).toBe(20);
|
||||
expect(ads.data[1].driver).toBeTruthy();
|
||||
expect(ads.data[1].passenger).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should return a data array with 8 recurrent driver ads', async () => {
|
||||
await createRecurrentDriverAds(8);
|
||||
const ads = await adRepository.findAll();
|
||||
expect(ads.data.length).toBe(8);
|
||||
expect(ads.total).toBe(8);
|
||||
expect(ads.data[2].driver).toBeTruthy();
|
||||
expect(ads.data[2].passenger).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should return a data array limited to 10 recurrent driver ads', async () => {
|
||||
await createRecurrentDriverAds(20);
|
||||
const ads = await adRepository.findAll();
|
||||
expect(ads.data.length).toBe(10);
|
||||
expect(ads.total).toBe(20);
|
||||
expect(ads.data[3].driver).toBeTruthy();
|
||||
expect(ads.data[3].passenger).toBeFalsy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('passengers', () => {
|
||||
it('should return a data array with 7 punctual passenger ads', async () => {
|
||||
await createPunctualPassengerAds(7);
|
||||
const ads = await adRepository.findAll();
|
||||
expect(ads.data.length).toBe(7);
|
||||
expect(ads.total).toBe(7);
|
||||
expect(ads.data[0].passenger).toBeTruthy();
|
||||
expect(ads.data[0].driver).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should return a data array limited to 10 punctual passenger ads', async () => {
|
||||
await createPunctualPassengerAds(15);
|
||||
const ads = await adRepository.findAll();
|
||||
expect(ads.data.length).toBe(10);
|
||||
expect(ads.total).toBe(15);
|
||||
expect(ads.data[1].passenger).toBeTruthy();
|
||||
expect(ads.data[1].driver).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should return a data array with 7 recurrent passenger ads', async () => {
|
||||
await createRecurrentPassengerAds(7);
|
||||
const ads = await adRepository.findAll();
|
||||
expect(ads.data.length).toBe(7);
|
||||
expect(ads.total).toBe(7);
|
||||
expect(ads.data[2].passenger).toBeTruthy();
|
||||
expect(ads.data[2].driver).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should return a data array limited to 10 recurrent passenger ads', async () => {
|
||||
await createRecurrentPassengerAds(15);
|
||||
const ads = await adRepository.findAll();
|
||||
expect(ads.data.length).toBe(10);
|
||||
expect(ads.total).toBe(15);
|
||||
expect(ads.data[3].passenger).toBeTruthy();
|
||||
expect(ads.data[3].driver).toBeFalsy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('drivers and passengers', () => {
|
||||
it('should return a data array with 6 punctual driver and passenger ads', async () => {
|
||||
await createPunctualDriverPassengerAds(6);
|
||||
const ads = await adRepository.findAll();
|
||||
expect(ads.data.length).toBe(6);
|
||||
expect(ads.total).toBe(6);
|
||||
expect(ads.data[0].passenger).toBeTruthy();
|
||||
expect(ads.data[0].driver).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should return a data array limited to 10 punctual driver and passenger ads', async () => {
|
||||
await createPunctualDriverPassengerAds(16);
|
||||
const ads = await adRepository.findAll();
|
||||
expect(ads.data.length).toBe(10);
|
||||
expect(ads.total).toBe(16);
|
||||
expect(ads.data[1].passenger).toBeTruthy();
|
||||
expect(ads.data[1].driver).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should return a data array with 6 recurrent driver and passenger ads', async () => {
|
||||
await createRecurrentDriverPassengerAds(6);
|
||||
const ads = await adRepository.findAll();
|
||||
expect(ads.data.length).toBe(6);
|
||||
expect(ads.total).toBe(6);
|
||||
expect(ads.data[2].passenger).toBeTruthy();
|
||||
expect(ads.data[2].driver).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should return a data array limited to 10 recurrent driver and passenger ads', async () => {
|
||||
await createRecurrentDriverPassengerAds(16);
|
||||
const ads = await adRepository.findAll();
|
||||
expect(ads.data.length).toBe(10);
|
||||
expect(ads.total).toBe(16);
|
||||
expect(ads.data[3].passenger).toBeTruthy();
|
||||
expect(ads.data[3].driver).toBeTruthy();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('findOneByUuid', () => {
|
||||
it('should return an ad', async () => {
|
||||
await createPunctualDriverAds(1);
|
||||
const ad = await adRepository.findOneByUuid(baseUuid.uuid);
|
||||
expect(ad.uuid).toBe(baseUuid.uuid);
|
||||
});
|
||||
|
||||
it('should return null', async () => {
|
||||
const ad = await adRepository.findOneByUuid(
|
||||
'544572be-11fb-4244-8235-587221fc9104',
|
||||
);
|
||||
expect(ad).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('create', () => {
|
||||
it('should create an ad', async () => {
|
||||
const beforeCount = await prismaService.ad.count();
|
||||
|
||||
const adToCreate: Ad = new Ad();
|
||||
adToCreate.uuid = 'be459a29-7a41-4c0b-b371-abe90bfb6f00';
|
||||
adToCreate.userUuid = '4e52b54d-a729-4dbd-9283-f84a11bb2200';
|
||||
adToCreate.driver = true;
|
||||
adToCreate.passenger = false;
|
||||
adToCreate.fwdAzimuth = 0;
|
||||
adToCreate.backAzimuth = 180;
|
||||
adToCreate.waypoints = "'LINESTRING(6 47,6.1 47.1,6.2 47.2)'";
|
||||
adToCreate.direction =
|
||||
"'LINESTRING(6 47,6.05 47.05,6.1 47.1,6.15 47.15,6.2 47.2)'";
|
||||
adToCreate.seatsDriver = 3;
|
||||
adToCreate.seatsPassenger = 1;
|
||||
adToCreate.seatsUsed = 0;
|
||||
adToCreate.strict = false;
|
||||
adToCreate.frequency = Frequency.PUNCTUAL;
|
||||
adToCreate.fromDate = new Date(2023, 0, 1);
|
||||
adToCreate.toDate = new Date(2023, 0, 1);
|
||||
adToCreate.sunTime = new Date(2023, 0, 1, 6, 0, 0);
|
||||
adToCreate.monMargin = 900;
|
||||
adToCreate.tueMargin = 900;
|
||||
adToCreate.wedMargin = 900;
|
||||
adToCreate.thuMargin = 900;
|
||||
adToCreate.friMargin = 900;
|
||||
adToCreate.satMargin = 900;
|
||||
adToCreate.sunMargin = 900;
|
||||
const ad = await adRepository.createAd(adToCreate);
|
||||
|
||||
const afterCount = await prismaService.ad.count();
|
||||
|
||||
expect(afterCount - beforeCount).toBe(1);
|
||||
expect(ad.uuid).toBe('be459a29-7a41-4c0b-b371-abe90bfb6f00');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,38 +0,0 @@
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { DefaultParamsProvider } from '../../../../adapters/secondaries/default-params.provider';
|
||||
import { DefaultParams } from '../../../../domain/types/default-params.type';
|
||||
|
||||
const mockConfigService = {
|
||||
get: jest.fn().mockImplementation(() => 'some_default_value'),
|
||||
};
|
||||
|
||||
describe('DefaultParamsProvider', () => {
|
||||
let defaultParamsProvider: DefaultParamsProvider;
|
||||
|
||||
beforeAll(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
imports: [],
|
||||
providers: [
|
||||
DefaultParamsProvider,
|
||||
{
|
||||
provide: ConfigService,
|
||||
useValue: mockConfigService,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
defaultParamsProvider = module.get<DefaultParamsProvider>(
|
||||
DefaultParamsProvider,
|
||||
);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(defaultParamsProvider).toBeDefined();
|
||||
});
|
||||
|
||||
it('should provide default params', async () => {
|
||||
const params: DefaultParams = defaultParamsProvider.getParams();
|
||||
expect(params.GEOROUTER_URL).toBe('some_default_value');
|
||||
});
|
||||
});
|
||||
@@ -1,47 +0,0 @@
|
||||
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { Messager } from '../../../../adapters/secondaries/messager';
|
||||
|
||||
const mockAmqpConnection = {
|
||||
publish: jest.fn().mockImplementation(),
|
||||
};
|
||||
|
||||
const mockConfigService = {
|
||||
get: jest.fn().mockResolvedValue({
|
||||
RMQ_EXCHANGE: 'mobicoop',
|
||||
}),
|
||||
};
|
||||
|
||||
describe('Messager', () => {
|
||||
let messager: Messager;
|
||||
|
||||
beforeAll(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
imports: [],
|
||||
providers: [
|
||||
Messager,
|
||||
{
|
||||
provide: AmqpConnection,
|
||||
useValue: mockAmqpConnection,
|
||||
},
|
||||
{
|
||||
provide: ConfigService,
|
||||
useValue: mockConfigService,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
messager = module.get<Messager>(Messager);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(messager).toBeDefined();
|
||||
});
|
||||
|
||||
it('should publish a message', async () => {
|
||||
jest.spyOn(mockAmqpConnection, 'publish');
|
||||
messager.publish('test.create.info', 'my-test');
|
||||
expect(mockAmqpConnection.publish).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
@@ -1,35 +0,0 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { TimezoneFinder } from '../../../../adapters/secondaries/timezone-finder';
|
||||
import { GeoTimezoneFinder } from '../../../../../geography/adapters/secondaries/geo-timezone-finder';
|
||||
|
||||
const mockGeoTimezoneFinder = {
|
||||
timezones: jest.fn().mockImplementation(() => ['Europe/Paris']),
|
||||
};
|
||||
|
||||
describe('Timezone Finder', () => {
|
||||
let timezoneFinder: TimezoneFinder;
|
||||
|
||||
beforeAll(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
imports: [],
|
||||
providers: [
|
||||
TimezoneFinder,
|
||||
{
|
||||
provide: GeoTimezoneFinder,
|
||||
useValue: mockGeoTimezoneFinder,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
timezoneFinder = module.get<TimezoneFinder>(TimezoneFinder);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(timezoneFinder).toBeDefined();
|
||||
});
|
||||
it('should get timezone for Nancy(France) as Europe/Paris', () => {
|
||||
const timezones = timezoneFinder.timezones(6.179373, 48.687913);
|
||||
expect(timezones.length).toBe(1);
|
||||
expect(timezones[0]).toBe('Europe/Paris');
|
||||
});
|
||||
});
|
||||
@@ -1,176 +0,0 @@
|
||||
import { CreateAdRequest } from '../../../domain/dtos/create-ad.request';
|
||||
import { CreateAdUseCase } from '../../../domain/usecases/create-ad.usecase';
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { AutomapperModule } from '@automapper/nestjs';
|
||||
import { classes } from '@automapper/classes';
|
||||
import { AdRepository } from '../../../adapters/secondaries/ad.repository';
|
||||
import { CreateAdCommand } from '../../../commands/create-ad.command';
|
||||
import { Ad } from '../../../domain/entities/ad';
|
||||
import { AdProfile } from '../../../mappers/ad.profile';
|
||||
import { Frequency } from '../../../domain/types/frequency.enum';
|
||||
import { RouteKey } from '../../../domain/entities/geography';
|
||||
import { DatabaseException } from '../../../../database/exceptions/database.exception';
|
||||
import { Route } from '../../../../geography/domain/entities/route';
|
||||
|
||||
const mockAdRepository = {
|
||||
createAd: jest.fn().mockImplementation((ad) => {
|
||||
if (ad.uuid == '00000000-0000-0000-0000-000000000000')
|
||||
throw new DatabaseException();
|
||||
return new Ad();
|
||||
}),
|
||||
};
|
||||
const mockGeorouterCreator = {
|
||||
create: jest.fn().mockImplementation(() => ({
|
||||
route: jest.fn().mockImplementation(() => [
|
||||
{
|
||||
key: RouteKey.DRIVER,
|
||||
route: <Route>{
|
||||
points: [],
|
||||
fwdAzimuth: 0,
|
||||
backAzimuth: 180,
|
||||
distance: 20000,
|
||||
duration: 1800,
|
||||
},
|
||||
},
|
||||
{
|
||||
key: RouteKey.PASSENGER,
|
||||
route: <Route>{
|
||||
points: [],
|
||||
fwdAzimuth: 0,
|
||||
backAzimuth: 180,
|
||||
distance: 20000,
|
||||
duration: 1800,
|
||||
},
|
||||
},
|
||||
{
|
||||
key: RouteKey.COMMON,
|
||||
route: <Route>{
|
||||
points: [],
|
||||
fwdAzimuth: 0,
|
||||
backAzimuth: 180,
|
||||
distance: 20000,
|
||||
duration: 1800,
|
||||
},
|
||||
},
|
||||
]),
|
||||
})),
|
||||
};
|
||||
const mockParamsProvider = {
|
||||
getParams: jest.fn().mockImplementation(() => ({
|
||||
DEFAULT_TIMEZONE: 'Europe/Paris',
|
||||
GEOROUTER_TYPE: 'graphhopper',
|
||||
GEOROUTER_URL: 'localhost',
|
||||
})),
|
||||
};
|
||||
const mockTimezoneFinder = {
|
||||
timezones: jest.fn().mockImplementation(() => ['Europe/Paris']),
|
||||
};
|
||||
const mockDirectionEncoder = {
|
||||
encode: jest.fn(),
|
||||
};
|
||||
|
||||
const createAdRequest: CreateAdRequest = {
|
||||
uuid: '77c55dfc-c28b-4026-942e-f94e95401fb1',
|
||||
userUuid: 'dfd993f6-7889-4876-9570-5e1d7b6e3f42',
|
||||
driver: true,
|
||||
passenger: false,
|
||||
frequency: Frequency.RECURRENT,
|
||||
fromDate: new Date('2023-04-26'),
|
||||
toDate: new Date('2024-04-25'),
|
||||
monTime: '07:00',
|
||||
tueTime: '07:00',
|
||||
wedTime: '07:00',
|
||||
thuTime: '07:00',
|
||||
friTime: '07:00',
|
||||
satTime: null,
|
||||
sunTime: null,
|
||||
monMargin: 900,
|
||||
tueMargin: 900,
|
||||
wedMargin: 900,
|
||||
thuMargin: 900,
|
||||
friMargin: 900,
|
||||
satMargin: 900,
|
||||
sunMargin: 900,
|
||||
seatsDriver: 3,
|
||||
seatsPassenger: 1,
|
||||
strict: false,
|
||||
waypoints: [
|
||||
{ lon: 6, lat: 45 },
|
||||
{ lon: 6.5, lat: 45.5 },
|
||||
],
|
||||
};
|
||||
|
||||
const setUuid = async (uuid: string): Promise<void> => {
|
||||
createAdRequest.uuid = uuid;
|
||||
};
|
||||
|
||||
const setIsDriver = async (isDriver: boolean): Promise<void> => {
|
||||
createAdRequest.driver = isDriver;
|
||||
};
|
||||
|
||||
const setIsPassenger = async (isPassenger: boolean): Promise<void> => {
|
||||
createAdRequest.passenger = isPassenger;
|
||||
};
|
||||
|
||||
describe('CreateAdUseCase', () => {
|
||||
let createAdUseCase: CreateAdUseCase;
|
||||
|
||||
beforeAll(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
imports: [AutomapperModule.forRoot({ strategyInitializer: classes() })],
|
||||
providers: [
|
||||
{
|
||||
provide: AdRepository,
|
||||
useValue: mockAdRepository,
|
||||
},
|
||||
{
|
||||
provide: 'GeorouterCreator',
|
||||
useValue: mockGeorouterCreator,
|
||||
},
|
||||
{
|
||||
provide: 'ParamsProvider',
|
||||
useValue: mockParamsProvider,
|
||||
},
|
||||
{
|
||||
provide: 'TimezoneFinder',
|
||||
useValue: mockTimezoneFinder,
|
||||
},
|
||||
{
|
||||
provide: 'DirectionEncoder',
|
||||
useValue: mockDirectionEncoder,
|
||||
},
|
||||
AdProfile,
|
||||
CreateAdUseCase,
|
||||
],
|
||||
}).compile();
|
||||
|
||||
createAdUseCase = module.get<CreateAdUseCase>(CreateAdUseCase);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(createAdUseCase).toBeDefined();
|
||||
});
|
||||
|
||||
describe('execute', () => {
|
||||
it('should create an ad as driver', async () => {
|
||||
const ad = await createAdUseCase.execute(
|
||||
new CreateAdCommand(createAdRequest),
|
||||
);
|
||||
expect(ad).toBeInstanceOf(Ad);
|
||||
});
|
||||
it('should create an ad as passenger', async () => {
|
||||
await setIsDriver(false);
|
||||
await setIsPassenger(true);
|
||||
const ad = await createAdUseCase.execute(
|
||||
new CreateAdCommand(createAdRequest),
|
||||
);
|
||||
expect(ad).toBeInstanceOf(Ad);
|
||||
});
|
||||
it('should throw an exception if repository fails', async () => {
|
||||
await setUuid('00000000-0000-0000-0000-000000000000');
|
||||
await expect(
|
||||
createAdUseCase.execute(new CreateAdCommand(createAdRequest)),
|
||||
).rejects.toBeInstanceOf(DatabaseException);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,138 +0,0 @@
|
||||
import { Role } from '../../../domain/types/role.enum';
|
||||
import { Geography } from '../../../domain/entities/geography';
|
||||
import { Coordinate } from '../../../../geography/domain/entities/coordinate';
|
||||
import { IGeorouter } from '../../../../geography/domain/interfaces/georouter.interface';
|
||||
import { GeorouterSettings } from '../../../../geography/domain/types/georouter-settings.type';
|
||||
import { Route } from '../../../../geography/domain/entities/route';
|
||||
import { IGeodesic } from '../../../../geography/domain/interfaces/geodesic.interface';
|
||||
|
||||
const simpleCoordinates: Coordinate[] = [
|
||||
{
|
||||
lon: 6,
|
||||
lat: 47,
|
||||
},
|
||||
{
|
||||
lon: 6.1,
|
||||
lat: 47.1,
|
||||
},
|
||||
];
|
||||
|
||||
const complexCoordinates: Coordinate[] = [
|
||||
{
|
||||
lon: 6,
|
||||
lat: 47,
|
||||
},
|
||||
{
|
||||
lon: 6.1,
|
||||
lat: 47.1,
|
||||
},
|
||||
{
|
||||
lon: 6.2,
|
||||
lat: 47.2,
|
||||
},
|
||||
];
|
||||
|
||||
const mockGeodesic: IGeodesic = {
|
||||
inverse: jest.fn(),
|
||||
};
|
||||
|
||||
const driverRoute: Route = new Route(mockGeodesic);
|
||||
driverRoute.distance = 25000;
|
||||
|
||||
const commonRoute: Route = new Route(mockGeodesic);
|
||||
commonRoute.distance = 20000;
|
||||
|
||||
const mockGeorouter: IGeorouter = {
|
||||
route: jest
|
||||
.fn()
|
||||
.mockResolvedValueOnce([
|
||||
{
|
||||
key: 'driver',
|
||||
route: driverRoute,
|
||||
},
|
||||
])
|
||||
.mockResolvedValueOnce([
|
||||
{
|
||||
key: 'passenger',
|
||||
route: commonRoute,
|
||||
},
|
||||
])
|
||||
.mockResolvedValueOnce([
|
||||
{
|
||||
key: 'common',
|
||||
route: commonRoute,
|
||||
},
|
||||
])
|
||||
.mockResolvedValueOnce([
|
||||
{
|
||||
key: 'driver',
|
||||
route: driverRoute,
|
||||
},
|
||||
{
|
||||
key: 'passenger',
|
||||
route: commonRoute,
|
||||
},
|
||||
]),
|
||||
};
|
||||
|
||||
const georouterSettings: GeorouterSettings = {
|
||||
withDistance: false,
|
||||
withPoints: true,
|
||||
withTime: false,
|
||||
};
|
||||
|
||||
describe('Geography entity', () => {
|
||||
it('should be defined', () => {
|
||||
expect(new Geography(simpleCoordinates)).toBeDefined();
|
||||
});
|
||||
|
||||
it('should create a route as driver', async () => {
|
||||
const geography = new Geography(complexCoordinates);
|
||||
await geography.createRoutes(
|
||||
[Role.DRIVER],
|
||||
mockGeorouter,
|
||||
georouterSettings,
|
||||
);
|
||||
expect(geography.driverRoute).toBeDefined();
|
||||
expect(geography.passengerRoute).toBeUndefined();
|
||||
expect(geography.driverRoute.distance).toBe(25000);
|
||||
});
|
||||
|
||||
it('should create a route as passenger', async () => {
|
||||
const geography = new Geography(simpleCoordinates);
|
||||
await geography.createRoutes(
|
||||
[Role.PASSENGER],
|
||||
mockGeorouter,
|
||||
georouterSettings,
|
||||
);
|
||||
expect(geography.driverRoute).toBeUndefined();
|
||||
expect(geography.passengerRoute).toBeDefined();
|
||||
expect(geography.passengerRoute.distance).toBe(20000);
|
||||
});
|
||||
|
||||
it('should create routes as driver and passenger with simple coordinates', async () => {
|
||||
const geography = new Geography(simpleCoordinates);
|
||||
await geography.createRoutes(
|
||||
[Role.DRIVER, Role.PASSENGER],
|
||||
mockGeorouter,
|
||||
georouterSettings,
|
||||
);
|
||||
expect(geography.driverRoute).toBeDefined();
|
||||
expect(geography.passengerRoute).toBeDefined();
|
||||
expect(geography.driverRoute.distance).toBe(20000);
|
||||
expect(geography.passengerRoute.distance).toBe(20000);
|
||||
});
|
||||
|
||||
it('should create routes as driver and passenger with complex coordinates', async () => {
|
||||
const geography = new Geography(complexCoordinates);
|
||||
await geography.createRoutes(
|
||||
[Role.DRIVER, Role.PASSENGER],
|
||||
mockGeorouter,
|
||||
georouterSettings,
|
||||
);
|
||||
expect(geography.driverRoute).toBeDefined();
|
||||
expect(geography.passengerRoute).toBeDefined();
|
||||
expect(geography.driverRoute.distance).toBe(25000);
|
||||
expect(geography.passengerRoute.distance).toBe(20000);
|
||||
});
|
||||
});
|
||||
@@ -1,53 +0,0 @@
|
||||
import { TimeConverter } from '../../../domain/entities/time-converter';
|
||||
|
||||
describe('TimeConverter', () => {
|
||||
it('should be defined', () => {
|
||||
expect(new TimeConverter()).toBeDefined();
|
||||
});
|
||||
|
||||
it('should convert a Europe/Paris datetime to utc datetime', () => {
|
||||
expect(
|
||||
TimeConverter.toUtcDatetime(
|
||||
new Date('2023-05-01'),
|
||||
'07:00',
|
||||
'Europe/Paris',
|
||||
).getUTCHours(),
|
||||
).toBe(6);
|
||||
});
|
||||
|
||||
it('should return undefined when trying to convert a Europe/Paris datetime to utc datetime without a valid date', () => {
|
||||
expect(
|
||||
TimeConverter.toUtcDatetime(undefined, '07:00', 'Europe/Paris'),
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
TimeConverter.toUtcDatetime(
|
||||
new Date('2023-13-01'),
|
||||
'07:00',
|
||||
'Europe/Paris',
|
||||
),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return undefined when trying to convert a Europe/Paris datetime to utc datetime without a valid time', () => {
|
||||
expect(
|
||||
TimeConverter.toUtcDatetime(
|
||||
new Date('2023-05-01'),
|
||||
undefined,
|
||||
'Europe/Paris',
|
||||
),
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
TimeConverter.toUtcDatetime(new Date('2023-05-01'), 'a', 'Europe/Paris'),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return undefined when trying to convert a datetime to utc datetime without a valid timezone', () => {
|
||||
expect(
|
||||
TimeConverter.toUtcDatetime(
|
||||
new Date('2023-12-01'),
|
||||
'07:00',
|
||||
'OlympusMons/Mars',
|
||||
),
|
||||
).toBeUndefined();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user