|
1 | 1 | 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 |
6 | 4 | from allocation.model import OrderLine |
| 5 | +if typing.TYPE_CHECKING: |
| 6 | + from allocation import unit_of_work |
| 7 | + |
7 | 8 |
|
8 | 9 |
|
9 | 10 | 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 |
12 | 12 | ): |
13 | 13 | with uow: |
14 | | - product = uow.products.get(sku=sku) |
| 14 | + product = uow.products.get(sku=event.sku) |
15 | 15 | if product is None: |
16 | | - product = model.Product(sku, batches=[]) |
| 16 | + product = model.Product(event.sku, batches=[]) |
17 | 17 | 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 | + )) |
19 | 21 | uow.commit() |
20 | 22 |
|
21 | 23 |
|
22 | 24 | def allocate( |
23 | | - orderid: str, sku: str, qty: int, |
24 | | - uow: unit_of_work.AbstractUnitOfWork |
| 25 | + event: events.AllocationRequired, uow: unit_of_work.AbstractUnitOfWork |
25 | 26 | ) -> str: |
26 | | - line = OrderLine(orderid, sku, qty) |
| 27 | + line = OrderLine(event.orderid, event.sku, event.qty) |
27 | 28 | with uow: |
28 | 29 | product = uow.products.get(sku=line.sku) |
29 | 30 | if product is None: |
30 | 31 | raise exceptions.InvalidSku(f'Invalid sku {line.sku}') |
31 | 32 | batchref = product.allocate(line) |
32 | 33 | uow.commit() |
33 | 34 | 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 | + ) |
0 commit comments