diff --git a/furniture-3d-system/src/server/services/finished-product-delivery.service.ts b/furniture-3d-system/src/server/services/finished-product-delivery.service.ts new file mode 100644 index 00000000..ff3ccd17 --- /dev/null +++ b/furniture-3d-system/src/server/services/finished-product-delivery.service.ts @@ -0,0 +1,41 @@ +import { finishedProductDeliveryInputSchema, type FinishedProductDeliveryInput } from '@/lib/finished-product-progress' +import { AppError, ErrorCode } from '@/server/core/errors' +import { + finishedProductProgressRepository, + type FinishedProductProgressRepository, +} from '@/server/repositories/finished-product-progress.repository' + +function parseDeliveryInput(input: unknown): FinishedProductDeliveryInput { + const parsed = finishedProductDeliveryInputSchema.safeParse(input) + if (parsed.success) return parsed.data + throw new AppError(ErrorCode.VALIDATION_FAILED, parsed.error.issues[0]?.message ?? '送货记录格式不正确') +} + +export class FinishedProductDeliveryService { + constructor(private readonly repository: FinishedProductProgressRepository = finishedProductProgressRepository) {} + + parse(input: unknown) { + return parseDeliveryInput(input) + } + + async list(itemId: string) { + return this.repository.listDeliveryRecords(itemId) + } + + async create(itemId: string, input: unknown, actor: { id: string; name: string }) { + return this.repository.createDeliveryRecord(itemId, parseDeliveryInput(input), actor) + } + + async update(deliveryId: string, input: unknown, actor: { id: string; name: string }) { + const current = await this.repository.findDeliveryRecordById(deliveryId) + if (!current) return null + if (current.status === 'VOID') throw new AppError(ErrorCode.VALIDATION_FAILED, '已作废的送货记录不能修改') + return this.repository.updateDeliveryRecord(current.id, parseDeliveryInput(input), actor) + } + + async void(deliveryId: string, actor: { id: string; name: string }) { + return this.repository.voidDeliveryRecord(deliveryId, actor) + } +} + +export const finishedProductDeliveryService = new FinishedProductDeliveryService()