19 Commits
Author SHA1 Message Date
toanhoangandClaude Sonnet 5 2037e6939e Merge branch 'feat/asset-item-model-id'
Retrofit AssetItem.model (string) -> modelId (FK to AssetModel) —
closes out Lane B (AssetCategory -> AssetCategoryAttributeDef +
AssetModel -> dynamic specs validation -> AssetItem.modelId).
Verified green by test-runner (225/225 tests, tsc/lint/build clean)
before merge.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-08-02 21:11:13 -04:00
toanhoang 2a5792df67 fix: retrofit AssetItem.model to modelId FK
AssetItem.model was a free-text string with no referential integrity.
Now a required FK to AssetModel, mirroring the Request.employeeId and
License.vendorId retrofit precedent (no backfill needed, no real
AssetItem data existed).
2026-08-02 21:07:12 -04:00
toanhoangandClaude Sonnet 5 d280279322 Merge branch 'feat/asset-model-dynamic-specs'
Wire dynamic specs validation into AssetModel creation: builds a
runtime Zod schema from the category's AssetCategoryAttributeDef
rows, rejects unknown keys, enforces required keys. Verified green
by test-runner (223/223 tests, tsc/lint/build clean) before merge.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-08-02 21:02:21 -04:00
toanhoang dc4c438ca9 feat: wire dynamic specs validation into AssetModel creation 2026-08-02 20:58:19 -04:00
toanhoangandClaude Sonnet 5 bcd41e4ba3 Merge branch 'feat/asset-model'
AssetModel entity: Prisma model+migration, Zod+CRUD (categoryId FK
+ business rule requiring valueGroup=high_value, specs JSONB loosely
validated for now), API routes, tests. Verified green by test-runner
(217/217 tests, tsc/lint/build clean) before merge.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-08-02 20:54:06 -04:00
toanhoang 0fdf82be61 test: add AssetModel API tests 2026-08-02 20:49:24 -04:00
toanhoang c9b595118a feat: add AssetModel API routes 2026-08-02 20:48:09 -04:00
toanhoang 1c530a3162 feat: add AssetModel validation + CRUD (src/lib/asset-models.ts) 2026-08-02 20:48:09 -04:00
toanhoang c02235f381 feat: add AssetModel Prisma model + migration 2026-08-02 20:46:48 -04:00
toanhoangandClaude Sonnet 5 d3536b60ad Merge branch 'feat/asset-category-attribute-def'
AssetCategoryAttributeDef entity: Prisma model+migration (dataType
enum, composite unique (categoryId,key)), Zod+CRUD (P2002+P2003),
API routes, tests. Verified green by test-runner (192/192 tests,
tsc/lint/build clean) before merge.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-08-02 20:44:32 -04:00
toanhoang b91a1d2185 test: add AssetCategoryAttributeDef API tests
28 tests covering lib-level validation/CRUD and route-level HTTP behavior:
malformed JSON, non-object body, invalid dataType, categoryId FK violation,
duplicate (categoryId, key) rejected with a clean 400 (not 500), and same
key allowed across two different categories (the "not globally unique"
case), plus auth gating.
2026-08-02 20:40:17 -04:00
toanhoang 59425a159b feat: add AssetCategoryAttributeDef API routes
POST/GET /api/asset-category-attribute-defs, auth-gated, thin route mirroring
src/app/api/asset-categories/route.ts.
2026-08-02 20:38:46 -04:00
toanhoang 123cbce2bc feat: add AssetCategoryAttributeDef validation + CRUD (src/lib/asset-category-attribute-defs.ts)
Zod schema + validate function, Prisma-backed CRUD following the
{ok,value}|{ok,error} pattern from the start. createAssetCategoryAttributeDef()
catches P2002 for the composite (categoryId, key) unique constraint (mirrors
createAssetItem()'s serialNumber handling) and P2003 for the categoryId FK
(mirrors createPurchaseOrder()).
2026-08-02 20:38:41 -04:00
toanhoang f7b9566b04 feat: add AssetCategoryAttributeDef Prisma model + migration
Per-category dynamic attribute definitions (key/label/dataType/required/
sortOrder), FK to AssetCategory, composite unique (categoryId, key) so the
same key is allowed across different categories but not duplicated within
one. dataType is a new closed Prisma enum (AssetAttributeDataType), a
dev-defined technical set distinct from AssetCategoryValueGroup.
2026-08-02 20:38:06 -04:00
toanhoangandClaude Sonnet 5 065c8cb59e Merge branch 'feat/asset-category'
AssetCategory entity: Prisma model+migration (valueGroup enum
high_value/low_value), Zod+CRUD, API routes, tests. Verified green by
test-runner (164/164 tests, tsc/lint/build clean) before merge.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-08-02 20:36:20 -04:00
toanhoang aaab8fd9b7 test: add AssetCategory API tests 2026-08-02 20:32:38 -04:00
toanhoang 63eb3df33d feat: add AssetCategory API routes 2026-08-02 20:31:27 -04:00
toanhoang 20ec7a11df feat: add AssetCategory validation + CRUD (src/lib/asset-categories.ts) 2026-08-02 20:31:22 -04:00
toanhoang 69fa00012b feat: add AssetCategory Prisma model + migration 2026-08-02 20:30:38 -04:00
17 changed files with 2681 additions and 46 deletions
@@ -0,0 +1,12 @@
-- CreateEnum
CREATE TYPE "AssetCategoryValueGroup" AS ENUM ('high_value', 'low_value');
-- CreateTable
CREATE TABLE "asset_categories" (
"id" UUID NOT NULL,
"name" TEXT NOT NULL,
"value_group" "AssetCategoryValueGroup" NOT NULL,
"created_at" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "asset_categories_pkey" PRIMARY KEY ("id")
);
@@ -0,0 +1,22 @@
-- CreateEnum
CREATE TYPE "AssetAttributeDataType" AS ENUM ('string', 'number', 'boolean', 'date');
-- CreateTable
CREATE TABLE "asset_category_attribute_defs" (
"id" UUID NOT NULL,
"category_id" UUID NOT NULL,
"key" TEXT NOT NULL,
"label" TEXT NOT NULL,
"data_type" "AssetAttributeDataType" NOT NULL,
"required" BOOLEAN NOT NULL,
"sort_order" INTEGER NOT NULL,
"created_at" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "asset_category_attribute_defs_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "asset_category_attribute_defs_category_id_key_key" ON "asset_category_attribute_defs"("category_id", "key");
-- AddForeignKey
ALTER TABLE "asset_category_attribute_defs" ADD CONSTRAINT "asset_category_attribute_defs_category_id_fkey" FOREIGN KEY ("category_id") REFERENCES "asset_categories"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
@@ -0,0 +1,14 @@
-- CreateTable
CREATE TABLE "asset_models" (
"id" UUID NOT NULL,
"category_id" UUID NOT NULL,
"manufacturer" TEXT NOT NULL,
"name" TEXT NOT NULL,
"specs" JSONB NOT NULL DEFAULT '{}',
"created_at" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "asset_models_pkey" PRIMARY KEY ("id")
);
-- AddForeignKey
ALTER TABLE "asset_models" ADD CONSTRAINT "asset_models_category_id_fkey" FOREIGN KEY ("category_id") REFERENCES "asset_categories"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
@@ -0,0 +1,13 @@
/*
Warnings:
- You are about to drop the column `model` on the `asset_items` table. All the data in the column will be lost.
- Added the required column `model_id` to the `asset_items` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "asset_items" DROP COLUMN "model",
ADD COLUMN "model_id" UUID NOT NULL;
-- AddForeignKey
ALTER TABLE "asset_items" ADD CONSTRAINT "asset_items_model_id_fkey" FOREIGN KEY ("model_id") REFERENCES "asset_models"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
+129 -1
View File
@@ -97,10 +97,20 @@ enum AssetItemStatus {
// a formal PO (e.g. historical stock predating the system). Set at
// intake/creation time only — no update endpoint for this field yet, same
// precedent as License.vendorId being set-at-creation with no PATCH.
// `modelId` was a free-text `model` String until the "Cập nhật 2026-08-02
// (2)" retrofit (docs/asset-license-management.md's "Ràng buộc dữ liệu"
// bullet) — same precedent as Request.employeeId/License.vendorId (string
// -> FK once the referenced entity existed). No production `AssetItem` data
// existed at the time of this change, so this was a plain column-type
// migration, not a backfill. Required (not nullable) — every AssetItem must
// belong to a real AssetModel. AssetItem deliberately does NOT carry its own
// `categoryId` — category is derived via `modelId -> AssetModel.categoryId`
// to avoid two sources of truth that could drift apart.
model AssetItem {
id String @id @default(uuid()) @db.Uuid
serialNumber String @unique @map("serial_number")
model String
modelId String @map("model_id") @db.Uuid
model AssetModel @relation(fields: [modelId], references: [id])
purchaseDate DateTime @map("purchase_date") @db.Date
warrantyEndDate DateTime? @map("warranty_end_date") @db.Date
status AssetItemStatus @default(in_stock)
@@ -216,6 +226,124 @@ model PurchaseOrder {
@@map("purchase_orders")
}
// Fixed, closed set of 2 values tied to the domain (see docs/
// asset-license-management.md's "## Phân loại tài sản: High-value vs
// Low-value/Consumable" section) — native Postgres enum, same rationale as
// RequestType/AssetItemStatus/LicenseType above. Determines downstream
// rules for entities that will attach to a category later (not built in
// this task): AssetModel.categoryId must point to a `high_value` category,
// ConsumableItem.categoryId must point to a `low_value` category.
enum AssetCategoryValueGroup {
high_value
low_value
}
// Asset classification catalog — see docs/asset-license-management.md's
// "## Phân loại tài sản" section and the `AssetCategory` row in the "Data
// model (sơ bộ)" table. Standalone entity, no FKs (same starting shape as
// Vendor before it grew relations) — `AssetModel` and
// `AssetCategoryAttributeDef` will attach to this via `categoryId` in
// later tasks (not this one). IT admin defines the concrete categories
// (e.g. "Laptop", "Cáp HDMI") freely; only `valueGroup` is a fixed 2-value
// set.
model AssetCategory {
id String @id @default(uuid()) @db.Uuid
name String
valueGroup AssetCategoryValueGroup @map("value_group")
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
attributeDefs AssetCategoryAttributeDef[]
// Back-relation for AssetModel.categoryId — see "Cập nhật 2026-08-02 (2)"
// in docs/asset-license-management.md. Only categories with
// valueGroup=high_value are ever actually referenced by an AssetModel (a
// business rule enforced in src/lib/asset-models.ts's createAssetModel(),
// not expressible as a Prisma-level constraint), but the relation itself
// is not restricted to high_value categories at the schema level.
assetModels AssetModel[]
@@map("asset_categories")
}
// Fixed, closed set of 4 values — a DEV-defined technical set (which Zod
// validator to build dynamically for AssetModel.specs at write time: string
// -> z.string(), number -> z.number(), boolean -> z.boolean(), date ->
// z.coerce.date()). Deliberately a different kind of "fixed set" than
// AssetCategoryValueGroup above: that one is a closed business classification
// IT admins never extend; this one is a closed set of *primitive types* the
// app's dynamic-attribute machinery knows how to handle. Still modeled as a
// native Postgres enum per the same codebase convention (RequestType /
// AssetItemStatus / LicenseType / AssetCategoryValueGroup), not a String +
// hand-rolled check.
enum AssetAttributeDataType {
string
number
boolean
date
}
// Per-category dynamic attribute definitions — see docs/
// asset-license-management.md's "Cập nhật 2026-08-02 (3): thêm
// AssetCategoryAttributeDef" section and the `AssetCategoryAttributeDef` row
// in the "Data model (sơ bộ)" table. Defines the *shape* of attributes a
// category cares about (e.g. category "Laptop" -> key `ram_gb`, label
// "RAM (GB)", dataType number); the actual *values* live in
// AssetModel.specs (JSONB), not built in this task. Each AssetCategory owns
// its own independent set of attribute defs — deliberately NOT a shared
// attribute library across categories (stakeholder explicitly rejected that
// design to keep things simple at current scale), so the same `key` string
// existing under two different categoryId rows is normal, not a collision —
// only unique *within* a category, hence the composite unique below (not a
// plain @unique on key alone). `sortOrder` is the display order on the
// dynamic form, no uniqueness constraint of its own (ties/gaps are fine,
// it's just a sort key).
model AssetCategoryAttributeDef {
id String @id @default(uuid()) @db.Uuid
categoryId String @map("category_id") @db.Uuid
category AssetCategory @relation(fields: [categoryId], references: [id])
key String
label String
dataType AssetAttributeDataType @map("data_type")
required Boolean
sortOrder Int @map("sort_order")
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
@@unique([categoryId, key])
@@map("asset_category_attribute_defs")
}
// Sits between AssetCategory and AssetItem — see docs/
// asset-license-management.md's "Cập nhật 2026-08-02 (2)" section and the
// `AssetModel` row in the "Data model (sơ bộ)" table. 1 AssetModel has many
// AssetItem serials (AssetItem.modelId FK, not built in this task).
// `manufacturer`/`name` are deliberately two separate String fields (not
// merged into one), per the doc, to allow filtering/reporting by
// manufacturer alone (e.g. "Dell") without string-parsing a combined field.
// `categoryId` is a required FK to AssetCategory — the DB layer doesn't
// enforce that the referenced category is `high_value` (Prisma/Postgres FK
// constraints can't express that), that business rule is checked in
// src/lib/asset-models.ts's createAssetModel() via an explicit lookup
// before the create, per the doc's "Ràng buộc dữ liệu" section. `specs` is
// JSONB, defaulting to an empty object — the real per-category dynamic
// attribute *values* (whose *shape* is defined by
// AssetCategoryAttributeDef rows for this model's category). This task
// deliberately does NOT validate specs against the category's attribute
// defs (loose z.record(z.unknown()) validation only, see
// src/lib/asset-models.ts) — that dynamic cross-validation is a separate
// follow-up task per the doc's "Cập nhật 2026-08-02 (3)" section.
model AssetModel {
id String @id @default(uuid()) @db.Uuid
categoryId String @map("category_id") @db.Uuid
category AssetCategory @relation(fields: [categoryId], references: [id])
manufacturer String
name String
specs Json @default("{}")
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
// Back-relation for the AssetItem.model -> AssetItem.modelId retrofit —
// see "Cập nhật 2026-08-02 (2)" in docs/asset-license-management.md.
assetItems AssetItem[]
@@map("asset_models")
}
// Minimal internal auth accounts — NOT company SSO (deliberately ruled out,
// see CLAUDE.md / auth decision notes). A handful of trusted IT staff
// accounts, created by hand via scripts/create-user.ts, no self-registration.
+51
View File
@@ -0,0 +1,51 @@
import { NextResponse } from "next/server";
import { auth } from "@/auth";
import {
createAssetCategory,
listAssetCategories,
validateCreateAssetCategoryInput,
} from "@/lib/asset-categories";
// Mirrors src/app/api/licenses/route.ts's pattern — middleware.ts excludes
// /api from route protection, so this checks the session itself. Flat
// permission model (CLAUDE.md): only "logged in or not", no role check.
export async function POST(request: Request): Promise<NextResponse> {
const session = await auth();
if (!session) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
let body: unknown;
try {
body = await request.json();
} catch {
return NextResponse.json({ error: "Request body must be valid JSON" }, { status: 400 });
}
if (typeof body !== "object" || body === null) {
return NextResponse.json({ error: "Request body must be a JSON object" }, { status: 400 });
}
const { name, valueGroup } = body as Record<string, unknown>;
const validation = validateCreateAssetCategoryInput({ name, valueGroup });
if (!validation.ok) {
return NextResponse.json({ error: validation.error }, { status: 400 });
}
const created = await createAssetCategory(validation.value);
if (!created.ok) {
return NextResponse.json({ error: created.error }, { status: 400 });
}
return NextResponse.json(created.value, { status: 201 });
}
export async function GET(): Promise<NextResponse> {
const session = await auth();
if (!session) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const assetCategories = await listAssetCategories();
return NextResponse.json(assetCategories, { status: 200 });
}
@@ -0,0 +1,59 @@
import { NextResponse } from "next/server";
import { auth } from "@/auth";
import {
createAssetCategoryAttributeDef,
listAssetCategoryAttributeDefs,
validateCreateAssetCategoryAttributeDefInput,
} from "@/lib/asset-category-attribute-defs";
// Mirrors src/app/api/asset-categories/route.ts's pattern — middleware.ts
// excludes /api from route protection, so this checks the session itself.
// Flat permission model (CLAUDE.md): only "logged in or not", no role check.
export async function POST(request: Request): Promise<NextResponse> {
const session = await auth();
if (!session) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
let body: unknown;
try {
body = await request.json();
} catch {
return NextResponse.json({ error: "Request body must be valid JSON" }, { status: 400 });
}
if (typeof body !== "object" || body === null) {
return NextResponse.json({ error: "Request body must be a JSON object" }, { status: 400 });
}
const { categoryId, key, label, dataType, required, sortOrder } =
body as Record<string, unknown>;
const validation = validateCreateAssetCategoryAttributeDefInput({
categoryId,
key,
label,
dataType,
required,
sortOrder,
});
if (!validation.ok) {
return NextResponse.json({ error: validation.error }, { status: 400 });
}
const created = await createAssetCategoryAttributeDef(validation.value);
if (!created.ok) {
return NextResponse.json({ error: created.error }, { status: 400 });
}
return NextResponse.json(created.value, { status: 201 });
}
export async function GET(): Promise<NextResponse> {
const session = await auth();
if (!session) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const assetCategoryAttributeDefs = await listAssetCategoryAttributeDefs();
return NextResponse.json(assetCategoryAttributeDefs, { status: 200 });
}
+52
View File
@@ -0,0 +1,52 @@
import { NextResponse } from "next/server";
import { auth } from "@/auth";
import {
createAssetModel,
listAssetModels,
validateCreateAssetModelInput,
} from "@/lib/asset-models";
// Mirrors src/app/api/asset-category-attribute-defs/route.ts's pattern —
// middleware.ts excludes /api from route protection, so this checks the
// session itself. Flat permission model (CLAUDE.md): only "logged in or
// not", no role check.
export async function POST(request: Request): Promise<NextResponse> {
const session = await auth();
if (!session) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
let body: unknown;
try {
body = await request.json();
} catch {
return NextResponse.json({ error: "Request body must be valid JSON" }, { status: 400 });
}
if (typeof body !== "object" || body === null) {
return NextResponse.json({ error: "Request body must be a JSON object" }, { status: 400 });
}
const { categoryId, manufacturer, name, specs } = body as Record<string, unknown>;
const validation = validateCreateAssetModelInput({ categoryId, manufacturer, name, specs });
if (!validation.ok) {
return NextResponse.json({ error: validation.error }, { status: 400 });
}
const created = await createAssetModel(validation.value);
if (!created.ok) {
return NextResponse.json({ error: created.error }, { status: 400 });
}
return NextResponse.json(created.value, { status: 201 });
}
export async function GET(): Promise<NextResponse> {
const session = await auth();
if (!session) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const assetModels = await listAssetModels();
return NextResponse.json(assetModels, { status: 200 });
}
+2 -2
View File
@@ -22,11 +22,11 @@ export async function POST(request: Request): Promise<NextResponse> {
return NextResponse.json({ error: "Request body must be a JSON object" }, { status: 400 });
}
const { serialNumber, model, purchaseDate, warrantyEndDate, purchaseOrderId } =
const { serialNumber, modelId, purchaseDate, warrantyEndDate, purchaseOrderId } =
body as Record<string, unknown>;
const validation = validateCreateAssetItemInput({
serialNumber,
model,
modelId,
purchaseDate,
warrantyEndDate,
purchaseOrderId,
+107
View File
@@ -0,0 +1,107 @@
import { z } from "zod";
import { prisma } from "./prisma";
export type AssetCategoryValueGroup = "high_value" | "low_value";
export interface AssetCategoryRecord {
id: string;
name: string;
value_group: AssetCategoryValueGroup;
created_at: string;
}
export interface CreateAssetCategoryInput {
name: string;
valueGroup: AssetCategoryValueGroup;
}
export type ValidationResult =
| { ok: true; value: CreateAssetCategoryInput }
| { ok: false; error: string };
// Mirrors src/lib/vendors.ts's nonEmptyString helper.
function nonEmptyString(message: string) {
return z.string({ required_error: message, invalid_type_error: message }).refine(
(value) => value.trim() !== "",
{ message }
);
}
const createAssetCategorySchema = z.object({
name: nonEmptyString("name is required and must be a non-empty string"),
// Fixed 2-value set (see docs/asset-license-management.md's "## Phân
// loại tài sản" section) — mirrors src/lib/licenses.ts's licenseType
// z.enum() pattern.
valueGroup: z.enum(["high_value", "low_value"], {
errorMap: () => ({
message: "valueGroup is required and must be either 'high_value' or 'low_value'",
}),
}),
});
/**
* Validates raw (untyped) asset-category-creation input. name must be a
* non-empty string, valueGroup must be one of the two fixed enum values
* (per the "## Phân loại tài sản" spec in docs/asset-license-management.md).
*/
export function validateCreateAssetCategoryInput(input: {
name?: unknown;
valueGroup?: unknown;
}): ValidationResult {
const result = createAssetCategorySchema.safeParse(input);
if (!result.success) {
const message = result.error.issues[0]?.message ?? "Invalid input";
return { ok: false, error: message };
}
return { ok: true, value: result.data };
}
function toRecord(row: {
id: string;
name: string;
valueGroup: AssetCategoryValueGroup;
createdAt: Date;
}): AssetCategoryRecord {
return {
id: row.id,
name: row.name,
value_group: row.valueGroup,
created_at: row.createdAt.toISOString(),
};
}
export type CreateAssetCategoryResult =
| { ok: true; value: AssetCategoryRecord }
| { ok: false; error: string };
/**
* Creates an AssetCategory. No unique constraint on any field, no FKs
* either (see docs/asset-license-management.md's "## Phân loại tài sản"
* spec — standalone entity, same starting shape as Vendor before it grew
* relations) — so there's no known Prisma error code to catch/translate
* here today. Still returns the `{ok,value}|{ok,error}` shape (rather than
* a bare Promise<AssetCategoryRecord> like createVendor()) to match the
* convention used across the newer entities (AssetItem/License) — see the
* code-reviewer finding on createLicense() in CLAUDE.md's "Trạng thái hiện
* tại": adding this shape up front avoids a silent 500 if a unique
* constraint is added later (e.g. on `name`) without remembering to also
* add P2002 handling here.
*/
export async function createAssetCategory(
input: CreateAssetCategoryInput
): Promise<CreateAssetCategoryResult> {
const created = await prisma.assetCategory.create({
data: {
name: input.name,
valueGroup: input.valueGroup,
},
});
return { ok: true, value: toRecord(created) };
}
export async function listAssetCategories(): Promise<AssetCategoryRecord[]> {
const rows = await prisma.assetCategory.findMany({
orderBy: [{ createdAt: "desc" }, { id: "desc" }],
});
return rows.map(toRecord);
}
+169
View File
@@ -0,0 +1,169 @@
import { z } from "zod";
import { Prisma } from "@prisma/client";
import { prisma } from "./prisma";
export type AssetAttributeDataType = "string" | "number" | "boolean" | "date";
export interface AssetCategoryAttributeDefRecord {
id: string;
category_id: string;
key: string;
label: string;
data_type: AssetAttributeDataType;
required: boolean;
sort_order: number;
created_at: string;
}
export interface CreateAssetCategoryAttributeDefInput {
categoryId: string;
key: string;
label: string;
dataType: AssetAttributeDataType;
required: boolean;
sortOrder: number;
}
export type ValidationResult =
| { ok: true; value: CreateAssetCategoryAttributeDefInput }
| { ok: false; error: string };
// Mirrors src/lib/asset-categories.ts's nonEmptyString helper.
function nonEmptyString(message: string) {
return z.string({ required_error: message, invalid_type_error: message }).refine(
(value) => value.trim() !== "",
{ message }
);
}
const createAssetCategoryAttributeDefSchema = z.object({
// FK to AssetCategory — only a UUID *format* check here, whether it
// references a real category is left to Prisma's own FK constraint at
// insert time (see createAssetCategoryAttributeDef()'s P2003 handling
// below). Mirrors src/lib/purchase-orders.ts's vendorId handling.
categoryId: z
.string({ required_error: "categoryId is required and must be a valid UUID string" })
.uuid("categoryId is required and must be a valid UUID string"),
// Slug-style, e.g. "ram_gb" — only unique *within* a category (composite
// unique (categoryId, key) at the DB layer), not globally. Just requires
// non-empty here; the DB-level uniqueness is enforced by
// createAssetCategoryAttributeDef()'s P2002 handling below.
key: nonEmptyString("key is required and must be a non-empty string"),
label: nonEmptyString("label is required and must be a non-empty string"),
// Fixed 4-value set (see docs/asset-license-management.md's "Cập nhật
// 2026-08-02 (3)" section) — a DEV-defined closed technical set, mirrors
// src/lib/asset-categories.ts's valueGroup z.enum() pattern.
dataType: z.enum(["string", "number", "boolean", "date"], {
errorMap: () => ({
message: "dataType is required and must be one of 'string', 'number', 'boolean', 'date'",
}),
}),
required: z.boolean({
required_error: "required is required and must be a boolean",
invalid_type_error: "required is required and must be a boolean",
}),
sortOrder: z.number({
required_error: "sortOrder is required and must be an integer",
invalid_type_error: "sortOrder is required and must be an integer",
})
.int("sortOrder is required and must be an integer"),
});
/**
* Validates raw (untyped) asset-category-attribute-def-creation input.
* categoryId must be a valid UUID string, key/label must be non-empty
* strings, dataType must be one of the four fixed enum values, required
* must be a boolean, sortOrder must be an integer. Whether categoryId
* references a real AssetCategory, and whether (categoryId, key) is
* actually unique, are both left to Prisma's constraints at insert time
* (see createAssetCategoryAttributeDef() below).
*/
export function validateCreateAssetCategoryAttributeDefInput(input: {
categoryId?: unknown;
key?: unknown;
label?: unknown;
dataType?: unknown;
required?: unknown;
sortOrder?: unknown;
}): ValidationResult {
const result = createAssetCategoryAttributeDefSchema.safeParse(input);
if (!result.success) {
const message = result.error.issues[0]?.message ?? "Invalid input";
return { ok: false, error: message };
}
return { ok: true, value: result.data };
}
function toRecord(row: {
id: string;
categoryId: string;
key: string;
label: string;
dataType: AssetAttributeDataType;
required: boolean;
sortOrder: number;
createdAt: Date;
}): AssetCategoryAttributeDefRecord {
return {
id: row.id,
category_id: row.categoryId,
key: row.key,
label: row.label,
data_type: row.dataType,
required: row.required,
sort_order: row.sortOrder,
created_at: row.createdAt.toISOString(),
};
}
export type CreateAssetCategoryAttributeDefResult =
| { ok: true; value: AssetCategoryAttributeDefRecord }
| { ok: false; error: string };
/**
* Creates an AssetCategoryAttributeDef. If (categoryId, key) collides with
* an existing row, Prisma raises P2002 (composite unique constraint
* violation) — caught here and translated into a clean {ok:false,error},
* same pattern as createAssetItem()'s serialNumber P2002 handling in
* src/lib/assets.ts. Note this is a *composite* uniqueness (unique within a
* category, not globally) — the same `key` under a *different* categoryId
* is expected to succeed, per docs/asset-license-management.md's "Cập nhật
* 2026-08-02 (3)" section. If categoryId doesn't reference an existing
* AssetCategory, Prisma raises P2003 (FK constraint violation) — caught the
* same way, mirrors src/lib/purchase-orders.ts's createPurchaseOrder() P2003
* handling.
*/
export async function createAssetCategoryAttributeDef(
input: CreateAssetCategoryAttributeDefInput
): Promise<CreateAssetCategoryAttributeDefResult> {
try {
const created = await prisma.assetCategoryAttributeDef.create({
data: {
categoryId: input.categoryId,
key: input.key,
label: input.label,
dataType: input.dataType,
required: input.required,
sortOrder: input.sortOrder,
},
});
return { ok: true, value: toRecord(created) };
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2002") {
return { ok: false, error: "key already exists for this category" };
}
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2003") {
return { ok: false, error: "categoryId does not reference an existing asset category" };
}
throw error;
}
}
export async function listAssetCategoryAttributeDefs(): Promise<
AssetCategoryAttributeDefRecord[]
> {
const rows = await prisma.assetCategoryAttributeDef.findMany({
orderBy: [{ createdAt: "desc" }, { id: "desc" }],
});
return rows.map(toRecord);
}
+214
View File
@@ -0,0 +1,214 @@
import { z } from "zod";
import { Prisma } from "@prisma/client";
import type { AssetAttributeDataType } from "./asset-category-attribute-defs";
import { prisma } from "./prisma";
export interface AssetModelRecord {
id: string;
category_id: string;
manufacturer: string;
name: string;
specs: Record<string, unknown>;
created_at: string;
}
export interface CreateAssetModelInput {
categoryId: string;
manufacturer: string;
name: string;
specs: Record<string, unknown>;
}
export type ValidationResult =
| { ok: true; value: CreateAssetModelInput }
| { ok: false; error: string };
// Mirrors src/lib/asset-category-attribute-defs.ts's nonEmptyString helper.
function nonEmptyString(message: string) {
return z.string({ required_error: message, invalid_type_error: message }).refine(
(value) => value.trim() !== "",
{ message }
);
}
const createAssetModelSchema = z.object({
// FK to AssetCategory — only a UUID *format* check here, whether it
// references a real category (and whether that category is
// valueGroup=high_value) is left to createAssetModel()'s explicit lookup
// below — that's a business rule, not something pure Zod schema
// validation on this input alone can express. Mirrors
// src/lib/asset-category-attribute-defs.ts's categoryId handling.
categoryId: z
.string({ required_error: "categoryId is required and must be a valid UUID string" })
.uuid("categoryId is required and must be a valid UUID string"),
manufacturer: nonEmptyString("manufacturer is required and must be a non-empty string"),
name: nonEmptyString("name is required and must be a non-empty string"),
// Deliberately LOOSE at this stage — just "a plain JSON object" (no
// arrays, no primitives at the top level). The category-specific dynamic
// shape (which keys are allowed, which are required, and their types) is
// validated separately in createAssetModel() below, via a runtime Zod
// schema built from that category's AssetCategoryAttributeDef rows — it
// can't be done here because that requires a DB lookup keyed on
// categoryId, which this pure-input-shape schema doesn't have access to.
// Defaults to {} when omitted, matching the DB column default.
specs: z
.record(z.unknown(), {
invalid_type_error: "specs must be a JSON object",
})
.optional()
.default({}),
});
/**
* Validates raw (untyped) asset-model-creation input. categoryId must be a
* valid UUID string, manufacturer/name must be non-empty strings, specs
* must be a plain JSON object (or omitted, defaulting to {}). Whether
* categoryId references a real, high_value AssetCategory is checked in
* createAssetModel() below, not here — that requires a DB lookup.
*/
export function validateCreateAssetModelInput(input: {
categoryId?: unknown;
manufacturer?: unknown;
name?: unknown;
specs?: unknown;
}): ValidationResult {
const result = createAssetModelSchema.safeParse(input);
if (!result.success) {
const message = result.error.issues[0]?.message ?? "Invalid input";
return { ok: false, error: message };
}
return { ok: true, value: result.data };
}
function toRecord(row: {
id: string;
categoryId: string;
manufacturer: string;
name: string;
specs: Prisma.JsonValue;
createdAt: Date;
}): AssetModelRecord {
return {
id: row.id,
category_id: row.categoryId,
manufacturer: row.manufacturer,
name: row.name,
specs: (row.specs as Record<string, unknown>) ?? {},
created_at: row.createdAt.toISOString(),
};
}
// Maps an AssetCategoryAttributeDef.dataType to the corresponding Zod leaf
// type, per docs/asset-license-management.md's "Cập nhật 2026-08-02 (3)"
// section: string -> z.string(), number -> z.number(), boolean ->
// z.boolean(), date -> z.coerce.date() (coerce so a JSON-transported date
// *string*, e.g. "2027-01-01", is accepted — JSON has no native Date type).
function zodTypeForDataType(dataType: AssetAttributeDataType): z.ZodTypeAny {
switch (dataType) {
case "string":
return z.string();
case "number":
return z.number();
case "boolean":
return z.boolean();
case "date":
return z.coerce.date();
}
}
/**
* Builds a Zod object schema AT RUNTIME from a category's
* AssetCategoryAttributeDef rows: one shape entry per attribute def (typed
* per zodTypeForDataType() above), optional when `required` is false via
* `.optional()`, and `.strict()` on the overall object so any `specs` key
* not defined for this category is rejected (no unknown keys allowed), per
* the doc section referenced above.
*/
function buildSpecsSchema(
attributeDefs: { key: string; dataType: AssetAttributeDataType; required: boolean }[]
): z.ZodTypeAny {
const shape: Record<string, z.ZodTypeAny> = {};
for (const def of attributeDefs) {
const leaf = zodTypeForDataType(def.dataType);
shape[def.key] = def.required ? leaf : leaf.optional();
}
return z.object(shape).strict();
}
export type CreateAssetModelResult =
| { ok: true; value: AssetModelRecord }
| { ok: false; error: string };
/**
* Creates an AssetModel. categoryId must reference a real AssetCategory
* (checked here via an explicit prisma.assetCategory.findUnique() lookup —
* not left to Prisma's P2003 FK-violation catch like other entities'
* simple FK checks, e.g. src/lib/asset-category-attribute-defs.ts's
* categoryId handling), because there's an additional business rule to
* enforce beyond "does this row exist": the referenced category's
* valueGroup must be `high_value` (per docs/asset-license-management.md's
* "Ràng buộc dữ liệu" section under "Cập nhật 2026-08-02 (2)") — a rule
* that can't be expressed as a Postgres FK constraint, so it can't be
* caught via a Prisma error code the way a plain missing-FK case can. If
* the category doesn't exist at all, or exists but is `low_value`, this
* returns a distinct {ok:false,error} for each case rather than a raw
* P2003/500.
*/
export async function createAssetModel(
input: CreateAssetModelInput
): Promise<CreateAssetModelResult> {
const category = await prisma.assetCategory.findUnique({
where: { id: input.categoryId },
});
if (!category) {
return { ok: false, error: "categoryId does not reference an existing asset category" };
}
if (category.valueGroup !== "high_value") {
return { ok: false, error: "categoryId must reference a category with valueGroup=high_value" };
}
// Dynamic specs validation, per docs/asset-license-management.md's
// "Cập nhật 2026-08-02 (3)" section: build a Zod schema at runtime from
// this category's AssetCategoryAttributeDef rows, and validate the
// incoming specs object against it. Deliberately done here (after the
// category existence/high_value checks above), not inside
// validateCreateAssetModelInput()'s static schema, because it needs a DB
// lookup keyed on categoryId.
const attributeDefs = await prisma.assetCategoryAttributeDef.findMany({
where: { categoryId: input.categoryId },
});
const specsSchema = buildSpecsSchema(attributeDefs);
const specsResult = specsSchema.safeParse(input.specs);
if (!specsResult.success) {
const message = specsResult.error.issues[0]?.message ?? "Invalid specs";
const path = specsResult.error.issues[0]?.path.join(".");
return {
ok: false,
error: path ? `specs.${path}: ${message}` : `specs: ${message}`,
};
}
const created = await prisma.assetModel.create({
data: {
categoryId: input.categoryId,
manufacturer: input.manufacturer,
name: input.name,
// input.specs is validated by Zod to be a plain JSON object
// (Record<string, unknown>), but Prisma's generated InputJsonValue
// type doesn't structurally accept an index-signature Record — cast
// is safe here since the Zod schema already guarantees JSON-shaped
// values.
specs: input.specs as Prisma.InputJsonValue,
},
});
return { ok: true, value: toRecord(created) };
}
export async function listAssetModels(): Promise<AssetModelRecord[]> {
const rows = await prisma.assetModel.findMany({
orderBy: [{ createdAt: "desc" }, { id: "desc" }],
});
return rows.map(toRecord);
}
+34 -18
View File
@@ -7,7 +7,7 @@ export type AssetItemStatus = "in_stock" | "assigned" | "repair" | "retired";
export interface AssetItemRecord {
id: string;
serial_number: string;
model: string;
model_id: string;
purchase_date: string;
warranty_end_date: string | null;
status: AssetItemStatus;
@@ -17,7 +17,7 @@ export interface AssetItemRecord {
export interface CreateAssetItemInput {
serialNumber: string;
model: string;
modelId: string;
purchaseDate: Date;
warrantyEndDate?: Date;
purchaseOrderId?: string | null;
@@ -47,7 +47,15 @@ function dateString(message: string) {
const createAssetItemSchema = z.object({
serialNumber: nonEmptyString("serialNumber is required and must be a non-empty string"),
model: nonEmptyString("model is required and must be a non-empty string"),
// Required FK to AssetModel — replaced the free-text `model` String (see
// schema.prisma / docs/asset-license-management.md's "Cập nhật 2026-08-02
// (2)" section). Only a UUID *format* check here, whether it references a
// real AssetModel is left to Prisma's own FK constraint at insert time
// (see createAssetItem()'s P2003 handling below). Mirrors
// src/lib/licenses.ts's vendorId handling.
modelId: z
.string({ required_error: "modelId is required and must be a valid UUID string" })
.uuid("modelId is required and must be a valid UUID string"),
purchaseDate: dateString("purchaseDate is required and must be a valid date string"),
warrantyEndDate: dateString("warrantyEndDate must be a valid date string").optional(),
// FK to PurchaseOrder — nullable/optional, supply-side traceability (not
@@ -64,17 +72,17 @@ const createAssetItemSchema = z.object({
});
/**
* Validates raw (untyped) asset-item-creation input. serialNumber/model
* must be non-empty strings, purchaseDate must be a valid date string,
* warrantyEndDate is optional but must be a valid date string if present.
* purchaseOrderId is optional/nullable but must be a valid UUID if present.
* status is not accepted here — new assets always start "in_stock" (same
* pattern as Request always starting "Draft"), later states are set via
* other operations, not at creation.
* Validates raw (untyped) asset-item-creation input. serialNumber must be a
* non-empty string, modelId must be a valid UUID string, purchaseDate must
* be a valid date string, warrantyEndDate is optional but must be a valid
* date string if present. purchaseOrderId is optional/nullable but must be
* a valid UUID if present. status is not accepted here — new assets always
* start "in_stock" (same pattern as Request always starting "Draft"), later
* states are set via other operations, not at creation.
*/
export function validateCreateAssetItemInput(input: {
serialNumber?: unknown;
model?: unknown;
modelId?: unknown;
purchaseDate?: unknown;
warrantyEndDate?: unknown;
purchaseOrderId?: unknown;
@@ -90,7 +98,7 @@ export function validateCreateAssetItemInput(input: {
function toRecord(row: {
id: string;
serialNumber: string;
model: string;
modelId: string;
purchaseDate: Date;
warrantyEndDate: Date | null;
status: AssetItemStatus;
@@ -100,7 +108,7 @@ function toRecord(row: {
return {
id: row.id,
serial_number: row.serialNumber,
model: row.model,
model_id: row.modelId,
purchase_date: row.purchaseDate.toISOString(),
warranty_end_date: row.warrantyEndDate ? row.warrantyEndDate.toISOString() : null,
status: row.status,
@@ -119,10 +127,14 @@ export type CreateAssetItemResult =
* caught here and translated into the same `{ok:false,error}` shape as
* input validation failures, rather than letting a raw Prisma error leak
* to the API layer. Mirrors createEmployee()/createRequest()'s P2003
* handling for FK violations. If purchaseOrderId doesn't reference an
* existing PurchaseOrder, Prisma raises P2003 (FK constraint violation) —
* caught the same way, mirrors src/lib/purchase-orders.ts's
* createPurchaseOrder() P2003 handling.
* handling for FK violations. Two separate FKs can fail here (modelId ->
* AssetModel, purchaseOrderId -> PurchaseOrder) — Prisma raises the same
* error code (P2003) for either, so the FK constraint name in
* `error.meta.field_name` is used to tell them apart and return a targeted
* message, rather than a raw Prisma error leaking to the API layer. Mirrors
* src/lib/licenses.ts's createLicense() / src/lib/purchase-orders.ts's
* createPurchaseOrder() pattern for disambiguating between two FKs on the
* same model.
*/
export async function createAssetItem(
input: CreateAssetItemInput
@@ -131,7 +143,7 @@ export async function createAssetItem(
const created = await prisma.assetItem.create({
data: {
serialNumber: input.serialNumber,
model: input.model,
modelId: input.modelId,
purchaseDate: input.purchaseDate,
warrantyEndDate: input.warrantyEndDate ?? null,
purchaseOrderId: input.purchaseOrderId ?? null,
@@ -143,8 +155,12 @@ export async function createAssetItem(
return { ok: false, error: "serialNumber already exists" };
}
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2003") {
const fieldName = typeof error.meta?.field_name === "string" ? error.meta.field_name : "";
if (fieldName.includes("purchase_order")) {
return { ok: false, error: "purchaseOrderId does not reference an existing purchase order" };
}
return { ok: false, error: "modelId does not reference an existing asset model" };
}
throw error;
}
}
+306
View File
@@ -0,0 +1,306 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { Mock } from "vitest";
import type { Session } from "next-auth";
import {
createAssetCategory,
listAssetCategories,
validateCreateAssetCategoryInput,
} from "@/lib/asset-categories";
import { prisma } from "@/lib/prisma";
// Note: this file has two layers of coverage, mirroring tests/vendors.test.ts:
// - lib-level tests below exercise src/lib/asset-categories.ts directly
// (validation, valueGroup enum, list ordering).
// - route-level tests (further down) exercise
// src/app/api/asset-categories/route.ts over HTTP, including the auth()
// gate — mirrors tests/vendors.test.ts's @/auth mocking pattern.
vi.mock("@/auth", () => ({ auth: vi.fn() }));
import { GET, POST } from "@/app/api/asset-categories/route";
import { auth } from "@/auth";
const mockedAuth = auth as unknown as Mock<() => Promise<Session | null>>;
const testSession: Session = {
user: { id: "test-user-id" },
expires: new Date(Date.now() + 60 * 60 * 1000).toISOString(),
};
function postRequest(body: unknown): Request {
return new Request("http://localhost/api/asset-categories", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(body),
});
}
beforeEach(async () => {
// Each test starts from an empty table so listing/order assertions are
// deterministic.
await prisma.assetCategory.deleteMany();
// Default to a logged-in session — the lib-level tests below don't go
// through the route at all (so this is a no-op for them), and the
// route-level tests exercise business logic, not auth gating (that's
// covered by the "auth gating" describe block further down).
mockedAuth.mockResolvedValue(testSession);
});
describe("validateCreateAssetCategoryInput", () => {
it("accepts a valid high_value category", () => {
const result = validateCreateAssetCategoryInput({
name: "Laptop",
valueGroup: "high_value",
});
expect(result.ok).toBe(true);
if (result.ok) {
expect(result.value).toMatchObject({ name: "Laptop", valueGroup: "high_value" });
}
});
it("accepts a valid low_value category", () => {
const result = validateCreateAssetCategoryInput({
name: "Cáp HDMI",
valueGroup: "low_value",
});
expect(result.ok).toBe(true);
if (result.ok) {
expect(result.value).toMatchObject({ name: "Cáp HDMI", valueGroup: "low_value" });
}
});
it("rejects an empty name", () => {
const result = validateCreateAssetCategoryInput({
name: "",
valueGroup: "high_value",
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toMatch(/name/i);
}
});
it("rejects a missing name", () => {
const result = validateCreateAssetCategoryInput({
valueGroup: "high_value",
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toMatch(/name/i);
}
});
it("rejects an invalid valueGroup", () => {
const result = validateCreateAssetCategoryInput({
name: "Laptop",
valueGroup: "medium_value",
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toMatch(/valueGroup/i);
}
});
it("rejects a missing valueGroup", () => {
const result = validateCreateAssetCategoryInput({
name: "Laptop",
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toMatch(/valueGroup/i);
}
});
});
describe("createAssetCategory", () => {
it("creates a high_value category", async () => {
const result = validateCreateAssetCategoryInput({
name: "Laptop",
valueGroup: "high_value",
});
expect(result.ok).toBe(true);
if (!result.ok) return;
const created = await createAssetCategory(result.value);
expect(created.ok).toBe(true);
if (!created.ok) return;
expect(created.value).toMatchObject({ name: "Laptop", value_group: "high_value" });
expect(typeof created.value.id).toBe("string");
expect(created.value.id.length).toBeGreaterThan(0);
expect(Number.isNaN(Date.parse(created.value.created_at))).toBe(false);
});
it("creates a low_value category", async () => {
const result = validateCreateAssetCategoryInput({
name: "Cáp HDMI",
valueGroup: "low_value",
});
expect(result.ok).toBe(true);
if (!result.ok) return;
const created = await createAssetCategory(result.value);
expect(created.ok).toBe(true);
if (!created.ok) return;
expect(created.value).toMatchObject({ name: "Cáp HDMI", value_group: "low_value" });
});
});
describe("listAssetCategories", () => {
it("lists created categories, newest first", async () => {
const first = validateCreateAssetCategoryInput({ name: "Cáp HDMI", valueGroup: "low_value" });
expect(first.ok).toBe(true);
if (first.ok) await createAssetCategory(first.value);
// Ensure a distinct created_at ordering even on fast filesystems/clocks.
await new Promise((resolve) => setTimeout(resolve, 5));
const second = validateCreateAssetCategoryInput({ name: "Laptop", valueGroup: "high_value" });
expect(second.ok).toBe(true);
if (second.ok) await createAssetCategory(second.value);
const records = await listAssetCategories();
expect(records).toHaveLength(2);
// newest first
expect(records[0]).toMatchObject({ name: "Laptop" });
expect(records[1]).toMatchObject({ name: "Cáp HDMI" });
});
it("returns an empty list when there are no categories", async () => {
const records = await listAssetCategories();
expect(records).toEqual([]);
});
});
describe("POST /api/asset-categories", () => {
it("creates a high_value category on valid input", async () => {
const res = await POST(postRequest({ name: "Laptop", valueGroup: "high_value" }));
expect(res.status).toBe(201);
const body = await res.json();
expect(body).toMatchObject({ name: "Laptop", value_group: "high_value" });
expect(typeof body.id).toBe("string");
expect(body.id.length).toBeGreaterThan(0);
expect(Number.isNaN(Date.parse(body.created_at))).toBe(false);
});
it("creates a low_value category on valid input", async () => {
const res = await POST(postRequest({ name: "Cáp HDMI", valueGroup: "low_value" }));
expect(res.status).toBe(201);
const body = await res.json();
expect(body).toMatchObject({ name: "Cáp HDMI", value_group: "low_value" });
});
it("rejects an empty name", async () => {
const res = await POST(postRequest({ name: "", valueGroup: "high_value" }));
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/name/i);
});
it("rejects an invalid valueGroup", async () => {
const res = await POST(postRequest({ name: "Laptop", valueGroup: "medium_value" }));
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/valueGroup/i);
});
it("rejects a missing valueGroup", async () => {
const res = await POST(postRequest({ name: "Laptop" }));
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/valueGroup/i);
});
it("rejects an invalid JSON body", async () => {
const req = new Request("http://localhost/api/asset-categories", {
method: "POST",
headers: { "content-type": "application/json" },
body: "not json",
});
const res = await POST(req);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/JSON/i);
});
it("rejects a non-object JSON body", async () => {
// Note: arrays are `typeof "object"` in JS, so they pass the route's
// object check (same behavior as /api/vendors) — use a JSON string
// primitive here to actually exercise the "not an object" branch.
const res = await POST(postRequest("not an object"));
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/JSON object/i);
});
});
describe("GET /api/asset-categories", () => {
it("lists created categories, newest first", async () => {
await POST(postRequest({ name: "Cáp HDMI", valueGroup: "low_value" }));
// Ensure a distinct created_at ordering even on fast filesystems/clocks.
await new Promise((resolve) => setTimeout(resolve, 5));
await POST(postRequest({ name: "Laptop", valueGroup: "high_value" }));
const res = await GET();
expect(res.status).toBe(200);
const body = await res.json();
expect(Array.isArray(body)).toBe(true);
expect(body).toHaveLength(2);
// newest first
expect(body[0]).toMatchObject({ name: "Laptop" });
expect(body[1]).toMatchObject({ name: "Cáp HDMI" });
});
it("returns an empty list when there are no categories", async () => {
const res = await GET();
expect(res.status).toBe(200);
const body = await res.json();
expect(body).toEqual([]);
});
});
describe("auth gating on /api/asset-categories", () => {
// Mirrors tests/vendors.test.ts's "auth gating" block — only checks
// session presence, per the flat permission model (no role/permission
// logic).
it("POST /api/asset-categories returns 401 without a session", async () => {
mockedAuth.mockResolvedValue(null);
const res = await POST(postRequest({ name: "Laptop", valueGroup: "high_value" }));
expect(res.status).toBe(401);
const body = await res.json();
expect(body).toEqual({ error: "Unauthorized" });
// And it must not have created anything.
const remaining = await prisma.assetCategory.findMany();
expect(remaining).toHaveLength(0);
});
it("GET /api/asset-categories returns 401 without a session", async () => {
mockedAuth.mockResolvedValue(null);
const res = await GET();
expect(res.status).toBe(401);
const body = await res.json();
expect(body).toEqual({ error: "Unauthorized" });
});
});
+660
View File
@@ -0,0 +1,660 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { Mock } from "vitest";
import type { Session } from "next-auth";
import {
createAssetCategoryAttributeDef,
listAssetCategoryAttributeDefs,
validateCreateAssetCategoryAttributeDefInput,
} from "@/lib/asset-category-attribute-defs";
import { prisma } from "@/lib/prisma";
// Note: this file has two layers of coverage, mirroring
// tests/purchase-orders.test.ts:
// - lib-level tests below exercise src/lib/asset-category-attribute-defs.ts
// directly (validation, FK/unique error handling, list ordering).
// - route-level tests (further down) exercise
// src/app/api/asset-category-attribute-defs/route.ts over HTTP, including
// the auth() gate.
vi.mock("@/auth", () => ({ auth: vi.fn() }));
import { GET, POST } from "@/app/api/asset-category-attribute-defs/route";
import { auth } from "@/auth";
const mockedAuth = auth as unknown as Mock<() => Promise<Session | null>>;
const testSession: Session = {
user: { id: "test-user-id" },
expires: new Date(Date.now() + 60 * 60 * 1000).toISOString(),
};
function postRequest(body: unknown): Request {
return new Request("http://localhost/api/asset-category-attribute-defs", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(body),
});
}
// AssetCategoryAttributeDef.categoryId is a real FK to AssetCategory (see
// schema.prisma / src/lib/asset-category-attribute-defs.ts) — tests that
// need valid ids create real rows first and use their ids. Mirrors
// tests/purchase-orders.test.ts's createTestVendor()/createTestEmployee()
// helpers.
function createTestAssetCategory(overrides: { name?: string } = {}) {
return prisma.assetCategory.create({
data: {
name: overrides.name ?? "Laptop",
valueGroup: "high_value",
},
});
}
beforeEach(async () => {
// Each test starts from empty tables so listing/order/uniqueness
// assertions are deterministic. asset_category_attribute_defs must be
// cleared before asset_categories since it has an FK (ON DELETE RESTRICT)
// to it.
await prisma.assetCategoryAttributeDef.deleteMany();
await prisma.assetCategory.deleteMany();
// Default to a logged-in session — the lib-level tests below don't go
// through the route at all (so this is a no-op for them), and the
// route-level tests exercise business logic, not auth gating (that's
// covered by the "auth gating" describe block further down).
mockedAuth.mockResolvedValue(testSession);
});
describe("validateCreateAssetCategoryAttributeDefInput", () => {
it("accepts valid input", () => {
const result = validateCreateAssetCategoryAttributeDefInput({
categoryId: "11111111-1111-1111-1111-111111111111",
key: "ram_gb",
label: "RAM (GB)",
dataType: "number",
required: true,
sortOrder: 1,
});
expect(result.ok).toBe(true);
if (result.ok) {
expect(result.value).toMatchObject({
categoryId: "11111111-1111-1111-1111-111111111111",
key: "ram_gb",
label: "RAM (GB)",
dataType: "number",
required: true,
sortOrder: 1,
});
}
});
it("accepts each of the four fixed dataType values", () => {
for (const dataType of ["string", "number", "boolean", "date"] as const) {
const result = validateCreateAssetCategoryAttributeDefInput({
categoryId: "11111111-1111-1111-1111-111111111111",
key: `key_${dataType}`,
label: "Label",
dataType,
required: false,
sortOrder: 0,
});
expect(result.ok).toBe(true);
}
});
it("rejects a missing categoryId", () => {
const result = validateCreateAssetCategoryAttributeDefInput({
key: "ram_gb",
label: "RAM (GB)",
dataType: "number",
required: true,
sortOrder: 1,
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toMatch(/categoryId/i);
}
});
it("rejects a categoryId that is not a valid UUID", () => {
const result = validateCreateAssetCategoryAttributeDefInput({
categoryId: "not-a-uuid",
key: "ram_gb",
label: "RAM (GB)",
dataType: "number",
required: true,
sortOrder: 1,
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toMatch(/categoryId/i);
}
});
it("rejects an empty key", () => {
const result = validateCreateAssetCategoryAttributeDefInput({
categoryId: "11111111-1111-1111-1111-111111111111",
key: "",
label: "RAM (GB)",
dataType: "number",
required: true,
sortOrder: 1,
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toMatch(/key/i);
}
});
it("rejects an empty label", () => {
const result = validateCreateAssetCategoryAttributeDefInput({
categoryId: "11111111-1111-1111-1111-111111111111",
key: "ram_gb",
label: "",
dataType: "number",
required: true,
sortOrder: 1,
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toMatch(/label/i);
}
});
it("rejects an invalid dataType", () => {
const result = validateCreateAssetCategoryAttributeDefInput({
categoryId: "11111111-1111-1111-1111-111111111111",
key: "ram_gb",
label: "RAM (GB)",
dataType: "float",
required: true,
sortOrder: 1,
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toMatch(/dataType/i);
}
});
it("rejects a missing dataType", () => {
const result = validateCreateAssetCategoryAttributeDefInput({
categoryId: "11111111-1111-1111-1111-111111111111",
key: "ram_gb",
label: "RAM (GB)",
required: true,
sortOrder: 1,
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toMatch(/dataType/i);
}
});
it("rejects a non-boolean required", () => {
const result = validateCreateAssetCategoryAttributeDefInput({
categoryId: "11111111-1111-1111-1111-111111111111",
key: "ram_gb",
label: "RAM (GB)",
dataType: "number",
required: "yes",
sortOrder: 1,
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toMatch(/required/i);
}
});
it("rejects a non-integer sortOrder", () => {
const result = validateCreateAssetCategoryAttributeDefInput({
categoryId: "11111111-1111-1111-1111-111111111111",
key: "ram_gb",
label: "RAM (GB)",
dataType: "number",
required: true,
sortOrder: 1.5,
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toMatch(/sortOrder/i);
}
});
it("rejects a missing sortOrder", () => {
const result = validateCreateAssetCategoryAttributeDefInput({
categoryId: "11111111-1111-1111-1111-111111111111",
key: "ram_gb",
label: "RAM (GB)",
dataType: "number",
required: true,
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toMatch(/sortOrder/i);
}
});
});
describe("createAssetCategoryAttributeDef", () => {
it("creates an attribute def for a real category", async () => {
const category = await createTestAssetCategory();
const result = validateCreateAssetCategoryAttributeDefInput({
categoryId: category.id,
key: "ram_gb",
label: "RAM (GB)",
dataType: "number",
required: true,
sortOrder: 1,
});
expect(result.ok).toBe(true);
if (!result.ok) return;
const created = await createAssetCategoryAttributeDef(result.value);
expect(created.ok).toBe(true);
if (!created.ok) return;
expect(created.value).toMatchObject({
category_id: category.id,
key: "ram_gb",
label: "RAM (GB)",
data_type: "number",
required: true,
sort_order: 1,
});
expect(typeof created.value.id).toBe("string");
expect(created.value.id.length).toBeGreaterThan(0);
expect(Number.isNaN(Date.parse(created.value.created_at))).toBe(false);
});
it("returns a clean error when categoryId doesn't reference an existing category", async () => {
const result = validateCreateAssetCategoryAttributeDefInput({
categoryId: "00000000-0000-0000-0000-000000000000",
key: "ram_gb",
label: "RAM (GB)",
dataType: "number",
required: true,
sortOrder: 1,
});
expect(result.ok).toBe(true);
if (!result.ok) return;
const created = await createAssetCategoryAttributeDef(result.value);
expect(created.ok).toBe(false);
if (created.ok) return;
expect(created.error).toMatch(/categoryId/i);
expect(created.error).toMatch(/does not reference an existing asset category/i);
const remaining = await prisma.assetCategoryAttributeDef.findMany();
expect(remaining).toHaveLength(0);
});
it("rejects a duplicate key within the same category with a clean 400-style error", async () => {
const category = await createTestAssetCategory();
const first = validateCreateAssetCategoryAttributeDefInput({
categoryId: category.id,
key: "ram_gb",
label: "RAM (GB)",
dataType: "number",
required: true,
sortOrder: 1,
});
expect(first.ok).toBe(true);
if (first.ok) {
const firstCreated = await createAssetCategoryAttributeDef(first.value);
expect(firstCreated.ok).toBe(true);
}
const second = validateCreateAssetCategoryAttributeDefInput({
categoryId: category.id,
key: "ram_gb",
label: "RAM (GB) duplicate",
dataType: "number",
required: false,
sortOrder: 2,
});
expect(second.ok).toBe(true);
if (!second.ok) return;
const created = await createAssetCategoryAttributeDef(second.value);
expect(created.ok).toBe(false);
if (created.ok) return;
expect(created.error).toMatch(/key/i);
const remaining = await prisma.assetCategoryAttributeDef.findMany();
expect(remaining).toHaveLength(1);
});
it("allows the same key across two different categories", async () => {
const laptop = await createTestAssetCategory({ name: "Laptop" });
const monitor = await createTestAssetCategory({ name: "Màn hình" });
const first = validateCreateAssetCategoryAttributeDefInput({
categoryId: laptop.id,
key: "weight_kg",
label: "Khối lượng (kg)",
dataType: "number",
required: false,
sortOrder: 1,
});
expect(first.ok).toBe(true);
if (first.ok) {
const firstCreated = await createAssetCategoryAttributeDef(first.value);
expect(firstCreated.ok).toBe(true);
}
const second = validateCreateAssetCategoryAttributeDefInput({
categoryId: monitor.id,
key: "weight_kg",
label: "Khối lượng (kg)",
dataType: "number",
required: false,
sortOrder: 1,
});
expect(second.ok).toBe(true);
if (!second.ok) return;
const created = await createAssetCategoryAttributeDef(second.value);
expect(created.ok).toBe(true);
if (!created.ok) return;
expect(created.value.category_id).toBe(monitor.id);
expect(created.value.key).toBe("weight_kg");
const remaining = await prisma.assetCategoryAttributeDef.findMany();
expect(remaining).toHaveLength(2);
});
});
describe("listAssetCategoryAttributeDefs", () => {
it("lists created attribute defs, newest first", async () => {
const category = await createTestAssetCategory();
const first = validateCreateAssetCategoryAttributeDefInput({
categoryId: category.id,
key: "ram_gb",
label: "RAM (GB)",
dataType: "number",
required: true,
sortOrder: 1,
});
expect(first.ok).toBe(true);
if (first.ok) await createAssetCategoryAttributeDef(first.value);
// Ensure a distinct created_at ordering even on fast filesystems/clocks.
await new Promise((resolve) => setTimeout(resolve, 5));
const second = validateCreateAssetCategoryAttributeDefInput({
categoryId: category.id,
key: "cpu",
label: "CPU",
dataType: "string",
required: false,
sortOrder: 2,
});
expect(second.ok).toBe(true);
if (second.ok) await createAssetCategoryAttributeDef(second.value);
const records = await listAssetCategoryAttributeDefs();
expect(records).toHaveLength(2);
// newest first
expect(records[0]).toMatchObject({ key: "cpu" });
expect(records[1]).toMatchObject({ key: "ram_gb" });
});
it("returns an empty list when there are no attribute defs", async () => {
const records = await listAssetCategoryAttributeDefs();
expect(records).toEqual([]);
});
});
describe("POST /api/asset-category-attribute-defs", () => {
it("creates an attribute def on valid input", async () => {
const category = await createTestAssetCategory();
const res = await POST(
postRequest({
categoryId: category.id,
key: "ram_gb",
label: "RAM (GB)",
dataType: "number",
required: true,
sortOrder: 1,
})
);
expect(res.status).toBe(201);
const body = await res.json();
expect(body).toMatchObject({
category_id: category.id,
key: "ram_gb",
label: "RAM (GB)",
data_type: "number",
required: true,
sort_order: 1,
});
expect(typeof body.id).toBe("string");
expect(body.id.length).toBeGreaterThan(0);
expect(Number.isNaN(Date.parse(body.created_at))).toBe(false);
});
it("rejects an invalid dataType", async () => {
const category = await createTestAssetCategory();
const res = await POST(
postRequest({
categoryId: category.id,
key: "ram_gb",
label: "RAM (GB)",
dataType: "float",
required: true,
sortOrder: 1,
})
);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/dataType/i);
});
it("rejects a categoryId that is a well-formed UUID but doesn't reference an existing category", async () => {
const res = await POST(
postRequest({
categoryId: "00000000-0000-0000-0000-000000000000",
key: "ram_gb",
label: "RAM (GB)",
dataType: "number",
required: true,
sortOrder: 1,
})
);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/categoryId/i);
expect(body.error).toMatch(/does not reference an existing asset category/i);
const remaining = await prisma.assetCategoryAttributeDef.findMany();
expect(remaining).toHaveLength(0);
});
it("rejects a duplicate (categoryId, key) with a clean 400, not a raw 500", async () => {
const category = await createTestAssetCategory();
const first = await POST(
postRequest({
categoryId: category.id,
key: "ram_gb",
label: "RAM (GB)",
dataType: "number",
required: true,
sortOrder: 1,
})
);
expect(first.status).toBe(201);
const second = await POST(
postRequest({
categoryId: category.id,
key: "ram_gb",
label: "RAM (GB) duplicate",
dataType: "number",
required: false,
sortOrder: 2,
})
);
expect(second.status).toBe(400);
const body = await second.json();
expect(body.error).toMatch(/key/i);
const remaining = await prisma.assetCategoryAttributeDef.findMany();
expect(remaining).toHaveLength(1);
});
it("allows the same key across two different categories via the route", async () => {
const laptop = await createTestAssetCategory({ name: "Laptop" });
const monitor = await createTestAssetCategory({ name: "Màn hình" });
const first = await POST(
postRequest({
categoryId: laptop.id,
key: "weight_kg",
label: "Khối lượng (kg)",
dataType: "number",
required: false,
sortOrder: 1,
})
);
expect(first.status).toBe(201);
const second = await POST(
postRequest({
categoryId: monitor.id,
key: "weight_kg",
label: "Khối lượng (kg)",
dataType: "number",
required: false,
sortOrder: 1,
})
);
expect(second.status).toBe(201);
const remaining = await prisma.assetCategoryAttributeDef.findMany();
expect(remaining).toHaveLength(2);
});
it("rejects an invalid JSON body", async () => {
const req = new Request("http://localhost/api/asset-category-attribute-defs", {
method: "POST",
headers: { "content-type": "application/json" },
body: "not json",
});
const res = await POST(req);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/JSON/i);
});
it("rejects a non-object JSON body", async () => {
// Note: arrays are `typeof "object"` in JS, so they pass the route's
// object check (same behavior as /api/asset-categories) — use a JSON
// string primitive here to actually exercise the "not an object" branch.
const res = await POST(postRequest("not an object"));
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/JSON object/i);
});
});
describe("GET /api/asset-category-attribute-defs", () => {
it("lists created attribute defs, newest first", async () => {
const category = await createTestAssetCategory();
await POST(
postRequest({
categoryId: category.id,
key: "ram_gb",
label: "RAM (GB)",
dataType: "number",
required: true,
sortOrder: 1,
})
);
// Ensure a distinct created_at ordering even on fast filesystems/clocks.
await new Promise((resolve) => setTimeout(resolve, 5));
await POST(
postRequest({
categoryId: category.id,
key: "cpu",
label: "CPU",
dataType: "string",
required: false,
sortOrder: 2,
})
);
const res = await GET();
expect(res.status).toBe(200);
const body = await res.json();
expect(Array.isArray(body)).toBe(true);
expect(body).toHaveLength(2);
// newest first
expect(body[0]).toMatchObject({ key: "cpu" });
expect(body[1]).toMatchObject({ key: "ram_gb" });
});
it("returns an empty list when there are no attribute defs", async () => {
const res = await GET();
expect(res.status).toBe(200);
const body = await res.json();
expect(body).toEqual([]);
});
});
describe("auth gating on /api/asset-category-attribute-defs", () => {
// Mirrors tests/asset-categories.test.ts's "auth gating" block — only
// checks session presence, per the flat permission model (no
// role/permission logic).
it("POST /api/asset-category-attribute-defs returns 401 without a session", async () => {
const category = await createTestAssetCategory();
mockedAuth.mockResolvedValue(null);
const res = await POST(
postRequest({
categoryId: category.id,
key: "ram_gb",
label: "RAM (GB)",
dataType: "number",
required: true,
sortOrder: 1,
})
);
expect(res.status).toBe(401);
const body = await res.json();
expect(body).toEqual({ error: "Unauthorized" });
// And it must not have created anything.
const remaining = await prisma.assetCategoryAttributeDef.findMany();
expect(remaining).toHaveLength(0);
});
it("GET /api/asset-category-attribute-defs returns 401 without a session", async () => {
mockedAuth.mockResolvedValue(null);
const res = await GET();
expect(res.status).toBe(401);
const body = await res.json();
expect(body).toEqual({ error: "Unauthorized" });
});
});
+731
View File
@@ -0,0 +1,731 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { Mock } from "vitest";
import type { Session } from "next-auth";
import {
createAssetModel,
listAssetModels,
validateCreateAssetModelInput,
} from "@/lib/asset-models";
import { prisma } from "@/lib/prisma";
// Note: this file has two layers of coverage, mirroring
// tests/asset-category-attribute-defs.test.ts:
// - lib-level tests below exercise src/lib/asset-models.ts directly
// (validation, FK/business-rule error handling, list ordering).
// - route-level tests (further down) exercise
// src/app/api/asset-models/route.ts over HTTP, including the auth() gate,
// malformed JSON, and non-object bodies.
vi.mock("@/auth", () => ({ auth: vi.fn() }));
import { GET, POST } from "@/app/api/asset-models/route";
import { auth } from "@/auth";
const mockedAuth = auth as unknown as Mock<() => Promise<Session | null>>;
const testSession: Session = {
user: { id: "test-user-id" },
expires: new Date(Date.now() + 60 * 60 * 1000).toISOString(),
};
function postRequest(body: unknown): Request {
return new Request("http://localhost/api/asset-models", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(body),
});
}
// AssetModel.categoryId is a real FK to AssetCategory, and the referenced
// category must be valueGroup=high_value (see docs/
// asset-license-management.md's "Cập nhật 2026-08-02 (2)" section). Tests
// that need valid ids create real rows first and use their ids. Mirrors
// tests/asset-category-attribute-defs.test.ts's createTestAssetCategory()
// helper.
function createTestAssetCategory(
overrides: { name?: string; valueGroup?: "high_value" | "low_value" } = {}
) {
return prisma.assetCategory.create({
data: {
name: overrides.name ?? "Laptop",
valueGroup: overrides.valueGroup ?? "high_value",
},
});
}
// Helper for the dynamic-specs-validation tests below: creates an
// AssetCategoryAttributeDef row directly via Prisma (no need to go through
// src/lib/asset-category-attribute-defs.ts's validation for test setup).
// Mirrors tests/asset-category-attribute-defs.test.ts's
// createTestAssetCategory() helper pattern.
function createTestAttributeDef(overrides: {
categoryId: string;
key: string;
label?: string;
dataType: "string" | "number" | "boolean" | "date";
required: boolean;
sortOrder?: number;
}) {
return prisma.assetCategoryAttributeDef.create({
data: {
categoryId: overrides.categoryId,
key: overrides.key,
label: overrides.label ?? overrides.key,
dataType: overrides.dataType,
required: overrides.required,
sortOrder: overrides.sortOrder ?? 0,
},
});
}
beforeEach(async () => {
// Each test starts from empty tables so listing/order assertions are
// deterministic. asset_models must be cleared before asset_categories
// since it has an FK (ON DELETE RESTRICT) to it. asset_category_attribute_defs
// also has an FK (ON DELETE RESTRICT) to asset_categories, so it must be
// cleared before asset_categories too — mirrors tests/
// asset-category-attribute-defs.test.ts's cleanup order.
await prisma.assetModel.deleteMany();
await prisma.assetCategoryAttributeDef.deleteMany();
await prisma.assetCategory.deleteMany();
// Default to a logged-in session — the lib-level tests below don't go
// through the route at all (so this is a no-op for them), and the
// route-level tests exercise business logic, not auth gating (that's
// covered by the "auth gating" describe block further down).
mockedAuth.mockResolvedValue(testSession);
});
describe("validateCreateAssetModelInput", () => {
it("accepts valid input with a non-trivial specs object", () => {
const result = validateCreateAssetModelInput({
categoryId: "11111111-1111-1111-1111-111111111111",
manufacturer: "Dell",
name: "Latitude 5420",
specs: { ram_gb: 16, cpu: "i7-1165G7" },
});
expect(result.ok).toBe(true);
if (result.ok) {
expect(result.value).toMatchObject({
categoryId: "11111111-1111-1111-1111-111111111111",
manufacturer: "Dell",
name: "Latitude 5420",
specs: { ram_gb: 16, cpu: "i7-1165G7" },
});
}
});
it("defaults specs to {} when omitted", () => {
const result = validateCreateAssetModelInput({
categoryId: "11111111-1111-1111-1111-111111111111",
manufacturer: "Dell",
name: "Latitude 5420",
});
expect(result.ok).toBe(true);
if (result.ok) {
expect(result.value.specs).toEqual({});
}
});
it("rejects a missing categoryId", () => {
const result = validateCreateAssetModelInput({
manufacturer: "Dell",
name: "Latitude 5420",
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toMatch(/categoryId/i);
}
});
it("rejects a categoryId that is not a valid UUID", () => {
const result = validateCreateAssetModelInput({
categoryId: "not-a-uuid",
manufacturer: "Dell",
name: "Latitude 5420",
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toMatch(/categoryId/i);
}
});
it("rejects an empty manufacturer", () => {
const result = validateCreateAssetModelInput({
categoryId: "11111111-1111-1111-1111-111111111111",
manufacturer: "",
name: "Latitude 5420",
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toMatch(/manufacturer/i);
}
});
it("rejects an empty name", () => {
const result = validateCreateAssetModelInput({
categoryId: "11111111-1111-1111-1111-111111111111",
manufacturer: "Dell",
name: "",
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toMatch(/name/i);
}
});
it("rejects a specs value that is not a plain object", () => {
const result = validateCreateAssetModelInput({
categoryId: "11111111-1111-1111-1111-111111111111",
manufacturer: "Dell",
name: "Latitude 5420",
specs: "not an object",
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toMatch(/specs/i);
}
});
it("rejects a specs value that is an array", () => {
const result = validateCreateAssetModelInput({
categoryId: "11111111-1111-1111-1111-111111111111",
manufacturer: "Dell",
name: "Latitude 5420",
specs: ["not", "an", "object"],
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toMatch(/specs/i);
}
});
});
describe("createAssetModel", () => {
it("creates a model for a real high_value category, with a non-trivial specs object", async () => {
const category = await createTestAssetCategory({ valueGroup: "high_value" });
await createTestAttributeDef({
categoryId: category.id,
key: "ram_gb",
dataType: "number",
required: true,
});
await createTestAttributeDef({
categoryId: category.id,
key: "cpu",
dataType: "string",
required: true,
});
const result = validateCreateAssetModelInput({
categoryId: category.id,
manufacturer: "Dell",
name: "Latitude 5420",
specs: { ram_gb: 16, cpu: "i7-1165G7" },
});
expect(result.ok).toBe(true);
if (!result.ok) return;
const created = await createAssetModel(result.value);
expect(created.ok).toBe(true);
if (!created.ok) return;
expect(created.value).toMatchObject({
category_id: category.id,
manufacturer: "Dell",
name: "Latitude 5420",
specs: { ram_gb: 16, cpu: "i7-1165G7" },
});
expect(typeof created.value.id).toBe("string");
expect(created.value.id.length).toBeGreaterThan(0);
expect(Number.isNaN(Date.parse(created.value.created_at))).toBe(false);
});
it("defaults specs to {} in the created record when omitted", async () => {
const category = await createTestAssetCategory({ valueGroup: "high_value" });
const result = validateCreateAssetModelInput({
categoryId: category.id,
manufacturer: "Apple",
name: "MacBook Pro 14",
});
expect(result.ok).toBe(true);
if (!result.ok) return;
const created = await createAssetModel(result.value);
expect(created.ok).toBe(true);
if (!created.ok) return;
expect(created.value.specs).toEqual({});
});
it("returns a clean error when categoryId doesn't reference an existing category", async () => {
const result = validateCreateAssetModelInput({
categoryId: "00000000-0000-0000-0000-000000000000",
manufacturer: "Dell",
name: "Latitude 5420",
});
expect(result.ok).toBe(true);
if (!result.ok) return;
const created = await createAssetModel(result.value);
expect(created.ok).toBe(false);
if (created.ok) return;
expect(created.error).toMatch(/categoryId/i);
expect(created.error).toMatch(/does not reference an existing asset category/i);
const remaining = await prisma.assetModel.findMany();
expect(remaining).toHaveLength(0);
});
it("returns a distinct clean error when categoryId references an existing but low_value category", async () => {
const category = await createTestAssetCategory({
name: "Cáp HDMI",
valueGroup: "low_value",
});
const result = validateCreateAssetModelInput({
categoryId: category.id,
manufacturer: "Dell",
name: "Latitude 5420",
});
expect(result.ok).toBe(true);
if (!result.ok) return;
const created = await createAssetModel(result.value);
expect(created.ok).toBe(false);
if (created.ok) return;
expect(created.error).toMatch(/high_value/i);
// Distinct message from the "doesn't exist at all" case.
expect(created.error).not.toMatch(/does not reference an existing asset category/i);
const remaining = await prisma.assetModel.findMany();
expect(remaining).toHaveLength(0);
});
});
// Dynamic specs validation, per docs/asset-license-management.md's "Cập
// nhật 2026-08-02 (3)" section: createAssetModel() builds a Zod schema at
// runtime from the category's AssetCategoryAttributeDef rows and validates
// `specs` against it — unknown keys rejected, required keys enforced, and
// each key's value type-checked per its dataType.
describe("createAssetModel: dynamic specs validation", () => {
it("accepts specs that supply all required keys with correct types", async () => {
const category = await createTestAssetCategory({ valueGroup: "high_value" });
await createTestAttributeDef({
categoryId: category.id,
key: "ram_gb",
dataType: "number",
required: true,
});
await createTestAttributeDef({
categoryId: category.id,
key: "cpu",
dataType: "string",
required: true,
});
await createTestAttributeDef({
categoryId: category.id,
key: "has_touchscreen",
dataType: "boolean",
required: true,
});
const result = validateCreateAssetModelInput({
categoryId: category.id,
manufacturer: "Dell",
name: "Latitude 5420",
specs: { ram_gb: 16, cpu: "i7-1165G7", has_touchscreen: false },
});
expect(result.ok).toBe(true);
if (!result.ok) return;
const created = await createAssetModel(result.value);
expect(created.ok).toBe(true);
if (!created.ok) return;
expect(created.value.specs).toEqual({ ram_gb: 16, cpu: "i7-1165G7", has_touchscreen: false });
});
it("rejects specs missing a required key with a clean 400-style error", async () => {
const category = await createTestAssetCategory({ valueGroup: "high_value" });
await createTestAttributeDef({
categoryId: category.id,
key: "ram_gb",
dataType: "number",
required: true,
});
const result = validateCreateAssetModelInput({
categoryId: category.id,
manufacturer: "Dell",
name: "Latitude 5420",
specs: {},
});
expect(result.ok).toBe(true);
if (!result.ok) return;
const created = await createAssetModel(result.value);
expect(created.ok).toBe(false);
if (created.ok) return;
expect(created.error).toMatch(/ram_gb/i);
const remaining = await prisma.assetModel.findMany();
expect(remaining).toHaveLength(0);
});
it("rejects a specs value of the wrong type for its dataType (string where number is expected)", async () => {
const category = await createTestAssetCategory({ valueGroup: "high_value" });
await createTestAttributeDef({
categoryId: category.id,
key: "ram_gb",
dataType: "number",
required: true,
});
const result = validateCreateAssetModelInput({
categoryId: category.id,
manufacturer: "Dell",
name: "Latitude 5420",
specs: { ram_gb: "16" },
});
expect(result.ok).toBe(true);
if (!result.ok) return;
const created = await createAssetModel(result.value);
expect(created.ok).toBe(false);
if (created.ok) return;
expect(created.error).toMatch(/ram_gb/i);
const remaining = await prisma.assetModel.findMany();
expect(remaining).toHaveLength(0);
});
it("rejects a specs key not defined as an AssetCategoryAttributeDef for that category", async () => {
const category = await createTestAssetCategory({ valueGroup: "high_value" });
await createTestAttributeDef({
categoryId: category.id,
key: "ram_gb",
dataType: "number",
required: true,
});
const result = validateCreateAssetModelInput({
categoryId: category.id,
manufacturer: "Dell",
name: "Latitude 5420",
specs: { ram_gb: 16, unknown_key: "surprise" },
});
expect(result.ok).toBe(true);
if (!result.ok) return;
const created = await createAssetModel(result.value);
expect(created.ok).toBe(false);
if (created.ok) return;
expect(created.error).toMatch(/specs/i);
const remaining = await prisma.assetModel.findMany();
expect(remaining).toHaveLength(0);
});
it("accepts specs that omit an optional (required=false) attribute def key", async () => {
const category = await createTestAssetCategory({ valueGroup: "high_value" });
await createTestAttributeDef({
categoryId: category.id,
key: "ram_gb",
dataType: "number",
required: true,
});
await createTestAttributeDef({
categoryId: category.id,
key: "weight_kg",
dataType: "number",
required: false,
});
const result = validateCreateAssetModelInput({
categoryId: category.id,
manufacturer: "Dell",
name: "Latitude 5420",
specs: { ram_gb: 16 },
});
expect(result.ok).toBe(true);
if (!result.ok) return;
const created = await createAssetModel(result.value);
expect(created.ok).toBe(true);
if (!created.ok) return;
expect(created.value.specs).toEqual({ ram_gb: 16 });
});
it("accepts a valid date string for a date-typed attribute def", async () => {
const category = await createTestAssetCategory({ valueGroup: "high_value" });
await createTestAttributeDef({
categoryId: category.id,
key: "warranty_end",
dataType: "date",
required: true,
});
const result = validateCreateAssetModelInput({
categoryId: category.id,
manufacturer: "Dell",
name: "Latitude 5420",
specs: { warranty_end: "2027-01-01" },
});
expect(result.ok).toBe(true);
if (!result.ok) return;
const created = await createAssetModel(result.value);
expect(created.ok).toBe(true);
if (!created.ok) return;
expect(created.value.specs).toEqual({ warranty_end: "2027-01-01" });
});
});
describe("listAssetModels", () => {
it("lists created models, newest first", async () => {
const category = await createTestAssetCategory({ valueGroup: "high_value" });
const first = validateCreateAssetModelInput({
categoryId: category.id,
manufacturer: "Dell",
name: "Latitude 5420",
});
expect(first.ok).toBe(true);
if (first.ok) await createAssetModel(first.value);
// Ensure a distinct created_at ordering even on fast filesystems/clocks.
await new Promise((resolve) => setTimeout(resolve, 5));
const second = validateCreateAssetModelInput({
categoryId: category.id,
manufacturer: "Apple",
name: "MacBook Pro 14",
});
expect(second.ok).toBe(true);
if (second.ok) await createAssetModel(second.value);
const records = await listAssetModels();
expect(records).toHaveLength(2);
// newest first
expect(records[0]).toMatchObject({ name: "MacBook Pro 14" });
expect(records[1]).toMatchObject({ name: "Latitude 5420" });
});
it("returns an empty list when there are no models", async () => {
const records = await listAssetModels();
expect(records).toEqual([]);
});
});
describe("POST /api/asset-models", () => {
it("creates a model on valid input", async () => {
const category = await createTestAssetCategory({ valueGroup: "high_value" });
await createTestAttributeDef({
categoryId: category.id,
key: "ram_gb",
dataType: "number",
required: true,
});
await createTestAttributeDef({
categoryId: category.id,
key: "cpu",
dataType: "string",
required: true,
});
const res = await POST(
postRequest({
categoryId: category.id,
manufacturer: "Dell",
name: "Latitude 5420",
specs: { ram_gb: 16, cpu: "i7-1165G7" },
})
);
expect(res.status).toBe(201);
const body = await res.json();
expect(body).toMatchObject({
category_id: category.id,
manufacturer: "Dell",
name: "Latitude 5420",
specs: { ram_gb: 16, cpu: "i7-1165G7" },
});
expect(typeof body.id).toBe("string");
expect(body.id.length).toBeGreaterThan(0);
expect(Number.isNaN(Date.parse(body.created_at))).toBe(false);
});
it("defaults specs to {} when omitted from the request body", async () => {
const category = await createTestAssetCategory({ valueGroup: "high_value" });
const res = await POST(
postRequest({
categoryId: category.id,
manufacturer: "Dell",
name: "Latitude 5420",
})
);
expect(res.status).toBe(201);
const body = await res.json();
expect(body.specs).toEqual({});
});
it("rejects a missing manufacturer", async () => {
const category = await createTestAssetCategory({ valueGroup: "high_value" });
const res = await POST(
postRequest({
categoryId: category.id,
name: "Latitude 5420",
})
);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/manufacturer/i);
});
it("rejects a categoryId that is a well-formed UUID but doesn't reference an existing category", async () => {
const res = await POST(
postRequest({
categoryId: "00000000-0000-0000-0000-000000000000",
manufacturer: "Dell",
name: "Latitude 5420",
})
);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/categoryId/i);
expect(body.error).toMatch(/does not reference an existing asset category/i);
const remaining = await prisma.assetModel.findMany();
expect(remaining).toHaveLength(0);
});
it("rejects a categoryId referencing an existing but low_value category, with a distinct 400", async () => {
const category = await createTestAssetCategory({
name: "Cáp HDMI",
valueGroup: "low_value",
});
const res = await POST(
postRequest({
categoryId: category.id,
manufacturer: "Dell",
name: "Latitude 5420",
})
);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/high_value/i);
const remaining = await prisma.assetModel.findMany();
expect(remaining).toHaveLength(0);
});
it("rejects an invalid JSON body", async () => {
const req = new Request("http://localhost/api/asset-models", {
method: "POST",
headers: { "content-type": "application/json" },
body: "not json",
});
const res = await POST(req);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/JSON/i);
});
it("rejects a non-object JSON body", async () => {
// Note: arrays are `typeof "object"` in JS, so they pass the route's
// object check (same behavior as /api/asset-category-attribute-defs) —
// use a JSON string primitive here to actually exercise the "not an
// object" branch.
const res = await POST(postRequest("not an object"));
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/JSON object/i);
});
});
describe("GET /api/asset-models", () => {
it("lists created models, newest first", async () => {
const category = await createTestAssetCategory({ valueGroup: "high_value" });
await POST(
postRequest({
categoryId: category.id,
manufacturer: "Dell",
name: "Latitude 5420",
})
);
// Ensure a distinct created_at ordering even on fast filesystems/clocks.
await new Promise((resolve) => setTimeout(resolve, 5));
await POST(
postRequest({
categoryId: category.id,
manufacturer: "Apple",
name: "MacBook Pro 14",
})
);
const res = await GET();
expect(res.status).toBe(200);
const body = await res.json();
expect(Array.isArray(body)).toBe(true);
expect(body).toHaveLength(2);
// newest first
expect(body[0]).toMatchObject({ name: "MacBook Pro 14" });
expect(body[1]).toMatchObject({ name: "Latitude 5420" });
});
it("returns an empty list when there are no models", async () => {
const res = await GET();
expect(res.status).toBe(200);
const body = await res.json();
expect(body).toEqual([]);
});
});
describe("auth gating on /api/asset-models", () => {
// Mirrors tests/asset-category-attribute-defs.test.ts's "auth gating"
// block — only checks session presence, per the flat permission model (no
// role/permission logic).
it("POST /api/asset-models returns 401 without a session", async () => {
const category = await createTestAssetCategory({ valueGroup: "high_value" });
mockedAuth.mockResolvedValue(null);
const res = await POST(
postRequest({
categoryId: category.id,
manufacturer: "Dell",
name: "Latitude 5420",
})
);
expect(res.status).toBe(401);
const body = await res.json();
expect(body).toEqual({ error: "Unauthorized" });
// And it must not have created anything.
const remaining = await prisma.assetModel.findMany();
expect(remaining).toHaveLength(0);
});
it("GET /api/asset-models returns 401 without a session", async () => {
mockedAuth.mockResolvedValue(null);
const res = await GET();
expect(res.status).toBe(401);
const body = await res.json();
expect(body).toEqual({ error: "Unauthorized" });
});
});
+105 -24
View File
@@ -50,16 +50,52 @@ function createTestPurchaseOrder(overrides: { vendorId?: string } = {}) {
});
}
// AssetItem.modelId is a real FK to AssetModel (see schema.prisma /
// src/lib/assets.ts), and AssetModel.categoryId is a real FK to
// AssetCategory — tests that need a valid modelId create the full chain
// first and use the model's id. Mirrors tests/asset-models.test.ts's
// createTestAssetCategory() helper. Category is always high_value here —
// AssetModel only ever references high_value categories per
// src/lib/asset-models.ts's business rule.
function createTestAssetCategory(overrides: { name?: string } = {}) {
return prisma.assetCategory.create({
data: {
name: overrides.name ?? "Laptop",
valueGroup: "high_value",
},
});
}
function createTestAssetModel(overrides: { categoryId?: string } = {}) {
return prisma.assetModel.create({
data: {
categoryId: overrides.categoryId as string,
manufacturer: "Dell",
name: "Latitude 5440",
},
});
}
async function createTestModelId(): Promise<string> {
const category = await createTestAssetCategory();
const model = await createTestAssetModel({ categoryId: category.id });
return model.id;
}
beforeEach(async () => {
// Each test starts from empty tables so listing/order assertions are
// deterministic. asset_items must be cleared before purchase_orders/
// vendors since asset_items has FKs (ON DELETE SET NULL to
// purchase_orders) — deleteMany order avoids relying on that cascade
// behavior in tests. purchase_orders must be cleared before vendors
// (ON DELETE RESTRICT to vendors, mirrors tests/purchase-orders.test.ts).
// vendors/asset_models/asset_categories since asset_items has FKs to all
// of them — deleteMany order avoids relying on cascade behavior in tests.
// purchase_orders must be cleared before vendors (ON DELETE RESTRICT to
// vendors, mirrors tests/purchase-orders.test.ts). asset_models must be
// cleared before asset_categories (ON DELETE RESTRICT, mirrors
// tests/asset-models.test.ts's cleanup order).
await prisma.assetItem.deleteMany();
await prisma.purchaseOrder.deleteMany();
await prisma.vendor.deleteMany();
await prisma.assetModel.deleteMany();
await prisma.assetCategory.deleteMany();
// Default to a logged-in session — the tests below exercise business
// logic, not auth gating (that's covered by the "auth gating" describe
// block further down).
@@ -68,8 +104,9 @@ beforeEach(async () => {
describe("POST /api/assets", () => {
it("creates an asset item in in_stock status on valid input, with no warrantyEndDate", async () => {
const modelId = await createTestModelId();
const res = await POST(
postRequest({ serialNumber: "SN-001", model: "Dell Latitude 5440", purchaseDate: "2026-01-15" })
postRequest({ serialNumber: "SN-001", modelId, purchaseDate: "2026-01-15" })
);
expect(res.status).toBe(201);
@@ -77,7 +114,7 @@ describe("POST /api/assets", () => {
expect(body).toMatchObject({
serial_number: "SN-001",
model: "Dell Latitude 5440",
model_id: modelId,
status: "in_stock",
warranty_end_date: null,
});
@@ -88,10 +125,11 @@ describe("POST /api/assets", () => {
});
it("creates an asset item with a warrantyEndDate", async () => {
const modelId = await createTestModelId();
const res = await POST(
postRequest({
serialNumber: "SN-002",
model: "Dell Latitude 5440",
modelId,
purchaseDate: "2026-01-15",
warrantyEndDate: "2029-01-15",
})
@@ -103,24 +141,26 @@ describe("POST /api/assets", () => {
});
it("rejects an empty serialNumber", async () => {
const res = await POST(postRequest({ serialNumber: "", model: "Dell", purchaseDate: "2026-01-15" }));
const modelId = await createTestModelId();
const res = await POST(postRequest({ serialNumber: "", modelId, purchaseDate: "2026-01-15" }));
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/serialNumber/i);
});
it("rejects an empty model", async () => {
const res = await POST(postRequest({ serialNumber: "SN-003", model: "", purchaseDate: "2026-01-15" }));
it("rejects a missing modelId", async () => {
const res = await POST(postRequest({ serialNumber: "SN-003", purchaseDate: "2026-01-15" }));
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/model/i);
expect(body.error).toMatch(/modelId/i);
});
it("rejects an invalid purchaseDate", async () => {
const modelId = await createTestModelId();
const res = await POST(
postRequest({ serialNumber: "SN-004", model: "Dell", purchaseDate: "not-a-date" })
postRequest({ serialNumber: "SN-004", modelId, purchaseDate: "not-a-date" })
);
expect(res.status).toBe(400);
@@ -129,10 +169,11 @@ describe("POST /api/assets", () => {
});
it("rejects an invalid warrantyEndDate", async () => {
const modelId = await createTestModelId();
const res = await POST(
postRequest({
serialNumber: "SN-005",
model: "Dell",
modelId,
purchaseDate: "2026-01-15",
warrantyEndDate: "not-a-date",
})
@@ -144,13 +185,14 @@ describe("POST /api/assets", () => {
});
it("creates an asset item with a purchaseOrderId that references a real purchase order", async () => {
const modelId = await createTestModelId();
const vendor = await createTestVendor();
const purchaseOrder = await createTestPurchaseOrder({ vendorId: vendor.id });
const res = await POST(
postRequest({
serialNumber: "SN-007",
model: "Dell Latitude 5440",
modelId,
purchaseDate: "2026-01-15",
purchaseOrderId: purchaseOrder.id,
})
@@ -162,8 +204,9 @@ describe("POST /api/assets", () => {
});
it("creates an asset item with no purchaseOrderId (stays null)", async () => {
const modelId = await createTestModelId();
const res = await POST(
postRequest({ serialNumber: "SN-008", model: "Dell Latitude 5440", purchaseDate: "2026-01-15" })
postRequest({ serialNumber: "SN-008", modelId, purchaseDate: "2026-01-15" })
);
expect(res.status).toBe(201);
@@ -172,10 +215,11 @@ describe("POST /api/assets", () => {
});
it("rejects a purchaseOrderId that is a well-formed UUID but doesn't reference an existing purchase order", async () => {
const modelId = await createTestModelId();
const res = await POST(
postRequest({
serialNumber: "SN-009",
model: "Dell Latitude 5440",
modelId,
purchaseDate: "2026-01-15",
purchaseOrderId: "00000000-0000-0000-0000-000000000000",
})
@@ -192,10 +236,11 @@ describe("POST /api/assets", () => {
});
it("rejects a purchaseOrderId that is not a valid UUID", async () => {
const modelId = await createTestModelId();
const res = await POST(
postRequest({
serialNumber: "SN-012",
model: "Dell Latitude 5440",
modelId,
purchaseDate: "2026-01-15",
purchaseOrderId: "not-a-uuid",
})
@@ -207,8 +252,9 @@ describe("POST /api/assets", () => {
});
it("rejects a duplicate serialNumber", async () => {
await POST(postRequest({ serialNumber: "SN-006", model: "Dell", purchaseDate: "2026-01-15" }));
const res = await POST(postRequest({ serialNumber: "SN-006", model: "HP", purchaseDate: "2026-02-01" }));
const modelId = await createTestModelId();
await POST(postRequest({ serialNumber: "SN-006", modelId, purchaseDate: "2026-01-15" }));
const res = await POST(postRequest({ serialNumber: "SN-006", modelId, purchaseDate: "2026-02-01" }));
expect(res.status).toBe(400);
const body = await res.json();
@@ -218,14 +264,48 @@ describe("POST /api/assets", () => {
const remaining = await prisma.assetItem.findMany();
expect(remaining).toHaveLength(1);
});
it("rejects a modelId that is not a valid UUID", async () => {
const res = await POST(
postRequest({ serialNumber: "SN-013", modelId: "not-a-uuid", purchaseDate: "2026-01-15" })
);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/modelId/i);
// And it must not have created anything.
const remaining = await prisma.assetItem.findMany();
expect(remaining).toHaveLength(0);
});
it("rejects a modelId that is a well-formed UUID but doesn't reference an existing asset model", async () => {
const res = await POST(
postRequest({
serialNumber: "SN-014",
modelId: "00000000-0000-0000-0000-000000000000",
purchaseDate: "2026-01-15",
})
);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/modelId/i);
expect(body.error).toMatch(/does not reference an existing asset model/i);
// And it must not have created anything.
const remaining = await prisma.assetItem.findMany();
expect(remaining).toHaveLength(0);
});
});
describe("GET /api/assets", () => {
it("lists created asset items, newest first", async () => {
await POST(postRequest({ serialNumber: "SN-010", model: "Dell", purchaseDate: "2026-01-15" }));
const modelId = await createTestModelId();
await POST(postRequest({ serialNumber: "SN-010", modelId, purchaseDate: "2026-01-15" }));
// Ensure a distinct created_at ordering even on fast filesystems/clocks.
await new Promise((resolve) => setTimeout(resolve, 5));
await POST(postRequest({ serialNumber: "SN-011", model: "HP", purchaseDate: "2026-02-01" }));
await POST(postRequest({ serialNumber: "SN-011", modelId, purchaseDate: "2026-02-01" }));
const res = await GET();
expect(res.status).toBe(200);
@@ -235,8 +315,8 @@ describe("GET /api/assets", () => {
expect(body).toHaveLength(2);
// newest first
expect(body[0]).toMatchObject({ serial_number: "SN-011", model: "HP" });
expect(body[1]).toMatchObject({ serial_number: "SN-010", model: "Dell" });
expect(body[0]).toMatchObject({ serial_number: "SN-011", model_id: modelId });
expect(body[1]).toMatchObject({ serial_number: "SN-010", model_id: modelId });
});
it("returns an empty list when there are no asset items", async () => {
@@ -252,10 +332,11 @@ describe("auth gating on /api/assets", () => {
// session presence, per the flat permission model (no role/permission
// logic).
it("POST /api/assets returns 401 without a session", async () => {
const modelId = await createTestModelId();
mockedAuth.mockResolvedValue(null);
const res = await POST(
postRequest({ serialNumber: "SN-020", model: "Dell", purchaseDate: "2026-01-15" })
postRequest({ serialNumber: "SN-020", modelId, purchaseDate: "2026-01-15" })
);
expect(res.status).toBe(401);