feat(assignments): emit ADJUSTMENT movement on quantity change
This commit is contained in:
@@ -244,8 +244,17 @@ export async function updateAssignmentUseCase(
|
||||
return updateAssignmentError({ id: ["Assignment not found"] })
|
||||
}
|
||||
|
||||
if (
|
||||
assignment.status === "RETURNED" ||
|
||||
assignment.status === "CANCELLED"
|
||||
) {
|
||||
return updateAssignmentError({ id: ["Assignment is closed"] })
|
||||
}
|
||||
|
||||
let item = null
|
||||
|
||||
if (itemId) {
|
||||
const item = await ItemService.findById(itemId, tx)
|
||||
item = await ItemService.findById(itemId, tx)
|
||||
|
||||
if (!item) {
|
||||
return updateAssignmentError({ itemId: ["Item not found"] })
|
||||
@@ -308,6 +317,22 @@ export async function updateAssignmentUseCase(
|
||||
tx,
|
||||
)
|
||||
} else {
|
||||
if (item && assignment.quantity !== quantity) {
|
||||
const stockDelta = quantity - (assignment.quantity ?? 0)
|
||||
const adjustmentItemId = item.id
|
||||
await ItemService.updateStock(adjustmentItemId, -stockDelta, tx)
|
||||
await MovementService.create(
|
||||
{
|
||||
type: "ADJUSTMENT",
|
||||
itemId: adjustmentItemId,
|
||||
quantity: Math.abs(stockDelta),
|
||||
stockDeltaSign: stockDelta < 0 ? 1 : -1,
|
||||
userId: actorId,
|
||||
},
|
||||
tx,
|
||||
)
|
||||
}
|
||||
|
||||
await AssignmentService.update(
|
||||
id,
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user