mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-28 05:30:55 +00:00
add hard capacity ceiling to weight limit rules
This commit is contained in:
@@ -1,4 +1,10 @@
|
||||
import { ConflictException, Inject, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import {
|
||||
BadRequestException,
|
||||
ConflictException,
|
||||
Inject,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { CreateWeightLimitRuleDto } from '../dto/create-weight-limit-rule.dto';
|
||||
import { UpdateWeightLimitRuleDto } from '../dto/update-weight-limit-rule.dto';
|
||||
import { WeightLimitRule } from '../entities/weight-limit-rule.entity';
|
||||
@@ -62,13 +68,31 @@ export class WeightLimitRulesService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Capacity is the hard ceiling; the VGM limit is the soft overweight
|
||||
* threshold. A ceiling below the threshold would make every overweight
|
||||
* booking impossible to create, which is never what the operator means.
|
||||
*/
|
||||
private assertCapacityAboveVgmLimit(
|
||||
maxVgmTons: number,
|
||||
maxCapacityTons: number | null | undefined,
|
||||
): void {
|
||||
if (maxCapacityTons != null && Number(maxCapacityTons) < Number(maxVgmTons)) {
|
||||
throw new BadRequestException(
|
||||
'Max capacity must be greater than or equal to the max VGM limit.',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** Create a new weight limit rule. */
|
||||
async create(dto: CreateWeightLimitRuleDto): Promise<WeightLimitRule> {
|
||||
await this.assertNoDuplicate(dto.containerTypeId, dto.tradeDirection);
|
||||
this.assertCapacityAboveVgmLimit(dto.maxVgmTons, dto.maxCapacityTons);
|
||||
return this.repository.create({
|
||||
containerTypeId: dto.containerTypeId,
|
||||
tradeDirection: dto.tradeDirection,
|
||||
maxVgmTons: dto.maxVgmTons,
|
||||
maxCapacityTons: dto.maxCapacityTons ?? null,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -79,6 +103,12 @@ export class WeightLimitRulesService {
|
||||
if (dto.containerTypeId !== undefined) patch.containerTypeId = dto.containerTypeId;
|
||||
if (dto.tradeDirection !== undefined) patch.tradeDirection = dto.tradeDirection;
|
||||
if (dto.maxVgmTons !== undefined) patch.maxVgmTons = dto.maxVgmTons;
|
||||
if (dto.maxCapacityTons !== undefined) patch.maxCapacityTons = dto.maxCapacityTons;
|
||||
|
||||
this.assertCapacityAboveVgmLimit(
|
||||
patch.maxVgmTons ?? Number(existing.maxVgmTons),
|
||||
patch.maxCapacityTons !== undefined ? patch.maxCapacityTons : existing.maxCapacityTons,
|
||||
);
|
||||
|
||||
// Re-check uniqueness when the identity (container/direction) changes.
|
||||
if (dto.containerTypeId !== undefined || dto.tradeDirection !== undefined) {
|
||||
|
||||
Reference in New Issue
Block a user