Skip to content

Commit c7683d2

Browse files
authored
feat: add removeQuantity action (#347)
This pull request introduces support for the `removeQuantity` action on inventory entries, allowing quantities to be subtracted from stock. The change includes both the implementation and a corresponding test to ensure correct behavior. **Inventory entry actions:** * Added the `removeQuantity` action to the `InventoryEntryUpdateHandler`, which decreases `quantityOnStock` and `availableQuantity` by the specified amount, ensuring the values do not go below zero. (`src/repositories/inventory-entry/actions.ts`, [[1]](diffhunk://#diff-861d61030e0231fbe9a417cfd51139b676940efcdc4d0fae7f19e9487d3532f1R4) [[2]](diffhunk://#diff-861d61030e0231fbe9a417cfd51139b676940efcdc4d0fae7f19e9487d3532f1R30-R40) **Testing:** * Added a test for the `removeQuantity` action to verify that the inventory entry's quantities are updated correctly when the action is used. (`src/services/inventory-entry.test.ts`, [src/services/inventory-entry.test.tsR96-R110](diffhunk://#diff-b679eb4155c512751fad7a0fd2f910a54086c7097ddbbcdfb4cd4ebe4a9cfde3R96-R110)) **Documentation:** * Updated the changeset to document the addition of the `removeQuantity` action for inventory entries. (`.changeset/old-sites-clean.md`, [.changeset/old-sites-clean.mdR1-R5](diffhunk://#diff-27612412040630d66d6bec197223f696b30cebc93747a1ac4d46e39656b4f041R1-R5))
1 parent cfbc4ac commit c7683d2

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

.changeset/old-sites-clean.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@labdigital/commercetools-mock": minor
3+
---
4+
5+
Add removeQuantity action for inventory entries

src/repositories/inventory-entry/actions.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type {
22
InventoryEntry,
33
InventoryEntryChangeQuantityAction,
4+
InventoryEntryRemoveQuantityAction,
45
InventoryEntrySetCustomFieldAction,
56
InventoryEntrySetCustomTypeAction,
67
InventoryEntrySetExpectedDeliveryAction,
@@ -26,6 +27,17 @@ export class InventoryEntryUpdateHandler
2627
resource.availableQuantity = quantity;
2728
}
2829

30+
removeQuantity(
31+
context: RepositoryContext,
32+
resource: Writable<InventoryEntry>,
33+
{ quantity }: InventoryEntryRemoveQuantityAction,
34+
) {
35+
const newQuantity = Math.max(0, resource.quantityOnStock - quantity);
36+
resource.quantityOnStock = newQuantity;
37+
// don't know active reservations so just set to same value
38+
resource.availableQuantity = newQuantity;
39+
}
40+
2941
setCustomField(
3042
context: RepositoryContext,
3143
resource: InventoryEntry,

src/services/inventory-entry.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@ describe("Inventory Entry Update Actions", () => {
9393
expect(response.body.quantityOnStock).toBe(300);
9494
});
9595

96+
test("removeQuantity", async () => {
97+
assert(inventoryEntry, "inventory entry not created");
98+
99+
const response = await supertest(ctMock.app)
100+
.post(`/dummy/inventory/${inventoryEntry.id}`)
101+
.send({
102+
version: 1,
103+
actions: [{ action: "removeQuantity", quantity: 15 }],
104+
});
105+
expect(response.status).toBe(200);
106+
expect(response.body.version).toBe(2);
107+
expect(response.body.availableQuantity).toBe(85);
108+
expect(response.body.quantityOnStock).toBe(85);
109+
});
110+
96111
test("set custom type", async () => {
97112
assert(inventoryEntry, "inventory entry not created");
98113
assert(customType, "custom type not created");

0 commit comments

Comments
 (0)