add quantity cap for GENERAL contracts and implement capacity tracking

- Updated ContractClearanceService and ContractsController to remove region parameter from queue method.
- Enhanced ContractsRepository to attach contract files for download and added attachContractFiles method.
- Modified ContractsService to persist cargo scope with quantity cap based on contract kind.
- Introduced quantityCap field in CreateContractCargoScopeDto and ContractCargoScope entity.
- Implemented capacity tracking in the frontend with ContractCapacityNotice component to display remaining bookable quantities.
- Updated various components and services to support new capacity features, including hooks and API calls.
- Added migration to include quantity_cap column in contract_cargo_scope table.
This commit is contained in:
Marshal
2026-06-28 17:32:18 +00:00
parent e1d54746c2
commit 11c7f1bb74
27 changed files with 512 additions and 74 deletions

View File

@@ -1,7 +1,7 @@
import { BaseRepository } from '@edr/api-common';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { DataSource, IsNull, Repository, SelectQueryBuilder } from 'typeorm';
import { DataSource, In, IsNull, Repository, SelectQueryBuilder } from 'typeorm';
import { FileRecord } from '../files/entities/file.entity';
import { Contract } from './entities/contract.entity';
@@ -132,6 +132,10 @@ export class ContractsRepository extends BaseRepository<Contract> {
.take(pageSize)
.getManyAndCount();
// Attach the generated contract PDF to each row so list/home can offer a
// direct download. Loaded separately to keep pagination counts correct.
await this.attachContractFiles(items);
const totalPages = pageSize > 0 ? Math.ceil(total / pageSize) : 0;
return {
items,
@@ -147,6 +151,28 @@ export class ContractsRepository extends BaseRepository<Contract> {
};
}
/**
* Load contract-resource files for the given contracts and attach them to
* `contract.files`. Kept separate from the paginated query so the one-to-many
* join doesn't inflate the page count.
*/
private async attachContractFiles(contracts: Contract[]): Promise<void> {
if (contracts.length === 0) return;
const ids = contracts.map((c) => c.id);
const files = await this.dataSource.getRepository(FileRecord).find({
where: { resource: 'contracts', resourceId: In(ids), deletedAt: IsNull() },
});
const byContract = new Map<string, FileRecord[]>();
for (const file of files) {
const list = byContract.get(file.resourceId) ?? [];
list.push(file);
byContract.set(file.resourceId, list);
}
for (const contract of contracts) {
contract.files = byContract.get(contract.id) ?? [];
}
}
async getStatusCounts(): Promise<Record<string, number>> {
const rows = await this.repository
.createQueryBuilder('contract')