Skip to content

Commit fee9a1e

Browse files
committed
make sure deallocation fixes view model too [deallocation_to_readmodel]
1 parent e538054 commit fee9a1e

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

src/allocation/domain/events.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ class Allocated(Event):
1414
batchref: str
1515

1616

17+
@dataclass
18+
class Deallocated(Event):
19+
orderid: str
20+
sku: str
21+
qty: int
22+
23+
1724
@dataclass
1825
class OutOfStock(Event):
1926
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
batch._purchased_quantity = qty
3636
while batch.available_quantity < 0:
3737
line = batch.deallocate_one()
38-
self.events.append(commands.Allocate(line.orderid, line.sku, line.qty))
38+
self.events.append(events.Deallocated(line.orderid, line.sku, line.qty))
3939

4040

4141
@dataclass(unsafe_hash=True)

src/allocation/service_layer/handlers.py

Lines changed: 26 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
@@ -39,6 +40,16 @@ def allocate(
3940
uow.commit()
4041

4142

43+
def reallocate(
44+
event: events.Deallocated,
45+
uow: unit_of_work.AbstractUnitOfWork,
46+
):
47+
with uow:
48+
product = uow.products.get(sku=event.sku)
49+
product.events.append(commands.Allocate(**asdict(event)))
50+
uow.commit()
51+
52+
4253
def change_batch_quantity(
4354
cmd: commands.ChangeBatchQuantity,
4455
uow: unit_of_work.AbstractUnitOfWork,
@@ -82,3 +93,18 @@ def add_allocation_to_read_model(
8293
dict(orderid=event.orderid, sku=event.sku, batchref=event.batchref),
8394
)
8495
uow.commit()
96+
97+
98+
def remove_allocation_from_read_model(
99+
event: events.Deallocated,
100+
uow: unit_of_work.SqlAlchemyUnitOfWork,
101+
):
102+
with uow:
103+
uow.session.execute(
104+
"""
105+
DELETE FROM allocations_view
106+
WHERE orderid = :orderid AND sku = :sku
107+
""",
108+
dict(orderid=event.orderid, sku=event.sku),
109+
)
110+
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
@@ -21,3 +21,15 @@ def test_allocations_view(sqlite_session_factory):
2121
{"sku": "sku1", "batchref": "sku1batch"},
2222
{"sku": "sku2", "batchref": "sku2batch"},
2323
]
24+
25+
26+
def test_deallocation(sqlite_session_factory):
27+
uow = unit_of_work.SqlAlchemyUnitOfWork(sqlite_session_factory)
28+
messagebus.handle(commands.CreateBatch("b1", "sku1", 50, None), uow)
29+
messagebus.handle(commands.CreateBatch("b2", "sku1", 50, today), uow)
30+
messagebus.handle(commands.Allocate("o1", "sku1", 40), uow)
31+
messagebus.handle(commands.ChangeBatchQuantity("b1", 10), uow)
32+
33+
assert views.allocations("o1", uow) == [
34+
{"sku": "sku1", "batchref": "b2"},
35+
]

0 commit comments

Comments
 (0)