Skip to content

Commit 3ee6ed8

Browse files
committed
Start moving to handlers [services_to_handlers]
1 parent 82c54b2 commit 3ee6ed8

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

src/allocation/email.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
def send_mail(*args):
1+
def send(*args):
22
print('SENDING EMAIL:', *args)

src/allocation/handlers.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,45 @@
11
from __future__ import annotations
2-
from typing import Optional
3-
from datetime import date
4-
5-
from allocation import exceptions, model, unit_of_work
2+
import typing
3+
from allocation import events, email, exceptions, model
64
from allocation.model import OrderLine
5+
if typing.TYPE_CHECKING:
6+
from allocation import unit_of_work
7+
78

89

910
def add_batch(
10-
ref: str, sku: str, qty: int, eta: Optional[date],
11-
uow: unit_of_work.AbstractUnitOfWork
11+
event: events.BatchCreated, uow: unit_of_work.AbstractUnitOfWork
1212
):
1313
with uow:
14-
product = uow.products.get(sku=sku)
14+
product = uow.products.get(sku=event.sku)
1515
if product is None:
16-
product = model.Product(sku, batches=[])
16+
product = model.Product(event.sku, batches=[])
1717
uow.products.add(product)
18-
product.batches.append(model.Batch(ref, sku, qty, eta))
18+
product.batches.append(model.Batch(
19+
event.ref, event.sku, event.qty, event.eta
20+
))
1921
uow.commit()
2022

2123

2224
def allocate(
23-
orderid: str, sku: str, qty: int,
24-
uow: unit_of_work.AbstractUnitOfWork
25+
event: events.AllocationRequired, uow: unit_of_work.AbstractUnitOfWork
2526
) -> str:
26-
line = OrderLine(orderid, sku, qty)
27+
line = OrderLine(event.orderid, event.sku, event.qty)
2728
with uow:
2829
product = uow.products.get(sku=line.sku)
2930
if product is None:
3031
raise exceptions.InvalidSku(f'Invalid sku {line.sku}')
3132
batchref = product.allocate(line)
3233
uow.commit()
3334
return batchref
35+
36+
37+
# pylint: disable=unused-argument
38+
39+
def send_out_of_stock_notification(
40+
event: events.OutOfStock, uow: unit_of_work.AbstractUnitOfWork,
41+
):
42+
email.send(
43+
'stock@made.com',
44+
f'Out of stock for {event.sku}',
45+
)

src/allocation/messagebus.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
from typing import List, Dict, Callable, Type
2-
from allocation import email, events
2+
from allocation import events, handlers
33

44

55
def handle(event: events.Event):
66
for handler in HANDLERS[type(event)]:
77
handler(event)
88

99

10-
def send_out_of_stock_notification(event: events.OutOfStock):
11-
email.send_mail(
12-
'stock@made.com',
13-
f'Out of stock for {event.sku}',
14-
)
15-
16-
1710
HANDLERS = {
18-
events.OutOfStock: [send_out_of_stock_notification],
19-
11+
events.BatchCreated: [handlers.add_batch],
12+
events.AllocationRequired: [handlers.allocate],
13+
events.OutOfStock: [handlers.send_out_of_stock_notification],
2014
} # type: Dict[Type[events.Event], List[Callable]]

0 commit comments

Comments
 (0)