import { Injectable } from '@nestjs/common'; import { MatcherRepository } from '../../../database/domain/matcher-repository'; import { Ad } from '../../domain/entities/ad'; import { DatabaseException } from '../../../database/exceptions/database.exception'; import { Frequency } from '../../domain/types/frequency.enum'; @Injectable() export class AdRepository extends MatcherRepository { protected model = 'ad'; async createAd(ad: Partial): Promise { 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): Partial { return { uuid: `'${ad.uuid}'`, driver: ad.driver ? 'true' : 'false', passenger: ad.passenger ? 'true' : 'false', frequency: ad.frequency, fromDate: `'${ad.fromDate}'`, toDate: `'${ad.toDate}'`, monTime: `'${ad.monTime}'`, tueTime: `'${ad.tueTime}'`, wedTime: `'${ad.wedTime}'`, thuTime: `'${ad.thuTime}'`, friTime: `'${ad.friTime}'`, satTime: `'${ad.satTime}'`, sunTime: `'${ad.sunTime}'`, }; } } type AdFields = { uuid: string; driver: string; passenger: string; frequency: Frequency; fromDate: string; toDate: string; monTime: string; tueTime: string; wedTime: string; thuTime: string; friTime: string; satTime: string; sunTime: string; monMargin: number; tueMargin: number; wedMargin: number; thuMargin: number; friMargin: number; satMargin: number; sunMargin: number; driverDuration: number; driverDistance: number; passengerDuration: number; passengerDistance: number; originType: number; destinationType: number; waypoints: string; direction: string; fwdAzimuth: number; backAzimuth: number; seatsDriver: number; seatsPassenger: number; seatsUsed: number; createdAt: string; updatedAt: string; };