feat(movements): gate StockMovementLine on trackingType QUANTITY

This commit is contained in:
2026-06-25 03:22:08 +02:00
parent 8f7a406e83
commit a0a1e1bdc8
7 changed files with 257 additions and 16 deletions
@@ -1,6 +1,7 @@
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest"
import type { PrismaClient } from "@/generated/prisma/client"
import {
createTestCategory,
createTestItem,
createTestPerson,
createTestUser,
@@ -38,6 +39,56 @@ afterAll(async () => {
})
describe("assignment use-cases", () => {
it("creates a SERIALIZED assignment without writing a stock line", async () => {
const actor = await createTestUser(prisma)
const person = await createTestPerson(prisma)
const category = await createTestCategory(prisma)
const item = await prisma.item.create({
data: {
sku: "SERIALIZED-ASSIGNMENT-SKU",
name: "Serial Item",
trackingType: "SERIALIZED",
stock: 0,
category: { connect: { id: category.id } },
},
})
const asset = await prisma.asset.create({
data: {
serialNumber: "SERIALIZED-ASSIGNMENT-ASSET-001",
itemId: item.id,
status: "AVAILABLE",
},
})
const result = await createAssignmentUseCase({
actorId: actor.id,
personId: person.id,
assetId: asset.id,
lines: [{ itemId: item.id, quantity: 1 }],
})
expect(result.success).toBe(true)
if (!result.success) throw new Error("Expected assignment creation success")
const [updatedItem, updatedAsset, movements] = await Promise.all([
prisma.item.findUniqueOrThrow({ where: { id: item.id } }),
prisma.asset.findUniqueOrThrow({ where: { id: asset.id } }),
prisma.inventoryMovement.findMany({
include: { stockLines: true, assetLines: true },
orderBy: [{ createdAt: "asc" }, { id: "asc" }],
}),
])
expect(updatedItem.stock).toBe(0)
expect(updatedAsset.status).toBe("ASSIGNED")
expect(movements).toHaveLength(1)
expect(movements[0].stockLines).toEqual([])
expect(movements[0].assetLines).toHaveLength(1)
expect(movements[0].assetLines[0]).toMatchObject({
assetId: asset.id,
})
})
it("creates an assignment, decrements stock, and records an ASSIGNMENT movement", async () => {
const actor = await createTestUser(prisma)
const person = await createTestPerson(prisma)