78 lines
2.2 KiB
TypeScript
78 lines
2.2 KiB
TypeScript
import { z } from "zod";
|
|
import { uuid, warehouse } from "./commerce.ts";
|
|
import { material } from "./procurement.ts";
|
|
|
|
const quantity = z.string().regex(/^\d{1,9}\.\d{3}$/);
|
|
const positiveQuantity = quantity.refine((value) => value !== "0.000");
|
|
const signedQuantity = z.string().regex(/^-?\d{1,9}\.\d{3}$/);
|
|
|
|
export const receiptPostingInput = z.object({
|
|
warehouseId: uuid,
|
|
idempotencyKey: uuid,
|
|
}).strict();
|
|
|
|
export const receiptPosting = z.object({
|
|
id: uuid,
|
|
receiptId: uuid,
|
|
warehouseId: uuid,
|
|
createdAt: z.string().datetime(),
|
|
entries: z.array(z.object({
|
|
id: uuid,
|
|
stockItemId: uuid,
|
|
receiptLineId: uuid,
|
|
kind: z.literal("RECEIPT_ACCEPTED"),
|
|
delta: quantity.refine((value) => value !== "0.000"),
|
|
})),
|
|
});
|
|
|
|
export const materialStockItem = z.object({
|
|
id: uuid,
|
|
warehouse,
|
|
material,
|
|
onHand: quantity,
|
|
});
|
|
|
|
export const materialStockEntry = z.object({
|
|
id: uuid,
|
|
kind: z.string(),
|
|
delta: signedQuantity,
|
|
createdAt: z.string().datetime(),
|
|
});
|
|
|
|
const transferFields = z.object({
|
|
materialId: uuid,
|
|
fromWarehouseId: uuid,
|
|
toWarehouseId: uuid,
|
|
});
|
|
export const materialTransferInput = transferFields.extend({
|
|
quantity: z.string().regex(/^(?:0|[1-9]\d{0,8})\.\d{3}$/, "Use three decimal places")
|
|
.refine((value) => value !== "0.000", "Enter a positive quantity"),
|
|
idempotencyKey: uuid,
|
|
}).strict().refine((value) => value.fromWarehouseId !== value.toWarehouseId, {
|
|
path: ["toWarehouseId"],
|
|
message: "Choose a different destination warehouse.",
|
|
});
|
|
|
|
export const materialTransfer = transferFields.extend({
|
|
id: uuid,
|
|
fromStockItemId: uuid,
|
|
toStockItemId: uuid,
|
|
quantity: positiveQuantity,
|
|
createdAt: z.string().datetime(),
|
|
});
|
|
|
|
export const materialAdjustmentInput = z.object({
|
|
materialId: uuid,
|
|
warehouseId: uuid,
|
|
delta: z.string().regex(/^-?(?:0|[1-9]\d{0,8})\.\d{3}$/, "Use three decimal places and a minus sign for decreases")
|
|
.refine((value) => value !== "0.000" && value !== "-0.000", "Enter a nonzero change"),
|
|
reason: z.string().trim().min(3).max(200),
|
|
idempotencyKey: uuid,
|
|
}).strict();
|
|
|
|
export const materialAdjustment = materialAdjustmentInput.omit({ idempotencyKey: true }).extend({
|
|
id: uuid,
|
|
stockItemId: uuid,
|
|
createdAt: z.string().datetime(),
|
|
}).strip();
|