File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Load diff This file was deleted.
Original file line number Diff line number Diff line change 1+ #pylint: disable=too-few-public-methods
2+ import abc
3+ import smtplib
4+ from allocation import config
5+
6+
7+ class AbstractNotifications (abc .ABC ):
8+
9+ @abc .abstractmethod
10+ def send (self , destination , message ):
11+ raise NotImplementedError
12+
13+
14+ DEFAULT_HOST = config .get_email_host_and_port ()['host' ]
15+ DEFAULT_PORT = config .get_email_host_and_port ()['port' ]
16+
17+ class EmailNotifications (AbstractNotifications ):
18+
19+ def __init__ (self , smtp_host = DEFAULT_HOST , port = DEFAULT_PORT ):
20+ self .server = smtplib .SMTP (smtp_host , port = port )
21+ self .server .noop ()
22+
23+ def send (self , destination , message ):
24+ msg = f'Subject: allocation service notification\n { message } '
25+ self .server .sendmail (
26+ from_addr = 'allocations@example.com' ,
27+ to_addrs = [destination ],
28+ msg = msg
29+ )
Original file line number Diff line number Diff line change @@ -19,3 +19,8 @@ def get_redis_host_and_port():
1919 port = 63791 if host == 'localhost' else 6379
2020 return dict (host = host , port = port )
2121
22+ def get_email_host_and_port ():
23+ host = os .environ .get ('EMAIL_HOST' , 'localhost' )
24+ port = 11025 if host == 'localhost' else 1025
25+ http_port = 18025 if host == 'localhost' else 8025
26+ return dict (host = host , port = port , http_port = http_port )
Original file line number Diff line number Diff line change 55from allocation .domain import commands , events , model
66from allocation .domain .model import OrderLine
77if TYPE_CHECKING :
8+ from allocation .adapters import notifications
89 from . import unit_of_work
910
1011
1112class InvalidSku (Exception ):
1213 pass
1314
1415
16+
1517def add_batch (
1618 cmd : commands .CreateBatch , uow : unit_of_work .AbstractUnitOfWork
1719):
@@ -59,9 +61,9 @@ def change_batch_quantity(
5961#pylint: disable=unused-argument
6062
6163def send_out_of_stock_notification (
62- event : events .OutOfStock , send_mail : Callable ,
64+ event : events .OutOfStock , notifications : notifications . AbstractNotifications ,
6365):
64- send_mail (
66+ notifications . send (
6567 'stock@made.com' ,
6668 f'Out of stock for { event .sku } ' ,
6769 )
Original file line number Diff line number Diff line change 77from . import handlers
88
99if TYPE_CHECKING :
10- from allocation .service_layer import unit_of_work
10+ from . import unit_of_work
11+ from allocation .adapters import notifications
1112
1213logger = logging .getLogger (__name__ )
1314
@@ -20,11 +21,11 @@ class MessageBus:
2021 def __init__ (
2122 self ,
2223 uow : unit_of_work .AbstractUnitOfWork ,
23- send_mail : Callable ,
24+ notifications : notifications . AbstractNotifications ,
2425 publish : Callable ,
2526 ):
2627 self .uow = uow
27- self .dependencies = dict (uow = uow , send_mail = send_mail , publish = publish )
28+ self .dependencies = dict (uow = uow , notifications = notifications , publish = publish )
2829
2930
3031 def handle (self , message : Message ):
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ def sqlite_bus(sqlite_session_factory):
1212 uow = unit_of_work .SqlAlchemyUnitOfWork (sqlite_session_factory )
1313 bus = messagebus .MessageBus (
1414 uow = uow ,
15- send_mail = mock .Mock (),
15+ notifications = mock .Mock (),
1616 publish = mock .Mock (),
1717 )
1818 uow .bus = bus
You can’t perform that action at this time.
0 commit comments