mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-30 04:08:11 +00:00
per-user trade-direction access scope
This commit is contained in:
@@ -22,6 +22,7 @@ import {
|
||||
IYardsRepository,
|
||||
YARDS_REPOSITORY,
|
||||
} from "../rule-engine/interfaces/yards.repository.interface";
|
||||
import { YardFacilitiesService } from "../rule-engine/services/yard-facilities.service";
|
||||
import {
|
||||
BookingReferenceCargoTypeChildDto,
|
||||
BookingReferenceCargoTypeGroupDto,
|
||||
@@ -170,11 +171,18 @@ export class BookingReferenceDataService {
|
||||
private readonly shippingLinesRepository: IShippingLinesRepository,
|
||||
@Inject(CARGO_TYPES_REPOSITORY)
|
||||
private readonly cargoTypesRepository: ICargoTypesRepository,
|
||||
private readonly yardFacilitiesService: YardFacilitiesService,
|
||||
) { }
|
||||
|
||||
async getReferenceData(): Promise<BookingReferenceDataDto> {
|
||||
const [yards, containerTypes, serviceTypes, shippingLines, cargoTypes] =
|
||||
await Promise.all([
|
||||
const [
|
||||
yards,
|
||||
containerTypes,
|
||||
serviceTypes,
|
||||
shippingLines,
|
||||
cargoTypes,
|
||||
facilityYards,
|
||||
] = await Promise.all([
|
||||
this.yardsRepository.findAll({
|
||||
where: { isActive: true },
|
||||
order: { displayOrder: "ASC", code: "ASC" },
|
||||
@@ -195,17 +203,30 @@ export class BookingReferenceDataService {
|
||||
where: { isActive: true },
|
||||
order: { displayOrder: "ASC", code: "ASC" },
|
||||
}),
|
||||
this.yardFacilitiesService.listFacilityYards(),
|
||||
]);
|
||||
|
||||
// Every active yard is still listed; a yard with no facility record simply
|
||||
// reports no capability, so the forms drop it from the pickers themselves.
|
||||
const facilityByYardId = new Map(facilityYards.map((f) => [f.yardId, f]));
|
||||
|
||||
return {
|
||||
yard: yards.map(
|
||||
(y): BookingReferenceYardDto => ({
|
||||
yard: yards.map((y): BookingReferenceYardDto => {
|
||||
const facility = facilityByYardId.get(y.id);
|
||||
return {
|
||||
id: y.id,
|
||||
name: y.label,
|
||||
code: y.code,
|
||||
country: y.country,
|
||||
}),
|
||||
),
|
||||
hasContainerFacilityOrigin:
|
||||
facility?.hasContainerFacilityOrigin ?? false,
|
||||
hasBulkFacilityOrigin: facility?.hasBulkFacilityOrigin ?? false,
|
||||
hasContainerFacilityDestination:
|
||||
facility?.hasContainerFacilityDestination ?? false,
|
||||
hasBulkFacilityDestination:
|
||||
facility?.hasBulkFacilityDestination ?? false,
|
||||
};
|
||||
}),
|
||||
containers: groupContainersBySize(containerTypes),
|
||||
service: serviceTypes.map(
|
||||
(s): BookingReferenceServiceDto => ({
|
||||
|
||||
@@ -43,6 +43,8 @@ import {
|
||||
RoAmendmentDto,
|
||||
} from '../contracts/dto/phased-clearance.dto';
|
||||
import { BookingReferenceDataService } from './booking-reference-data.service';
|
||||
import { scopedDirections } from '../user-trade-access/trade-scope.util';
|
||||
import { UserTradeAccessService } from '../user-trade-access/user-trade-access.service';
|
||||
import { BookingsService } from './bookings.service';
|
||||
import { BookingReferenceDataDto } from './dto/booking-reference-data.dto';
|
||||
import { CreateBookingDto } from './dto/create-booking.dto';
|
||||
@@ -150,6 +152,7 @@ export class BookingsController {
|
||||
private readonly containerReceiptService: ContainerReceiptService,
|
||||
private readonly firstMileService: FirstMileService,
|
||||
private readonly lastMileService: LastMileService,
|
||||
private readonly userTradeAccessService: UserTradeAccessService,
|
||||
) {}
|
||||
|
||||
@Post()
|
||||
@@ -217,7 +220,16 @@ export class BookingsController {
|
||||
// Staff (backoffice) see every booking. Customers (portal) are always
|
||||
// force-scoped to their own company, regardless of any companyId they pass.
|
||||
if (hasFreightPermission(user, FREIGHT_PERMS.bookings.view)) {
|
||||
return this.bookingsService.findAll(filter);
|
||||
// Per-user trade-direction scope (import/export/intercity checkboxes).
|
||||
const allowed =
|
||||
await this.userTradeAccessService.resolveAllowedDirections(user);
|
||||
const dirs = scopedDirections(allowed, filter.tradeDirection);
|
||||
return this.bookingsService.findAll(
|
||||
filter,
|
||||
undefined,
|
||||
undefined,
|
||||
dirs ?? undefined,
|
||||
);
|
||||
}
|
||||
// Global Logistics has clearance:view but NOT bookings:view — it is scoped
|
||||
// to the customs document-clearance queue only and never sees the general
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Module, forwardRef } from "@nestjs/common";
|
||||
import { UserTradeAccessModule } from "../user-trade-access/user-trade-access.module";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { TypeOrmModule } from "@nestjs/typeorm";
|
||||
import { ExchangeModule, ExchangeOptions } from "@edr/api-common";
|
||||
@@ -80,6 +81,7 @@ import { VehiclesModule } from "../vehicles/vehicles.module";
|
||||
MinioModule,
|
||||
VehiclesModule,
|
||||
CompaniesModule,
|
||||
UserTradeAccessModule,
|
||||
// CustomersModule,
|
||||
RuleEngineModule,
|
||||
FileUploadSettingsModule,
|
||||
|
||||
@@ -17,6 +17,7 @@ import { ContainerType } from '../rule-engine/entities/container-type.entity';
|
||||
import { Contract } from '../contracts/entities/contract.entity';
|
||||
import { ContractRateSnapshot } from '../contracts/entities/contract-rate-snapshot.entity';
|
||||
import { ContractRoute } from '../contracts/entities/contract-route.entity';
|
||||
import { applyDirectionScope } from '../user-trade-access/trade-scope.util';
|
||||
import { BookingCargoModifier } from './entities/booking-cargo-modifier.entity';
|
||||
import {
|
||||
BookingDocumentReview,
|
||||
@@ -64,6 +65,8 @@ export interface BookingListFilterOptions {
|
||||
freightType?: string;
|
||||
bookingType?: string;
|
||||
tradeDirection?: string;
|
||||
/** Per-user trade-direction scope — `[]` matches nothing. */
|
||||
tradeDirections?: string[];
|
||||
paymentCurrency?: string;
|
||||
paymentStatus?: string;
|
||||
excludePaymentStatus?: string;
|
||||
@@ -1000,6 +1003,9 @@ export class BookingsRepository extends BaseRepository<Booking> {
|
||||
tradeDirection: options.tradeDirection,
|
||||
});
|
||||
}
|
||||
if (options.tradeDirections) {
|
||||
applyDirectionScope(qb, 'booking.trade_direction', options.tradeDirections);
|
||||
}
|
||||
if (options.paymentCurrency) {
|
||||
qb.andWhere('booking.payment_currency = :paymentCurrency', {
|
||||
paymentCurrency: options.paymentCurrency,
|
||||
|
||||
@@ -1619,6 +1619,7 @@ export class BookingsService {
|
||||
filter: FilterBookingDto,
|
||||
forceCompanyId?: string,
|
||||
forceCompanyProfileId?: string,
|
||||
tradeDirections?: string[],
|
||||
): Promise<PaginatedBookings> {
|
||||
const page = filter.page ?? 1;
|
||||
const pageSize = filter.pageSize ?? 20;
|
||||
@@ -1638,6 +1639,7 @@ export class BookingsService {
|
||||
// ANDs both, so cross-company access is impossible.
|
||||
companyId: forceCompanyId ?? filter.companyId,
|
||||
companyProfileId: forceCompanyProfileId ?? filter.companyProfileId,
|
||||
tradeDirections,
|
||||
contractType: filter.contractType,
|
||||
serviceTypeId: filter.serviceTypeId,
|
||||
cargoTypeId: filter.cargoTypeId,
|
||||
|
||||
@@ -13,6 +13,18 @@ export class BookingReferenceYardDto {
|
||||
|
||||
@ApiProperty({ example: 'Ethiopia' })
|
||||
country!: string;
|
||||
|
||||
@ApiProperty({ description: 'Can load containers onto a train here.' })
|
||||
hasContainerFacilityOrigin!: boolean;
|
||||
|
||||
@ApiProperty({ description: 'Can load bulk cargo onto a train here.' })
|
||||
hasBulkFacilityOrigin!: boolean;
|
||||
|
||||
@ApiProperty({ description: 'Can receive containers off a train here.' })
|
||||
hasContainerFacilityDestination!: boolean;
|
||||
|
||||
@ApiProperty({ description: 'Can receive bulk cargo off a train here.' })
|
||||
hasBulkFacilityDestination!: boolean;
|
||||
}
|
||||
|
||||
export class BookingReferenceContainerTypeDto {
|
||||
|
||||
Reference in New Issue
Block a user