Generate filtering/sorting and searchable dropdowns
Includes backend q search + generated list UX from DSL.
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
"test:cov": "jest --coverage",
|
||||
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
|
||||
"test:e2e": "jest --config ./test/jest-e2e.json",
|
||||
"generate:from-dsl": "node ../generation/generate.mjs --apply --dsl examples/TOiR.domain.dsl",
|
||||
"postinstall": "prisma generate"
|
||||
},
|
||||
"prisma": {
|
||||
|
||||
@@ -32,43 +32,43 @@ enum RepairOrderStatus {
|
||||
}
|
||||
|
||||
model EquipmentType {
|
||||
code String @id
|
||||
name String
|
||||
manufacturer String?
|
||||
code String @id
|
||||
name String
|
||||
manufacturer String?
|
||||
maintenanceIntervalHours Int?
|
||||
overhaulIntervalHours Int?
|
||||
equipment Equipment[]
|
||||
overhaulIntervalHours Int?
|
||||
equipment Equipment[]
|
||||
}
|
||||
|
||||
model Equipment {
|
||||
id String @id @default(uuid())
|
||||
inventoryNumber String @unique
|
||||
serialNumber String?
|
||||
name String
|
||||
equipmentTypeCode String
|
||||
equipmentType EquipmentType @relation(fields: [equipmentTypeCode], references: [code])
|
||||
status EquipmentStatus @default(Active)
|
||||
location String?
|
||||
commissionedAt DateTime?
|
||||
totalEngineHours Decimal?
|
||||
engineHoursSinceLastRepair Decimal?
|
||||
lastRepairAt DateTime?
|
||||
notes String?
|
||||
repairOrders RepairOrder[]
|
||||
id String @id @default(uuid())
|
||||
inventoryNumber String @unique
|
||||
serialNumber String?
|
||||
name String
|
||||
equipmentTypeCode String
|
||||
status EquipmentStatus @default(Active)
|
||||
location String?
|
||||
commissionedAt DateTime?
|
||||
totalEngineHours Decimal?
|
||||
engineHoursSinceLastRepair Decimal?
|
||||
lastRepairAt DateTime?
|
||||
notes String?
|
||||
equipmentType EquipmentType @relation(fields: [equipmentTypeCode], references: [code])
|
||||
repairOrders RepairOrder[]
|
||||
}
|
||||
|
||||
model RepairOrder {
|
||||
id String @id @default(uuid())
|
||||
number String @unique
|
||||
equipmentId String
|
||||
equipment Equipment @relation(fields: [equipmentId], references: [id])
|
||||
repairKind RepairKind
|
||||
status RepairOrderStatus @default(Draft)
|
||||
plannedAt DateTime
|
||||
startedAt DateTime?
|
||||
completedAt DateTime?
|
||||
contractor String?
|
||||
engineHoursAtRepair Decimal?
|
||||
description String?
|
||||
notes String?
|
||||
id String @id @default(uuid())
|
||||
number String @unique
|
||||
equipmentId String
|
||||
repairKind RepairKind
|
||||
status RepairOrderStatus @default(Draft)
|
||||
plannedAt DateTime
|
||||
startedAt DateTime?
|
||||
completedAt DateTime?
|
||||
contractor String?
|
||||
engineHoursAtRepair Decimal?
|
||||
description String?
|
||||
notes String?
|
||||
equipment Equipment @relation(fields: [equipmentId], references: [id])
|
||||
}
|
||||
|
||||
@@ -3,89 +3,161 @@ import { PrismaClient } from '@prisma/client';
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function main() {
|
||||
const equipmentType = await prisma.equipmentType.upsert({
|
||||
where: { code: 'pump' },
|
||||
update: {},
|
||||
create: {
|
||||
const equipmentTypes = [
|
||||
{
|
||||
code: 'pump',
|
||||
name: 'Насосный агрегат',
|
||||
manufacturer: 'АО НасосПром',
|
||||
maintenanceIntervalHours: 2000,
|
||||
overhaulIntervalHours: 16000,
|
||||
},
|
||||
});
|
||||
|
||||
const equipmentType2 = await prisma.equipmentType.upsert({
|
||||
where: { code: 'compressor' },
|
||||
update: {},
|
||||
create: {
|
||||
{
|
||||
code: 'compressor',
|
||||
name: 'Компрессорная установка',
|
||||
manufacturer: 'ОАО Компрессормаш',
|
||||
maintenanceIntervalHours: 1500,
|
||||
overhaulIntervalHours: 12000,
|
||||
},
|
||||
});
|
||||
|
||||
const equipment = await prisma.equipment.upsert({
|
||||
where: { inventoryNumber: 'INV-001' },
|
||||
update: {},
|
||||
create: {
|
||||
inventoryNumber: 'INV-001',
|
||||
serialNumber: 'SN-2024-0001',
|
||||
name: 'Насос ЦНС 180-212',
|
||||
equipmentTypeCode: 'pump',
|
||||
status: 'Active',
|
||||
location: 'Куст №5, скважина 42',
|
||||
commissionedAt: new Date('2023-06-15'),
|
||||
totalEngineHours: 4500,
|
||||
engineHoursSinceLastRepair: 1200,
|
||||
{
|
||||
code: 'generator',
|
||||
name: 'Дизель-генератор',
|
||||
manufacturer: 'АО ЭнергоМаш',
|
||||
maintenanceIntervalHours: 500,
|
||||
overhaulIntervalHours: 6000,
|
||||
},
|
||||
});
|
||||
|
||||
const equipment2 = await prisma.equipment.upsert({
|
||||
where: { inventoryNumber: 'INV-002' },
|
||||
update: {},
|
||||
create: {
|
||||
inventoryNumber: 'INV-002',
|
||||
serialNumber: 'SN-2024-0002',
|
||||
name: 'Компрессор 4ВМ10-120/9',
|
||||
equipmentTypeCode: 'compressor',
|
||||
status: 'Active',
|
||||
location: 'ГКС-3',
|
||||
commissionedAt: new Date('2022-03-10'),
|
||||
totalEngineHours: 8200,
|
||||
engineHoursSinceLastRepair: 800,
|
||||
{
|
||||
code: 'valve',
|
||||
name: 'Запорная арматура',
|
||||
manufacturer: 'ЗАО АрматурПром',
|
||||
maintenanceIntervalHours: 1000,
|
||||
overhaulIntervalHours: 10000,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.repairOrder.upsert({
|
||||
where: { number: 'RO-2026-001' },
|
||||
update: {},
|
||||
create: {
|
||||
number: 'RO-2026-001',
|
||||
equipmentId: equipment.id,
|
||||
repairKind: 'TO',
|
||||
status: 'Approved',
|
||||
plannedAt: new Date('2026-04-01'),
|
||||
contractor: 'ООО СервисРемонт',
|
||||
engineHoursAtRepair: 4500,
|
||||
description: 'Плановое техническое обслуживание насосного агрегата',
|
||||
{
|
||||
code: 'sensor',
|
||||
name: 'Датчик давления',
|
||||
manufacturer: 'ООО ПриборСервис',
|
||||
maintenanceIntervalHours: 800,
|
||||
overhaulIntervalHours: 8000,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.repairOrder.upsert({
|
||||
where: { number: 'RO-2026-002' },
|
||||
update: {},
|
||||
create: {
|
||||
number: 'RO-2026-002',
|
||||
equipmentId: equipment2.id,
|
||||
repairKind: 'TR',
|
||||
status: 'Draft',
|
||||
plannedAt: new Date('2026-05-15'),
|
||||
description: 'Текущий ремонт компрессорной установки',
|
||||
{
|
||||
code: 'motor',
|
||||
name: 'Электродвигатель',
|
||||
manufacturer: 'ПАО ЭлектроМотор',
|
||||
maintenanceIntervalHours: 1200,
|
||||
overhaulIntervalHours: 14000,
|
||||
},
|
||||
});
|
||||
{
|
||||
code: 'fan',
|
||||
name: 'Вентилятор',
|
||||
manufacturer: 'АО ВентПром',
|
||||
maintenanceIntervalHours: 700,
|
||||
overhaulIntervalHours: 9000,
|
||||
},
|
||||
{
|
||||
code: 'heat-exchanger',
|
||||
name: 'Теплообменник',
|
||||
manufacturer: 'ОАО ТеплоТех',
|
||||
maintenanceIntervalHours: 1800,
|
||||
overhaulIntervalHours: 15000,
|
||||
},
|
||||
{
|
||||
code: 'filter',
|
||||
name: 'Фильтровальная установка',
|
||||
manufacturer: 'ООО ФильтрТех',
|
||||
maintenanceIntervalHours: 600,
|
||||
overhaulIntervalHours: 7000,
|
||||
},
|
||||
{
|
||||
code: 'separator',
|
||||
name: 'Сепаратор',
|
||||
manufacturer: 'АО СепараторМаш',
|
||||
maintenanceIntervalHours: 1600,
|
||||
overhaulIntervalHours: 13000,
|
||||
},
|
||||
{
|
||||
code: 'transformer',
|
||||
name: 'Трансформатор',
|
||||
manufacturer: 'ПАО ТрансЭнерго',
|
||||
maintenanceIntervalHours: 2500,
|
||||
overhaulIntervalHours: 20000,
|
||||
},
|
||||
] as const;
|
||||
|
||||
for (const type of equipmentTypes) {
|
||||
await prisma.equipmentType.upsert({
|
||||
where: { code: type.code },
|
||||
update: { ...type },
|
||||
create: { ...type },
|
||||
});
|
||||
}
|
||||
|
||||
const equipmentRecords: { id: string; inventoryNumber: string; name: string }[] = [];
|
||||
for (let i = 1; i <= 11; i++) {
|
||||
const type = equipmentTypes[(i - 1) % equipmentTypes.length];
|
||||
const inventoryNumber = `INV-${String(i).padStart(3, '0')}`;
|
||||
const serialNumber = `SN-2026-${String(i).padStart(4, '0')}`;
|
||||
const record = await prisma.equipment.upsert({
|
||||
where: { inventoryNumber },
|
||||
update: {
|
||||
serialNumber,
|
||||
name: `${type.name} #${i}`,
|
||||
equipmentTypeCode: type.code,
|
||||
status: i % 5 === 0 ? 'Repair' : 'Active',
|
||||
location: i % 2 === 0 ? `Площадка ${Math.ceil(i / 2)}` : `Цех ${Math.ceil(i / 3)}`,
|
||||
commissionedAt: new Date(2022, (i - 1) % 12, 1 + ((i - 1) % 28)),
|
||||
totalEngineHours: 1000 + i * 350,
|
||||
engineHoursSinceLastRepair: 200 + i * 25,
|
||||
},
|
||||
create: {
|
||||
inventoryNumber,
|
||||
serialNumber,
|
||||
name: `${type.name} #${i}`,
|
||||
equipmentTypeCode: type.code,
|
||||
status: i % 5 === 0 ? 'Repair' : 'Active',
|
||||
location: i % 2 === 0 ? `Площадка ${Math.ceil(i / 2)}` : `Цех ${Math.ceil(i / 3)}`,
|
||||
commissionedAt: new Date(2022, (i - 1) % 12, 1 + ((i - 1) % 28)),
|
||||
totalEngineHours: 1000 + i * 350,
|
||||
engineHoursSinceLastRepair: 200 + i * 25,
|
||||
},
|
||||
});
|
||||
equipmentRecords.push({ id: record.id, inventoryNumber: record.inventoryNumber, name: record.name });
|
||||
}
|
||||
|
||||
const repairKinds = ['TO', 'TR', 'TRE', 'KR', 'AR', 'MP'] as const;
|
||||
const statuses = ['Draft', 'Approved', 'InWork', 'Done', 'Cancelled'] as const;
|
||||
|
||||
for (let i = 1; i <= 11; i++) {
|
||||
const number = `RO-2026-${String(i).padStart(3, '0')}`;
|
||||
const equipment = equipmentRecords[(i - 1) % equipmentRecords.length];
|
||||
await prisma.repairOrder.upsert({
|
||||
where: { number },
|
||||
update: {
|
||||
equipmentId: equipment.id,
|
||||
repairKind: repairKinds[(i - 1) % repairKinds.length],
|
||||
status: statuses[(i - 1) % statuses.length],
|
||||
plannedAt: new Date(2026, ((i - 1) % 12), 1 + ((i - 1) % 28)),
|
||||
startedAt: i % 4 === 0 ? new Date(2026, ((i - 1) % 12), 2 + ((i - 1) % 28)) : null,
|
||||
completedAt: i % 5 === 0 ? new Date(2026, ((i - 1) % 12), 5 + ((i - 1) % 28)) : null,
|
||||
contractor: i % 3 === 0 ? 'ООО СервисРемонт' : 'АО ТехПодряд',
|
||||
engineHoursAtRepair: 1000 + i * 350,
|
||||
description: `Заявка на ремонт ${equipment.inventoryNumber} (${equipment.name})`,
|
||||
notes: i % 2 === 0 ? 'Тестовая заметка' : null,
|
||||
},
|
||||
create: {
|
||||
number,
|
||||
equipmentId: equipment.id,
|
||||
repairKind: repairKinds[(i - 1) % repairKinds.length],
|
||||
status: statuses[(i - 1) % statuses.length],
|
||||
plannedAt: new Date(2026, ((i - 1) % 12), 1 + ((i - 1) % 28)),
|
||||
startedAt: i % 4 === 0 ? new Date(2026, ((i - 1) % 12), 2 + ((i - 1) % 28)) : null,
|
||||
completedAt: i % 5 === 0 ? new Date(2026, ((i - 1) % 12), 5 + ((i - 1) % 28)) : null,
|
||||
contractor: i % 3 === 0 ? 'ООО СервисРемонт' : 'АО ТехПодряд',
|
||||
engineHoursAtRepair: 1000 + i * 350,
|
||||
description: `Заявка на ремонт ${equipment.inventoryNumber} (${equipment.name})`,
|
||||
notes: i % 2 === 0 ? 'Тестовая заметка' : null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
console.log('Seed data created successfully');
|
||||
}
|
||||
|
||||
@@ -7,8 +7,7 @@ import { EquipmentModule } from './modules/equipment/equipment.module';
|
||||
import { RepairOrderModule } from './modules/repair-order/repair-order.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({ isGlobal: true }),
|
||||
imports: [ConfigModule.forRoot({ isGlobal: true }),
|
||||
PrismaModule,
|
||||
HealthModule,
|
||||
EquipmentTypeModule,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
export class CreateEquipmentTypeDto {
|
||||
code: string;
|
||||
name: string;
|
||||
code?: string;
|
||||
name!: string;
|
||||
manufacturer?: string;
|
||||
maintenanceIntervalHours?: number;
|
||||
overhaulIntervalHours?: number;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
export class UpdateEquipmentTypeDto {
|
||||
name?: string;
|
||||
manufacturer?: string;
|
||||
maintenanceIntervalHours?: number;
|
||||
overhaulIntervalHours?: number;
|
||||
id?: string;
|
||||
code?: string | null;
|
||||
name?: string | null;
|
||||
manufacturer?: string | null;
|
||||
maintenanceIntervalHours?: number | null;
|
||||
overhaulIntervalHours?: number | null;
|
||||
}
|
||||
|
||||
@@ -6,33 +6,33 @@ import { UpdateEquipmentTypeDto } from './dto/update-equipment-type.dto';
|
||||
|
||||
@Controller('equipment-types')
|
||||
export class EquipmentTypeController {
|
||||
constructor(private readonly equipmentTypeService: EquipmentTypeService) {}
|
||||
constructor(private readonly service: EquipmentTypeService) {}
|
||||
|
||||
@Get()
|
||||
async findAll(@Query() query: any, @Res() res: Response) {
|
||||
const result = await this.equipmentTypeService.findAll(query);
|
||||
const result = await this.service.findAll(query);
|
||||
res.set('Content-Range', `equipment-types ${query._start || 0}-${query._end || result.total}/${result.total}`);
|
||||
res.set('Access-Control-Expose-Headers', 'Content-Range');
|
||||
return res.json(result.data);
|
||||
}
|
||||
|
||||
@Get(':code')
|
||||
findOne(@Param('code') code: string) {
|
||||
return this.equipmentTypeService.findOne(code);
|
||||
findOne(@Param('code') id: string) {
|
||||
return this.service.findOne(id);
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@Body() dto: CreateEquipmentTypeDto) {
|
||||
return this.equipmentTypeService.create(dto);
|
||||
return this.service.create(dto);
|
||||
}
|
||||
|
||||
@Patch(':code')
|
||||
update(@Param('code') code: string, @Body() dto: UpdateEquipmentTypeDto) {
|
||||
return this.equipmentTypeService.update(code, dto);
|
||||
update(@Param('code') id: string, @Body() dto: UpdateEquipmentTypeDto) {
|
||||
return this.service.update(id, dto);
|
||||
}
|
||||
|
||||
@Delete(':code')
|
||||
remove(@Param('code') code: string) {
|
||||
return this.equipmentTypeService.remove(code);
|
||||
remove(@Param('code') id: string) {
|
||||
return this.service.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { PrismaService } from '../../prisma/prisma.service';
|
||||
import { CreateEquipmentTypeDto } from './dto/create-equipment-type.dto';
|
||||
import { UpdateEquipmentTypeDto } from './dto/update-equipment-type.dto';
|
||||
@@ -7,72 +8,66 @@ import { UpdateEquipmentTypeDto } from './dto/update-equipment-type.dto';
|
||||
export class EquipmentTypeService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async findAll(query: {
|
||||
_start?: string;
|
||||
_end?: string;
|
||||
_sort?: string;
|
||||
_order?: string;
|
||||
[key: string]: any;
|
||||
}) {
|
||||
async findAll(query: { _start?: string; _end?: string; _sort?: string; _order?: string; [key: string]: any }) {
|
||||
const start = parseInt(query._start) || 0;
|
||||
const end = parseInt(query._end) || 10;
|
||||
const take = end - start;
|
||||
const skip = start;
|
||||
const sortField = 'code';
|
||||
const sortField = query._sort || 'code';
|
||||
const sortOrder = (query._order || 'ASC').toLowerCase() as 'asc' | 'desc';
|
||||
|
||||
const where: any = {};
|
||||
|
||||
if (query.q) {
|
||||
const q = String(query.q);
|
||||
const ors: any[] = [];
|
||||
ors.push({ code: { contains: q, mode: 'insensitive' } });
|
||||
ors.push({ name: { contains: q, mode: 'insensitive' } });
|
||||
ors.push({ manufacturer: { contains: q, mode: 'insensitive' } });
|
||||
if (ors.length) where.OR = ors;
|
||||
}
|
||||
|
||||
if (query.code) where.code = { contains: query.code, mode: 'insensitive' };
|
||||
if (query.name) where.name = { contains: query.name, mode: 'insensitive' };
|
||||
if (query.manufacturer)
|
||||
where.manufacturer = {
|
||||
contains: query.manufacturer,
|
||||
mode: 'insensitive',
|
||||
};
|
||||
if (query.manufacturer) where.manufacturer = { contains: query.manufacturer, mode: 'insensitive' };
|
||||
|
||||
// Enum multi-value support (e.g. status=A&status=B)
|
||||
|
||||
|
||||
if (query.id) {
|
||||
const ids = Array.isArray(query.id) ? query.id : [query.id];
|
||||
where.code = { in: ids };
|
||||
}
|
||||
|
||||
const [data, total] = await Promise.all([
|
||||
this.prisma.equipmentType.findMany({
|
||||
where,
|
||||
skip,
|
||||
take,
|
||||
orderBy: { [sortField]: sortOrder },
|
||||
}),
|
||||
this.prisma.equipmentType.findMany({ where, skip, take, orderBy: { [sortField]: sortOrder } }),
|
||||
this.prisma.equipmentType.count({ where }),
|
||||
]);
|
||||
|
||||
return {
|
||||
data: data.map((item) => ({ id: item.code, ...item })),
|
||||
total,
|
||||
};
|
||||
const mapped = data.map((r: any) => ({ id: r.code, ...r }));
|
||||
return { data: mapped, total };
|
||||
}
|
||||
|
||||
async findOne(code: string) {
|
||||
const record = await this.prisma.equipmentType.findUniqueOrThrow({
|
||||
where: { code },
|
||||
});
|
||||
return { id: record.code, ...record };
|
||||
async findOne(id: string) {
|
||||
const record = await this.prisma.equipmentType.findUniqueOrThrow({ where: { code: id } as any });
|
||||
return { id: (record as any).code, ...record };
|
||||
}
|
||||
|
||||
async create(dto: CreateEquipmentTypeDto) {
|
||||
const record = await this.prisma.equipmentType.create({ data: dto });
|
||||
return { id: record.code, ...record };
|
||||
const record = await this.prisma.equipmentType.create({ data: dto as any });
|
||||
return { id: (record as any).code, ...record };
|
||||
}
|
||||
|
||||
async update(code: string, dto: UpdateEquipmentTypeDto) {
|
||||
const { id, code: _pk, ...data } = dto as any;
|
||||
const record = await this.prisma.equipmentType.update({
|
||||
where: { code },
|
||||
data,
|
||||
});
|
||||
return { id: record.code, ...record };
|
||||
async update(id: string, dto: UpdateEquipmentTypeDto) {
|
||||
const data: any = { ...(dto as any) };
|
||||
delete data.id;
|
||||
delete data.code;
|
||||
const record = await this.prisma.equipmentType.update({ where: { code: id } as any, data });
|
||||
return { id: (record as any).code, ...record };
|
||||
}
|
||||
|
||||
async remove(code: string) {
|
||||
const record = await this.prisma.equipmentType.delete({ where: { code } });
|
||||
return { id: record.code, ...record };
|
||||
async remove(id: string) {
|
||||
const record = await this.prisma.equipmentType.delete({ where: { code: id } as any });
|
||||
return { id: (record as any).code, ...record };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
export class CreateEquipmentDto {
|
||||
inventoryNumber: string;
|
||||
inventoryNumber!: string;
|
||||
serialNumber?: string;
|
||||
name: string;
|
||||
equipmentTypeCode: string;
|
||||
status?: string;
|
||||
name!: string;
|
||||
equipmentTypeCode!: string;
|
||||
status!: string;
|
||||
location?: string;
|
||||
commissionedAt?: string;
|
||||
totalEngineHours?: string;
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
export class UpdateEquipmentDto {
|
||||
inventoryNumber?: string;
|
||||
serialNumber?: string;
|
||||
name?: string;
|
||||
equipmentTypeCode?: string;
|
||||
status?: string;
|
||||
location?: string;
|
||||
commissionedAt?: string;
|
||||
totalEngineHours?: string;
|
||||
engineHoursSinceLastRepair?: string;
|
||||
lastRepairAt?: string;
|
||||
notes?: string;
|
||||
id?: string | null;
|
||||
inventoryNumber?: string | null;
|
||||
serialNumber?: string | null;
|
||||
name?: string | null;
|
||||
equipmentTypeCode?: string | null;
|
||||
status?: string | null;
|
||||
location?: string | null;
|
||||
commissionedAt?: string | null;
|
||||
totalEngineHours?: string | null;
|
||||
engineHoursSinceLastRepair?: string | null;
|
||||
lastRepairAt?: string | null;
|
||||
notes?: string | null;
|
||||
}
|
||||
|
||||
@@ -6,11 +6,11 @@ import { UpdateEquipmentDto } from './dto/update-equipment.dto';
|
||||
|
||||
@Controller('equipment')
|
||||
export class EquipmentController {
|
||||
constructor(private readonly equipmentService: EquipmentService) {}
|
||||
constructor(private readonly service: EquipmentService) {}
|
||||
|
||||
@Get()
|
||||
async findAll(@Query() query: any, @Res() res: Response) {
|
||||
const result = await this.equipmentService.findAll(query);
|
||||
const result = await this.service.findAll(query);
|
||||
res.set('Content-Range', `equipment ${query._start || 0}-${query._end || result.total}/${result.total}`);
|
||||
res.set('Access-Control-Expose-Headers', 'Content-Range');
|
||||
return res.json(result.data);
|
||||
@@ -18,21 +18,21 @@ export class EquipmentController {
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.equipmentService.findOne(id);
|
||||
return this.service.findOne(id);
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@Body() dto: CreateEquipmentDto) {
|
||||
return this.equipmentService.create(dto);
|
||||
return this.service.create(dto);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() dto: UpdateEquipmentDto) {
|
||||
return this.equipmentService.update(id, dto);
|
||||
return this.service.update(id, dto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.equipmentService.remove(id);
|
||||
return this.service.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,16 +4,6 @@ import { PrismaService } from '../../prisma/prisma.service';
|
||||
import { CreateEquipmentDto } from './dto/create-equipment.dto';
|
||||
import { UpdateEquipmentDto } from './dto/update-equipment.dto';
|
||||
|
||||
function serializeRecord(record: any) {
|
||||
return {
|
||||
...record,
|
||||
totalEngineHours: record.totalEngineHours?.toString() ?? null,
|
||||
engineHoursSinceLastRepair: record.engineHoursSinceLastRepair?.toString() ?? null,
|
||||
commissionedAt: record.commissionedAt?.toISOString() ?? null,
|
||||
lastRepairAt: record.lastRepairAt?.toISOString() ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class EquipmentService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
@@ -27,62 +17,63 @@ export class EquipmentService {
|
||||
const sortOrder = (query._order || 'ASC').toLowerCase() as 'asc' | 'desc';
|
||||
|
||||
const where: any = {};
|
||||
|
||||
if (query.q) {
|
||||
const q = String(query.q);
|
||||
const ors: any[] = [];
|
||||
ors.push({ inventoryNumber: { contains: q, mode: 'insensitive' } });
|
||||
ors.push({ serialNumber: { contains: q, mode: 'insensitive' } });
|
||||
ors.push({ name: { contains: q, mode: 'insensitive' } });
|
||||
ors.push({ equipmentTypeCode: { contains: q, mode: 'insensitive' } });
|
||||
ors.push({ location: { contains: q, mode: 'insensitive' } });
|
||||
ors.push({ notes: { contains: q, mode: 'insensitive' } });
|
||||
if (ors.length) where.OR = ors;
|
||||
}
|
||||
|
||||
if (query.inventoryNumber) where.inventoryNumber = { contains: query.inventoryNumber, mode: 'insensitive' };
|
||||
if (query.serialNumber) where.serialNumber = { contains: query.serialNumber, mode: 'insensitive' };
|
||||
if (query.name) where.name = { contains: query.name, mode: 'insensitive' };
|
||||
if (query.equipmentTypeCode) where.equipmentTypeCode = query.equipmentTypeCode;
|
||||
if (query.status) where.status = query.status;
|
||||
if (query.equipmentTypeCode) where.equipmentTypeCode = { contains: query.equipmentTypeCode, mode: 'insensitive' };
|
||||
if (query.location) where.location = { contains: query.location, mode: 'insensitive' };
|
||||
if (query.notes) where.notes = { contains: query.notes, mode: 'insensitive' };
|
||||
|
||||
// Enum multi-value support (e.g. status=A&status=B)
|
||||
if (query.status) { const vals = Array.isArray(query.status) ? query.status : [query.status]; where.status = vals.length > 1 ? { in: vals } : vals[0]; }
|
||||
|
||||
if (query.id) {
|
||||
const ids = Array.isArray(query.id) ? query.id : [query.id];
|
||||
where.id = { in: ids };
|
||||
}
|
||||
|
||||
const [data, total] = await Promise.all([
|
||||
this.prisma.equipment.findMany({
|
||||
where,
|
||||
skip,
|
||||
take,
|
||||
orderBy: { [sortField]: sortOrder },
|
||||
}),
|
||||
this.prisma.equipment.findMany({ where, skip, take, orderBy: { [sortField]: sortOrder } }),
|
||||
this.prisma.equipment.count({ where }),
|
||||
]);
|
||||
|
||||
return {
|
||||
data: data.map(serializeRecord),
|
||||
total,
|
||||
};
|
||||
const mapped = data;
|
||||
return { data: mapped, total };
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const record = await this.prisma.equipment.findUniqueOrThrow({ where: { id } });
|
||||
return serializeRecord(record);
|
||||
const record = await this.prisma.equipment.findUniqueOrThrow({ where: { id: id } as any });
|
||||
return record;
|
||||
}
|
||||
|
||||
async create(dto: CreateEquipmentDto) {
|
||||
const data: any = { ...dto };
|
||||
if (dto.commissionedAt) data.commissionedAt = new Date(dto.commissionedAt);
|
||||
if (dto.lastRepairAt) data.lastRepairAt = new Date(dto.lastRepairAt);
|
||||
if (dto.totalEngineHours) data.totalEngineHours = new Prisma.Decimal(dto.totalEngineHours);
|
||||
if (dto.engineHoursSinceLastRepair) data.engineHoursSinceLastRepair = new Prisma.Decimal(dto.engineHoursSinceLastRepair);
|
||||
|
||||
const record = await this.prisma.equipment.create({ data });
|
||||
return serializeRecord(record);
|
||||
const record = await this.prisma.equipment.create({ data: dto as any });
|
||||
return record;
|
||||
}
|
||||
|
||||
async update(id: string, dto: UpdateEquipmentDto) {
|
||||
const { id: _pk, ...rest } = dto as any;
|
||||
const data: any = { ...rest };
|
||||
if (data.commissionedAt) data.commissionedAt = new Date(data.commissionedAt);
|
||||
if (data.lastRepairAt) data.lastRepairAt = new Date(data.lastRepairAt);
|
||||
if (data.totalEngineHours !== undefined && data.totalEngineHours !== null) data.totalEngineHours = new Prisma.Decimal(data.totalEngineHours);
|
||||
if (data.engineHoursSinceLastRepair !== undefined && data.engineHoursSinceLastRepair !== null) data.engineHoursSinceLastRepair = new Prisma.Decimal(data.engineHoursSinceLastRepair);
|
||||
|
||||
const record = await this.prisma.equipment.update({ where: { id }, data });
|
||||
return serializeRecord(record);
|
||||
const data: any = { ...(dto as any) };
|
||||
delete data.id;
|
||||
delete data.id;
|
||||
const record = await this.prisma.equipment.update({ where: { id: id } as any, data });
|
||||
return record;
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
const record = await this.prisma.equipment.delete({ where: { id } });
|
||||
return serializeRecord(record);
|
||||
const record = await this.prisma.equipment.delete({ where: { id: id } as any });
|
||||
return record;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
export class CreateRepairOrderDto {
|
||||
number: string;
|
||||
equipmentId: string;
|
||||
repairKind: string;
|
||||
status?: string;
|
||||
plannedAt: string;
|
||||
number!: string;
|
||||
equipmentId!: string;
|
||||
repairKind!: string;
|
||||
status!: string;
|
||||
plannedAt!: string;
|
||||
startedAt?: string;
|
||||
completedAt?: string;
|
||||
contractor?: string;
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
export class UpdateRepairOrderDto {
|
||||
number?: string;
|
||||
equipmentId?: string;
|
||||
repairKind?: string;
|
||||
status?: string;
|
||||
plannedAt?: string;
|
||||
startedAt?: string;
|
||||
completedAt?: string;
|
||||
contractor?: string;
|
||||
engineHoursAtRepair?: string;
|
||||
description?: string;
|
||||
notes?: string;
|
||||
id?: string | null;
|
||||
number?: string | null;
|
||||
equipmentId?: string | null;
|
||||
repairKind?: string | null;
|
||||
status?: string | null;
|
||||
plannedAt?: string | null;
|
||||
startedAt?: string | null;
|
||||
completedAt?: string | null;
|
||||
contractor?: string | null;
|
||||
engineHoursAtRepair?: string | null;
|
||||
description?: string | null;
|
||||
notes?: string | null;
|
||||
}
|
||||
|
||||
@@ -6,11 +6,11 @@ import { UpdateRepairOrderDto } from './dto/update-repair-order.dto';
|
||||
|
||||
@Controller('repair-orders')
|
||||
export class RepairOrderController {
|
||||
constructor(private readonly repairOrderService: RepairOrderService) {}
|
||||
constructor(private readonly service: RepairOrderService) {}
|
||||
|
||||
@Get()
|
||||
async findAll(@Query() query: any, @Res() res: Response) {
|
||||
const result = await this.repairOrderService.findAll(query);
|
||||
const result = await this.service.findAll(query);
|
||||
res.set('Content-Range', `repair-orders ${query._start || 0}-${query._end || result.total}/${result.total}`);
|
||||
res.set('Access-Control-Expose-Headers', 'Content-Range');
|
||||
return res.json(result.data);
|
||||
@@ -18,21 +18,21 @@ export class RepairOrderController {
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.repairOrderService.findOne(id);
|
||||
return this.service.findOne(id);
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@Body() dto: CreateRepairOrderDto) {
|
||||
return this.repairOrderService.create(dto);
|
||||
return this.service.create(dto);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() dto: UpdateRepairOrderDto) {
|
||||
return this.repairOrderService.update(id, dto);
|
||||
return this.service.update(id, dto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.repairOrderService.remove(id);
|
||||
return this.service.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,16 +4,6 @@ import { PrismaService } from '../../prisma/prisma.service';
|
||||
import { CreateRepairOrderDto } from './dto/create-repair-order.dto';
|
||||
import { UpdateRepairOrderDto } from './dto/update-repair-order.dto';
|
||||
|
||||
function serializeRecord(record: any) {
|
||||
return {
|
||||
...record,
|
||||
engineHoursAtRepair: record.engineHoursAtRepair?.toString() ?? null,
|
||||
plannedAt: record.plannedAt?.toISOString() ?? null,
|
||||
startedAt: record.startedAt?.toISOString() ?? null,
|
||||
completedAt: record.completedAt?.toISOString() ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class RepairOrderService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
@@ -27,62 +17,60 @@ export class RepairOrderService {
|
||||
const sortOrder = (query._order || 'ASC').toLowerCase() as 'asc' | 'desc';
|
||||
|
||||
const where: any = {};
|
||||
|
||||
if (query.q) {
|
||||
const q = String(query.q);
|
||||
const ors: any[] = [];
|
||||
ors.push({ number: { contains: q, mode: 'insensitive' } });
|
||||
ors.push({ contractor: { contains: q, mode: 'insensitive' } });
|
||||
ors.push({ description: { contains: q, mode: 'insensitive' } });
|
||||
ors.push({ notes: { contains: q, mode: 'insensitive' } });
|
||||
if (ors.length) where.OR = ors;
|
||||
}
|
||||
|
||||
if (query.number) where.number = { contains: query.number, mode: 'insensitive' };
|
||||
if (query.equipmentId) where.equipmentId = query.equipmentId;
|
||||
if (query.repairKind) where.repairKind = query.repairKind;
|
||||
if (query.status) where.status = query.status;
|
||||
if (query.contractor) where.contractor = { contains: query.contractor, mode: 'insensitive' };
|
||||
if (query.description) where.description = { contains: query.description, mode: 'insensitive' };
|
||||
if (query.notes) where.notes = { contains: query.notes, mode: 'insensitive' };
|
||||
|
||||
// Enum multi-value support (e.g. status=A&status=B)
|
||||
if (query.repairKind) { const vals = Array.isArray(query.repairKind) ? query.repairKind : [query.repairKind]; where.repairKind = vals.length > 1 ? { in: vals } : vals[0]; }
|
||||
if (query.status) { const vals = Array.isArray(query.status) ? query.status : [query.status]; where.status = vals.length > 1 ? { in: vals } : vals[0]; }
|
||||
|
||||
if (query.id) {
|
||||
const ids = Array.isArray(query.id) ? query.id : [query.id];
|
||||
where.id = { in: ids };
|
||||
}
|
||||
|
||||
const [data, total] = await Promise.all([
|
||||
this.prisma.repairOrder.findMany({
|
||||
where,
|
||||
skip,
|
||||
take,
|
||||
orderBy: { [sortField]: sortOrder },
|
||||
}),
|
||||
this.prisma.repairOrder.findMany({ where, skip, take, orderBy: { [sortField]: sortOrder } }),
|
||||
this.prisma.repairOrder.count({ where }),
|
||||
]);
|
||||
|
||||
return {
|
||||
data: data.map(serializeRecord),
|
||||
total,
|
||||
};
|
||||
const mapped = data;
|
||||
return { data: mapped, total };
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const record = await this.prisma.repairOrder.findUniqueOrThrow({ where: { id } });
|
||||
return serializeRecord(record);
|
||||
const record = await this.prisma.repairOrder.findUniqueOrThrow({ where: { id: id } as any });
|
||||
return record;
|
||||
}
|
||||
|
||||
async create(dto: CreateRepairOrderDto) {
|
||||
const data: any = { ...dto };
|
||||
if (dto.plannedAt) data.plannedAt = new Date(dto.plannedAt);
|
||||
if (dto.startedAt) data.startedAt = new Date(dto.startedAt);
|
||||
if (dto.completedAt) data.completedAt = new Date(dto.completedAt);
|
||||
if (dto.engineHoursAtRepair) data.engineHoursAtRepair = new Prisma.Decimal(dto.engineHoursAtRepair);
|
||||
|
||||
const record = await this.prisma.repairOrder.create({ data });
|
||||
return serializeRecord(record);
|
||||
const record = await this.prisma.repairOrder.create({ data: dto as any });
|
||||
return record;
|
||||
}
|
||||
|
||||
async update(id: string, dto: UpdateRepairOrderDto) {
|
||||
const { id: _pk, ...rest } = dto as any;
|
||||
const data: any = { ...rest };
|
||||
if (data.plannedAt) data.plannedAt = new Date(data.plannedAt);
|
||||
if (data.startedAt) data.startedAt = new Date(data.startedAt);
|
||||
if (data.completedAt) data.completedAt = new Date(data.completedAt);
|
||||
if (data.engineHoursAtRepair !== undefined && data.engineHoursAtRepair !== null) data.engineHoursAtRepair = new Prisma.Decimal(data.engineHoursAtRepair);
|
||||
|
||||
const record = await this.prisma.repairOrder.update({ where: { id }, data });
|
||||
return serializeRecord(record);
|
||||
const data: any = { ...(dto as any) };
|
||||
delete data.id;
|
||||
delete data.id;
|
||||
const record = await this.prisma.repairOrder.update({ where: { id: id } as any, data });
|
||||
return record;
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
const record = await this.prisma.repairOrder.delete({ where: { id } });
|
||||
return serializeRecord(record);
|
||||
const record = await this.prisma.repairOrder.delete({ where: { id: id } as any });
|
||||
return record;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user