fix search

This commit is contained in:
natib21
2026-07-17 15:01:30 +00:00
parent a29689772b
commit 6ee771c953
8 changed files with 89 additions and 28 deletions

View File

@@ -29,6 +29,13 @@ export class ListWagonsQueryDto {
@IsUUID()
trainId?: string;
@ApiPropertyOptional({
description: 'Filter by run number — matches export OR import run (e.g. 8001).',
})
@IsOptional()
@IsString()
trainNumber?: string;
@ApiPropertyOptional({ default: 'wagonNumber' })
@IsOptional()
@IsString()

View File

@@ -6,7 +6,7 @@ import {
ConflictException,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, DataSource, FindOptionsOrder, FindOptionsWhere, ILike, In } from 'typeorm';
import { Repository, DataSource, In } from 'typeorm';
import { CreateWagonDto } from './dto/create-wagon.dto';
import { ListWagonsQueryDto } from './dto/list-wagons-query.dto';
import { UpdateWagonDto } from './dto/update-wagon.dto';
@@ -44,22 +44,41 @@ export class WagonsService {
}
async findAll(query: ListWagonsQueryDto = {}): Promise<Wagon[]> {
const where: FindOptionsWhere<Wagon>[] | FindOptionsWhere<Wagon> = [];
const search = query.search?.trim();
const trainId = query.trainId?.trim();
const wagonTypeId = query.wagonTypeId?.trim();
const filters: FindOptionsWhere<Wagon> = {
...(query.status ? { status: query.status } : {}),
...(query.currentYardId ? { currentYardId: query.currentYardId } : {}),
...(trainId ? { trainId } : {}),
...(wagonTypeId ? { wagonTypeId } : {}),
};
const trainNumber = query.trainNumber?.trim();
// QueryBuilder (not find) because both search and the trainNumber filter span
// two columns each (export/import run) — an OR that FindOptions cannot express
// without cross-producting into conflicting branches. Soft-deleted rows are
// still excluded automatically (BaseEntity's @DeleteDateColumn).
const qb = this.wagonRepo
.createQueryBuilder('w')
.leftJoinAndSelect('w.currentYard', 'currentYard')
.leftJoinAndSelect('w.wagonType', 'wagonType');
if (query.status) qb.andWhere('w.status = :status', { status: query.status });
if (query.currentYardId)
qb.andWhere('w.currentYardId = :currentYardId', { currentYardId: query.currentYardId });
if (trainId) qb.andWhere('w.trainId = :trainId', { trainId });
if (wagonTypeId) qb.andWhere('w.wagonTypeId = :wagonTypeId', { wagonTypeId });
// Filter by run: the odd export run identifies the pair, so match either
// column — a wagon carries export on one, import on the other.
if (trainNumber) {
qb.andWhere(
'(w.exportTrainNumber = :trainNumber OR w.importTrainNumber = :trainNumber)',
{ trainNumber },
);
}
// Search matches the wagon number or either run number.
if (search) {
where.push({
wagonNumber: ILike(`%${search}%`),
...filters,
});
qb.andWhere(
'(w.wagonNumber ILIKE :search OR w.exportTrainNumber ILIKE :search OR w.importTrainNumber ILIKE :search)',
{ search: `%${search}%` },
);
}
// Spec columns (tare, payload) are no longer sortable here — they live on the
@@ -75,14 +94,14 @@ export class WagonsService {
? (query.sortBy as keyof Wagon)
: 'wagonNumber';
const sortOrder = query.sortOrder?.toUpperCase() === 'DESC' ? 'DESC' : 'ASC';
qb.orderBy(`w.${sortBy}`, sortOrder);
return this.wagonRepo.find({
where: search ? where : filters,
relations: { currentYard: true, wagonType: true },
order: { [sortBy]: sortOrder } as FindOptionsOrder<Wagon>,
skip: query.page && query.limit ? (Number(query.page) - 1) * Number(query.limit) : undefined,
take: query.limit ? Number(query.limit) : undefined,
});
if (query.page && query.limit) {
qb.skip((Number(query.page) - 1) * Number(query.limit));
}
if (query.limit) qb.take(Number(query.limit));
return qb.getMany();
}
async findById(id: string): Promise<Wagon> {