Auto-fill AssetItem.warrantyEndDate from AssetModel.warrantyMonths
CI / test (push) Failing after 8s

AssetModel gains an optional warrantyMonths (standard vendor warranty
length). createAssetItem() now defaults warrantyEndDate to purchaseDate +
warrantyMonths (month-end clamped, e.g. Dec 31 + 2mo -> Feb 28, not Mar 3)
whenever the caller omits warrantyEndDate -- still overridable per item
for batches with extended/different warranty terms. /asset-models UI gets
an input for the new field; /assets needs no change since it already
omits warrantyEndDate from the payload when left blank.

Hand-written migration + manual smoke test (tsx script, not Vitest),
same deviation as the other 2026-08-2x work in this branch -- see
CLAUDE.md. tsc/lint clean.
This commit is contained in:
toanhoang
2026-08-21 09:10:55 -04:00
parent 8ac95c974b
commit d9b360c1df
7 changed files with 110 additions and 4 deletions
+14
View File
@@ -9,6 +9,7 @@ export interface AssetModelRecord {
manufacturer: string;
name: string;
specs: Record<string, unknown>;
warranty_months: number | null;
created_at: string;
}
@@ -17,6 +18,7 @@ export interface CreateAssetModelInput {
manufacturer: string;
name: string;
specs: Record<string, unknown>;
warrantyMonths?: number | null;
}
export type ValidationResult =
@@ -57,6 +59,14 @@ const createAssetModelSchema = z.object({
})
.optional()
.default({}),
// Optional standard warranty length -- see schema.prisma's
// AssetModel.warrantyMonths comment for what this drives.
warrantyMonths: z
.number({ invalid_type_error: "warrantyMonths must be a positive integer" })
.int("warrantyMonths must be a positive integer")
.positive("warrantyMonths must be a positive integer")
.optional()
.nullable(),
});
/**
@@ -71,6 +81,7 @@ export function validateCreateAssetModelInput(input: {
manufacturer?: unknown;
name?: unknown;
specs?: unknown;
warrantyMonths?: unknown;
}): ValidationResult {
const result = createAssetModelSchema.safeParse(input);
if (!result.success) {
@@ -86,6 +97,7 @@ function toRecord(row: {
manufacturer: string;
name: string;
specs: Prisma.JsonValue;
warrantyMonths: number | null;
createdAt: Date;
}): AssetModelRecord {
return {
@@ -94,6 +106,7 @@ function toRecord(row: {
manufacturer: row.manufacturer,
name: row.name,
specs: (row.specs as Record<string, unknown>) ?? {},
warranty_months: row.warrantyMonths,
created_at: row.createdAt.toISOString(),
};
}
@@ -201,6 +214,7 @@ export async function createAssetModel(
// is safe here since the Zod schema already guarantees JSON-shaped
// values.
specs: input.specs as Prisma.InputJsonValue,
warrantyMonths: input.warrantyMonths ?? null,
},
});
return { ok: true, value: toRecord(created) };