Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
lib-py-b2b / notification.py
Size: Mime:
import logging
from os import environ
from .customer import Customer
from botocore.exceptions import ClientError
from .email import Email
from .address import Address
from enum import Enum


logger = logging.getLogger(__name__)

default_email_list = ['les.stroud@barnhardt.net', 'dennis.lee@barnhardt.net']


class NotificationData:
    def __init__(self, order_id: str, message: str):
        self.order_id = order_id
        self.message = message

    def as_dict(self):
        return vars(self)


class ErrorNotificationData(NotificationData):
    def __init__(self, order_id:str, message: str, errors: list):
        super().__init__(order_id, message)
        self.errors = errors


class AddressNotificationData(NotificationData):
    def __init__(self, order_id:str, message: str, original_address: Address, recommended_address: Address = None):
        super().__init__(order_id, message)
        self.original_address = original_address
        self.recommended_address = recommended_address


class NotificationType(Enum):
    TECHNICAL = ('TECHNICAL', 'error_message.jinja2', 'send_technical_errors_to')
    BUSINESS = ('BUSINESS', 'error_message.jinja2', 'send_business_errors_to')
    ADDRESS = ('ADDRESS', 'address_message.jinja2', 'send_address_errors_to')
    CUSTSVC = ('CUSTSVC', 'error_message.jinja2', 'send_address_errors_to')
    OPERATIONS = ('OPERATIONS', 'notification.jinja2', 'send_operations_notifications_to')

    def __init__(self, name, template_name, customer_recipient_key):
        self.thename = name
        self.template_name = template_name
        self.customer_recipient_key = customer_recipient_key

    def render(self, data: NotificationData):
        from jinja2 import PackageLoader, Environment
        env = Environment(loader=PackageLoader('lib_b2b', 'notification_templates'))
        template = env.get_template(self.template_name)
        rendered = template.render(data.as_dict())
        return rendered


class Notifier:
    @staticmethod
    def notify(notification_type: NotificationType, customer, subject: str, data: NotificationData, recipients: [str] = None):
        """
        send an email notifcation to the appropriate folks for a particular customer and notification type.
        :param notification_type: Of type NotificationType. Determines the template and recipient list to use
        :param customer: The customer object or customer_edi_id of the order where the issue occurred
        :param subject: The subject of the email to be sent
        :param data: a subclass of NotificationData that contains the data necessary for the notification template
        :param recipients: Optional.  By default, will pull the list from the customer record based on the type of notification.
        :return: None
        """
        try:
            _customer = Customer.for_(customer)
            _recipients = recipients or _customer.get(notification_type.customer_recipient_key, default_email_list)
            _body = notification_type.render(data)
            email = Email(_recipients, subject, _body)
            email.send(from_addr='no-reply@barnhardtapps.net')
        except ClientError as e:
            logger.error(f"Unable to find recipients for address notification for customer {customer}.", e)