Skip to content

Commit f7350a8

Browse files
committed
change_batch_quantity on product, needed change to batch, also emit allocated event. [change_batch_model_layer]
1 parent 2fe5e9d commit f7350a8

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

src/allocation/model.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ def allocate(self, line: OrderLine) -> str:
2525
self.events.append(events.OutOfStock(line.sku))
2626
return None
2727

28+
def change_batch_quantity(self, ref: str, qty: int):
29+
batch = next(b for b in self.batches if b.reference == ref)
30+
batch._purchased_quantity = qty
31+
while batch.available_quantity < 0:
32+
line = batch.deallocate_one()
33+
self.events.append(
34+
events.AllocationRequired(line.orderid, line.sku, line.qty)
35+
)
2836

2937
@dataclass(unsafe_hash=True)
3038
class OrderLine:
@@ -65,9 +73,8 @@ def allocate(self, line: OrderLine):
6573
if self.can_allocate(line):
6674
self._allocations.add(line)
6775

68-
def deallocate(self, line: OrderLine):
69-
if line in self._allocations:
70-
self._allocations.remove(line)
76+
def deallocate_one(self) -> OrderLine:
77+
return self._allocations.pop()
7178

7279
@property
7380
def allocated_quantity(self) -> int:

tests/unit/test_batches.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,3 @@ def test_allocation_is_idempotent():
3939
batch.allocate(line)
4040
batch.allocate(line)
4141
assert batch.available_quantity == 18
42-
43-
def test_deallocate():
44-
batch, line = make_batch_and_line("EXPENSIVE-FOOTSTOOL", 20, 2)
45-
batch.allocate(line)
46-
batch.deallocate(line)
47-
assert batch.available_quantity == 20
48-
49-
def test_can_only_deallocate_allocated_lines():
50-
batch, unallocated_line = make_batch_and_line("DECORATIVE-TRINKET", 20, 2)
51-
batch.deallocate(unallocated_line)
52-
assert batch.available_quantity == 20
53-

0 commit comments

Comments
 (0)