feat(assets): pair ASSIGNMENT movement on itemChanged AVAILABLE→ASSIGNED
This commit is contained in:
@@ -453,6 +453,18 @@ export async function updateAssetUseCase(
|
|||||||
},
|
},
|
||||||
tx,
|
tx,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
await MovementService.create(
|
||||||
|
{
|
||||||
|
assetId: id,
|
||||||
|
quantity: 1,
|
||||||
|
type: "ASSIGNMENT",
|
||||||
|
details: `Asset assigned from item ${transition.previousItemId} to item ${transition.nextItemId}`,
|
||||||
|
userId: actorId,
|
||||||
|
previousStatus: transition.previousStatus,
|
||||||
|
},
|
||||||
|
tx,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (transition.willBeAssigned && transition.nextPersonId) {
|
if (transition.willBeAssigned && transition.nextPersonId) {
|
||||||
@@ -526,19 +538,21 @@ export async function updateAssetUseCase(
|
|||||||
tx,
|
tx,
|
||||||
)
|
)
|
||||||
|
|
||||||
await MovementService.create(
|
if (!(transition.itemChanged && transition.wasAvailable)) {
|
||||||
{
|
await MovementService.create(
|
||||||
type: "ASSIGNMENT",
|
{
|
||||||
quantity: 1,
|
type: "ASSIGNMENT",
|
||||||
itemId,
|
quantity: 1,
|
||||||
assetId: id,
|
itemId,
|
||||||
personId: transition.nextPersonId,
|
assetId: id,
|
||||||
assignmentId: createdAssignment.id,
|
personId: transition.nextPersonId,
|
||||||
userId: actorId,
|
assignmentId: createdAssignment.id,
|
||||||
previousStatus: transition.previousStatus,
|
userId: actorId,
|
||||||
},
|
previousStatus: transition.previousStatus,
|
||||||
tx,
|
},
|
||||||
)
|
tx,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -693,4 +693,143 @@ describe("asset use-cases", () => {
|
|||||||
assetId: created.assetId,
|
assetId: created.assetId,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("writes a paired OUT and ASSIGNMENT on itemChanged AVAILABLE to ASSIGNED for QUANTITY items", async () => {
|
||||||
|
const actor = await createTestUser(prisma)
|
||||||
|
const person = await createTestPerson(prisma)
|
||||||
|
const sourceItem = await createTestItem(prisma, { stock: 0 })
|
||||||
|
const targetItem = await createTestItem(prisma, { stock: 0 })
|
||||||
|
|
||||||
|
const created = await createAssetUseCase({
|
||||||
|
actorId: actor.id,
|
||||||
|
itemId: sourceItem.id,
|
||||||
|
serialNumber: "ASSET-W2-QTY-001",
|
||||||
|
status: "AVAILABLE",
|
||||||
|
})
|
||||||
|
expect(created.success).toBe(true)
|
||||||
|
if (!created.success) throw new Error("Expected asset creation success")
|
||||||
|
|
||||||
|
const updated = await updateAssetUseCase({
|
||||||
|
actorId: actor.id,
|
||||||
|
id: created.assetId,
|
||||||
|
itemId: targetItem.id,
|
||||||
|
serialNumber: "ASSET-W2-QTY-001",
|
||||||
|
status: "ASSIGNED",
|
||||||
|
personId: person.id,
|
||||||
|
})
|
||||||
|
expect(updated.success).toBe(true)
|
||||||
|
if (!updated.success) throw new Error("Expected asset update success")
|
||||||
|
|
||||||
|
const movements = await prisma.inventoryMovement.findMany({
|
||||||
|
include: { stockLines: true, assetLines: true },
|
||||||
|
orderBy: [{ createdAt: "asc" }, { id: "asc" }],
|
||||||
|
})
|
||||||
|
|
||||||
|
// RECEIPT (from create) + OUT (on A) + ASSIGNMENT (on B) = 3 total
|
||||||
|
expect(movements).toHaveLength(3)
|
||||||
|
expect(movements.map((movement) => movement.type)).toEqual([
|
||||||
|
"RECEIPT",
|
||||||
|
"ISSUE",
|
||||||
|
"ASSIGNMENT",
|
||||||
|
])
|
||||||
|
|
||||||
|
const outMovement = movements[1]
|
||||||
|
const assignmentMovement = movements[2]
|
||||||
|
|
||||||
|
expect(outMovement.stockLines[0]).toMatchObject({
|
||||||
|
itemId: sourceItem.id,
|
||||||
|
stockDelta: -1,
|
||||||
|
previousStock: 1,
|
||||||
|
newStock: 0,
|
||||||
|
})
|
||||||
|
expect(outMovement.assetLines).toHaveLength(1)
|
||||||
|
expect(outMovement.assetLines[0]).toMatchObject({
|
||||||
|
assetId: created.assetId,
|
||||||
|
previousStatus: "AVAILABLE",
|
||||||
|
newStatus: "ASSIGNED",
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(assignmentMovement.stockLines).toEqual([])
|
||||||
|
expect(assignmentMovement.assetLines).toHaveLength(1)
|
||||||
|
expect(assignmentMovement.assetLines[0]).toMatchObject({
|
||||||
|
assetId: created.assetId,
|
||||||
|
previousStatus: "AVAILABLE",
|
||||||
|
newStatus: "ASSIGNED",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it("writes a paired OUT and ASSIGNMENT on itemChanged AVAILABLE to ASSIGNED for SERIALIZED items", async () => {
|
||||||
|
const actor = await createTestUser(prisma)
|
||||||
|
const person = await createTestPerson(prisma)
|
||||||
|
const category = await createTestCategory(prisma)
|
||||||
|
const sourceItem = await prisma.item.create({
|
||||||
|
data: {
|
||||||
|
sku: "W2-SERIAL-A-SKU",
|
||||||
|
name: "Serial A",
|
||||||
|
trackingType: "SERIALIZED",
|
||||||
|
stock: 0,
|
||||||
|
category: { connect: { id: category.id } },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
const targetItem = await prisma.item.create({
|
||||||
|
data: {
|
||||||
|
sku: "W2-SERIAL-B-SKU",
|
||||||
|
name: "Serial B",
|
||||||
|
trackingType: "SERIALIZED",
|
||||||
|
stock: 0,
|
||||||
|
category: { connect: { id: category.id } },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const created = await createAssetUseCase({
|
||||||
|
actorId: actor.id,
|
||||||
|
itemId: sourceItem.id,
|
||||||
|
serialNumber: "ASSET-W2-SER-001",
|
||||||
|
status: "AVAILABLE",
|
||||||
|
})
|
||||||
|
expect(created.success).toBe(true)
|
||||||
|
if (!created.success) throw new Error("Expected asset creation success")
|
||||||
|
|
||||||
|
const updated = await updateAssetUseCase({
|
||||||
|
actorId: actor.id,
|
||||||
|
id: created.assetId,
|
||||||
|
itemId: targetItem.id,
|
||||||
|
serialNumber: "ASSET-W2-SER-001",
|
||||||
|
status: "ASSIGNED",
|
||||||
|
personId: person.id,
|
||||||
|
})
|
||||||
|
expect(updated.success).toBe(true)
|
||||||
|
if (!updated.success) throw new Error("Expected asset update success")
|
||||||
|
|
||||||
|
const movements = await prisma.inventoryMovement.findMany({
|
||||||
|
include: { stockLines: true, assetLines: true },
|
||||||
|
orderBy: [{ createdAt: "asc" }, { id: "asc" }],
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(movements).toHaveLength(3)
|
||||||
|
expect(movements.map((movement) => movement.type)).toEqual([
|
||||||
|
"RECEIPT",
|
||||||
|
"ISSUE",
|
||||||
|
"ASSIGNMENT",
|
||||||
|
])
|
||||||
|
|
||||||
|
const outMovement = movements[1]
|
||||||
|
const assignmentMovement = movements[2]
|
||||||
|
|
||||||
|
expect(outMovement.stockLines).toEqual([])
|
||||||
|
expect(outMovement.assetLines).toHaveLength(1)
|
||||||
|
expect(outMovement.assetLines[0]).toMatchObject({
|
||||||
|
assetId: created.assetId,
|
||||||
|
previousStatus: "AVAILABLE",
|
||||||
|
newStatus: "ASSIGNED",
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(assignmentMovement.stockLines).toEqual([])
|
||||||
|
expect(assignmentMovement.assetLines).toHaveLength(1)
|
||||||
|
expect(assignmentMovement.assetLines[0]).toMatchObject({
|
||||||
|
assetId: created.assetId,
|
||||||
|
previousStatus: "AVAILABLE",
|
||||||
|
newStatus: "ASSIGNED",
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user