import { Injectable, NotFoundException } from '@nestjs/common'; import { WorkOrderRepository } from './work-order.repository'; import { PartRepository } from './part.repository'; import { WarrantyRepository } from './warranty.repository'; import { WorkOrder, WorkOrderStatus } from './entities/work-order.entity'; import { Part } from './entities/part.entity'; import { Warranty } from './entities/warranty.entity'; import { CreateWorkOrderDto, UpdateWorkOrderDto, CreatePartDto, UpdatePartDto, CreateWarrantyDto, } from './dto/create-maintenance-depth.dto'; @Injectable() export class MaintenanceDepthService { constructor( private readonly workOrderRepository: WorkOrderRepository, private readonly partRepository: PartRepository, private readonly warrantyRepository: WarrantyRepository, ) {} // ---- Work Orders ---- async createWorkOrder(dto: CreateWorkOrderDto): Promise { return this.workOrderRepository.create({ ...dto, openedAt: dto.openedAt ? new Date(dto.openedAt) : new Date(), closedAt: dto.closedAt ? new Date(dto.closedAt) : undefined, }); } async findWorkOrders(filters: { vehicleId?: string; status?: WorkOrderStatus }) { return this.workOrderRepository.findFiltered(filters); } async findWorkOrderById(id: string): Promise { const workOrder = await this.workOrderRepository.findById(id); if (!workOrder) throw new NotFoundException(`Work order ${id} not found`); return workOrder; } async updateWorkOrder(id: string, dto: UpdateWorkOrderDto): Promise { await this.findWorkOrderById(id); const updated = await this.workOrderRepository.update(id, { ...dto, closedAt: dto.closedAt ? new Date(dto.closedAt) : undefined, }); return updated!; } async deleteWorkOrder(id: string): Promise<{ id: string; deleted: boolean }> { await this.findWorkOrderById(id); await this.workOrderRepository.softDelete(id); return { id, deleted: true }; } // ---- Parts / Tires ---- async createPart(dto: CreatePartDto): Promise { return this.partRepository.create({ ...dto }); } async findParts(filters: { category?: string; lowStock?: boolean }) { return this.partRepository.findFiltered(filters); } async updatePart(id: string, dto: UpdatePartDto): Promise { const part = await this.partRepository.findById(id); if (!part) throw new NotFoundException(`Part ${id} not found`); const updated = await this.partRepository.update(id, { ...dto }); return updated!; } async deletePart(id: string): Promise<{ id: string; deleted: boolean }> { const part = await this.partRepository.findById(id); if (!part) throw new NotFoundException(`Part ${id} not found`); await this.partRepository.softDelete(id); return { id, deleted: true }; } // ---- Warranties ---- async createWarranty(dto: CreateWarrantyDto): Promise { return this.warrantyRepository.create({ ...dto }); } async findWarranties(filters: { vehicleId?: string }) { return this.warrantyRepository.findFiltered(filters); } async deleteWarranty(id: string): Promise<{ id: string; deleted: boolean }> { const warranty = await this.warrantyRepository.findById(id); if (!warranty) throw new NotFoundException(`Warranty ${id} not found`); await this.warrantyRepository.softDelete(id); return { id, deleted: true }; } }