-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathservices.py
More file actions
37 lines (27 loc) · 909 Bytes
/
services.py
File metadata and controls
37 lines (27 loc) · 909 Bytes
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
from __future__ import annotations
from typing import Optional
from datetime import date
from domain import model
from domain.model import OrderLine
from adapters.repository import AbstractRepository
class InvalidSku(Exception):
pass
def is_valid_sku(sku, batches):
return sku in {b.sku for b in batches}
def add_batch(
ref: str, sku: str, qty: int, eta: Optional[date],
repo: AbstractRepository, session,
) -> None:
repo.add(model.Batch(ref, sku, qty, eta))
session.commit()
def allocate(
orderid: str, sku: str, qty: int,
repo: AbstractRepository, session
) -> Optional[str]:
line = OrderLine(orderid, sku, qty)
batches = repo.list()
if not is_valid_sku(line.sku, batches):
raise InvalidSku(f"Invalid sku {line.sku}")
updated_batch = model.allocate(line, batches)
session.commit()
return updated_batch.ref if updated_batch else None