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
+4 -1
View File
@@ -173,7 +173,7 @@ export async function createAssetUseCase(
)
: null
if (status === "AVAILABLE") {
if (status === "AVAILABLE" && item.trackingType === "QUANTITY") {
await ItemService.updateStock(itemId, 1, tx)
}
@@ -290,10 +290,13 @@ export async function updateAssetUseCase(
tx,
)
const isSerializedItem = item.trackingType === "SERIALIZED"
const shouldIncrementNextItemStock =
!isSerializedItem &&
transition.willBeAvailable &&
(!transition.wasAvailable || transition.itemChanged)
const shouldDecrementPreviousItemStock =
!isSerializedItem &&
transition.wasAvailable &&
(!transition.willBeAvailable || transition.itemChanged)
+12 -10
View File
@@ -146,7 +146,7 @@ export async function createAssignmentUseCase(
return createAssignmentError({ itemId: ["Item not found"] })
}
if (item.stock < quantity) {
if (item.trackingType === "QUANTITY" && item.stock < quantity) {
return createAssignmentError({
quantity: ["Item does not have enough stock"],
})
@@ -166,16 +166,18 @@ export async function createAssignmentUseCase(
}
}
const stockWasDecremented = await ItemService.decrementStockIfAvailable(
itemId,
quantity,
tx,
)
if (item.trackingType === "QUANTITY") {
const stockWasDecremented = await ItemService.decrementStockIfAvailable(
itemId,
quantity,
tx,
)
if (!stockWasDecremented) {
return createAssignmentError({
quantity: ["Item does not have enough stock"],
})
if (!stockWasDecremented) {
return createAssignmentError({
quantity: ["Item does not have enough stock"],
})
}
}
if (assetId) {
+13 -3
View File
@@ -85,12 +85,12 @@ export async function createItemUseCase(
minStock,
targetStock,
category: { connect: { id: categoryId } },
stock: stock || 0,
stock: trackingType === "SERIALIZED" ? 0 : stock || 0,
},
tx,
)
if (stock > 0) {
if (trackingType === "QUANTITY" && stock > 0) {
await MovementService.create(
{
type: "IN",
@@ -145,10 +145,14 @@ export async function updateItemUseCase(
return itemError({ name: ["An item with this name already exists"] })
}
const effectiveTrackingType =
trackingType ?? existingItem.trackingType
const isSerialized = effectiveTrackingType === "SERIALIZED"
await ItemService.update(
id,
{
stock: stock ?? existingItem.stock,
stock: isSerialized ? 0 : (stock ?? existingItem.stock),
name: name || existingItem.name,
trackingType: trackingType ?? existingItem.trackingType,
status: status ?? existingItem.status,
@@ -159,6 +163,12 @@ export async function updateItemUseCase(
tx,
)
if (isSerialized) {
return {
success: true,
}
}
const updatedStock = stock ?? existingItem.stock
if (updatedStock > existingItem.stock) {