WIP - first shot of massive ddd-hexagon refactor

This commit is contained in:
sbriat
2023-06-20 16:50:55 +02:00
parent cd84567107
commit e1989c0a52
124 changed files with 3651 additions and 2460 deletions

View File

@@ -1 +0,0 @@
export const PARAMS_PROVIDER = Symbol();

View File

@@ -0,0 +1,3 @@
export const PARAMS_PROVIDER = Symbol('PARAMS_PROVIDER');
export const TIMEZONE_FINDER = Symbol('TIMEZONE_FINDER');
export const AD_REPOSITORY = Symbol('AD_REPOSITORY');

228
src/modules/ad/ad.mapper.ts Normal file
View File

@@ -0,0 +1,228 @@
import { Mapper } from '@libs/ddd';
import { AdResponseDto } from './interface/dtos/ad.response.dto';
import { Inject, Injectable } from '@nestjs/common';
import { AdEntity } from './core/ad.entity';
import { AdModel, WaypointModel } from './infrastructure/ad.repository';
import { Frequency } from './core/ad.types';
import { WaypointProps } from './core/value-objects/waypoint.value-object';
import { v4 } from 'uuid';
import { PARAMS_PROVIDER, TIMEZONE_FINDER } from './ad.di-tokens';
import { TimezoneFinderPort } from './core/ports/timezone-finder.port';
import { Coordinates } from './core/types/coordinates';
import { DefaultParamsProviderPort } from './core/ports/default-params-provider.port';
import { DefaultParams } from './core/ports/default-params.type';
import { DateTime, TimeZone } from 'timezonecomplete';
/**
* 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, AdModel, AdResponseDto> {
private timezone: string;
private readonly defaultParams: DefaultParams;
constructor(
@Inject(PARAMS_PROVIDER)
private readonly defaultParamsProvider: DefaultParamsProviderPort,
@Inject(TIMEZONE_FINDER)
private readonly timezoneFinder: TimezoneFinderPort,
) {
this.defaultParams = defaultParamsProvider.getParams();
}
toPersistence = (entity: AdEntity): AdModel => {
const copy = entity.getProps();
const timezone = this.getTimezone(copy.waypoints[0].address.coordinates);
const now = new Date();
const record: AdModel = {
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),
monTime: copy.schedule.mon
? AdMapper.toUtcDatetime(
new Date(copy.fromDate),
copy.schedule.mon,
timezone,
)
: undefined,
tueTime: copy.schedule.tue
? AdMapper.toUtcDatetime(
new Date(copy.fromDate),
copy.schedule.tue,
timezone,
)
: undefined,
wedTime: copy.schedule.wed
? AdMapper.toUtcDatetime(
new Date(copy.fromDate),
copy.schedule.wed,
timezone,
)
: undefined,
thuTime: copy.schedule.thu
? AdMapper.toUtcDatetime(
new Date(copy.fromDate),
copy.schedule.thu,
timezone,
)
: undefined,
friTime: copy.schedule.fri
? AdMapper.toUtcDatetime(
new Date(copy.fromDate),
copy.schedule.fri,
timezone,
)
: undefined,
satTime: copy.schedule.sat
? AdMapper.toUtcDatetime(
new Date(copy.fromDate),
copy.schedule.sat,
timezone,
)
: undefined,
sunTime: copy.schedule.sun
? AdMapper.toUtcDatetime(
new Date(copy.fromDate),
copy.schedule.sun,
timezone,
)
: undefined,
monMargin: copy.marginDurations.mon,
tueMargin: copy.marginDurations.tue,
wedMargin: copy.marginDurations.wed,
thuMargin: copy.marginDurations.thu,
friMargin: copy.marginDurations.fri,
satMargin: copy.marginDurations.sat,
sunMargin: copy.marginDurations.sun,
seatsProposed: copy.seatsProposed,
seatsRequested: copy.seatsRequested,
strict: copy.strict,
waypoints: {
create: copy.waypoints.map((waypoint: WaypointProps) => ({
uuid: v4(),
position: waypoint.position,
name: waypoint.address.name,
houseNumber: waypoint.address.houseNumber,
street: waypoint.address.street,
locality: waypoint.address.locality,
postalCode: waypoint.address.postalCode,
country: waypoint.address.country,
lon: waypoint.address.coordinates.lon,
lat: waypoint.address.coordinates.lat,
createdAt: now,
updatedAt: now,
})),
},
createdAt: copy.createdAt,
updatedAt: copy.updatedAt,
};
return record;
};
toDomain = (record: AdModel): 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(),
toDate: record.toDate.toISOString(),
schedule: {
mon: record.monTime.toISOString(),
tue: record.tueTime.toISOString(),
wed: record.wedTime.toISOString(),
thu: record.thuTime.toISOString(),
fri: record.friTime.toISOString(),
sat: record.satTime.toISOString(),
sun: record.sunTime.toISOString(),
},
marginDurations: {
mon: record.monMargin,
tue: record.tueMargin,
wed: record.wedMargin,
thu: record.thuMargin,
fri: record.friMargin,
sat: record.satMargin,
sun: record.sunMargin,
},
seatsProposed: record.seatsProposed,
seatsRequested: record.seatsRequested,
strict: record.strict,
waypoints: record.waypoints.create.map((waypoint: WaypointModel) => ({
position: waypoint.position,
address: {
name: waypoint.name,
houseNumber: waypoint.houseNumber,
street: waypoint.street,
postalCode: waypoint.postalCode,
locality: waypoint.locality,
country: waypoint.country,
coordinates: {
lon: waypoint.lon,
lat: waypoint.lat,
},
},
})),
},
});
return entity;
};
toResponse = (entity: AdEntity): AdResponseDto => {
const props = entity.getProps();
const response = new AdResponseDto(entity);
response.uuid = props.id;
return response;
};
/* ^ Data returned to the user is whitelisted to avoid leaks.
If a new property is added, like password or a
credit card number, it won't be returned
unless you specifically allow this.
(avoid blacklisting, which will return everything
but blacklisted items, which can lead to a data leak).
*/
private getTimezone = (coordinates: Coordinates): string => {
try {
const timezones = this.timezoneFinder.timezones(
coordinates.lon,
coordinates.lat,
);
if (timezones.length > 0) return timezones[0];
} catch (e) {}
return this.defaultParams.DEFAULT_TIMEZONE;
};
private 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;
}
};
}

View File

@@ -1,25 +1,33 @@
import { Module } from '@nestjs/common';
import { AdController } from './adapters/primaries/ad.controller';
import { CreateAdGrpcController } from './interface/grpc-controllers/create-ad.grpc.controller';
import { DatabaseModule } from '../database/database.module';
import { CqrsModule } from '@nestjs/cqrs';
import { AdProfile } from './mappers/ad.profile';
import { AdsRepository } from './adapters/secondaries/ads.repository';
import { FindAdByUuidUseCase } from './domain/usecases/find-ad-by-uuid.usecase';
import { CreateAdUseCase } from './domain/usecases/create-ad.usecase';
import { DefaultParamsProvider } from './adapters/secondaries/default-params.provider';
import { PARAMS_PROVIDER } from './ad.constants';
import {
AD_REPOSITORY,
PARAMS_PROVIDER,
TIMEZONE_FINDER,
} from './ad.di-tokens';
import { MESSAGE_BROKER_PUBLISHER, MESSAGE_PUBLISHER } from 'src/app.constants';
import { MessageBrokerPublisher } from '@mobicoop/message-broker-module';
import { MessagePublisher } from './adapters/secondaries/message-publisher';
import { AdRepository } from './infrastructure/ad.repository';
import { DefaultParamsProvider } from './infrastructure/default-params-provider';
import { MessagePublisher } from './infrastructure/message-publisher';
import { PrismaService } from './infrastructure/prisma-service';
import { AdMapper } from './ad.mapper';
import { CreateAdService } from './core/commands/create-ad/create-ad.service';
import { TimezoneFinder } from './infrastructure/timezone-finder';
@Module({
imports: [DatabaseModule, CqrsModule],
controllers: [AdController],
controllers: [CreateAdGrpcController],
providers: [
AdProfile,
AdsRepository,
FindAdByUuidUseCase,
CreateAdUseCase,
CreateAdService,
PrismaService,
AdMapper,
{
provide: AD_REPOSITORY,
useClass: AdRepository,
},
{
provide: PARAMS_PROVIDER,
useClass: DefaultParamsProvider,
@@ -32,6 +40,10 @@ import { MessagePublisher } from './adapters/secondaries/message-publisher';
provide: MESSAGE_PUBLISHER,
useClass: MessagePublisher,
},
{
provide: TIMEZONE_FINDER,
useClass: TimezoneFinder,
},
],
})
export class AdModule {}

View File

@@ -1,59 +0,0 @@
import { Mapper } from '@automapper/core';
import { InjectMapper } from '@automapper/nestjs';
import { Controller, UsePipes } from '@nestjs/common';
import { CommandBus, QueryBus } from '@nestjs/cqrs';
import { GrpcMethod, RpcException } from '@nestjs/microservices';
import { RpcValidationPipe } from '../../../../utils/pipes/rpc.validation-pipe';
import { FindAdByUuidRequest } from '../../domain/dtos/find-ad-by-uuid.request';
import { AdPresenter } from './ad.presenter';
import { FindAdByUuidQuery } from '../../queries/find-ad-by-uuid.query';
import { Ad } from '../../domain/entities/ad';
import { CreateAdRequest } from '../../domain/dtos/create-ad.request';
import { CreateAdCommand } from '../../commands/create-ad.command';
import { DatabaseException } from '../../../database/exceptions/database.exception';
@UsePipes(
new RpcValidationPipe({
whitelist: false,
forbidUnknownValues: false,
}),
)
@Controller()
export class AdController {
constructor(
private readonly commandBus: CommandBus,
private readonly queryBus: QueryBus,
@InjectMapper() private readonly mapper: Mapper,
) {}
@GrpcMethod('AdsService', 'FindOneByUuid')
async findOnebyUuid(data: FindAdByUuidRequest): Promise<AdPresenter> {
try {
const ad = await this.queryBus.execute(new FindAdByUuidQuery(data));
return this.mapper.map(ad, Ad, AdPresenter);
} catch (e) {
throw new RpcException({
code: e.code,
message: e.message,
});
}
}
@GrpcMethod('AdsService', 'Create')
async createAd(data: CreateAdRequest): Promise<AdPresenter> {
try {
const ad = await this.commandBus.execute(new CreateAdCommand(data));
return this.mapper.map(ad, Ad, AdPresenter);
} catch (e) {
if (e instanceof DatabaseException) {
if (e.message.includes('Already exists')) {
throw new RpcException({
code: 6,
message: 'Ad already exists',
});
}
}
throw new RpcException({});
}
}
}

View File

@@ -1,83 +0,0 @@
syntax = "proto3";
package ad;
service AdsService {
rpc FindOneByUuid(AdByUuid) returns (Ad);
rpc FindAll(AdFilter) returns (Ads);
rpc Create(Ad) returns (AdByUuid);
rpc Update(Ad) returns (Ad);
rpc Delete(AdByUuid) returns (Empty);
}
message AdByUuid {
string uuid = 1;
}
message Ad {
string uuid = 1;
string userUuid = 2;
bool driver = 3;
bool passenger = 4;
Frequency frequency = 5;
optional string departure = 6;
string fromDate = 7;
string toDate = 8;
Schedule schedule = 9;
MarginDurations marginDurations = 10;
int32 seatsPassenger = 11;
int32 seatsDriver = 12;
bool strict = 13;
repeated Address addresses = 14;
}
message Schedule {
optional string mon = 1;
optional string tue = 2;
optional string wed = 3;
optional string thu = 4;
optional string fri = 5;
optional string sat = 6;
optional string sun = 7;
}
message MarginDurations {
int32 mon = 1;
int32 tue = 2;
int32 wed = 3;
int32 thu = 4;
int32 fri = 5;
int32 sat = 6;
int32 sun = 7;
}
message Address {
string uuid = 1;
int32 position = 2;
float lon = 3;
float lat = 4;
optional string name = 5;
optional string houseNumber = 6;
optional string street = 7;
optional string locality = 8;
optional string postalCode = 9;
string country = 10;
}
enum Frequency {
PUNCTUAL = 1;
RECURRENT = 2;
}
message AdFilter {
int32 page = 1;
int32 perPage = 2;
}
message Ads {
repeated Ad data = 1;
int32 total = 2;
}
message Empty {}

View File

@@ -1,8 +0,0 @@
import { Injectable } from '@nestjs/common';
import { AdRepository } from '../../../database/domain/ad-repository';
import { Ad } from '../../domain/entities/ad';
//TODO : properly implement mutate operation to prisma
@Injectable()
export class AdsRepository extends AdRepository<Ad> {
protected model = 'ad';
}

View File

@@ -1,9 +0,0 @@
import { CreateAdRequest } from '../domain/dtos/create-ad.request';
export class CreateAdCommand {
readonly createAdRequest: CreateAdRequest;
constructor(request: CreateAdRequest) {
this.createAdRequest = request;
}
}

View File

@@ -0,0 +1,137 @@
import { AggregateRoot, AggregateID } from '@libs/ddd';
import { v4 } from 'uuid';
import { AdCreatedDomainEvent } from './events/ad-created.domain-events';
import { AdProps, CreateAdProps, DefaultAdProps } from './ad.types';
import { Waypoint } from './value-objects/waypoint.value-object';
import { MarginDurationsProps } from './value-objects/margin-durations.value-object';
export class AdEntity extends AggregateRoot<AdProps> {
protected readonly _id: AggregateID;
static create = (
create: CreateAdProps,
defaultAdProps: DefaultAdProps,
): AdEntity => {
const id = v4();
const props: AdProps = { ...create };
const ad = new AdEntity({ id, props })
.setMissingMarginDurations(defaultAdProps.marginDurations)
.setMissingStrict(defaultAdProps.strict)
.setDefaultDriverAndPassengerParameters({
driver: defaultAdProps.driver,
passenger: defaultAdProps.passenger,
seatsProposed: defaultAdProps.seatsProposed,
seatsRequested: defaultAdProps.seatsRequested,
})
.setMissingWaypointsPosition();
ad.addEvent(
new AdCreatedDomainEvent({
aggregateId: id,
userId: props.userId,
driver: props.driver,
passenger: props.passenger,
frequency: props.frequency,
fromDate: props.fromDate,
toDate: props.toDate,
monTime: props.schedule.mon,
tueTime: props.schedule.tue,
wedTime: props.schedule.wed,
thuTime: props.schedule.thu,
friTime: props.schedule.fri,
satTime: props.schedule.sat,
sunTime: props.schedule.sun,
monMarginDuration: props.marginDurations.mon,
tueMarginDuration: props.marginDurations.tue,
wedMarginDuration: props.marginDurations.wed,
thuMarginDuration: props.marginDurations.thu,
friMarginDuration: props.marginDurations.fri,
satMarginDuration: props.marginDurations.sat,
sunMarginDuration: props.marginDurations.sun,
seatsProposed: props.seatsProposed,
seatsRequested: props.seatsRequested,
strict: props.strict,
waypoints: props.waypoints.map((waypoint: Waypoint) => ({
position: waypoint.position,
name: waypoint.address.name,
houseNumber: waypoint.address.houseNumber,
street: waypoint.address.street,
postalCode: waypoint.address.postalCode,
locality: waypoint.address.locality,
country: waypoint.address.postalCode,
lon: waypoint.address.coordinates.lon,
lat: waypoint.address.coordinates.lat,
})),
}),
);
return ad;
};
private setMissingMarginDurations = (
defaultMarginDurations: MarginDurationsProps,
): AdEntity => {
if (!this.props.marginDurations) this.props.marginDurations = {};
if (!this.props.marginDurations.mon)
this.props.marginDurations.mon = defaultMarginDurations.mon;
if (!this.props.marginDurations.tue)
this.props.marginDurations.tue = defaultMarginDurations.tue;
if (!this.props.marginDurations.wed)
this.props.marginDurations.wed = defaultMarginDurations.wed;
if (!this.props.marginDurations.thu)
this.props.marginDurations.thu = defaultMarginDurations.thu;
if (!this.props.marginDurations.fri)
this.props.marginDurations.fri = defaultMarginDurations.fri;
if (!this.props.marginDurations.sat)
this.props.marginDurations.sat = defaultMarginDurations.sat;
if (!this.props.marginDurations.sun)
this.props.marginDurations.sun = defaultMarginDurations.sun;
return this;
};
private setMissingStrict = (strict: boolean): AdEntity => {
if (this.props.strict === undefined) this.props.strict = strict;
return this;
};
private setDefaultDriverAndPassengerParameters = (
defaultDriverAndPassengerParameters: DefaultDriverAndPassengerParameters,
): AdEntity => {
this.props.driver = !!this.props.driver;
this.props.passenger = !!this.props.passenger;
if (!this.props.driver && !this.props.passenger) {
this.props.driver = defaultDriverAndPassengerParameters.driver;
this.props.seatsProposed =
defaultDriverAndPassengerParameters.seatsProposed;
this.props.passenger = defaultDriverAndPassengerParameters.passenger;
this.props.seatsRequested =
defaultDriverAndPassengerParameters.seatsRequested;
return this;
}
if (!this.props.seatsProposed || this.props.seatsProposed <= 0)
this.props.seatsProposed =
defaultDriverAndPassengerParameters.seatsProposed;
if (!this.props.seatsRequested || this.props.seatsRequested <= 0)
this.props.seatsRequested =
defaultDriverAndPassengerParameters.seatsRequested;
return this;
};
private setMissingWaypointsPosition = (): AdEntity => {
if (this.props.waypoints[0].position === undefined) {
for (let i = 0; i < this.props.waypoints.length; i++) {
this.props.waypoints[i].position = i;
}
}
return this;
};
validate(): void {
// entity business rules validation to protect it's invariant before saving entity to a database
}
}
interface DefaultDriverAndPassengerParameters {
driver: boolean;
passenger: boolean;
seatsProposed: number;
seatsRequested: number;
}

View File

@@ -0,0 +1,11 @@
import { ExceptionBase } from '@libs/exceptions';
export class AdAlreadyExistsError extends ExceptionBase {
static readonly message = 'Ad already exists';
public readonly code = 'AD.ALREADY_EXISTS';
constructor(cause?: Error, metadata?: unknown) {
super(AdAlreadyExistsError.message, cause, metadata);
}
}

View File

@@ -0,0 +1,49 @@
import { MarginDurationsProps } from './value-objects/margin-durations.value-object';
import { ScheduleProps } from './value-objects/schedule.value-object';
import { WaypointProps } from './value-objects/waypoint.value-object';
// All properties that an Ad has
export interface AdProps {
userId: string;
driver: boolean;
passenger: boolean;
frequency: Frequency;
fromDate: string;
toDate: string;
schedule: ScheduleProps;
marginDurations: MarginDurationsProps;
seatsProposed: number;
seatsRequested: number;
strict: boolean;
waypoints: WaypointProps[];
}
// Properties that are needed for an Ad creation
export interface CreateAdProps {
userId: string;
driver: boolean;
passenger: boolean;
frequency: Frequency;
fromDate: string;
toDate: string;
schedule: ScheduleProps;
marginDurations: MarginDurationsProps;
seatsProposed: number;
seatsRequested: number;
strict: boolean;
waypoints: WaypointProps[];
}
export interface DefaultAdProps {
driver: boolean;
passenger: boolean;
marginDurations: MarginDurationsProps;
strict: boolean;
seatsProposed: number;
seatsRequested: number;
}
export enum Frequency {
PUNCTUAL = 'PUNCTUAL',
RECURRENT = 'RECURRENT',
}

View File

@@ -0,0 +1,36 @@
import { Command, CommandProps } from '@libs/ddd';
import { Frequency } from '@modules/ad/core/ad.types';
import { Schedule } from '../../types/schedule';
import { MarginDurations } from '../../types/margin-durations';
import { Waypoint } from '../../types/waypoint';
export class CreateAdCommand extends Command {
readonly userId: string;
readonly driver?: boolean;
readonly passenger?: boolean;
readonly frequency?: Frequency;
readonly fromDate: string;
readonly toDate: string;
readonly schedule: Schedule;
readonly marginDurations?: MarginDurations;
readonly seatsProposed?: number;
readonly seatsRequested?: number;
readonly strict?: boolean;
readonly waypoints: Waypoint[];
constructor(props: CommandProps<CreateAdCommand>) {
super(props);
this.userId = props.userId;
this.driver = props.driver;
this.passenger = props.passenger;
this.frequency = props.frequency;
this.fromDate = props.fromDate;
this.toDate = props.toDate;
this.schedule = props.schedule;
this.marginDurations = props.marginDurations;
this.seatsProposed = props.seatsProposed;
this.seatsRequested = props.seatsRequested;
this.strict = props.strict;
this.waypoints = props.waypoints;
}
}

View File

@@ -0,0 +1,88 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { CreateAdCommand } from './create-ad.command';
import { DefaultParams } from '@modules/ad/core/ports/default-params.type';
import { Inject } from '@nestjs/common';
import { AD_REPOSITORY, PARAMS_PROVIDER } from '@modules/ad/ad.di-tokens';
import { AdRepositoryPort } from '@modules/ad/core/ports/ad.repository.port';
import { DefaultParamsProviderPort } from '@modules/ad/core/ports/default-params-provider.port';
import { Err, Ok, Result } from 'oxide.ts';
import { AggregateID } from '@libs/ddd';
import { AdAlreadyExistsError } from '@modules/ad/core/ad.errors';
import { AdEntity } from '@modules/ad/core/ad.entity';
import { ConflictException } from '@libs/exceptions';
import { Waypoint } from '../../types/waypoint';
@CommandHandler(CreateAdCommand)
export class CreateAdService implements ICommandHandler {
private readonly defaultParams: DefaultParams;
constructor(
@Inject(AD_REPOSITORY)
private readonly repository: AdRepositoryPort,
@Inject(PARAMS_PROVIDER)
private readonly defaultParamsProvider: DefaultParamsProviderPort,
) {
this.defaultParams = defaultParamsProvider.getParams();
}
async execute(
command: CreateAdCommand,
): Promise<Result<AggregateID, AdAlreadyExistsError>> {
const ad = AdEntity.create(
{
userId: command.userId,
driver: command.driver,
passenger: command.passenger,
frequency: command.frequency,
fromDate: command.fromDate,
toDate: command.toDate,
schedule: command.schedule,
marginDurations: command.marginDurations,
seatsProposed: command.seatsProposed,
seatsRequested: command.seatsRequested,
strict: command.strict,
waypoints: command.waypoints.map((waypoint: Waypoint) => ({
position: waypoint.position,
address: {
name: waypoint.name,
houseNumber: waypoint.houseNumber,
street: waypoint.street,
postalCode: waypoint.postalCode,
locality: waypoint.locality,
country: waypoint.country,
coordinates: {
lon: waypoint.lon,
lat: waypoint.lat,
},
},
})),
},
{
driver: this.defaultParams.DRIVER,
passenger: this.defaultParams.PASSENGER,
marginDurations: {
mon: this.defaultParams.MON_MARGIN,
tue: this.defaultParams.TUE_MARGIN,
wed: this.defaultParams.WED_MARGIN,
thu: this.defaultParams.THU_MARGIN,
fri: this.defaultParams.FRI_MARGIN,
sat: this.defaultParams.SAT_MARGIN,
sun: this.defaultParams.SUN_MARGIN,
},
strict: this.defaultParams.STRICT,
seatsProposed: this.defaultParams.SEATS_PROPOSED,
seatsRequested: this.defaultParams.SEATS_REQUESTED,
},
);
try {
await this.repository.insert(ad);
return Ok(ad.id);
} catch (error: any) {
if (error instanceof ConflictException) {
return Err(new AdAlreadyExistsError(error));
}
throw error;
}
}
}

View File

@@ -0,0 +1,68 @@
import { DomainEvent, DomainEventProps } from '@libs/ddd';
export class AdCreatedDomainEvent extends DomainEvent {
readonly userId: string;
readonly driver: boolean;
readonly passenger: boolean;
readonly frequency: string;
readonly fromDate: string;
readonly toDate: string;
readonly monTime: string;
readonly tueTime: string;
readonly wedTime: string;
readonly thuTime: string;
readonly friTime: string;
readonly satTime: string;
readonly sunTime: string;
readonly monMarginDuration: number;
readonly tueMarginDuration: number;
readonly wedMarginDuration: number;
readonly thuMarginDuration: number;
readonly friMarginDuration: number;
readonly satMarginDuration: number;
readonly sunMarginDuration: number;
readonly seatsProposed: number;
readonly seatsRequested: number;
readonly strict: boolean;
readonly waypoints: Waypoint[];
constructor(props: DomainEventProps<AdCreatedDomainEvent>) {
super(props);
this.userId = props.userId;
this.driver = props.driver;
this.passenger = props.passenger;
this.frequency = props.frequency;
this.fromDate = props.fromDate;
this.toDate = props.toDate;
this.monTime = props.monTime;
this.tueTime = props.tueTime;
this.wedTime = props.wedTime;
this.thuTime = props.thuTime;
this.friTime = props.friTime;
this.satTime = props.satTime;
this.sunTime = props.sunTime;
this.monMarginDuration = props.monMarginDuration;
this.tueMarginDuration = props.tueMarginDuration;
this.wedMarginDuration = props.wedMarginDuration;
this.thuMarginDuration = props.thuMarginDuration;
this.friMarginDuration = props.friMarginDuration;
this.satMarginDuration = props.satMarginDuration;
this.sunMarginDuration = props.sunMarginDuration;
this.seatsProposed = props.seatsProposed;
this.seatsRequested = props.seatsRequested;
this.strict = props.strict;
this.waypoints = props.waypoints;
}
}
export class Waypoint {
position: number;
name?: string;
houseNumber?: string;
street?: string;
locality?: string;
postalCode?: string;
country: string;
lon: number;
lat: number;
}

View File

@@ -0,0 +1,4 @@
import { RepositoryPort } from '@libs/ddd';
import { AdEntity } from '../ad.entity';
export type AdRepositoryPort = RepositoryPort<AdEntity>;

View File

@@ -0,0 +1,5 @@
import { DefaultParams } from './default-params.type';
export interface DefaultParamsProviderPort {
getParams(): DefaultParams;
}

View File

@@ -7,8 +7,9 @@ export type DefaultParams = {
SAT_MARGIN: number;
SUN_MARGIN: number;
DRIVER: boolean;
SEATS_PROVIDED: number;
SEATS_PROPOSED: number;
PASSENGER: boolean;
SEATS_REQUESTED: number;
STRICT: boolean;
DEFAULT_TIMEZONE: string;
};

View File

@@ -0,0 +1,3 @@
export interface TimezoneFinderPort {
timezones(lon: number, lat: number): string[];
}

View File

@@ -0,0 +1,9 @@
import { FindAdByIdRequestDTO } from '../../../interface/queries/find-ad-by-id/dtos/find-ad-by-id.request.dto';
export class FindAdByIdQuery {
readonly id: string;
constructor(findAdByIdRequestDTO: FindAdByIdRequestDTO) {
this.id = findAdByIdRequestDTO.id;
}
}

View File

@@ -0,0 +1,10 @@
import { Coordinates } from './coordinates';
export type Address = {
name?: string;
houseNumber?: string;
street?: string;
locality?: string;
postalCode?: string;
country: string;
} & Coordinates;

View File

@@ -0,0 +1,4 @@
export type Coordinates = {
lon: number;
lat: number;
};

View File

@@ -0,0 +1,9 @@
export type MarginDurations = {
mon?: number;
tue?: number;
wed?: number;
thu?: number;
fri?: number;
sat?: number;
sun?: number;
};

View File

@@ -0,0 +1,9 @@
export type Schedule = {
mon?: string;
tue?: string;
wed?: string;
thu?: string;
fri?: string;
sat?: string;
sun?: string;
};

View File

@@ -0,0 +1,5 @@
import { Address } from './address';
export type Waypoint = {
position?: number;
} & Address;

View File

@@ -0,0 +1,52 @@
import { ValueObject } from '@libs/ddd';
import { CoordinatesProps } from './coordinates.value-object';
/** Note:
* Value Objects with multiple properties can contain
* other Value Objects inside if needed.
* */
export interface AddressProps {
name?: string;
houseNumber?: string;
street?: string;
locality?: string;
postalCode?: string;
country: string;
coordinates: CoordinatesProps;
}
export class Address extends ValueObject<AddressProps> {
get name(): string {
return this.props.name;
}
get houseNumber(): string {
return this.props.houseNumber;
}
get street(): string {
return this.props.street;
}
get locality(): string {
return this.props.locality;
}
get postalCode(): string {
return this.props.postalCode;
}
get country(): string {
return this.props.country;
}
get coordinates(): CoordinatesProps {
return this.props.coordinates;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected validate(props: AddressProps): void {
return;
}
}

View File

@@ -0,0 +1,26 @@
import { ValueObject } from '@libs/ddd';
/** Note:
* Value Objects with multiple properties can contain
* other Value Objects inside if needed.
* */
export interface CoordinatesProps {
lon: number;
lat: number;
}
export class Coordinates extends ValueObject<CoordinatesProps> {
get lon(): number {
return this.props.lon;
}
get lat(): number {
return this.props.lat;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected validate(props: CoordinatesProps): void {
return;
}
}

View File

@@ -0,0 +1,79 @@
import { ValueObject } from '@libs/ddd';
/** Note:
* Value Objects with multiple properties can contain
* other Value Objects inside if needed.
* */
export interface MarginDurationsProps {
mon?: number;
tue?: number;
wed?: number;
thu?: number;
fri?: number;
sat?: number;
sun?: number;
}
export class MarginDurations extends ValueObject<MarginDurationsProps> {
get mon(): number {
return this.props.mon;
}
set mon(margin: number) {
this.props.mon = margin;
}
get tue(): number {
return this.props.tue;
}
set tue(margin: number) {
this.props.tue = margin;
}
get wed(): number {
return this.props.wed;
}
set wed(margin: number) {
this.props.wed = margin;
}
get thu(): number {
return this.props.thu;
}
set thu(margin: number) {
this.props.thu = margin;
}
get fri(): number {
return this.props.fri;
}
set fri(margin: number) {
this.props.fri = margin;
}
get sat(): number {
return this.props.sat;
}
set sat(margin: number) {
this.props.sat = margin;
}
get sun(): number {
return this.props.sun;
}
set sun(margin: number) {
this.props.sun = margin;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected validate(props: MarginDurationsProps): void {
return;
}
}

View File

@@ -0,0 +1,51 @@
import { ValueObject } from '@libs/ddd';
/** Note:
* Value Objects with multiple properties can contain
* other Value Objects inside if needed.
* */
export interface ScheduleProps {
mon?: string;
tue?: string;
wed?: string;
thu?: string;
fri?: string;
sat?: string;
sun?: string;
}
export class Schedule extends ValueObject<ScheduleProps> {
get mon(): string | undefined {
return this.props.mon;
}
get tue(): string | undefined {
return this.props.tue;
}
get wed(): string | undefined {
return this.props.wed;
}
get thu(): string | undefined {
return this.props.thu;
}
get fri(): string | undefined {
return this.props.fri;
}
get sat(): string | undefined {
return this.props.sat;
}
get sun(): string | undefined {
return this.props.sun;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected validate(props: ScheduleProps): void {
return;
}
}

View File

@@ -0,0 +1,27 @@
import { ValueObject } from '@libs/ddd';
import { AddressProps } from './address.value-object';
/** Note:
* Value Objects with multiple properties can contain
* other Value Objects inside if needed.
* */
export interface WaypointProps {
position: number;
address: AddressProps;
}
export class Waypoint extends ValueObject<WaypointProps> {
get position(): number {
return this.props.position;
}
get address(): AddressProps {
return this.props.address;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected validate(props: WaypointProps): void {
return;
}
}

View File

@@ -1,86 +0,0 @@
import { AutoMap } from '@automapper/classes';
import { Frequency } from '../types/frequency.enum';
import { Address } from '../entities/address';
export class AdCreation {
@AutoMap()
uuid: string;
@AutoMap()
userUuid: string;
@AutoMap()
driver: boolean;
@AutoMap()
passenger: boolean;
@AutoMap()
frequency: Frequency;
@AutoMap()
fromDate: Date;
@AutoMap()
toDate: Date;
@AutoMap()
monTime?: string;
@AutoMap()
tueTime?: string;
@AutoMap()
wedTime?: string;
@AutoMap()
thuTime?: string;
@AutoMap()
friTime?: string;
@AutoMap()
satTime?: string;
@AutoMap()
sunTime?: string;
@AutoMap()
monMargin: number;
@AutoMap()
tueMargin: number;
@AutoMap()
wedMargin: number;
@AutoMap()
thuMargin: number;
@AutoMap()
friMargin: number;
@AutoMap()
satMargin: number;
@AutoMap()
sunMargin: number;
@AutoMap()
seatsDriver: number;
@AutoMap()
seatsPassenger: number;
@AutoMap()
strict: boolean;
@AutoMap()
createdAt: Date;
@AutoMap()
updatedAt?: Date;
@AutoMap()
addresses: { create: Address[] };
}

View File

@@ -1,62 +0,0 @@
import { AutoMap } from '@automapper/classes';
import {
IsInt,
IsLatitude,
IsLongitude,
IsOptional,
IsString,
IsUUID,
} from 'class-validator';
export class AddressDTO {
@IsOptional()
@IsUUID(4)
@AutoMap()
uuid?: string;
@IsOptional()
@IsUUID(4)
@AutoMap()
adUuid?: string;
@IsOptional()
@IsInt()
@AutoMap()
position?: number;
@IsLongitude()
@AutoMap()
lon: number;
@IsLatitude()
@AutoMap()
lat: number;
@IsOptional()
@AutoMap()
name?: string;
@IsOptional()
@IsString()
@AutoMap()
houseNumber?: string;
@IsOptional()
@IsString()
@AutoMap()
street?: string;
@IsOptional()
@IsString()
@AutoMap()
locality?: string;
@IsOptional()
@IsString()
@AutoMap()
postalCode?: string;
@IsString()
@AutoMap()
country: string;
}

View File

@@ -1,107 +0,0 @@
import { AutoMap } from '@automapper/classes';
import {
IsOptional,
IsBoolean,
IsDate,
IsInt,
IsEnum,
ValidateNested,
ArrayMinSize,
IsUUID,
} from 'class-validator';
import { Frequency } from '../types/frequency.enum';
import { Transform, Type } from 'class-transformer';
import { intToFrequency } from './validators/frequency.mapping';
import { MarginDTO } from './margin.dto';
import { ScheduleDTO } from './schedule.dto';
import { AddressDTO } from './address.dto';
import { IsPunctualOrRecurrent } from './validators/decorators/is-punctual-or-recurrent.validator';
import { HasProperDriverSeats } from './validators/decorators/has-driver-seats.validator';
import { HasProperPassengerSeats } from './validators/decorators/has-passenger-seats.validator';
import { HasProperPositionIndexes } from './validators/decorators/address-position.validator';
export class CreateAdRequest {
@IsOptional()
@IsUUID(4)
@AutoMap()
uuid?: string;
@IsUUID(4)
@AutoMap()
userUuid: string;
@IsOptional()
@IsBoolean()
@AutoMap()
driver?: boolean;
@IsOptional()
@IsBoolean()
@AutoMap()
passenger?: boolean;
@Transform(({ value }) => intToFrequency(value), {
toClassOnly: true,
})
@IsEnum(Frequency)
@AutoMap()
frequency: Frequency;
@IsOptional()
@IsPunctualOrRecurrent()
@Type(() => Date)
@IsDate()
@AutoMap()
departure?: Date;
@IsOptional()
@IsPunctualOrRecurrent()
@Type(() => Date)
@IsDate()
@AutoMap()
fromDate?: Date;
@IsOptional()
@IsPunctualOrRecurrent()
@Type(() => Date)
@IsDate()
@AutoMap()
toDate?: Date;
@IsOptional()
@Type(() => ScheduleDTO)
@IsPunctualOrRecurrent()
@ValidateNested({ each: true })
@AutoMap()
schedule: ScheduleDTO = {};
@IsOptional()
@Type(() => MarginDTO)
@ValidateNested({ each: true })
@AutoMap()
marginDurations?: MarginDTO;
@IsOptional()
@HasProperDriverSeats()
@IsInt()
@AutoMap()
seatsDriver?: number;
@IsOptional()
@HasProperPassengerSeats()
@IsInt()
@AutoMap()
seatsPassenger?: number;
@IsOptional()
@IsBoolean()
@AutoMap()
strict?: boolean;
@ArrayMinSize(2)
@Type(() => AddressDTO)
@HasProperPositionIndexes()
@ValidateNested({ each: true })
@AutoMap()
addresses: AddressDTO[];
}

View File

@@ -1,13 +0,0 @@
import { AddressDTO } from '../address.dto';
export const hasProperPositionIndexes = (value: AddressDTO[]): boolean => {
if (value.every((address) => address.position === undefined)) return true;
if (value.every((address) => typeof address.position === 'number')) {
value.sort((a, b) => a.position - b.position);
for (let i = 1; i < value.length; i++) {
if (value[i - 1].position >= value[i].position) return false;
}
return true;
}
return false;
};

View File

@@ -1,24 +0,0 @@
import { ValidateBy, ValidationOptions, buildMessage } from 'class-validator';
import { AddressDTO } from '../../address.dto';
import { hasProperPositionIndexes } from '../address-position';
export const HasProperPositionIndexes = (
validationOptions?: ValidationOptions,
): PropertyDecorator =>
ValidateBy(
{
name: '',
constraints: [],
validator: {
validate: (value: AddressDTO[]): boolean =>
hasProperPositionIndexes(value),
defaultMessage: buildMessage(
() =>
`indexes position incorrect, please provide a complete list of indexes or ordened list of adresses from start to end of journey`,
validationOptions,
),
},
},
validationOptions,
);

View File

@@ -1,26 +0,0 @@
import {
ValidateBy,
ValidationArguments,
ValidationOptions,
buildMessage,
} from 'class-validator';
import { hasProperDriverSeats } from '../has-driver-seats';
export const HasProperDriverSeats = (
validationOptions?: ValidationOptions,
): PropertyDecorator =>
ValidateBy(
{
name: '',
constraints: [],
validator: {
validate: (value: any, args: ValidationArguments): boolean =>
hasProperDriverSeats(args),
defaultMessage: buildMessage(
() => `driver and driver seats are not correct`,
validationOptions,
),
},
},
validationOptions,
);

View File

@@ -1,27 +0,0 @@
import {
ValidateBy,
ValidationArguments,
ValidationOptions,
buildMessage,
} from 'class-validator';
import { isPunctualOrRecurrent } from '../is-punctual-or-recurrent';
export const IsPunctualOrRecurrent = (
validationOptions?: ValidationOptions,
): PropertyDecorator =>
ValidateBy(
{
name: '',
constraints: [],
validator: {
validate: (value, args: ValidationArguments): boolean =>
isPunctualOrRecurrent(args),
defaultMessage: buildMessage(
() =>
`the departure, from date, to date and schedule must be properly set on recurrent or punctual ad`,
validationOptions,
),
},
},
validationOptions,
);

View File

@@ -1,7 +0,0 @@
import { Frequency } from '../../types/frequency.enum';
export const intToFrequency = (index: number): Frequency => {
if (index == 1) return Frequency.PUNCTUAL;
if (index == 2) return Frequency.RECURRENT;
return undefined;
};

View File

@@ -1,19 +0,0 @@
import { ValidationArguments } from 'class-validator';
export const hasProperDriverSeats = (args: ValidationArguments): boolean => {
if (
args.object['driver'] === true &&
typeof args.object['seatsDriver'] === 'number'
)
return args.object['seatsDriver'] > 0;
if (
(args.object['driver'] === false ||
args.object['driver'] === null ||
args.object['driver'] === undefined) &&
(args.object['seatsDriver'] === 0 ||
args.object['seatsDriver'] === null ||
args.object['seatsDriver'] === undefined)
)
return true;
return false;
};

View File

@@ -1,19 +0,0 @@
import { ValidationArguments } from 'class-validator';
export const hasProperPassengerSeats = (args: ValidationArguments): boolean => {
if (
args.object['passenger'] === true &&
typeof args.object['seatsPassenger'] === 'number'
)
return args.object['seatsPassenger'] > 0;
if (
(args.object['passenger'] === false ||
args.object['passenger'] === null ||
args.object['passenger'] === undefined) &&
(args.object['seatsPassenger'] === 0 ||
args.object['seatsPassenger'] === null ||
args.object['seatsPassenger'] === undefined)
)
return true;
return false;
};

View File

@@ -1,16 +0,0 @@
import { ValidationArguments } from 'class-validator';
import { Frequency } from '../../types/frequency.enum';
const isPunctual = (args: ValidationArguments): boolean =>
args.object['frequency'] === Frequency.PUNCTUAL &&
args.object['departure'] instanceof Date &&
!Object.keys(args.object['schedule']).length;
const isRecurrent = (args: ValidationArguments): boolean =>
args.object['frequency'] === Frequency.RECURRENT &&
args.object['fromDate'] instanceof Date &&
args.object['toDate'] instanceof Date &&
Object.keys(args.object['schedule']).length > 0;
export const isPunctualOrRecurrent = (args: ValidationArguments): boolean =>
isPunctual(args) || isRecurrent(args);

View File

@@ -1,132 +0,0 @@
import { AutoMap } from '@automapper/classes';
import {
IsOptional,
IsString,
IsBoolean,
IsDate,
IsInt,
IsEnum,
ValidateNested,
ArrayMinSize,
IsUUID,
} from 'class-validator';
import { Address } from '../entities/address';
import { Frequency } from '../types/frequency.enum';
export class Ad {
@IsUUID(4)
@AutoMap()
uuid: string;
@IsUUID(4)
@AutoMap()
userUuid: string;
@IsBoolean()
@AutoMap()
driver: boolean;
@IsBoolean()
@AutoMap()
passenger: boolean;
@IsEnum(Frequency)
@AutoMap()
frequency: Frequency;
@IsDate()
@AutoMap()
fromDate: Date;
@IsDate()
@AutoMap()
toDate: Date;
@IsOptional()
@IsDate()
@AutoMap()
monTime?: string;
@IsOptional()
@IsString()
@AutoMap()
tueTime?: string;
@IsOptional()
@IsString()
@AutoMap()
wedTime?: string;
@IsOptional()
@IsString()
@AutoMap()
thuTime?: string;
@IsOptional()
@IsString()
@AutoMap()
friTime?: string;
@IsOptional()
@IsString()
@AutoMap()
satTime?: string;
@IsOptional()
@IsString()
@AutoMap()
sunTime?: string;
@IsInt()
@AutoMap()
monMargin: number;
@IsInt()
@AutoMap()
tueMargin: number;
@IsInt()
@AutoMap()
wedMargin: number;
@IsInt()
@AutoMap()
thuMargin: number;
@IsInt()
@AutoMap()
friMargin: number;
@IsInt()
@AutoMap()
satMargin: number;
@IsInt()
@AutoMap()
sunMargin: number;
@IsInt()
@AutoMap()
seatsDriver: number;
@IsInt()
@AutoMap()
seatsPassenger: number;
@IsBoolean()
@AutoMap()
strict: boolean;
@IsDate()
@AutoMap()
createdAt: Date;
@IsDate()
@AutoMap()
updatedAt?: Date;
@ArrayMinSize(2)
@ValidateNested({ each: true })
@AutoMap(() => [Address])
addresses: Address[];
}

View File

@@ -1,40 +0,0 @@
import { AutoMap } from '@automapper/classes';
import { IsInt, IsUUID } from 'class-validator';
export class Address {
@IsUUID(4)
@AutoMap()
uuid: string;
@IsUUID(4)
@AutoMap()
adUuid: string;
@IsInt()
@AutoMap()
position: number;
@AutoMap()
lon: number;
@AutoMap()
lat: number;
@AutoMap()
name?: string;
@AutoMap()
houseNumber?: string;
@AutoMap()
street?: string;
@AutoMap()
locality: string;
@AutoMap()
postalCode: string;
@AutoMap()
country: string;
}

View File

@@ -1,35 +0,0 @@
import { CreateAdRequest } from '../dtos/create-ad.request';
import { Day } from '../types/day.enum';
import { Frequency } from '../types/frequency.enum';
export class FrequencyNormaliser {
fromDateResolver(createAdRequest: CreateAdRequest): Date {
if (createAdRequest.frequency === Frequency.PUNCTUAL)
return createAdRequest.departure;
return createAdRequest.fromDate;
}
toDateResolver(createAdRequest: CreateAdRequest): Date {
if (createAdRequest.frequency === Frequency.PUNCTUAL)
return createAdRequest.departure;
return createAdRequest.toDate;
}
scheduleResolver = (
createAdRequest: CreateAdRequest,
day: number,
): string => {
if (
Object.keys(createAdRequest.schedule).length === 0 &&
createAdRequest.frequency == Frequency.PUNCTUAL &&
createAdRequest.departure.getDay() === day
)
return `${createAdRequest.departure
.getHours()
.toString()
.padStart(2, '0')}:${createAdRequest.departure
.getMinutes()
.toString()
.padStart(2, '0')}`;
return createAdRequest.schedule[Day[day]];
};
}

View File

@@ -1,4 +0,0 @@
import { DefaultParams } from '../types/default-params.type';
export interface IProvideParams {
getParams(): DefaultParams;
}

View File

@@ -1,9 +0,0 @@
export enum Day {
sun = 0,
mon,
tue,
wed,
thu,
fri,
sat,
}

View File

@@ -1,4 +0,0 @@
export enum Frequency {
PUNCTUAL = 'PUNCTUAL',
RECURRENT = 'RECURRENT',
}

View File

@@ -1,111 +0,0 @@
import { Mapper } from '@automapper/core';
import { InjectMapper } from '@automapper/nestjs';
import { Inject } from '@nestjs/common';
import { CommandHandler } from '@nestjs/cqrs';
import { AdsRepository } from '../../adapters/secondaries/ads.repository';
import { CreateAdCommand } from '../../commands/create-ad.command';
import { CreateAdRequest } from '../dtos/create-ad.request';
import { IProvideParams } from '../interfaces/param-provider.interface';
import { DefaultParams } from '../types/default-params.type';
import { AdCreation } from '../dtos/ad.creation';
import { Ad } from '../entities/ad';
import { PARAMS_PROVIDER } from '../../ad.constants';
import { IPublishMessage } from '../../../../interfaces/message-publisher';
import { MESSAGE_PUBLISHER } from '../../../../app.constants';
@CommandHandler(CreateAdCommand)
export class CreateAdUseCase {
private readonly defaultParams: DefaultParams;
private ad: AdCreation;
constructor(
private readonly repository: AdsRepository,
@Inject(MESSAGE_PUBLISHER)
private readonly messagePublisher: IPublishMessage,
@InjectMapper() private readonly mapper: Mapper,
@Inject(PARAMS_PROVIDER)
private readonly defaultParamsProvider: IProvideParams,
) {
this.defaultParams = defaultParamsProvider.getParams();
}
async execute(command: CreateAdCommand): Promise<Ad> {
this.ad = this.mapper.map(
command.createAdRequest,
CreateAdRequest,
AdCreation,
);
this.setDefaultMarginDurations();
this.setDefaultAddressesPosition();
this.setDefaultDriverAndPassengerParameters();
this.setDefaultStrict();
try {
const adCreated: Ad = await this.repository.create(this.ad);
this.messagePublisher.publish('ad.create', JSON.stringify(adCreated));
this.messagePublisher.publish(
'logging.ad.create.info',
JSON.stringify(adCreated),
);
return adCreated;
} catch (error) {
let key = 'logging.ad.create.crit';
if (error.message.includes('Already exists')) {
key = 'logging.ad.create.warning';
}
this.messagePublisher.publish(
key,
JSON.stringify({
command,
error,
}),
);
throw error;
}
}
private setDefaultMarginDurations = (): void => {
if (this.ad.monMargin === undefined)
this.ad.monMargin = this.defaultParams.MON_MARGIN;
if (this.ad.tueMargin === undefined)
this.ad.tueMargin = this.defaultParams.TUE_MARGIN;
if (this.ad.wedMargin === undefined)
this.ad.wedMargin = this.defaultParams.WED_MARGIN;
if (this.ad.thuMargin === undefined)
this.ad.thuMargin = this.defaultParams.THU_MARGIN;
if (this.ad.friMargin === undefined)
this.ad.friMargin = this.defaultParams.FRI_MARGIN;
if (this.ad.satMargin === undefined)
this.ad.satMargin = this.defaultParams.SAT_MARGIN;
if (this.ad.sunMargin === undefined)
this.ad.sunMargin = this.defaultParams.SUN_MARGIN;
};
private setDefaultStrict = (): void => {
if (this.ad.strict === undefined)
this.ad.strict = this.defaultParams.STRICT;
};
private setDefaultDriverAndPassengerParameters = (): void => {
this.ad.driver = !!this.ad.driver;
this.ad.passenger = !!this.ad.passenger;
if (!this.ad.driver && !this.ad.passenger) {
this.ad.driver = this.defaultParams.DRIVER;
this.ad.seatsDriver = this.defaultParams.SEATS_PROVIDED;
this.ad.passenger = this.defaultParams.PASSENGER;
this.ad.seatsPassenger = this.defaultParams.SEATS_REQUESTED;
return;
}
if (!this.ad.seatsDriver || this.ad.seatsDriver <= 0)
this.ad.seatsDriver = this.defaultParams.SEATS_PROVIDED;
if (!this.ad.seatsPassenger || this.ad.seatsPassenger <= 0)
this.ad.seatsPassenger = this.defaultParams.SEATS_REQUESTED;
};
private setDefaultAddressesPosition = (): void => {
if (this.ad.addresses.create[0].position === undefined) {
for (let i = 0; i < this.ad.addresses.create.length; i++) {
this.ad.addresses.create[i].position = i;
}
}
};
}

View File

@@ -1,33 +0,0 @@
import { Inject, NotFoundException } from '@nestjs/common';
import { QueryHandler } from '@nestjs/cqrs';
import { FindAdByUuidQuery } from '../../queries/find-ad-by-uuid.query';
import { AdsRepository } from '../../adapters/secondaries/ads.repository';
import { Ad } from '../entities/ad';
import { MESSAGE_PUBLISHER } from '../../../../app.constants';
import { IPublishMessage } from '../../../../interfaces/message-publisher';
@QueryHandler(FindAdByUuidQuery)
export class FindAdByUuidUseCase {
constructor(
private readonly repository: AdsRepository,
@Inject(MESSAGE_PUBLISHER)
private readonly messagePublisher: IPublishMessage,
) {}
async execute(findAdByUuid: FindAdByUuidQuery): Promise<Ad> {
try {
const ad = await this.repository.findOneByUuid(findAdByUuid.uuid);
if (!ad) throw new NotFoundException();
return ad;
} catch (error) {
this.messagePublisher.publish(
'logging.ad.read.warning',
JSON.stringify({
query: findAdByUuid,
error,
}),
);
throw error;
}
}
}

View File

@@ -0,0 +1,71 @@
import { Injectable, Logger } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { AdRepositoryPort } from '../core/ports/ad.repository.port';
import { AdEntity } from '../core/ad.entity';
import { PrismaRepositoryBase } from '@libs/db/prisma-repository.base';
import { AdMapper } from '../ad.mapper';
import { PrismaService } from './prisma-service';
export type AdModel = {
uuid: string;
userUuid: string;
driver: boolean;
passenger: boolean;
frequency: string;
fromDate: Date;
toDate: Date;
monTime: Date;
tueTime: Date;
wedTime: Date;
thuTime: Date;
friTime: Date;
satTime: Date;
sunTime: Date;
monMargin: number;
tueMargin: number;
wedMargin: number;
thuMargin: number;
friMargin: number;
satMargin: number;
sunMargin: number;
seatsProposed: number;
seatsRequested: number;
strict: boolean;
waypoints: {
create: WaypointModel[];
};
createdAt: Date;
updatedAt: Date;
};
export type WaypointModel = {
uuid: string;
position: number;
lon: number;
lat: number;
name?: string;
houseNumber?: string;
street?: string;
locality?: string;
postalCode?: string;
country: string;
createdAt: Date;
updatedAt: Date;
};
/**
* Repository is used for retrieving/saving domain entities
* */
@Injectable()
export class AdRepository
extends PrismaRepositoryBase<AdEntity, AdModel>
implements AdRepositoryPort
{
constructor(
prisma: PrismaService,
mapper: AdMapper,
eventEmitter: EventEmitter2,
) {
super(prisma.ad, mapper, eventEmitter, new Logger(AdRepository.name));
}
}

View File

@@ -1,10 +1,10 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { DefaultParams } from '../../domain/types/default-params.type';
import { IProvideParams } from '../../domain/interfaces/param-provider.interface';
import { DefaultParamsProviderPort } from '../core/ports/default-params-provider.port';
import { DefaultParams } from '../core/ports/default-params.type';
@Injectable()
export class DefaultParamsProvider implements IProvideParams {
export class DefaultParamsProvider implements DefaultParamsProviderPort {
constructor(private readonly configService: ConfigService) {}
getParams = (): DefaultParams => ({
MON_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')),
@@ -15,9 +15,10 @@ export class DefaultParamsProvider implements IProvideParams {
SAT_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')),
SUN_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')),
DRIVER: this.configService.get('ROLE') == 'driver',
SEATS_PROVIDED: parseInt(this.configService.get('SEATS_PROVIDED')),
SEATS_PROPOSED: parseInt(this.configService.get('SEATS_PROPOSED')),
PASSENGER: this.configService.get('ROLE') == 'passenger',
SEATS_REQUESTED: parseInt(this.configService.get('SEATS_REQUESTED')),
STRICT: this.configService.get('STRICT_FREQUENCY') == 'true',
DEFAULT_TIMEZONE: this.configService.get('DEFAULT_TIMEZONE'),
});
}

View File

@@ -1,10 +1,10 @@
import { Inject, Injectable } from '@nestjs/common';
import { MESSAGE_BROKER_PUBLISHER } from '../../../../app.constants';
import { MessageBrokerPublisher } from '@mobicoop/message-broker-module';
import { IPublishMessage } from 'src/interfaces/message-publisher';
import { MessagePublisherPort } from '@ports/message-publisher.port';
import { MESSAGE_BROKER_PUBLISHER } from '@src/app.constants';
@Injectable()
export class MessagePublisher implements IPublishMessage {
export class MessagePublisher implements MessagePublisherPort {
constructor(
@Inject(MESSAGE_BROKER_PUBLISHER)
private readonly messageBrokerPublisher: MessageBrokerPublisher,

View 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();
});
}
}

View File

@@ -0,0 +1,8 @@
import { Injectable } from '@nestjs/common';
import { TimezoneFinderPort } from '../core/ports/timezone-finder.port';
import { find } from 'geo-tz';
@Injectable()
export class TimezoneFinder implements TimezoneFinderPort {
timezones = (lon: number, lat: number): string[] => find(lat, lon);
}

View File

@@ -2,5 +2,5 @@ import { AutoMap } from '@automapper/classes';
export class AdPresenter {
@AutoMap()
uuid: string;
id: string;
}

View File

@@ -0,0 +1,80 @@
syntax = "proto3";
package ad;
service AdsService {
rpc FindOneById(AdById) returns (Ad);
rpc FindAll(AdFilter) returns (Ads);
rpc Create(Ad) returns (AdById);
rpc Update(Ad) returns (Ad);
rpc Delete(AdById) returns (Empty);
}
message AdById {
string id = 1;
}
message Ad {
string id = 1;
string userId = 2;
bool driver = 3;
bool passenger = 4;
Frequency frequency = 5;
string fromDate = 6;
string toDate = 7;
Schedule schedule = 8;
MarginDurations marginDurations = 9;
int32 seatsProposed = 10;
int32 seatsRequested = 11;
bool strict = 12;
repeated Waypoint waypoints = 13;
}
message Schedule {
string mon = 1;
string tue = 2;
string wed = 3;
string thu = 4;
string fri = 5;
string sat = 6;
string sun = 7;
}
message MarginDurations {
int32 mon = 1;
int32 tue = 2;
int32 wed = 3;
int32 thu = 4;
int32 fri = 5;
int32 sat = 6;
int32 sun = 7;
}
message Waypoint {
int32 position = 1;
float lon = 2;
float lat = 3;
string name = 4;
string houseNumber = 5;
string street = 6;
string locality = 7;
string postalCode = 8;
string country = 9;
}
enum Frequency {
PUNCTUAL = 1;
RECURRENT = 2;
}
message AdFilter {
int32 page = 1;
int32 perPage = 2;
}
message Ads {
repeated Ad data = 1;
int32 total = 2;
}
message Empty {}

View File

@@ -0,0 +1,6 @@
import { PaginatedResponseDto } from '@libs/api/paginated.response.base';
import { AdResponseDto } from './ad.response.dto';
export class AdPaginatedResponseDto extends PaginatedResponseDto<AdResponseDto> {
readonly data: readonly AdResponseDto[];
}

View File

@@ -0,0 +1,5 @@
import { ResponseBase } from '@libs/api/response.base';
export class AdResponseDto extends ResponseBase {
uuid: string;
}

View File

@@ -0,0 +1,43 @@
import { Controller, UsePipes } from '@nestjs/common';
import { CommandBus } from '@nestjs/cqrs';
import { GrpcMethod, RpcException } from '@nestjs/microservices';
import { RpcValidationPipe } from '../../../../utils/pipes/rpc.validation-pipe';
import { AdPresenter } from '../ad.presenter';
import { CreateAdRequestDTO } from './dtos/create-ad.request.dto';
import { CreateAdCommand } from '../../core/commands/create-ad/create-ad.command';
import { Result, match } from 'oxide.ts';
import { AggregateID } from '@libs/ddd';
import { AdAlreadyExistsError } from '../../core/ad.errors';
import { IdResponse } from '@libs/api/id.response.dto';
@UsePipes(
new RpcValidationPipe({
whitelist: false,
forbidUnknownValues: false,
}),
)
@Controller()
export class CreateAdGrpcController {
constructor(private readonly commandBus: CommandBus) {}
@GrpcMethod('AdsService', 'Create')
async create(data: CreateAdRequestDTO): Promise<AdPresenter> {
const result: Result<AggregateID, AdAlreadyExistsError> =
await this.commandBus.execute(new CreateAdCommand(data));
// Deciding what to do with a Result (similar to Rust matching)
// if Ok we return a response with an id
// if Error decide what to do with it depending on its type
return match(result, {
Ok: (id: string) => new IdResponse(id),
Err: (error: Error) => {
if (error instanceof AdAlreadyExistsError)
throw new RpcException({
code: 6,
message: 'Ad already exists',
});
throw new RpcException({});
},
});
}
}

View File

@@ -0,0 +1,33 @@
import { AutoMap } from '@automapper/classes';
import { IsOptional, IsString } from 'class-validator';
import { CoordinatesDTO } from './coordinates.dto';
export class AddressDTO extends CoordinatesDTO {
@IsOptional()
@AutoMap()
name?: string;
@IsOptional()
@IsString()
@AutoMap()
houseNumber?: string;
@IsOptional()
@IsString()
@AutoMap()
street?: string;
@IsOptional()
@IsString()
@AutoMap()
locality?: string;
@IsOptional()
@IsString()
@AutoMap()
postalCode?: string;
@IsString()
@AutoMap()
country: string;
}

View File

@@ -0,0 +1,12 @@
import { AutoMap } from '@automapper/classes';
import { IsLatitude, IsLongitude } from 'class-validator';
export class CoordinatesDTO {
@IsLongitude()
@AutoMap()
lon: number;
@IsLatitude()
@AutoMap()
lat: number;
}

View File

@@ -0,0 +1,91 @@
import { AutoMap } from '@automapper/classes';
import {
IsOptional,
IsBoolean,
IsInt,
IsEnum,
ValidateNested,
ArrayMinSize,
IsUUID,
IsArray,
IsISO8601,
} from 'class-validator';
import { Transform, Type } from 'class-transformer';
import { ScheduleDTO } from './schedule.dto';
import { MarginDurationsDTO } from './margin-durations.dto';
import { WaypointDTO } from './waypoint.dto';
import { intToFrequency } from './validators/frequency.mapping';
import { IsSchedule } from './validators/decorators/is-schedule.validator';
import { HasValidPositionIndexes } from './validators/decorators/valid-position-indexes.validator';
import { Frequency } from '@modules/ad/core/ad.types';
export class CreateAdRequestDTO {
@IsUUID(4)
@AutoMap()
userId: string;
@IsOptional()
@IsBoolean()
@AutoMap()
driver?: boolean;
@IsOptional()
@IsBoolean()
@AutoMap()
passenger?: boolean;
@Transform(({ value }) => intToFrequency(value), {
toClassOnly: true,
})
@IsEnum(Frequency)
@AutoMap()
frequency: Frequency;
@IsISO8601({
strict: true,
strictSeparator: true,
})
@AutoMap()
fromDate: string;
@IsISO8601({
strict: true,
strictSeparator: true,
})
@AutoMap()
toDate: string;
@Type(() => ScheduleDTO)
@IsSchedule()
@ValidateNested({ each: true })
@AutoMap()
schedule: ScheduleDTO;
@IsOptional()
@Type(() => MarginDurationsDTO)
@ValidateNested({ each: true })
@AutoMap()
marginDurations?: MarginDurationsDTO;
@IsOptional()
@IsInt()
@AutoMap()
seatsProposed?: number;
@IsOptional()
@IsInt()
@AutoMap()
seatsRequested?: number;
@IsOptional()
@IsBoolean()
@AutoMap()
strict?: boolean;
@IsArray()
@ArrayMinSize(2)
@HasValidPositionIndexes()
@ValidateNested({ each: true })
@AutoMap()
waypoints: WaypointDTO[];
}

View File

@@ -1,7 +1,7 @@
import { AutoMap } from '@automapper/classes';
import { IsInt, IsOptional } from 'class-validator';
export class MarginDTO {
export class MarginDurationsDTO {
@IsOptional()
@IsInt()
@AutoMap()

View File

@@ -4,9 +4,8 @@ import {
ValidationOptions,
buildMessage,
} from 'class-validator';
import { hasProperPassengerSeats } from '../has-passenger-seats';
export const HasProperPassengerSeats = (
export const IsSchedule = (
validationOptions?: ValidationOptions,
): PropertyDecorator =>
ValidateBy(
@@ -14,10 +13,11 @@ export const HasProperPassengerSeats = (
name: '',
constraints: [],
validator: {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
validate: (value, args: ValidationArguments): boolean =>
hasProperPassengerSeats(args),
Object.keys(value).length > 0,
defaultMessage: buildMessage(
() => `passenger and passenger seats are not correct`,
() => `schedule is invalid`,
validationOptions,
),
},

View File

@@ -0,0 +1,22 @@
import { ValidateBy, ValidationOptions, buildMessage } from 'class-validator';
import { hasValidPositionIndexes } from '../waypoint-position';
import { WaypointDTO } from '../../waypoint.dto';
export const HasValidPositionIndexes = (
validationOptions?: ValidationOptions,
): PropertyDecorator =>
ValidateBy(
{
name: '',
constraints: [],
validator: {
validate: (waypoints: WaypointDTO[]): boolean =>
hasValidPositionIndexes(waypoints),
defaultMessage: buildMessage(
() => `invalid waypoints positions`,
validationOptions,
),
},
},
validationOptions,
);

View File

@@ -0,0 +1,7 @@
import { Frequency } from '@modules/ad/core/ad.types';
export const intToFrequency = (frequencyAsInt: number): Frequency => {
if (frequencyAsInt == 1) return Frequency.PUNCTUAL;
if (frequencyAsInt == 2) return Frequency.RECURRENT;
throw new Error('Unknown frequency value');
};

View File

@@ -0,0 +1,15 @@
import { WaypointDTO } from '../waypoint.dto';
export const hasValidPositionIndexes = (waypoints: WaypointDTO[]): boolean => {
if (!waypoints) return;
if (waypoints.every((waypoint) => waypoint.position === undefined))
return true;
if (waypoints.every((waypoint) => typeof waypoint.position === 'number')) {
waypoints.sort((a, b) => a.position - b.position);
for (let i = 1; i < waypoints.length; i++) {
if (waypoints[i - 1].position >= waypoints[i].position) return false;
}
return true;
}
return false;
};

View File

@@ -0,0 +1,10 @@
import { AutoMap } from '@automapper/classes';
import { IsInt, IsOptional } from 'class-validator';
import { AddressDTO } from './address.dto';
export class WaypointDTO extends AddressDTO {
@IsOptional()
@IsInt()
@AutoMap()
position?: number;
}

View File

@@ -1,7 +1,7 @@
import { IsNotEmpty, IsString } from 'class-validator';
export class FindAdByUuidRequest {
export class FindAdByIdRequestDTO {
@IsString()
@IsNotEmpty()
uuid: string;
id: string;
}

View File

@@ -0,0 +1,37 @@
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 { FindAdByIdRequestDTO } from './dtos/find-ad-by-id.request.dto';
import { AdPresenter } from '../../ad.presenter';
import { FindAdByIdQuery } from '../../../core/queries/find-ad-by-id/find-ad-by-id.query';
import { AdEntity } from '../../../core/ad.entity';
@UsePipes(
new RpcValidationPipe({
whitelist: false,
forbidUnknownValues: false,
}),
)
@Controller()
export class FindAdByIdGrpcController {
constructor(
private readonly queryBus: QueryBus,
@InjectMapper() private readonly mapper: Mapper,
) {}
@GrpcMethod('AdsService', 'FindOneById')
async findOnebyId(data: FindAdByIdRequestDTO): Promise<AdPresenter> {
try {
const ad = await this.queryBus.execute(new FindAdByIdQuery(data));
return this.mapper.map(ad, AdEntity, AdPresenter);
} catch (e) {
throw new RpcException({
code: e.code,
message: e.message,
});
}
}
}

View File

@@ -1,139 +0,0 @@
import { createMap, forMember, mapFrom, Mapper } from '@automapper/core';
import { AutomapperProfile, InjectMapper } from '@automapper/nestjs';
import { Injectable } from '@nestjs/common';
import { Ad } from '../domain/entities/ad';
import { AdPresenter } from '../adapters/primaries/ad.presenter';
import { CreateAdRequest } from '../domain/dtos/create-ad.request';
import { AdCreation } from '../domain/dtos/ad.creation';
import { FrequencyNormaliser } from '../domain/entities/frequency.normaliser';
import { Day } from '../domain/types/day.enum';
@Injectable()
export class AdProfile extends AutomapperProfile {
frequencyNormaliser = new FrequencyNormaliser();
constructor(@InjectMapper() mapper: Mapper) {
super(mapper);
}
override get profile() {
return (mapper) => {
createMap(mapper, Ad, AdPresenter);
createMap(
mapper,
CreateAdRequest,
AdCreation,
forMember(
(destination) => destination.monMargin,
mapFrom((source) => source.marginDurations?.mon),
),
forMember(
(destination) => destination.tueMargin,
mapFrom((source) => source.marginDurations?.tue),
),
forMember(
(destination) => destination.wedMargin,
mapFrom((source) => source.marginDurations?.wed),
),
forMember(
(destination) => destination.thuMargin,
mapFrom((source) => source.marginDurations?.thu),
),
forMember(
(destination) => destination.friMargin,
mapFrom((source) => source.marginDurations?.fri),
),
forMember(
(destination) => destination.satMargin,
mapFrom((source) => source.marginDurations?.sat),
),
forMember(
(destination) => destination.sunMargin,
mapFrom((source) => source.marginDurations?.sun),
),
forMember(
(destination) => destination.monTime,
mapFrom((source) => source.schedule.mon),
),
forMember(
(destination) => destination.tueTime,
mapFrom((source) => source.schedule.tue),
),
forMember(
(destination) => destination.wedTime,
mapFrom((source) => source.schedule.wed),
),
forMember(
(destination) => destination.thuTime,
mapFrom((source) => source.schedule.thu),
),
forMember(
(destination) => destination.friTime,
mapFrom((source) => source.schedule.fri),
),
forMember(
(destination) => destination.satTime,
mapFrom((source) => source.schedule.sat),
),
forMember(
(destination) => destination.sunTime,
mapFrom((source) => source.schedule.sun),
),
forMember(
(destination) => destination.addresses.create,
mapFrom((source) => source.addresses),
),
forMember(
(destination) => destination.fromDate,
mapFrom((source) =>
this.frequencyNormaliser.fromDateResolver(source),
),
),
forMember(
(destination) => destination.toDate,
mapFrom((source) => this.frequencyNormaliser.toDateResolver(source)),
),
forMember(
(destination) => destination.monTime,
mapFrom((source) =>
this.frequencyNormaliser.scheduleResolver(source, Day.mon),
),
),
forMember(
(destination) => destination.tueTime,
mapFrom((source) =>
this.frequencyNormaliser.scheduleResolver(source, Day.tue),
),
),
forMember(
(destination) => destination.wedTime,
mapFrom((source) =>
this.frequencyNormaliser.scheduleResolver(source, Day.wed),
),
),
forMember(
(destination) => destination.thuTime,
mapFrom((source) =>
this.frequencyNormaliser.scheduleResolver(source, Day.thu),
),
),
forMember(
(destination) => destination.friTime,
mapFrom((source) =>
this.frequencyNormaliser.scheduleResolver(source, Day.fri),
),
),
forMember(
(destination) => destination.satTime,
mapFrom((source) =>
this.frequencyNormaliser.scheduleResolver(source, Day.sat),
),
),
forMember(
(destination) => destination.sunTime,
mapFrom((source) =>
this.frequencyNormaliser.scheduleResolver(source, Day.sun),
),
),
);
};
}
}

View File

@@ -1,18 +0,0 @@
import { Mapper, createMap } from '@automapper/core';
import { AutomapperProfile, InjectMapper } from '@automapper/nestjs';
import { Injectable } from '@nestjs/common';
import { AddressDTO } from '../domain/dtos/address.dto';
import { Address } from '../domain/entities/address';
@Injectable()
export class AdProfile extends AutomapperProfile {
constructor(@InjectMapper() mapper: Mapper) {
super(mapper);
}
override get profile() {
return (mapper) => {
createMap(mapper, AddressDTO, Address);
};
}
}

View File

@@ -1,9 +0,0 @@
import { FindAdByUuidRequest } from '../domain/dtos/find-ad-by-uuid.request';
export class FindAdByUuidQuery {
readonly uuid: string;
constructor(findAdByUuidRequest: FindAdByUuidRequest) {
this.uuid = findAdByUuidRequest.uuid;
}
}

View File

@@ -2,9 +2,9 @@ import { Test } from '@nestjs/testing';
import { PrismaService } from '../../../database/adapters/secondaries/prisma-service';
import { AdsRepository } from '../../adapters/secondaries/ads.repository';
import { DatabaseModule } from '../../../database/database.module';
import { Frequency } from '../../domain/types/frequency.enum';
import { AdCreation } from '../../domain/dtos/ad.creation';
import { Address } from '../../domain/entities/address';
import { Frequency } from '../../interface/commands/frequency.enum';
import { AdDTO } from '../../core/dtos/ad.dto';
import { Waypoint } from '../../core/entities/waypoint';
describe('Ad Repository', () => {
let prismaService: PrismaService;
@@ -400,7 +400,7 @@ describe('Ad Repository', () => {
describe('create', () => {
it('should create an punctual ad', async () => {
const beforeCount = await prismaService.ad.count();
const adToCreate: AdCreation = new AdCreation();
const adToCreate: AdDTO = new AdDTO();
adToCreate.uuid = 'be459a29-7a41-4c0b-b371-abe90bfb6f00';
adToCreate.userUuid = '4e52b54d-a729-4dbd-9283-f84a11bb2200';
@@ -417,11 +417,11 @@ describe('Ad Repository', () => {
adToCreate.friMargin = 900;
adToCreate.satMargin = 900;
adToCreate.sunMargin = 900;
adToCreate.seatsDriver = 3;
adToCreate.seatsPassenger = 0;
adToCreate.seatsProposed = 3;
adToCreate.seatsRequested = 0;
adToCreate.strict = false;
adToCreate.addresses = {
create: [address0 as Address, address1 as Address],
adToCreate.waypoints = {
create: [address0 as Waypoint, address1 as Waypoint],
};
const ad = await adsRepository.create(adToCreate);
@@ -432,7 +432,7 @@ describe('Ad Repository', () => {
});
it('should create an recurrent ad', async () => {
const beforeCount = await prismaService.ad.count();
const adToCreate: AdCreation = new AdCreation();
const adToCreate: AdDTO = new AdDTO();
adToCreate.uuid = '137a26fa-4b38-48ba-aecf-1a75f6b20f3d';
adToCreate.userUuid = '4e52b54d-a729-4dbd-9283-f84a11bb2200';
@@ -451,11 +451,11 @@ describe('Ad Repository', () => {
adToCreate.friMargin = 900;
adToCreate.satMargin = 900;
adToCreate.sunMargin = 900;
adToCreate.seatsDriver = 2;
adToCreate.seatsPassenger = 0;
adToCreate.seatsProposed = 2;
adToCreate.seatsRequested = 0;
adToCreate.strict = false;
adToCreate.addresses = {
create: [address0 as Address, address1 as Address],
adToCreate.waypoints = {
create: [address0 as Waypoint, address1 as Waypoint],
};
const ad = await adsRepository.create(adToCreate);

View File

@@ -1,7 +1,7 @@
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';
import { DefaultParamsProvider } from '../../../../adapters/secondaries/default-params-provider';
import { DefaultParams } from '../../../../core/ports/default-params.type';
const mockConfigService = {
get: jest.fn().mockImplementation(() => 'some_default_value'),

View File

@@ -0,0 +1,106 @@
import { Test, TestingModule } from '@nestjs/testing';
import { CreateAdService } from '@modules/ad/core/commands/create-ad/create-ad.service';
import { AD_REPOSITORY, PARAMS_PROVIDER } from '@modules/ad/ad.di-tokens';
import { DefaultParamsProviderPort } from '@modules/ad/core/ports/default-params-provider.port';
import { WaypointDTO } from '@modules/ad/interface/grpc-controllers/dtos/waypoint.dto';
import { CreateAdRequestDTO } from '@modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto';
import { Frequency } from '@modules/ad/core/ad.types';
import { CreateAdCommand } from '@modules/ad/core/commands/create-ad/create-ad.command';
import { Result } from 'oxide.ts';
import { AggregateID } from '@libs/ddd';
import { AdAlreadyExistsError } from '@modules/ad/core/ad.errors';
const originWaypoint: WaypointDTO = {
position: 0,
lon: 48.68944505415954,
lat: 6.176510296462267,
houseNumber: '5',
street: 'Avenue Foch',
locality: 'Nancy',
postalCode: '54000',
country: 'France',
};
const destinationWaypoint: WaypointDTO = {
position: 1,
lon: 48.8566,
lat: 2.3522,
locality: 'Paris',
postalCode: '75000',
country: 'France',
};
const punctualCreateAdRequest: CreateAdRequestDTO = {
userId: '4eb6a6af-ecfd-41c3-9118-473a507014d4',
fromDate: '2023-12-21',
toDate: '2023-12-21',
schedule: {
thu: '08:15',
},
driver: true,
passenger: true,
seatsRequested: 1,
frequency: Frequency.PUNCTUAL,
waypoints: [originWaypoint, destinationWaypoint],
};
const mockAdRepository = {
insert: jest.fn().mockImplementationOnce(() => {
return Promise.resolve({
uuid: '047a6ecf-23d4-4d3e-877c-3225d560a8da',
});
}),
};
const mockDefaultParamsProvider: DefaultParamsProviderPort = {
getParams: () => {
return {
MON_MARGIN: 900,
TUE_MARGIN: 900,
WED_MARGIN: 900,
THU_MARGIN: 900,
FRI_MARGIN: 900,
SAT_MARGIN: 900,
SUN_MARGIN: 900,
DRIVER: false,
SEATS_PROPOSED: 3,
PASSENGER: true,
SEATS_REQUESTED: 1,
STRICT: false,
DEFAULT_TIMEZONE: 'Europe/Paris',
};
},
};
describe('create-ad.service', () => {
let createAdService: CreateAdService;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
{
provide: AD_REPOSITORY,
useValue: mockAdRepository,
},
{
provide: PARAMS_PROVIDER,
useValue: mockDefaultParamsProvider,
},
CreateAdService,
],
}).compile();
createAdService = module.get<CreateAdService>(CreateAdService);
});
it('should be defined', () => {
expect(createAdService).toBeDefined();
});
describe('execution', () => {
const createAdCommand = new CreateAdCommand(punctualCreateAdRequest);
it('should create a new ad', async () => {
const result: Result<AggregateID, AdAlreadyExistsError> =
await createAdService.execute(createAdCommand);
expect(result.unwrap()).toBe('047a6ecf-23d4-4d3e-877c-3225d560a8da');
});
});
});

View File

@@ -1,20 +1,19 @@
import { Test, TestingModule } from '@nestjs/testing';
import { CreateAdUseCase } from '../../../domain/usecases/create-ad.usecase';
import { CreateAdRequest } from '../../../domain/dtos/create-ad.request';
import { CreateAdUseCase } from '../../../core/usecases/create-ad.usecase';
import { CreateAdRequestDTO } from '../../../interface/commands/create-ad.request.dto';
import { AdsRepository } from '../../../adapters/secondaries/ads.repository';
import { CreateAdCommand } from '../../../commands/create-ad.command';
import { CreateAdCommand } from '../../../interface/commands/create-ad.command';
import { AutomapperModule } from '@automapper/nestjs';
import { classes } from '@automapper/classes';
import { Frequency } from '../../../domain/types/frequency.enum';
import { Ad } from '../../../domain/entities/ad';
import { Frequency } from '../../../interface/commands/frequency.enum';
import { Ad } from '../../../core/entities/ad';
import { AdProfile } from '../../../mappers/ad.profile';
import { AddressDTO } from '../../../domain/dtos/address.dto';
import { AdCreation } from '../../../domain/dtos/ad.creation';
import { Address } from '../../../domain/entities/address';
import { PARAMS_PROVIDER } from '../../../ad.constants';
import { Waypoint } from '../../../interface/commands/waypoint';
import { AdDTO } from '../../../core/dtos/ad.dto';
import { PARAMS_PROVIDER } from '../../../ad.di-tokens';
import { MESSAGE_PUBLISHER } from '../../../../../app.constants';
const mockAddress1: AddressDTO = {
const originWaypoint: Waypoint = {
position: 0,
lon: 48.68944505415954,
lat: 6.176510296462267,
@@ -24,7 +23,7 @@ const mockAddress1: AddressDTO = {
postalCode: '54000',
country: 'France',
};
const mockAddress2: AddressDTO = {
const destinationWaypoint: Waypoint = {
position: 1,
lon: 48.8566,
lat: 2.3522,
@@ -32,38 +31,27 @@ const mockAddress2: AddressDTO = {
postalCode: '75000',
country: 'France',
};
const mockAddressWithoutPos1: AddressDTO = {
const originWaypointWithoutPosition: Waypoint = {
lon: 43.2965,
lat: 5.3698,
locality: 'Marseille',
postalCode: '13000',
country: 'France',
};
const mockAddressWithoutPos2: AddressDTO = {
const destinationWaypointWithoutPosition: Waypoint = {
lon: 43.7102,
lat: 7.262,
locality: 'Nice',
postalCode: '06000',
country: 'France',
};
const minimalRecurrentAdREquest: CreateAdRequest = {
userUuid: '224e0000-0000-4000-a000-000000000000',
frequency: Frequency.RECURRENT,
fromDate: new Date('01-05-2023'),
toDate: new Date('01-05-2024'),
schedule: {
mon: '08:00',
},
marginDurations: {},
addresses: [mockAddress1, mockAddress2],
};
const newAdRequest: CreateAdRequest = {
const newAdRequest: CreateAdRequestDTO = {
userUuid: '113e0000-0000-4000-a000-000000000000',
driver: true,
passenger: false,
frequency: Frequency.RECURRENT,
fromDate: new Date('01-05-2023'),
toDate: new Date('20-08-2023'),
fromDate: new Date('2023-05-01'),
toDate: new Date('2023-08-20'),
schedule: {
tue: '08:00',
wed: '08:30',
@@ -77,8 +65,8 @@ const newAdRequest: CreateAdRequest = {
sat: undefined,
sun: undefined,
},
seatsDriver: 2,
addresses: [mockAddress1, mockAddress2],
seatsProposed: 2,
waypoints: [originWaypoint, destinationWaypoint],
};
const mockMessagePublisher = {
@@ -95,7 +83,7 @@ const mockDefaultParamsProvider = {
SAT_MARGIN: 900,
SUN_MARGIN: 900,
DRIVER: false,
SEATS_PROVIDED: 0,
SEATS_PROPOSED: 3,
PASSENGER: true,
SEATS_REQUESTED: 1,
STRICT: false,
@@ -110,7 +98,7 @@ const mockAdRepository = {
return Promise.resolve({
...newAdRequest,
uuid: 'ad000000-0000-4000-a000-000000000000',
createdAt: new Date('01-05-2023'),
createdAt: new Date('2023-05-01'),
});
})
.mockImplementationOnce(() => {
@@ -148,10 +136,10 @@ describe('CreateAdUseCase', () => {
});
describe('execution', () => {
const newAdCommand = new CreateAdCommand(newAdRequest);
it('should create an new ad', async () => {
it('should create a new ad', async () => {
const newAd: Ad = await createAdUseCase.execute(newAdCommand);
expect(newAd.userUuid).toBe(newAdRequest.userUuid);
expect(newAd.uuid).toBeDefined();
expect(newAd.userUuid).toBe('113e0000-0000-4000-a000-000000000000');
expect(newAd.uuid).toBe('ad000000-0000-4000-a000-000000000000');
});
it('should throw an error if the ad already exists', async () => {
await expect(
@@ -165,68 +153,94 @@ describe('CreateAdUseCase', () => {
mockAdRepository.create.mockClear();
});
it('should define mimimal ad as 1 passager add', async () => {
const newAdCommand = new CreateAdCommand(minimalRecurrentAdREquest);
await createAdUseCase.execute(newAdCommand);
const expectedAdCreation = {
userUuid: minimalRecurrentAdREquest.userUuid,
frequency: minimalRecurrentAdREquest.frequency,
fromDate: minimalRecurrentAdREquest.fromDate,
toDate: minimalRecurrentAdREquest.toDate,
monTime: minimalRecurrentAdREquest.schedule.mon,
it('should define minimal recurrent ad as passenger', async () => {
const minimalRecurrentAdRequest: CreateAdRequestDTO = {
userUuid: '224e0000-0000-4000-a000-000000000000',
frequency: Frequency.RECURRENT,
fromDate: new Date('2023-05-01'),
toDate: new Date('2024-05-01'),
schedule: {
mon: '08:00',
},
waypoints: [originWaypoint, destinationWaypoint],
};
const newAdCommand = new CreateAdCommand(minimalRecurrentAdRequest);
const expectedAdCreation: AdDTO = {
userUuid: '224e0000-0000-4000-a000-000000000000',
frequency: Frequency.RECURRENT,
fromDate: new Date('2023-05-01'),
toDate: new Date('2024-05-01'),
monTime: '08:00',
tueTime: undefined,
wedTime: undefined,
thuTime: undefined,
friTime: undefined,
satTime: undefined,
sunTime: undefined,
monMargin: mockDefaultParamsProvider.getParams().MON_MARGIN,
tueMargin: mockDefaultParamsProvider.getParams().TUE_MARGIN,
wedMargin: mockDefaultParamsProvider.getParams().WED_MARGIN,
thuMargin: mockDefaultParamsProvider.getParams().THU_MARGIN,
friMargin: mockDefaultParamsProvider.getParams().FRI_MARGIN,
satMargin: mockDefaultParamsProvider.getParams().SAT_MARGIN,
sunMargin: mockDefaultParamsProvider.getParams().SUN_MARGIN,
driver: mockDefaultParamsProvider.getParams().DRIVER,
seatsDriver: mockDefaultParamsProvider.getParams().SEATS_PROVIDED,
passenger: mockDefaultParamsProvider.getParams().PASSENGER,
seatsPassenger: mockDefaultParamsProvider.getParams().SEATS_REQUESTED,
strict: mockDefaultParamsProvider.getParams().STRICT,
addresses: {
create: minimalRecurrentAdREquest.addresses as Address[],
monMargin: 900,
tueMargin: 900,
wedMargin: 900,
thuMargin: 900,
friMargin: 900,
satMargin: 900,
sunMargin: 900,
driver: false,
seatsProposed: 3,
passenger: true,
seatsRequested: 1,
strict: false,
waypoints: {
create: [
{
uuid: undefined,
adUuid: undefined,
position: 0,
lon: 48.68944505415954,
lat: 6.176510296462267,
houseNumber: '5',
street: 'Avenue Foch',
locality: 'Nancy',
postalCode: '54000',
country: 'France',
},
{
uuid: undefined,
adUuid: undefined,
position: 1,
lon: 48.8566,
lat: 2.3522,
locality: 'Paris',
postalCode: '75000',
country: 'France',
},
],
},
createdAt: undefined,
} as AdCreation;
expect(mockAdRepository.create).toBeCalledWith(expectedAdCreation);
};
await createAdUseCase.execute(newAdCommand);
expect(mockAdRepository.create).toBeCalledWith(expectedAdCreation, {
waypoints: true,
});
});
it('should create an passengerAd with addresses without position ', async () => {
const newPunctualPassengerAdRequest: CreateAdRequest = {
it('should create a passenger ad with waypoints without position', async () => {
const newPunctualPassengerAdRequest: CreateAdRequestDTO = {
userUuid: '113e0000-0000-4000-a000-000000000000',
passenger: true,
frequency: Frequency.PUNCTUAL,
departure: new Date('05-22-2023 09:36'),
marginDurations: {
mon: undefined,
tue: undefined,
wed: undefined,
thu: undefined,
fri: undefined,
sat: undefined,
sun: undefined,
},
seatsPassenger: 1,
addresses: [mockAddressWithoutPos1, mockAddressWithoutPos2],
departureDate: new Date('2023-05-22 09:36'),
seatsRequested: 1,
waypoints: [
originWaypointWithoutPosition,
destinationWaypointWithoutPosition,
],
schedule: {},
};
const newAdCommand = new CreateAdCommand(newPunctualPassengerAdRequest);
await createAdUseCase.execute(newAdCommand);
const expectedAdCreation = {
userUuid: newPunctualPassengerAdRequest.userUuid,
frequency: newPunctualPassengerAdRequest.frequency,
fromDate: newPunctualPassengerAdRequest.departure,
toDate: newPunctualPassengerAdRequest.departure,
const expectedAdCreation: AdDTO = {
userUuid: '113e0000-0000-4000-a000-000000000000',
frequency: Frequency.PUNCTUAL,
fromDate: new Date('2023-05-22'),
toDate: new Date('2023-05-22'),
monTime: '09:36',
tueTime: undefined,
wedTime: undefined,
@@ -234,24 +248,48 @@ describe('CreateAdUseCase', () => {
friTime: undefined,
satTime: undefined,
sunTime: undefined,
monMargin: mockDefaultParamsProvider.getParams().MON_MARGIN,
tueMargin: mockDefaultParamsProvider.getParams().TUE_MARGIN,
wedMargin: mockDefaultParamsProvider.getParams().WED_MARGIN,
thuMargin: mockDefaultParamsProvider.getParams().THU_MARGIN,
friMargin: mockDefaultParamsProvider.getParams().FRI_MARGIN,
satMargin: mockDefaultParamsProvider.getParams().SAT_MARGIN,
sunMargin: mockDefaultParamsProvider.getParams().SUN_MARGIN,
monMargin: 900,
tueMargin: 900,
wedMargin: 900,
thuMargin: 900,
friMargin: 900,
satMargin: 900,
sunMargin: 900,
driver: false,
seatsDriver: 0,
passenger: newPunctualPassengerAdRequest.passenger,
seatsPassenger: newPunctualPassengerAdRequest.seatsPassenger,
strict: mockDefaultParamsProvider.getParams().STRICT,
addresses: {
create: newPunctualPassengerAdRequest.addresses as Address[],
seatsProposed: 3,
passenger: true,
seatsRequested: 1,
strict: false,
waypoints: {
create: [
{
uuid: undefined,
adUuid: undefined,
position: 0,
lon: 43.2965,
lat: 5.3698,
locality: 'Marseille',
postalCode: '13000',
country: 'France',
},
{
uuid: undefined,
adUuid: undefined,
position: 1,
lon: 43.7102,
lat: 7.262,
locality: 'Nice',
postalCode: '06000',
country: 'France',
},
],
},
createdAt: undefined,
} as AdCreation;
expect(mockAdRepository.create).toBeCalledWith(expectedAdCreation);
};
await createAdUseCase.execute(newAdCommand);
expect(mockAdRepository.create).toBeCalledWith(expectedAdCreation, {
waypoints: true,
});
});
});
});

View File

@@ -2,8 +2,8 @@ import { NotFoundException } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { FindAdByUuidQuery } from '../../../queries/find-ad-by-uuid.query';
import { AdsRepository } from '../../../adapters/secondaries/ads.repository';
import { FindAdByUuidUseCase } from '../../../domain/usecases/find-ad-by-uuid.usecase';
import { FindAdByUuidRequest } from '../../../domain/dtos/find-ad-by-uuid.request';
import { FindAdByIdUseCase } from '../../../core/usecases/find-ad-by-uuid.usecase';
import { FindAdByIdRequestDTO } from '../../../interface/queries/find-ad-by-id/dtos/find-ad-by-id.request.dto';
import { MESSAGE_PUBLISHER } from '../../../../../app.constants';
const mockAd = {
@@ -27,7 +27,7 @@ const mockMessagePublisher = {
};
describe('FindAdByUuidUseCase', () => {
let findAdByUuidUseCase: FindAdByUuidUseCase;
let findAdByUuidUseCase: FindAdByIdUseCase;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [],
@@ -40,29 +40,29 @@ describe('FindAdByUuidUseCase', () => {
provide: MESSAGE_PUBLISHER,
useValue: mockMessagePublisher,
},
FindAdByUuidUseCase,
FindAdByIdUseCase,
],
}).compile();
findAdByUuidUseCase = module.get<FindAdByUuidUseCase>(FindAdByUuidUseCase);
findAdByUuidUseCase = module.get<FindAdByIdUseCase>(FindAdByIdUseCase);
});
it('should be defined', () => {
expect(findAdByUuidUseCase).toBeDefined();
});
describe('execute', () => {
it('should return an ad', async () => {
const findAdByUuidRequest: FindAdByUuidRequest =
new FindAdByUuidRequest();
findAdByUuidRequest.uuid = 'bb281075-1b98-4456-89d6-c643d3044a91';
const findAdByUuidRequest: FindAdByIdRequestDTO =
new FindAdByIdRequestDTO();
findAdByUuidRequest.id = 'bb281075-1b98-4456-89d6-c643d3044a91';
const ad = await findAdByUuidUseCase.execute(
new FindAdByUuidQuery(findAdByUuidRequest),
);
expect(ad).toBe(mockAd);
});
it('should throw an error if ad does not exist', async () => {
const findAdByUuidRequest: FindAdByUuidRequest =
new FindAdByUuidRequest();
findAdByUuidRequest.uuid = 'bb281075-1b98-4456-89d6-c643d3044a90';
const findAdByUuidRequest: FindAdByIdRequestDTO =
new FindAdByIdRequestDTO();
findAdByUuidRequest.id = 'bb281075-1b98-4456-89d6-c643d3044a90';
await expect(
findAdByUuidUseCase.execute(new FindAdByUuidQuery(findAdByUuidRequest)),
).rejects.toBeInstanceOf(NotFoundException);

View File

@@ -0,0 +1,140 @@
import { Day } from '../../../core/_old/types/day.enum';
import { CreateAdRequestDTO } from '../../../interface/commands/create-ad.request.dto';
import { ScheduleDTO } from '../../../core/dtos/schedule.dto';
import { FrequencyNormalizer } from '../../../core/entities/frequency.normalizer';
import { Frequency } from '../../../interface/commands/frequency.enum';
describe('recurrent normalizer transformer for punctual ad ', () => {
const frequencyNormalizer = new FrequencyNormalizer();
it('should transform punctual ad into recurrent ad', () => {
const punctualAd: CreateAdRequestDTO = {
userUuid: 'cb7ad514-ad0d-463f-9041-b79f9afe33aa',
frequency: Frequency.PUNCTUAL,
departureDate: new Date('2023-03-05T12:39:39+02:00'),
waypoints: [
{
position: 0,
lon: 48.68944505415954,
lat: 6.176510296462267,
houseNumber: '5',
street: 'Avenue Foch',
locality: 'Nancy',
postalCode: '54000',
country: 'France',
},
{
position: 1,
lon: 48.8566,
lat: 2.3522,
locality: 'Paris',
postalCode: '75000',
country: 'France',
},
],
};
// expect(frequencyNormalizer.fromDateResolver(punctualAd)).toStrictEqual(
// new Date('2023-03-05T12:39:39Z'),
// );
// expect(frequencyNormalizer.toDateResolver(punctualAd)).toStrictEqual(
// new Date('2023-03-05T12:39:39Z'),
// );
// expect(
// frequencyNormalizer.scheduleResolver(punctualAd, Day.mon),
// ).toBeUndefined();
// expect(
// frequencyNormalizer.scheduleResolver(punctualAd, Day.tue),
// ).toBeUndefined();
// expect(
// frequencyNormalizer.scheduleResolver(punctualAd, Day.wed),
// ).toBeUndefined();
// expect(
// frequencyNormalizer.scheduleResolver(punctualAd, Day.thu),
// ).toBeUndefined();
// expect(
// frequencyNormalizer.scheduleResolver(punctualAd, Day.fri),
// ).toBeUndefined();
// expect(
// frequencyNormalizer.scheduleResolver(punctualAd, Day.sat),
// ).toBeUndefined();
expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.sun)).toBe(
'12:39',
);
});
// it('should leave recurrent ad as is', () => {
// const recurrentAd: CreateAdRequest = {
// userUuid: '',
// frequency: Frequency.RECURRENT,
// schedule: {
// mon: '08:30',
// tue: '08:30',
// wed: '09:00',
// fri: '09:00',
// },
// waypoints: [],
// };
// expect(frequencyNormalizer.fromDateResolver(recurrentAd)).toBe(
// recurrentAd.departure,
// );
// expect(frequencyNormalizer.toDateResolver(recurrentAd)).toBe(
// recurrentAd.departure,
// );
// expect(frequencyNormalizer.scheduleResolver(recurrentAd, Day.mon)).toBe(
// recurrentAd.schedule.mon,
// );
// expect(frequencyNormalizer.scheduleResolver(recurrentAd, Day.tue)).toBe(
// recurrentAd.schedule.tue,
// );
// expect(frequencyNormalizer.scheduleResolver(recurrentAd, Day.wed)).toBe(
// recurrentAd.schedule.wed,
// );
// expect(frequencyNormalizer.scheduleResolver(recurrentAd, Day.thu)).toBe(
// recurrentAd.schedule.thu,
// );
// expect(frequencyNormalizer.scheduleResolver(recurrentAd, Day.fri)).toBe(
// recurrentAd.schedule.fri,
// );
// expect(frequencyNormalizer.scheduleResolver(recurrentAd, Day.sat)).toBe(
// recurrentAd.schedule.sat,
// );
// expect(frequencyNormalizer.scheduleResolver(recurrentAd, Day.sun)).toBe(
// recurrentAd.schedule.sun,
// );
// });
// it('should pass for each day of the week of a deprarture ', () => {
// const punctualAd: CreateAdRequest = {
// userUuid: '',
// frequency: Frequency.PUNCTUAL,
// departure: undefined,
// schedule: {} as ScheduleDTO,
// waypoints: [],
// };
// punctualAd.departure = new Date('05-01-2023 ');
// expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.mon)).toBe(
// '00:00',
// );
// punctualAd.departure = new Date('05-02-2023 06:32:45');
// expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.tue)).toBe(
// '06:32',
// );
// punctualAd.departure = new Date('05-03-2023 10:21');
// expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.wed)).toBe(
// '10:21',
// );
// punctualAd.departure = new Date('05-04-2023 11:06:00');
// expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.thu)).toBe(
// '11:06',
// );
// punctualAd.departure = new Date('05-05-2023 05:20');
// expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.fri)).toBe(
// '05:20',
// );
// punctualAd.departure = new Date('05-06-2023');
// expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.sat)).toBe(
// '00:00',
// );
// punctualAd.departure = new Date('05-07-2023');
// expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.sun)).toBe(
// '00:00',
// );
// });
});

View File

@@ -1,5 +1,5 @@
import { intToFrequency } from '../../../domain/dtos/validators/frequency.mapping';
import { Frequency } from '../../../domain/types/frequency.enum';
import { intToFrequency } from '../../../core/dtos/validators/frequency.mapping';
import { Frequency } from '../../../interface/commands/frequency.enum';
describe('frequency mapping function ', () => {
it('should return punctual', () => {
@@ -8,8 +8,8 @@ describe('frequency mapping function ', () => {
it('should return recurrent', () => {
expect(intToFrequency(2)).toBe(Frequency.RECURRENT);
});
it('should return undefined', () => {
expect(intToFrequency(0)).toBeUndefined();
expect(intToFrequency(3)).toBeUndefined();
it('should throw an error if frequency is unknown', () => {
expect(() => intToFrequency(0)).toThrow();
expect(() => intToFrequency(3)).toThrow();
});
});

View File

@@ -1,100 +0,0 @@
import { hasProperDriverSeats } from '../../../domain/dtos/validators/has-driver-seats';
describe('driver and/or driver seats validator', () => {
it('should validate if driver and drivers seats is not provided ', () => {
expect(
hasProperDriverSeats({
value: undefined,
constraints: [],
targetName: '',
object: { driver: undefined },
property: '',
}),
).toBe(true);
expect(
hasProperDriverSeats({
value: undefined,
constraints: [],
targetName: '',
object: { driver: false },
property: '',
}),
).toBe(true);
expect(
hasProperDriverSeats({
value: undefined,
constraints: [],
targetName: '',
object: { driver: null },
property: '',
}),
).toBe(true);
});
it('should not validate if driver is set to true but not the related seats is not provided or 0', () => {
expect(
hasProperDriverSeats({
value: undefined,
constraints: [],
targetName: '',
object: { driver: true },
property: '',
}),
).toBe(false);
expect(
hasProperDriverSeats({
value: undefined,
constraints: [],
targetName: '',
object: { driver: true, seatsDriver: 0 },
property: '',
}),
).toBe(false);
expect(
hasProperDriverSeats({
value: undefined,
constraints: [],
targetName: '',
object: { driver: true, seatsDriver: undefined },
property: '',
}),
).toBe(false);
expect(
hasProperDriverSeats({
value: undefined,
constraints: [],
targetName: '',
object: { driver: true, seatsDriver: null },
property: '',
}),
).toBe(false);
});
it('should not validate if driver seats are provided but driver is not set or set to false ', () => {
expect(
hasProperDriverSeats({
value: undefined,
constraints: [],
targetName: '',
object: { driver: false, seatsDriver: 1 },
property: '',
}),
).toBe(false);
expect(
hasProperDriverSeats({
value: undefined,
constraints: [],
targetName: '',
object: { driver: undefined, seatsDriver: 1 },
property: '',
}),
).toBe(false);
expect(
hasProperDriverSeats({
value: undefined,
constraints: [],
targetName: '',
object: { driver: null, seatsDriver: 1 },
property: '',
}),
).toBe(false);
});
});

View File

@@ -1,100 +0,0 @@
import { hasProperPassengerSeats } from '../../../domain/dtos/validators/has-passenger-seats';
describe('driver and/or passenger seats validator', () => {
it('should validate if passenger and passengers seats is not provided ', () => {
expect(
hasProperPassengerSeats({
value: undefined,
constraints: [],
targetName: '',
object: { passenger: undefined },
property: '',
}),
).toBe(true);
expect(
hasProperPassengerSeats({
value: undefined,
constraints: [],
targetName: '',
object: { passenger: false },
property: '',
}),
).toBe(true);
expect(
hasProperPassengerSeats({
value: undefined,
constraints: [],
targetName: '',
object: { passenger: null },
property: '',
}),
).toBe(true);
});
it('should not validate if passenger is set to true but not the related seats is not provided or 0', () => {
expect(
hasProperPassengerSeats({
value: undefined,
constraints: [],
targetName: '',
object: { passenger: true },
property: '',
}),
).toBe(false);
expect(
hasProperPassengerSeats({
value: undefined,
constraints: [],
targetName: '',
object: { passenger: true, seatsPassenger: 0 },
property: '',
}),
).toBe(false);
expect(
hasProperPassengerSeats({
value: undefined,
constraints: [],
targetName: '',
object: { passenger: true, seatsPassenger: undefined },
property: '',
}),
).toBe(false);
expect(
hasProperPassengerSeats({
value: undefined,
constraints: [],
targetName: '',
object: { passenger: true, seatsPassenger: null },
property: '',
}),
).toBe(false);
});
it('should not validate if passenger seats are provided but passenger is not set or set to false ', () => {
expect(
hasProperPassengerSeats({
value: undefined,
constraints: [],
targetName: '',
object: { passenger: false, seatsPassenger: 1 },
property: '',
}),
).toBe(false);
expect(
hasProperPassengerSeats({
value: undefined,
constraints: [],
targetName: '',
object: { passenger: undefined, seatsPassenger: 1 },
property: '',
}),
).toBe(false);
expect(
hasProperPassengerSeats({
value: undefined,
constraints: [],
targetName: '',
object: { passenger: null, seatsPassenger: 1 },
property: '',
}),
).toBe(false);
});
});

View File

@@ -1,10 +1,10 @@
import { isPunctualOrRecurrent } from '../../../domain/dtos/validators/is-punctual-or-recurrent';
import { Frequency } from '../../../domain/types/frequency.enum';
import { isPunctualOrRecurrent } from '../../../core/dtos/validators/is-punctual-or-recurrent';
import { Frequency } from '../../../interface/commands/frequency.enum';
describe('punctual or recurrent validators', () => {
describe('punctual case ', () => {
describe('valid cases', () => {
it('should validate with valid departure and empty schedule ', () => {
it('should validate with valid departure', () => {
expect(
isPunctualOrRecurrent({
value: undefined,
@@ -12,8 +12,7 @@ describe('punctual or recurrent validators', () => {
targetName: '',
object: {
frequency: Frequency.PUNCTUAL,
departure: new Date('01-02-2023'),
schedule: {},
departure: new Date('2023-02-01T18:00:00+02:00'),
},
property: '',
}),
@@ -21,7 +20,7 @@ describe('punctual or recurrent validators', () => {
});
});
describe('invalid cases ', () => {
it('should not validate with invalid departure and empty schedule and margin', () => {
it('should not validate without departure', () => {
expect(
isPunctualOrRecurrent({
value: undefined,
@@ -29,25 +28,6 @@ describe('punctual or recurrent validators', () => {
targetName: '',
object: {
frequency: Frequency.PUNCTUAL,
fromDate: new Date('20-10-2023'),
toDate: new Date('30-10-2023'),
},
property: '',
}),
).toBeFalsy();
});
it('should not validate with no empty schedule', () => {
expect(
isPunctualOrRecurrent({
value: undefined,
constraints: [],
targetName: '',
object: {
frequency: Frequency.PUNCTUAL,
departure: new Date('01-02-2023'),
schedule: {
mon: '08:30',
},
},
property: '',
}),
@@ -57,7 +37,7 @@ describe('punctual or recurrent validators', () => {
});
describe('recurrent case ', () => {
describe('valid cases', () => {
it('should validate with valid from date, to date and non empty schedule ', () => {
it('should validate with valid from date, to date and non empty schedule', () => {
expect(
isPunctualOrRecurrent({
value: undefined,
@@ -65,8 +45,8 @@ describe('punctual or recurrent validators', () => {
targetName: '',
object: {
frequency: Frequency.RECURRENT,
fromDate: new Date('01-15-2023'),
toDate: new Date('06-30-2023'),
fromDate: new Date('2023-01-15'),
toDate: new Date('2023-06-30'),
schedule: {
mon: '08:30',
},
@@ -77,7 +57,7 @@ describe('punctual or recurrent validators', () => {
});
});
describe('invalid cases ', () => {
it('should not validate with empty schedule ', () => {
it('should not validate with empty schedule', () => {
expect(
isPunctualOrRecurrent({
value: undefined,
@@ -85,8 +65,8 @@ describe('punctual or recurrent validators', () => {
targetName: '',
object: {
frequency: Frequency.RECURRENT,
fromDate: new Date('01-15-2023'),
toDate: new Date('06-30-2023'),
fromDate: new Date('2023-01-15'),
toDate: new Date('2023-06-30'),
schedule: {},
},
property: '',
@@ -101,8 +81,8 @@ describe('punctual or recurrent validators', () => {
targetName: '',
object: {
frequency: Frequency.RECURRENT,
departure: new Date('20-10-2023'),
toDate: new Date('30-10-2023'),
departure: new Date('2023-10-20'),
toDate: new Date('2023-10-30'),
},
property: '',
}),

View File

@@ -1,122 +0,0 @@
import { Day } from '../../../domain/types/day.enum';
import { CreateAdRequest } from '../../../domain/dtos/create-ad.request';
import { ScheduleDTO } from '../../../domain/dtos/schedule.dto';
import { FrequencyNormaliser } from '../../../domain/entities/frequency.normaliser';
import { Frequency } from '../../../domain/types/frequency.enum';
describe('recurrent normalizer transformer for punctual ad ', () => {
const recurrentNormaliser = new FrequencyNormaliser();
it('should transform punctual ad into recurrent ad ', () => {
const punctualAd: CreateAdRequest = {
userUuid: '',
frequency: Frequency.PUNCTUAL,
departure: new Date('05-03-2023 12:39:39 '),
schedule: {} as ScheduleDTO,
addresses: [],
};
expect(recurrentNormaliser.fromDateResolver(punctualAd)).toBe(
punctualAd.departure,
);
expect(recurrentNormaliser.toDateResolver(punctualAd)).toBe(
punctualAd.departure,
);
expect(
recurrentNormaliser.scheduleResolver(punctualAd, Day.mon),
).toBeUndefined();
expect(
recurrentNormaliser.scheduleResolver(punctualAd, Day.tue),
).toBeUndefined();
expect(recurrentNormaliser.scheduleResolver(punctualAd, Day.wed)).toBe(
'12:39',
);
expect(
recurrentNormaliser.scheduleResolver(punctualAd, Day.thu),
).toBeUndefined();
expect(
recurrentNormaliser.scheduleResolver(punctualAd, Day.fri),
).toBeUndefined();
expect(
recurrentNormaliser.scheduleResolver(punctualAd, Day.sat),
).toBeUndefined();
expect(
recurrentNormaliser.scheduleResolver(punctualAd, Day.sun),
).toBeUndefined();
});
it('should leave recurrent ad as is', () => {
const recurrentAd: CreateAdRequest = {
userUuid: '',
frequency: Frequency.RECURRENT,
schedule: {
mon: '08:30',
tue: '08:30',
wed: '09:00',
fri: '09:00',
},
addresses: [],
};
expect(recurrentNormaliser.fromDateResolver(recurrentAd)).toBe(
recurrentAd.departure,
);
expect(recurrentNormaliser.toDateResolver(recurrentAd)).toBe(
recurrentAd.departure,
);
expect(recurrentNormaliser.scheduleResolver(recurrentAd, Day.mon)).toBe(
recurrentAd.schedule.mon,
);
expect(recurrentNormaliser.scheduleResolver(recurrentAd, Day.tue)).toBe(
recurrentAd.schedule.tue,
);
expect(recurrentNormaliser.scheduleResolver(recurrentAd, Day.wed)).toBe(
recurrentAd.schedule.wed,
);
expect(recurrentNormaliser.scheduleResolver(recurrentAd, Day.thu)).toBe(
recurrentAd.schedule.thu,
);
expect(recurrentNormaliser.scheduleResolver(recurrentAd, Day.fri)).toBe(
recurrentAd.schedule.fri,
);
expect(recurrentNormaliser.scheduleResolver(recurrentAd, Day.sat)).toBe(
recurrentAd.schedule.sat,
);
expect(recurrentNormaliser.scheduleResolver(recurrentAd, Day.sun)).toBe(
recurrentAd.schedule.sun,
);
});
it('should pass for each day of the week of a deprarture ', () => {
const punctualAd: CreateAdRequest = {
userUuid: '',
frequency: Frequency.PUNCTUAL,
departure: undefined,
schedule: {} as ScheduleDTO,
addresses: [],
};
punctualAd.departure = new Date('05-01-2023 ');
expect(recurrentNormaliser.scheduleResolver(punctualAd, Day.mon)).toBe(
'00:00',
);
punctualAd.departure = new Date('05-02-2023 06:32:45');
expect(recurrentNormaliser.scheduleResolver(punctualAd, Day.tue)).toBe(
'06:32',
);
punctualAd.departure = new Date('05-03-2023 10:21');
expect(recurrentNormaliser.scheduleResolver(punctualAd, Day.wed)).toBe(
'10:21',
);
punctualAd.departure = new Date('05-04-2023 11:06:00');
expect(recurrentNormaliser.scheduleResolver(punctualAd, Day.thu)).toBe(
'11:06',
);
punctualAd.departure = new Date('05-05-2023 05:20');
expect(recurrentNormaliser.scheduleResolver(punctualAd, Day.fri)).toBe(
'05:20',
);
punctualAd.departure = new Date('05-06-2023');
expect(recurrentNormaliser.scheduleResolver(punctualAd, Day.sat)).toBe(
'00:00',
);
punctualAd.departure = new Date('05-07-2023');
expect(recurrentNormaliser.scheduleResolver(punctualAd, Day.sun)).toBe(
'00:00',
);
});
});

View File

@@ -1,7 +1,7 @@
import { AddressDTO } from '../../../domain/dtos/address.dto';
import { hasProperPositionIndexes } from '../../../domain/dtos/validators/address-position';
import { Waypoint } from '../../../interface/commands/waypoint';
import { hasValidPositionIndexes } from '../../../core/dtos/validators/waypoint-position';
describe('addresses position validators', () => {
const mockAddress1: AddressDTO = {
const mockAddress1: Waypoint = {
lon: 48.68944505415954,
lat: 6.176510296462267,
houseNumber: '5',
@@ -10,7 +10,7 @@ describe('addresses position validators', () => {
postalCode: '54000',
country: 'France',
};
const mockAddress2: AddressDTO = {
const mockAddress2: Waypoint = {
lon: 48.8566,
lat: 2.3522,
locality: 'Paris',
@@ -18,7 +18,7 @@ describe('addresses position validators', () => {
country: 'France',
};
const mockAddress3: AddressDTO = {
const mockAddress3: Waypoint = {
lon: 49.2628,
lat: 4.0347,
locality: 'Reims',
@@ -27,13 +27,13 @@ describe('addresses position validators', () => {
};
it('should validate if none of position is definded ', () => {
expect(
hasProperPositionIndexes([mockAddress1, mockAddress2, mockAddress3]),
hasValidPositionIndexes([mockAddress1, mockAddress2, mockAddress3]),
).toBeTruthy();
});
it('should throw an error if position are partialy defined ', () => {
mockAddress1.position = 0;
expect(
hasProperPositionIndexes([mockAddress1, mockAddress2, mockAddress3]),
hasValidPositionIndexes([mockAddress1, mockAddress2, mockAddress3]),
).toBeFalsy();
});
it('should throw an error if position are partialy defined ', () => {
@@ -41,7 +41,7 @@ describe('addresses position validators', () => {
mockAddress2.position = null;
mockAddress3.position = undefined;
expect(
hasProperPositionIndexes([mockAddress1, mockAddress2, mockAddress3]),
hasValidPositionIndexes([mockAddress1, mockAddress2, mockAddress3]),
).toBeFalsy();
});
@@ -50,7 +50,7 @@ describe('addresses position validators', () => {
mockAddress2.position = 1;
mockAddress3.position = 1;
expect(
hasProperPositionIndexes([mockAddress1, mockAddress2, mockAddress3]),
hasValidPositionIndexes([mockAddress1, mockAddress2, mockAddress3]),
).toBeFalsy();
});
it('should validate if all positions are defined and incremented', () => {
@@ -58,13 +58,13 @@ describe('addresses position validators', () => {
mockAddress2.position = 1;
mockAddress3.position = 2;
expect(
hasProperPositionIndexes([mockAddress1, mockAddress2, mockAddress3]),
hasValidPositionIndexes([mockAddress1, mockAddress2, mockAddress3]),
).toBeTruthy();
mockAddress1.position = 10;
mockAddress2.position = 0;
mockAddress3.position = 3;
expect(
hasProperPositionIndexes([mockAddress1, mockAddress2, mockAddress3]),
hasValidPositionIndexes([mockAddress1, mockAddress2, mockAddress3]),
).toBeTruthy();
});
});

View File

@@ -5,8 +5,8 @@ import {
HealthCheckResult,
} from '@nestjs/terminus';
import { MESSAGE_PUBLISHER } from 'src/app.constants';
import { IPublishMessage } from 'src/interfaces/message-publisher';
import { RepositoriesHealthIndicatorUseCase } from '../../domain/usecases/repositories.health-indicator.usecase';
import { MessagePublisherPort } from '@ports/message-publisher.port';
@Controller('health')
export class HealthController {
@@ -14,7 +14,7 @@ export class HealthController {
private readonly repositoriesHealthIndicatorUseCase: RepositoriesHealthIndicatorUseCase,
private healthCheckService: HealthCheckService,
@Inject(MESSAGE_PUBLISHER)
private readonly messagePublisher: IPublishMessage,
private readonly messagePublisher: MessagePublisherPort,
) {}
@Get()

View File

@@ -1,10 +1,10 @@
import { Inject, Injectable } from '@nestjs/common';
import { MESSAGE_BROKER_PUBLISHER } from '../../../../app.constants';
import { MessageBrokerPublisher } from '@mobicoop/message-broker-module';
import { IPublishMessage } from 'src/interfaces/message-publisher';
import { MessagePublisherPort } from '@ports/message-publisher.port';
@Injectable()
export class MessagePublisher implements IPublishMessage {
export class MessagePublisher implements MessagePublisherPort {
constructor(
@Inject(MESSAGE_BROKER_PUBLISHER)
private readonly messageBrokerPublisher: MessageBrokerPublisher,

View File

@@ -5,14 +5,14 @@ import {
HealthIndicatorResult,
} from '@nestjs/terminus';
import { ICheckRepository } from '../interfaces/check-repository.interface';
import { AdsRepository } from '../../../ad/adapters/secondaries/ads.repository';
import { AdRepository } from '@modules/ad/infrastructure/ad.repository';
@Injectable()
export class RepositoriesHealthIndicatorUseCase extends HealthIndicator {
private checkRepositories: ICheckRepository[];
constructor(private readonly adsRepository: AdsRepository) {
constructor(private readonly adRepository: AdRepository) {
super();
this.checkRepositories = [adsRepository];
this.checkRepositories = [adRepository];
}
isHealthy = async (key: string): Promise<HealthIndicatorResult> => {
try {

View File

@@ -1,6 +1,5 @@
import { Module } from '@nestjs/common';
import { HealthServerController } from './adapters/primaries/health-server.controller';
import { AdsRepository } from '../ad/adapters/secondaries/ads.repository';
import { DatabaseModule } from '../database/database.module';
import { HealthController } from './adapters/primaries/health.controller';
import { TerminusModule } from '@nestjs/terminus';
@@ -8,13 +7,14 @@ import { MESSAGE_BROKER_PUBLISHER, MESSAGE_PUBLISHER } from 'src/app.constants';
import { MessageBrokerPublisher } from '@mobicoop/message-broker-module';
import { MessagePublisher } from './adapters/secondaries/message-publisher';
import { RepositoriesHealthIndicatorUseCase } from './domain/usecases/repositories.health-indicator.usecase';
import { AdRepository } from '../ad/infrastructure/ad.repository';
@Module({
imports: [TerminusModule, DatabaseModule],
controllers: [HealthServerController, HealthController],
providers: [
RepositoriesHealthIndicatorUseCase,
AdsRepository,
AdRepository,
{
provide: MESSAGE_BROKER_PUBLISHER,
useClass: MessageBrokerPublisher,