Repository navigation
Expand file tree
/
Copy pathhandlers.py
More file actions
99 lines (79 loc) · 2.69 KB
/
Copy pathhandlers.py
File metadata and controls
99 lines (79 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#pylint: disable=unused-argument
from __future__ import annotations
from dataclasses import asdict
from typing import Callable, TYPE_CHECKING
from allocation.domain import commands, events, model
from allocation.domain.model import OrderLine
if TYPE_CHECKING:
from allocation.adapters import notifications
from . import unit_of_work
class InvalidSku(Exception):
pass
def add_batch(
cmd: commands.CreateBatch, uow: unit_of_work.AbstractUnitOfWork
):
with uow:
product = uow.products.get(sku=cmd.sku)
if product is None:
product = model.Product(cmd.sku, batches=[])
uow.products.add(product)
product.batches.append(model.Batch(
cmd.ref, cmd.sku, cmd.qty, cmd.eta
))
uow.commit()
def allocate(
cmd: commands.Allocate, uow: unit_of_work.AbstractUnitOfWork
):
line = OrderLine(cmd.orderid, cmd.sku, cmd.qty)
with uow:
product = uow.products.get(sku=line.sku)
if product is None:
raise InvalidSku(f'Invalid sku {line.sku}')
product.allocate(line)
uow.commit()
def reallocate(
event: events.Deallocated, uow: unit_of_work.AbstractUnitOfWork
):
with uow:
product = uow.products.get(sku=event.sku)
product.events.append(commands.Allocate(**asdict(event)))
uow.commit()
def change_batch_quantity(
cmd: commands.ChangeBatchQuantity, uow: unit_of_work.AbstractUnitOfWork
):
with uow:
product = uow.products.get_by_batchref(batchref=cmd.ref)
product.change_batch_quantity(ref=cmd.ref, qty=cmd.qty)
uow.commit()
#pylint: disable=unused-argument
def send_out_of_stock_notification(
event: events.OutOfStock, notifications: notifications.AbstractNotifications,
):
notifications.send(
'stock@made.com',
f'Out of stock for {event.sku}',
)
def publish_allocated_event(
event: events.Allocated, publish: Callable,
):
publish('line_allocated', event)
def add_allocation_to_read_model(
event: events.Allocated, uow: unit_of_work.SqlAlchemyUnitOfWork,
):
with uow:
uow.session.execute(
'INSERT INTO allocations_view (orderid, sku, batchref)'
' VALUES (:orderid, :sku, :batchref)',
dict(orderid=event.orderid, sku=event.sku, batchref=event.batchref)
)
uow.commit()
def remove_allocation_from_read_model(
event: events.Deallocated, uow: unit_of_work.SqlAlchemyUnitOfWork,
):
with uow:
uow.session.execute(
'DELETE FROM allocations_view '
' WHERE orderid = :orderid AND sku = :sku',
dict(orderid=event.orderid, sku=event.sku)
)
uow.commit()