feat(movements): gate StockMovementLine on trackingType QUANTITY
This commit is contained in:
@@ -151,6 +151,12 @@ describe("item use-cases", () => {
|
||||
minStock: 2,
|
||||
targetStock: 8,
|
||||
})
|
||||
|
||||
const stockMovements = await prisma.inventoryMovement.findMany({
|
||||
where: { stockLines: { some: { itemId: item.id } } },
|
||||
})
|
||||
|
||||
expect(stockMovements).toHaveLength(1)
|
||||
})
|
||||
|
||||
it("rejects duplicate item names", async () => {
|
||||
@@ -285,6 +291,78 @@ describe("item use-cases", () => {
|
||||
})
|
||||
})
|
||||
|
||||
it("creates a SERIALIZED item with stock value and persists stock as 0 with no movement", async () => {
|
||||
const actor = await createTestUser(prisma)
|
||||
const category = await createTestCategory(prisma)
|
||||
|
||||
const result = await createItemUseCase({
|
||||
actorId: actor.id,
|
||||
name: "Phone",
|
||||
categoryId: category.id,
|
||||
stock: 3,
|
||||
trackingType: "SERIALIZED",
|
||||
})
|
||||
|
||||
expect(result).toEqual({ success: true })
|
||||
|
||||
const item = await prisma.item.findUniqueOrThrow({
|
||||
where: { sku: "PHONE" },
|
||||
})
|
||||
|
||||
expect(item).toMatchObject({
|
||||
stock: 0,
|
||||
trackingType: "SERIALIZED",
|
||||
})
|
||||
|
||||
const movements = await prisma.inventoryMovement.findMany({
|
||||
where: { stockLines: { some: { itemId: item.id } } },
|
||||
})
|
||||
|
||||
expect(movements).toHaveLength(0)
|
||||
})
|
||||
|
||||
it("updates a SERIALIZED item with stock value and leaves stock unchanged with no movement", async () => {
|
||||
const actor = await createTestUser(prisma)
|
||||
const category = await createTestCategory(prisma)
|
||||
|
||||
const created = await createItemUseCase({
|
||||
actorId: actor.id,
|
||||
name: "Tablet",
|
||||
categoryId: category.id,
|
||||
stock: 0,
|
||||
trackingType: "SERIALIZED",
|
||||
})
|
||||
|
||||
expect(created).toEqual({ success: true })
|
||||
|
||||
const item = await prisma.item.findUniqueOrThrow({
|
||||
where: { sku: "TABLET" },
|
||||
})
|
||||
|
||||
const updated = await updateItemUseCase({
|
||||
actorId: actor.id,
|
||||
id: item.id,
|
||||
name: "Tablet",
|
||||
categoryId: category.id,
|
||||
stock: 5,
|
||||
trackingType: "SERIALIZED",
|
||||
})
|
||||
|
||||
expect(updated).toEqual({ success: true })
|
||||
|
||||
const refreshedItem = await prisma.item.findUniqueOrThrow({
|
||||
where: { id: item.id },
|
||||
})
|
||||
|
||||
expect(refreshedItem.stock).toBe(0)
|
||||
|
||||
const movements = await prisma.inventoryMovement.findMany({
|
||||
where: { stockLines: { some: { itemId: item.id } } },
|
||||
})
|
||||
|
||||
expect(movements).toHaveLength(0)
|
||||
})
|
||||
|
||||
it("blocks deleting items with stock and soft deletes empty items", async () => {
|
||||
const actor = await createTestUser(prisma)
|
||||
const category = await createTestCategory(prisma)
|
||||
|
||||
Reference in New Issue
Block a user