refactor(movements): normalize snapshot convention to mutate-then-write
This commit is contained in:
@@ -173,6 +173,10 @@ export async function createAssetUseCase(
|
||||
)
|
||||
: null
|
||||
|
||||
if (status === "AVAILABLE") {
|
||||
await ItemService.updateStock(itemId, 1, tx)
|
||||
}
|
||||
|
||||
await MovementService.create(
|
||||
{
|
||||
itemId,
|
||||
@@ -186,10 +190,6 @@ export async function createAssetUseCase(
|
||||
tx,
|
||||
)
|
||||
|
||||
if (status === "AVAILABLE") {
|
||||
await ItemService.updateStock(itemId, 1, tx)
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
assetId: newAsset.id,
|
||||
@@ -290,6 +290,37 @@ export async function updateAssetUseCase(
|
||||
tx,
|
||||
)
|
||||
|
||||
const shouldIncrementNextItemStock =
|
||||
transition.willBeAvailable &&
|
||||
(!transition.wasAvailable || transition.itemChanged)
|
||||
const shouldDecrementPreviousItemStock =
|
||||
transition.wasAvailable &&
|
||||
(!transition.willBeAvailable || transition.itemChanged)
|
||||
|
||||
if (shouldIncrementNextItemStock) {
|
||||
await ItemService.updateStock(transition.nextItemId, 1, tx)
|
||||
}
|
||||
|
||||
if (shouldDecrementPreviousItemStock) {
|
||||
if (!transition.previousItemId) {
|
||||
throw new AssetTransitionError({
|
||||
itemId: ["Previous item not found for available asset"],
|
||||
})
|
||||
}
|
||||
|
||||
const stockWasDecremented = await ItemService.decrementStockIfAvailable(
|
||||
transition.previousItemId,
|
||||
1,
|
||||
tx,
|
||||
)
|
||||
|
||||
if (!stockWasDecremented) {
|
||||
throw new AssetTransitionError({
|
||||
stock: ["Item does not have enough stock"],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
let closedActiveAssignment = false
|
||||
|
||||
if (transition.activeAssignment && !transition.willBeAssigned) {
|
||||
@@ -325,13 +356,6 @@ export async function updateAssetUseCase(
|
||||
closedActiveAssignment = true
|
||||
}
|
||||
|
||||
const shouldIncrementNextItemStock =
|
||||
transition.willBeAvailable &&
|
||||
(!transition.wasAvailable || transition.itemChanged)
|
||||
const shouldDecrementPreviousItemStock =
|
||||
transition.wasAvailable &&
|
||||
(!transition.willBeAvailable || transition.itemChanged)
|
||||
|
||||
if (
|
||||
transition.statusChanged &&
|
||||
!transition.hasPerson &&
|
||||
@@ -422,30 +446,6 @@ export async function updateAssetUseCase(
|
||||
)
|
||||
}
|
||||
|
||||
if (shouldIncrementNextItemStock) {
|
||||
await ItemService.updateStock(transition.nextItemId, 1, tx)
|
||||
}
|
||||
|
||||
if (shouldDecrementPreviousItemStock) {
|
||||
if (!transition.previousItemId) {
|
||||
throw new AssetTransitionError({
|
||||
itemId: ["Previous item not found for available asset"],
|
||||
})
|
||||
}
|
||||
|
||||
const stockWasDecremented = await ItemService.decrementStockIfAvailable(
|
||||
transition.previousItemId,
|
||||
1,
|
||||
tx,
|
||||
)
|
||||
|
||||
if (!stockWasDecremented) {
|
||||
throw new AssetTransitionError({
|
||||
stock: ["Item does not have enough stock"],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if (transition.willBeAssigned && transition.nextPersonId) {
|
||||
const activeAssignment = transition.activeAssignment
|
||||
|
||||
|
||||
Reference in New Issue
Block a user