Fix price inconsistency

This commit is contained in:
Roba Boru
2026-07-19 22:46:36 +03:00
parent 996c0d8896
commit d43229528e
6 changed files with 153 additions and 108 deletions

View File

@@ -595,10 +595,20 @@ export class SearchService {
});
const freeChildrenAllowed = groupFare.freeChildrenCount;
// Calculate per-passenger fare rate (engine called with 1 adult, 0 children — pure rate lookup)
// Pre-compute isFree per passenger index synchronously so the race-free
// counter assignment isn't corrupted by concurrent Promise.all resolution.
let freeChildrenUsed = 0;
const isFreeByIndex = categorised.map(p => {
if (p.category === 'CHILD' && freeChildrenUsed < freeChildrenAllowed) {
freeChildrenUsed++;
return true;
}
return false;
});
// Calculate per-passenger fare rate (engine called with 1 adult, 0 children — pure rate lookup)
const passengerLines = await Promise.all(
categorised.map(async (p) => {
categorised.map(async (p, idx) => {
const fare = await this.fareEngine.calculate({
routeId: schedule.routeId!,
originStationId: dto.originStationId,
@@ -610,8 +620,7 @@ export class SearchService {
childCount: 0,
});
const isFree = p.category === 'CHILD' && freeChildrenUsed < freeChildrenAllowed;
if (isFree) freeChildrenUsed++;
const isFree = isFreeByIndex[idx];
const fareMinor = isFree
? fare.premiumPerPassenger + fare.insurancePerPassenger