Skip to content

Commit 1fb5791

Browse files
committed
Start moving to handlers [services_to_handlers]
1 parent de0b49b commit 1fb5791

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

src/allocation/adapters/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)
Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from __future__ import annotations
2-
from typing import Optional, TYPE_CHECKING
3-
from datetime import date
4-
5-
from allocation.domain import model
2+
from typing import TYPE_CHECKING
3+
from allocation.domain import events, model
64
from allocation.domain.model import OrderLine
75

86
if TYPE_CHECKING:
@@ -14,27 +12,42 @@ class InvalidSku(Exception):
1412

1513

1614
def add_batch(
17-
ref: str, sku: str, qty: int, eta: Optional[date],
15+
event: events.BatchCreated,
1816
uow: unit_of_work.AbstractUnitOfWork,
1917
):
2018
with uow:
21-
product = uow.products.get(sku=sku)
19+
product = uow.products.get(sku=event.sku)
2220
if product is None:
23-
product = model.Product(sku, batches=[])
21+
product = model.Product(event.sku, batches=[])
2422
uow.products.add(product)
25-
product.batches.append(model.Batch(ref, sku, qty, eta))
23+
product.batches.append(
24+
model.Batch(event.ref, event.sku, event.qty, event.eta)
25+
)
2626
uow.commit()
2727

2828

2929
def allocate(
30-
orderid: str, sku: str, qty: int,
30+
event: events.AllocationRequired,
3131
uow: unit_of_work.AbstractUnitOfWork,
3232
) -> str:
33-
line = OrderLine(orderid, sku, qty)
33+
line = OrderLine(event.orderid, event.sku, event.qty)
3434
with uow:
3535
product = uow.products.get(sku=line.sku)
3636
if product is None:
3737
raise InvalidSku(f"Invalid sku {line.sku}")
3838
batchref = product.allocate(line)
3939
uow.commit()
4040
return batchref
41+
42+
43+
# pylint: disable=unused-argument
44+
45+
46+
def send_out_of_stock_notification(
47+
event: events.OutOfStock,
48+
uow: unit_of_work.AbstractUnitOfWork,
49+
):
50+
email.send(
51+
"stock@made.com",
52+
f"Out of stock for {event.sku}",
53+
)
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
from typing import List, Dict, Callable, Type
2-
from allocation.adapters import email
32
from allocation.domain import events
3+
from . import handlers
44

55

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

1010

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

0 commit comments

Comments
 (0)