2023-04-25 15:49:47 +00:00
|
|
|
import { Injectable } from '@nestjs/common';
|
2023-05-05 06:56:25 +00:00
|
|
|
import { MatcherRepository } from '../../../database/domain/matcher-repository';
|
2023-04-25 15:49:47 +00:00
|
|
|
import { Ad } from '../../domain/entities/ad';
|
2023-05-05 06:56:25 +00:00
|
|
|
import { DatabaseException } from '../../../database/exceptions/database.exception';
|
2023-04-25 15:49:47 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class AdRepository extends MatcherRepository<Ad> {
|
2023-05-11 15:47:55 +00:00
|
|
|
protected model = 'ad';
|
2023-04-25 15:49:47 +00:00
|
|
|
|
|
|
|
async createAd(ad: Partial<Ad>): Promise<Ad> {
|
|
|
|
try {
|
|
|
|
const affectedRowNumber = await this.createWithFields(
|
|
|
|
this.createFields(ad),
|
|
|
|
);
|
|
|
|
if (affectedRowNumber == 1) {
|
|
|
|
return this.findOneByUuid(ad.uuid);
|
|
|
|
}
|
|
|
|
throw new DatabaseException();
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private createFields(ad: Partial<Ad>): Partial<AdFields> {
|
|
|
|
return {
|
|
|
|
uuid: `'${ad.uuid}'`,
|
2023-05-12 14:23:42 +00:00
|
|
|
userUuid: `'${ad.userUuid}'`,
|
2023-04-25 15:49:47 +00:00
|
|
|
driver: ad.driver ? 'true' : 'false',
|
|
|
|
passenger: ad.passenger ? 'true' : 'false',
|
2023-05-12 14:23:42 +00:00
|
|
|
frequency: `'${ad.frequency}'`,
|
2023-05-23 12:00:39 +00:00
|
|
|
fromDate: `'${ad.fromDate.getFullYear()}-${
|
|
|
|
ad.fromDate.getMonth() + 1
|
|
|
|
}-${ad.fromDate.getDate()}'`,
|
|
|
|
toDate: `'${ad.toDate.getFullYear()}-${
|
|
|
|
ad.toDate.getMonth() + 1
|
|
|
|
}-${ad.toDate.getDate()}'`,
|
2023-05-12 14:23:42 +00:00
|
|
|
monTime: ad.monTime
|
2023-05-23 12:00:39 +00:00
|
|
|
? `'${ad.monTime.getFullYear()}-${
|
|
|
|
ad.monTime.getMonth() + 1
|
|
|
|
}-${ad.monTime.getDate()}T${ad.monTime.getHours()}:${ad.monTime.getMinutes()}Z'`
|
2023-05-12 14:23:42 +00:00
|
|
|
: 'NULL',
|
|
|
|
tueTime: ad.tueTime
|
2023-05-23 12:00:39 +00:00
|
|
|
? `'${ad.tueTime.getFullYear()}-${
|
|
|
|
ad.tueTime.getMonth() + 1
|
|
|
|
}-${ad.tueTime.getDate()}T${ad.tueTime.getHours()}:${ad.tueTime.getMinutes()}Z'`
|
2023-05-12 14:23:42 +00:00
|
|
|
: 'NULL',
|
|
|
|
wedTime: ad.wedTime
|
2023-05-23 12:00:39 +00:00
|
|
|
? `'${ad.wedTime.getFullYear()}-${
|
|
|
|
ad.wedTime.getMonth() + 1
|
|
|
|
}-${ad.wedTime.getDate()}T${ad.wedTime.getHours()}:${ad.wedTime.getMinutes()}Z'`
|
2023-05-12 14:23:42 +00:00
|
|
|
: 'NULL',
|
|
|
|
thuTime: ad.thuTime
|
2023-05-23 12:00:39 +00:00
|
|
|
? `'${ad.thuTime.getFullYear()}-${
|
|
|
|
ad.thuTime.getMonth() + 1
|
|
|
|
}-${ad.thuTime.getDate()}T${ad.thuTime.getHours()}:${ad.thuTime.getMinutes()}Z'`
|
2023-05-12 14:23:42 +00:00
|
|
|
: 'NULL',
|
|
|
|
friTime: ad.friTime
|
2023-05-23 12:00:39 +00:00
|
|
|
? `'${ad.friTime.getFullYear()}-${
|
|
|
|
ad.friTime.getMonth() + 1
|
|
|
|
}-${ad.friTime.getDate()}T${ad.friTime.getHours()}:${ad.friTime.getMinutes()}Z'`
|
2023-05-12 14:23:42 +00:00
|
|
|
: 'NULL',
|
|
|
|
satTime: ad.satTime
|
2023-05-23 12:00:39 +00:00
|
|
|
? `'${ad.satTime.getFullYear()}-${
|
|
|
|
ad.satTime.getMonth() + 1
|
|
|
|
}-${ad.satTime.getDate()}T${ad.satTime.getHours()}:${ad.satTime.getMinutes()}Z'`
|
2023-05-12 14:23:42 +00:00
|
|
|
: 'NULL',
|
|
|
|
sunTime: ad.sunTime
|
2023-05-23 12:00:39 +00:00
|
|
|
? `'${ad.sunTime.getFullYear()}-${
|
|
|
|
ad.sunTime.getMonth() + 1
|
|
|
|
}-${ad.sunTime.getDate()}T${ad.sunTime.getHours()}:${ad.sunTime.getMinutes()}Z'`
|
2023-05-12 14:23:42 +00:00
|
|
|
: 'NULL',
|
|
|
|
monMargin: ad.monMargin,
|
|
|
|
tueMargin: ad.tueMargin,
|
|
|
|
wedMargin: ad.wedMargin,
|
|
|
|
thuMargin: ad.thuMargin,
|
|
|
|
friMargin: ad.friMargin,
|
|
|
|
satMargin: ad.satMargin,
|
|
|
|
sunMargin: ad.sunMargin,
|
|
|
|
fwdAzimuth: ad.fwdAzimuth,
|
|
|
|
backAzimuth: ad.backAzimuth,
|
|
|
|
driverDuration: ad.driverDuration ?? 'NULL',
|
|
|
|
driverDistance: ad.driverDistance ?? 'NULL',
|
|
|
|
passengerDuration: ad.passengerDuration ?? 'NULL',
|
|
|
|
passengerDistance: ad.passengerDistance ?? 'NULL',
|
|
|
|
waypoints: ad.waypoints,
|
|
|
|
direction: ad.direction,
|
|
|
|
seatsDriver: ad.seatsDriver,
|
|
|
|
seatsPassenger: ad.seatsPassenger,
|
|
|
|
seatsUsed: ad.seatsUsed ?? 0,
|
|
|
|
strict: ad.strict,
|
2023-04-25 15:49:47 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type AdFields = {
|
|
|
|
uuid: string;
|
2023-05-12 14:23:42 +00:00
|
|
|
userUuid: string;
|
2023-04-25 15:49:47 +00:00
|
|
|
driver: string;
|
|
|
|
passenger: string;
|
2023-05-12 14:23:42 +00:00
|
|
|
frequency: string;
|
2023-04-25 15:49:47 +00:00
|
|
|
fromDate: string;
|
|
|
|
toDate: string;
|
|
|
|
monTime: string;
|
|
|
|
tueTime: string;
|
|
|
|
wedTime: string;
|
|
|
|
thuTime: string;
|
|
|
|
friTime: string;
|
|
|
|
satTime: string;
|
|
|
|
sunTime: string;
|
|
|
|
monMargin: number;
|
|
|
|
tueMargin: number;
|
|
|
|
wedMargin: number;
|
|
|
|
thuMargin: number;
|
|
|
|
friMargin: number;
|
|
|
|
satMargin: number;
|
|
|
|
sunMargin: number;
|
2023-05-12 14:23:42 +00:00
|
|
|
driverDuration?: number | 'NULL';
|
|
|
|
driverDistance?: number | 'NULL';
|
|
|
|
passengerDuration?: number | 'NULL';
|
|
|
|
passengerDistance?: number | 'NULL';
|
2023-04-25 15:49:47 +00:00
|
|
|
waypoints: string;
|
|
|
|
direction: string;
|
|
|
|
fwdAzimuth: number;
|
|
|
|
backAzimuth: number;
|
2023-05-12 14:23:42 +00:00
|
|
|
seatsDriver?: number;
|
|
|
|
seatsPassenger?: number;
|
|
|
|
seatsUsed?: number;
|
|
|
|
strict: boolean;
|
2023-04-25 15:49:47 +00:00
|
|
|
createdAt: string;
|
|
|
|
updatedAt: string;
|
|
|
|
};
|