mirror of
https://gitlab.com/mobicoop/v3/service/matcher.git
synced 2026-01-01 04:52:40 +00:00
create dedicated selectors
This commit is contained in:
@@ -7,10 +7,7 @@ import { AD_MESSAGE_PUBLISHER } from '../ad.di-tokens';
|
||||
import { AdEntity } from '../core/domain/ad.entity';
|
||||
import { AdMapper } from '../ad.mapper';
|
||||
import { ExtendedPrismaRepositoryBase } from '@mobicoop/ddd-library/dist/db/prisma-repository.base';
|
||||
import { Frequency, Role } from '../core/domain/ad.types';
|
||||
import { Candidate } from '../core/application/types/algorithm.types';
|
||||
import { AdSelector } from './ad.selector';
|
||||
import { MatchQuery } from '../core/application/queries/match/match.query';
|
||||
import { Frequency } from '../core/domain/ad.types';
|
||||
|
||||
export type AdBaseModel = {
|
||||
uuid: string;
|
||||
@@ -34,7 +31,6 @@ export type AdBaseModel = {
|
||||
|
||||
export type AdReadModel = AdBaseModel & {
|
||||
waypoints: string;
|
||||
direction: string;
|
||||
schedule: ScheduleItemModel[];
|
||||
};
|
||||
|
||||
@@ -58,6 +54,39 @@ export type ScheduleItemModel = {
|
||||
updatedAt: Date;
|
||||
};
|
||||
|
||||
export type RawAdBaseModel = {
|
||||
uuid: string;
|
||||
driver: boolean;
|
||||
passenger: boolean;
|
||||
frequency: Frequency;
|
||||
fromDate: Date;
|
||||
toDate: Date;
|
||||
seatsProposed: number;
|
||||
seatsRequested: number;
|
||||
strict: boolean;
|
||||
driverDuration?: number;
|
||||
driverDistance?: number;
|
||||
passengerDuration?: number;
|
||||
passengerDistance?: number;
|
||||
fwdAzimuth: number;
|
||||
backAzimuth: number;
|
||||
waypoints: string;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
};
|
||||
|
||||
export type RawScheduleItemModel = {
|
||||
day: number;
|
||||
time: Date;
|
||||
margin: number;
|
||||
};
|
||||
|
||||
export type RawAdModel = RawAdBaseModel & RawScheduleItemModel;
|
||||
|
||||
export type RawAdReadModel = RawAdBaseModel & {
|
||||
schedule: RawScheduleItemModel[];
|
||||
};
|
||||
|
||||
/**
|
||||
* Repository is used for retrieving/saving domain entities
|
||||
* */
|
||||
@@ -91,39 +120,50 @@ export class AdRepository
|
||||
);
|
||||
}
|
||||
|
||||
getCandidates = async (query: MatchQuery): Promise<Candidate[]> => {
|
||||
// let candidates: Candidate[] = [];
|
||||
const sqlQueries: QueryRole[] = [];
|
||||
if (query.driver)
|
||||
sqlQueries.push({
|
||||
query: AdSelector.select(Role.DRIVER, query),
|
||||
role: Role.DRIVER,
|
||||
});
|
||||
if (query.passenger)
|
||||
sqlQueries.push({
|
||||
query: AdSelector.select(Role.PASSENGER, query),
|
||||
role: Role.PASSENGER,
|
||||
});
|
||||
const results = await Promise.all(
|
||||
sqlQueries.map(
|
||||
async (queryRole: QueryRole) =>
|
||||
({
|
||||
ads: (await this.queryRawUnsafe(queryRole.query)) as AdEntity[],
|
||||
role: queryRole.role,
|
||||
} as AdsRole),
|
||||
),
|
||||
getCandidates = async (queryString: string): Promise<AdReadModel[]> =>
|
||||
this.toReadModels((await this.queryRawUnsafe(queryString)) as RawAdModel[]);
|
||||
|
||||
private toReadModels = (rawAds: RawAdModel[]): AdReadModel[] => {
|
||||
const rawAdReadModels: RawAdReadModel[] = rawAds.map(
|
||||
(rawAd: RawAdModel) => ({
|
||||
uuid: rawAd.uuid,
|
||||
driver: rawAd.driver,
|
||||
passenger: rawAd.passenger,
|
||||
frequency: rawAd.frequency,
|
||||
fromDate: rawAd.fromDate,
|
||||
toDate: rawAd.toDate,
|
||||
schedule: [
|
||||
{
|
||||
day: rawAd.day,
|
||||
time: rawAd.time,
|
||||
margin: rawAd.margin,
|
||||
},
|
||||
],
|
||||
seatsProposed: rawAd.seatsProposed,
|
||||
seatsRequested: rawAd.seatsRequested,
|
||||
strict: rawAd.strict,
|
||||
driverDuration: rawAd.driverDuration,
|
||||
driverDistance: rawAd.driverDistance,
|
||||
passengerDuration: rawAd.passengerDuration,
|
||||
passengerDistance: rawAd.passengerDistance,
|
||||
fwdAzimuth: rawAd.fwdAzimuth,
|
||||
backAzimuth: rawAd.backAzimuth,
|
||||
waypoints: rawAd.waypoints,
|
||||
createdAt: rawAd.createdAt,
|
||||
updatedAt: rawAd.updatedAt,
|
||||
}),
|
||||
);
|
||||
// console.log(results[0].ads);
|
||||
return [];
|
||||
const adReadModels: AdReadModel[] = [];
|
||||
rawAdReadModels.forEach((adReadModel: AdReadModel) => {
|
||||
const ad: AdReadModel | undefined = adReadModels.find(
|
||||
(arm: AdReadModel) => arm.uuid == adReadModel.uuid,
|
||||
);
|
||||
if (ad) {
|
||||
ad.schedule.push(...adReadModel.schedule);
|
||||
} else {
|
||||
adReadModels.push(adReadModel);
|
||||
}
|
||||
});
|
||||
return adReadModels;
|
||||
};
|
||||
}
|
||||
|
||||
type QueryRole = {
|
||||
query: string;
|
||||
role: Role;
|
||||
};
|
||||
|
||||
type AdsRole = {
|
||||
ads: AdEntity[];
|
||||
role: Role;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user