Skip to content

Commit 508b734

Browse files
committed
make sure deallocation fixes view model too [deallocation_to_readmodel]
1 parent fc79aff commit 508b734

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

src/allocation/domain/events.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ class Allocated(Event):
1111
qty: int
1212
batchref: str
1313

14+
@dataclass
15+
class Deallocated(Event):
16+
orderid: str
17+
sku: str
18+
qty: int
19+
1420
@dataclass
1521
class OutOfStock(Event):
1622
sku: str

src/allocation/domain/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def change_batch_quantity(self, ref: str, qty: int):
3535
while batch.available_quantity < 0:
3636
line = batch.deallocate_one()
3737
self.events.append(
38-
commands.Allocate(line.orderid, line.sku, line.qty)
38+
events.Deallocated(line.orderid, line.sku, line.qty)
3939
)
4040

4141
@dataclass(unsafe_hash=True)

src/allocation/service_layer/handlers.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pylint: disable=unused-argument
22
from __future__ import annotations
3+
from dataclasses import asdict
34
from typing import TYPE_CHECKING
45
from allocation.adapters import email, redis_eventpublisher
56
from allocation.domain import commands, events, model
@@ -37,6 +38,13 @@ def allocate(
3738
product.allocate(line)
3839
uow.commit()
3940

41+
def reallocate(
42+
event: events.Deallocated, uow: unit_of_work.AbstractUnitOfWork
43+
):
44+
with uow:
45+
product = uow.products.get(sku=event.sku)
46+
product.events.append(commands.Allocate(**asdict(event)))
47+
uow.commit()
4048

4149
def change_batch_quantity(
4250
cmd: commands.ChangeBatchQuantity, uow: unit_of_work.AbstractUnitOfWork
@@ -74,3 +82,14 @@ def add_allocation_to_read_model(
7482
dict(orderid=event.orderid, sku=event.sku, batchref=event.batchref)
7583
)
7684
uow.commit()
85+
86+
def remove_allocation_from_read_model(
87+
event: events.Deallocated, uow: unit_of_work.SqlAlchemyUnitOfWork,
88+
):
89+
with uow:
90+
uow.session.execute(
91+
'DELETE FROM allocations_view '
92+
' WHERE orderid = :orderid AND sku = :sku',
93+
dict(orderid=event.orderid, sku=event.sku)
94+
)
95+
uow.commit()

src/allocation/service_layer/messagebus.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ def handle_command(
6060
handlers.publish_allocated_event,
6161
handlers.add_allocation_to_read_model
6262
],
63+
events.Deallocated: [
64+
handlers.remove_allocation_from_read_model,
65+
handlers.reallocate,
66+
],
6367
events.OutOfStock: [handlers.send_out_of_stock_notification],
6468
} # type: Dict[Type[events.Event], List[Callable]]
6569

tests/integration/test_views.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,15 @@ def test_allocations_view(sqlite_session_factory):
1919
{'sku': 'sku1', 'batchref': 'sku1batch'},
2020
{'sku': 'sku2', 'batchref': 'sku2batch'},
2121
]
22+
23+
24+
def test_deallocation(sqlite_session_factory):
25+
uow = unit_of_work.SqlAlchemyUnitOfWork(sqlite_session_factory)
26+
messagebus.handle(commands.CreateBatch('b1', 'sku1', 50, None), uow)
27+
messagebus.handle(commands.CreateBatch('b2', 'sku1', 50, date.today()), uow)
28+
messagebus.handle(commands.Allocate('o1', 'sku1', 40), uow)
29+
messagebus.handle(commands.ChangeBatchQuantity('b1', 10), uow)
30+
31+
assert views.allocations('o1', uow) == [
32+
{'sku': 'sku1', 'batchref': 'b2'},
33+
]

0 commit comments

Comments
 (0)