Skip to content

Commit be5b945

Browse files
committed
change_batch_quantity on product, needed change to batch, also emit allocated event. [change_batch_model_layer]
1 parent 8f67133 commit be5b945

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

src/allocation/domain/model.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ def allocate(self, line: OrderLine) -> str:
2222
self.events.append(events.OutOfStock(line.sku))
2323
return None
2424

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

2635
@dataclass(unsafe_hash=True)
2736
class OrderLine:
@@ -60,9 +69,8 @@ def allocate(self, line: OrderLine):
6069
if self.can_allocate(line):
6170
self._allocations.add(line)
6271

63-
def deallocate(self, line: OrderLine):
64-
if line in self._allocations:
65-
self._allocations.remove(line)
72+
def deallocate_one(self) -> OrderLine:
73+
return self._allocations.pop()
6674

6775
@property
6876
def allocated_quantity(self) -> int:

tests/unit/test_batches.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,3 @@ def test_allocation_is_idempotent():
4444
batch.allocate(line)
4545
batch.allocate(line)
4646
assert batch.available_quantity == 18
47-
48-
49-
def test_deallocate():
50-
batch, line = make_batch_and_line("EXPENSIVE-FOOTSTOOL", 20, 2)
51-
batch.allocate(line)
52-
batch.deallocate(line)
53-
assert batch.available_quantity == 20
54-
55-
56-
def test_can_only_deallocate_allocated_lines():
57-
batch, unallocated_line = make_batch_and_line("DECORATIVE-TRINKET", 20, 2)
58-
batch.deallocate(unallocated_line)
59-
assert batch.available_quantity == 20

0 commit comments

Comments
 (0)