|
1 | 1 | # pylint: disable=unused-argument |
2 | 2 | from __future__ import annotations |
| 3 | +from dataclasses import asdict |
3 | 4 | from typing import TYPE_CHECKING |
4 | 5 | from allocation.adapters import email, redis_eventpublisher |
5 | 6 | from allocation.domain import commands, events, model |
@@ -39,6 +40,16 @@ def allocate( |
39 | 40 | uow.commit() |
40 | 41 |
|
41 | 42 |
|
| 43 | +def reallocate( |
| 44 | + event: events.Deallocated, |
| 45 | + uow: unit_of_work.AbstractUnitOfWork, |
| 46 | +): |
| 47 | + with uow: |
| 48 | + product = uow.products.get(sku=event.sku) |
| 49 | + product.events.append(commands.Allocate(**asdict(event))) |
| 50 | + uow.commit() |
| 51 | + |
| 52 | + |
42 | 53 | def change_batch_quantity( |
43 | 54 | cmd: commands.ChangeBatchQuantity, |
44 | 55 | uow: unit_of_work.AbstractUnitOfWork, |
@@ -82,3 +93,18 @@ def add_allocation_to_read_model( |
82 | 93 | dict(orderid=event.orderid, sku=event.sku, batchref=event.batchref), |
83 | 94 | ) |
84 | 95 | uow.commit() |
| 96 | + |
| 97 | + |
| 98 | +def remove_allocation_from_read_model( |
| 99 | + event: events.Deallocated, |
| 100 | + uow: unit_of_work.SqlAlchemyUnitOfWork, |
| 101 | +): |
| 102 | + with uow: |
| 103 | + uow.session.execute( |
| 104 | + """ |
| 105 | + DELETE FROM allocations_view |
| 106 | + WHERE orderid = :orderid AND sku = :sku |
| 107 | + """, |
| 108 | + dict(orderid=event.orderid, sku=event.sku), |
| 109 | + ) |
| 110 | + uow.commit() |
0 commit comments