Example #1
0
async def process_event(event_message: dict, flask_app):
    """Process the incoming queue event message."""
    if not flask_app:
        raise QueueException('Flask App not available.')

    with flask_app.app_context():
        message_type = event_message.get('type', None)
        email_msg = None
        email_dict = None
        token = RestService.get_service_account_token()
        if message_type == 'account.mailer':
            email_msg = json.loads(event_message.get('data'))
            email_dict = payment_completed.process(email_msg)
        elif message_type == 'bc.registry.payment.refundRequest':
            email_msg = event_message.get('data')
            email_dict = refund_requested.process(email_msg)
        elif event_message.get('type',
                               None) == 'bc.registry.payment.padAccountCreate':
            email_msg = event_message.get('data')
            email_dict = pad_confirmation.process(email_msg, token)

        logger.debug('Extracted email msg: %s', email_dict)
        process_email(email_dict, FLASK_APP, token)
Example #2
0
async def process_event(event_message: dict, flask_app):
    """Process the incoming queue event message."""
    if not flask_app:
        raise QueueException('Flask App not available.')

    with flask_app.app_context():
        message_type = event_message.get('type', None)
        email_msg = event_message.get('data')
        email_msg['logo_url'] = minio_service.MinioService.get_minio_public_url('bc_logo_for_email.png')
        email_dict = None
        token = RestService.get_service_account_token()
        logger.debug('message_type recieved %s', message_type)
        if message_type == 'account.mailer':
            email_dict = payment_completed.process(email_msg)
        elif message_type == MessageType.REFUND_REQUEST.value:
            email_dict = refund_requested.process(email_msg)
        elif message_type == MessageType.PAD_ACCOUNT_CREATE.value:
            email_msg['registry_logo_url'] = minio_service.MinioService.get_minio_public_url('bc_registry_logo_pdf.svg')
            email_dict = pad_confirmation.process(email_msg, token)
        elif message_type == MessageType.NSF_LOCK_ACCOUNT.value:
            logger.debug('lock account message recieved:')
            template_name = TemplateType.NSF_LOCK_ACCOUNT_TEMPLATE_NAME.value
            org_id = email_msg.get('accountId')
            admin_coordinator_emails = get_member_emails(org_id, (ADMIN, COORDINATOR))
            subject = SubjectType.NSF_LOCK_ACCOUNT_SUBJECT.value
            logo_url = email_msg.get('logo_url')
            email_dict = common_mailer.process(org_id, admin_coordinator_emails, template_name,
                                               subject, logo_url=logo_url)
        elif message_type == MessageType.NSF_UNLOCK_ACCOUNT.value:
            logger.debug('unlock account message recieved')
            template_name = TemplateType.NSF_UNLOCK_ACCOUNT_TEMPLATE_NAME.value
            org_id = email_msg.get('accountId')
            admin_coordinator_emails = get_member_emails(org_id, (ADMIN, COORDINATOR))
            subject = SubjectType.NSF_UNLOCK_ACCOUNT_SUBJECT.value
            logo_url = email_msg.get('logo_url')
            email_dict = common_mailer.process(org_id, admin_coordinator_emails,
                                               template_name, subject, logo_url=logo_url)
        elif message_type == MessageType.ACCOUNT_CONFIRMATION_PERIOD_OVER.value:
            template_name = TemplateType.ACCOUNT_CONF_OVER_TEMPLATE_NAME.value
            org_id = email_msg.get('accountId')
            nsf_fee = email_msg.get('nsfFee')
            admin_coordinator_emails = get_member_emails(org_id, (ADMIN,))
            subject = SubjectType.ACCOUNT_CONF_OVER_SUBJECT.value
            logo_url = email_msg.get('logo_url')
            email_dict = common_mailer.process(org_id, admin_coordinator_emails, template_name, subject,
                                               nsf_fee=nsf_fee, logo_url=logo_url)
        elif message_type in (MessageType.TEAM_MODIFIED.value, MessageType.TEAM_MEMBER_INVITED.value):
            logger.debug('Team Modified message received')
            template_name = TemplateType.TEAM_MODIFIED_TEMPLATE_NAME.value
            org_id = email_msg.get('accountId')
            admin_coordinator_emails = get_member_emails(org_id, (ADMIN,))
            subject = SubjectType.TEAM_MODIFIED_SUBJECT.value
            logo_url = email_msg.get('logo_url')
            email_dict = common_mailer.process(org_id, admin_coordinator_emails, template_name,
                                               subject, logo_url=logo_url)
        elif message_type == MessageType.ADMIN_REMOVED.value:
            logger.debug('ADMIN_REMOVED message received')
            template_name = TemplateType.ADMIN_REMOVED_TEMPLATE_NAME.value
            org_id = email_msg.get('accountId')
            recipient_email = email_msg.get('recipientEmail')
            subject = SubjectType.ADMIN_REMOVED_SUBJECT.value
            logo_url = email_msg.get('logo_url')
            email_dict = common_mailer.process(org_id, recipient_email, template_name,
                                               subject, logo_url=logo_url)
        elif message_type == MessageType.PAD_INVOICE_CREATED.value:
            template_name = TemplateType.PAD_INVOICE_CREATED_TEMPLATE_NAME.value
            org_id = email_msg.get('accountId')
            admin_coordinator_emails = get_member_emails(org_id, (ADMIN,))
            subject = SubjectType.PAD_INVOICE_CREATED.value
            args = {
                'nsf_fee': email_msg.get('nsfFee'),
                'invoice_total': email_msg.get('invoice_total'),
            }
            logo_url = email_msg.get('logo_url')
            email_dict = common_mailer.process(org_id, admin_coordinator_emails, template_name,
                                               subject, logo_url=logo_url,
                                               **args)
        elif message_type in (MessageType.ONLINE_BANKING_OVER_PAYMENT.value,
                              MessageType.ONLINE_BANKING_UNDER_PAYMENT.value, MessageType.ONLINE_BANKING_PAYMENT.value):

            if message_type == MessageType.ONLINE_BANKING_OVER_PAYMENT.value:
                template_name = TemplateType.ONLINE_BANKING_OVER_PAYMENT_TEMPLATE_NAME.value
            elif message_type == MessageType.ONLINE_BANKING_UNDER_PAYMENT.value:
                template_name = TemplateType.ONLINE_BANKING_UNDER_PAYMENT_TEMPLATE_NAME.value
            else:
                template_name = TemplateType.ONLINE_BANKING_PAYMENT_TEMPLATE_NAME.value

            org_id = email_msg.get('accountId')
            admin_emails = get_member_emails(org_id, (ADMIN,))
            subject = SubjectType.ONLINE_BANKING_PAYMENT_SUBJECT.value
            args = {
                'title': subject,
                'paid_amount': email_msg.get('amount'),
                'credit_amount': email_msg.get('creditAmount'),
            }
            logo_url = email_msg.get('logo_url')
            email_dict = common_mailer.process(org_id, admin_emails, template_name,
                                               subject, logo_url=logo_url,
                                               **args)
        elif message_type == MessageType.PAD_SETUP_FAILED.value:
            template_name = TemplateType.PAD_SETUP_FAILED_TEMPLATE_NAME.value
            org_id = email_msg.get('accountId')
            admin_coordinator_emails = get_member_emails(org_id, (ADMIN,))
            subject = SubjectType.PAD_SETUP_FAILED.value
            args = {
                'accountId': email_msg.get('accountId'),
            }
            logo_url = email_msg.get('logo_url')
            email_dict = common_mailer.process(org_id, admin_coordinator_emails, template_name,
                                               subject, logo_url=logo_url,
                                               **args)
        elif message_type == MessageType.PAYMENT_PENDING.value:
            template_name = TemplateType.PAYMENT_PENDING_TEMPLATE_NAME.value
            org_id = email_msg.get('accountId')
            admin_coordinator_emails = get_member_emails(org_id, (ADMIN,))
            subject = SubjectType.PAYMENT_PENDING.value
            args = {
                'accountId': email_msg.get('accountId'),
                'cfsAccountId': email_msg.get('cfsAccountId'),
                'transactionAmount': email_msg.get('transactionAmount'),
            }
            logo_url = email_msg.get('logo_url')
            email_dict = common_mailer.process(org_id, admin_coordinator_emails, template_name,
                                               subject, logo_url=logo_url,
                                               **args)
        elif message_type == MessageType.EJV_FAILED.value:
            email_dict = ejv_failures.process(email_msg)

        if email_dict:
            logger.debug('Extracted email msg Recipient: %s ', email_dict.get('recipients', ''))
            process_email(email_dict, FLASK_APP, token)
        else:
            # TODO probably an unhandled event.handle better
            logger.error('No email content generated')
Example #3
0
async def process_event(event_message: dict, flask_app):
    """Process the incoming queue event message."""
    if not flask_app:
        raise QueueException('Flask App not available.')

    with flask_app.app_context():
        message_type = event_message.get('type', None)
        email_msg = event_message.get('data')
        email_msg[
            'logo_url'] = minio_service.MinioService.get_minio_public_url(
                'bc_logo_for_email.png')
        email_dict = None
        token = RestService.get_service_account_token()
        logger.debug('message_type recieved %s', message_type)
        if message_type == 'account.mailer':
            email_dict = payment_completed.process(email_msg)
        elif message_type in ([
                MessageType.REFUND_DIRECT_PAY_REQUEST.value,
                MessageType.REFUND_DRAWDOWN_REQUEST.value
        ]):
            email_dict = refund_requested.process(event_message)
        elif message_type == MessageType.PAD_ACCOUNT_CREATE.value:
            email_msg[
                'registry_logo_url'] = minio_service.MinioService.get_minio_public_url(
                    'bc_registry_logo_pdf.svg')
            email_dict = pad_confirmation.process(email_msg, token)
        elif message_type == MessageType.NSF_LOCK_ACCOUNT.value:
            logger.debug('lock account message recieved:')
            template_name = TemplateType.NSF_LOCK_ACCOUNT_TEMPLATE_NAME.value
            org_id = email_msg.get('accountId')
            admin_coordinator_emails = get_member_emails(
                org_id, (ADMIN, COORDINATOR))
            subject = SubjectType.NSF_LOCK_ACCOUNT_SUBJECT.value
            logo_url = email_msg.get('logo_url')
            email_dict = common_mailer.process(org_id,
                                               admin_coordinator_emails,
                                               template_name,
                                               subject,
                                               logo_url=logo_url)
        elif message_type == MessageType.NSF_UNLOCK_ACCOUNT.value:
            logger.debug('unlock account message recieved')
            template_name = TemplateType.NSF_UNLOCK_ACCOUNT_TEMPLATE_NAME.value
            org_id = email_msg.get('accountId')
            admin_coordinator_emails = get_member_emails(
                org_id, (ADMIN, COORDINATOR))
            subject = SubjectType.NSF_UNLOCK_ACCOUNT_SUBJECT.value
            logo_url = email_msg.get('logo_url')
            email_dict = common_mailer.process(org_id,
                                               admin_coordinator_emails,
                                               template_name,
                                               subject,
                                               logo_url=logo_url)
        elif message_type == MessageType.ACCOUNT_CONFIRMATION_PERIOD_OVER.value:
            template_name = TemplateType.ACCOUNT_CONF_OVER_TEMPLATE_NAME.value
            org_id = email_msg.get('accountId')
            nsf_fee = email_msg.get('nsfFee')
            admin_coordinator_emails = get_member_emails(org_id, (ADMIN, ))
            subject = SubjectType.ACCOUNT_CONF_OVER_SUBJECT.value
            logo_url = email_msg.get('logo_url')
            email_dict = common_mailer.process(org_id,
                                               admin_coordinator_emails,
                                               template_name,
                                               subject,
                                               nsf_fee=nsf_fee,
                                               logo_url=logo_url)
        elif message_type in (MessageType.TEAM_MODIFIED.value,
                              MessageType.TEAM_MEMBER_INVITED.value):
            logger.debug('Team Modified message received')
            template_name = TemplateType.TEAM_MODIFIED_TEMPLATE_NAME.value
            org_id = email_msg.get('accountId')
            admin_coordinator_emails = get_member_emails(org_id, (ADMIN, ))
            subject = SubjectType.TEAM_MODIFIED_SUBJECT.value
            logo_url = email_msg.get('logo_url')
            email_dict = common_mailer.process(org_id,
                                               admin_coordinator_emails,
                                               template_name,
                                               subject,
                                               logo_url=logo_url)
        elif message_type == MessageType.ADMIN_REMOVED.value:
            logger.debug('ADMIN_REMOVED message received')
            template_name = TemplateType.ADMIN_REMOVED_TEMPLATE_NAME.value
            org_id = email_msg.get('accountId')
            recipient_email = email_msg.get('recipientEmail')
            subject = SubjectType.ADMIN_REMOVED_SUBJECT.value
            logo_url = email_msg.get('logo_url')
            email_dict = common_mailer.process(org_id,
                                               recipient_email,
                                               template_name,
                                               subject,
                                               logo_url=logo_url)
        elif message_type == MessageType.PAD_INVOICE_CREATED.value:
            template_name = TemplateType.PAD_INVOICE_CREATED_TEMPLATE_NAME.value
            org_id = email_msg.get('accountId')
            admin_coordinator_emails = get_member_emails(org_id, (ADMIN, ))
            subject = SubjectType.PAD_INVOICE_CREATED.value
            invoice_process_date = datetime.fromisoformat(
                email_msg.get('invoice_process_date'))
            args = {
                'nsf_fee':
                email_msg.get('nsfFee'),
                'invoice_total':
                email_msg.get('invoice_total'),
                'invoice_process_date':
                get_local_formatted_date(invoice_process_date, '%m-%d-%Y')
            }
            logo_url = email_msg.get('logo_url')
            email_dict = common_mailer.process(org_id,
                                               admin_coordinator_emails,
                                               template_name,
                                               subject,
                                               logo_url=logo_url,
                                               **args)
        elif message_type in (MessageType.ONLINE_BANKING_OVER_PAYMENT.value,
                              MessageType.ONLINE_BANKING_UNDER_PAYMENT.value,
                              MessageType.ONLINE_BANKING_PAYMENT.value):

            if message_type == MessageType.ONLINE_BANKING_OVER_PAYMENT.value:
                template_name = TemplateType.ONLINE_BANKING_OVER_PAYMENT_TEMPLATE_NAME.value
            elif message_type == MessageType.ONLINE_BANKING_UNDER_PAYMENT.value:
                template_name = TemplateType.ONLINE_BANKING_UNDER_PAYMENT_TEMPLATE_NAME.value
            else:
                template_name = TemplateType.ONLINE_BANKING_PAYMENT_TEMPLATE_NAME.value

            org_id = email_msg.get('accountId')
            admin_emails = get_member_emails(org_id, (ADMIN, ))
            subject = SubjectType.ONLINE_BANKING_PAYMENT_SUBJECT.value
            args = {
                'title': subject,
                'paid_amount': email_msg.get('amount'),
                'credit_amount': email_msg.get('creditAmount'),
            }
            logo_url = email_msg.get('logo_url')
            email_dict = common_mailer.process(org_id,
                                               admin_emails,
                                               template_name,
                                               subject,
                                               logo_url=logo_url,
                                               **args)
        elif message_type == MessageType.PAD_SETUP_FAILED.value:
            template_name = TemplateType.PAD_SETUP_FAILED_TEMPLATE_NAME.value
            org_id = email_msg.get('accountId')
            admin_coordinator_emails = get_member_emails(org_id, (ADMIN, ))
            subject = SubjectType.PAD_SETUP_FAILED.value
            args = {
                'accountId': email_msg.get('accountId'),
            }
            logo_url = email_msg.get('logo_url')
            email_dict = common_mailer.process(org_id,
                                               admin_coordinator_emails,
                                               template_name,
                                               subject,
                                               logo_url=logo_url,
                                               **args)
        elif message_type == MessageType.PAYMENT_PENDING.value:
            template_name = TemplateType.PAYMENT_PENDING_TEMPLATE_NAME.value
            org_id = email_msg.get('accountId')
            admin_coordinator_emails = get_member_emails(org_id, (ADMIN, ))
            subject = SubjectType.PAYMENT_PENDING.value
            args = {
                'accountId': email_msg.get('accountId'),
                'cfsAccountId': email_msg.get('cfsAccountId'),
                'transactionAmount': email_msg.get('transactionAmount'),
            }
            logo_url = email_msg.get('logo_url')
            email_dict = common_mailer.process(org_id,
                                               admin_coordinator_emails,
                                               template_name,
                                               subject,
                                               logo_url=logo_url,
                                               **args)
        elif message_type == MessageType.EJV_FAILED.value:
            email_dict = ejv_failures.process(email_msg)
        elif message_type == MessageType.RESET_PASSCODE.value:
            template_name = TemplateType.RESET_PASSCODE_TEMPLATE_NAME.value

            subject = SubjectType.RESET_PASSCODE.value
            email_msg.update({'header': Constants.RESET_PASSCODE_HEADER.value})

            email_dict = common_mailer.process(
                org_id=None,
                recipients=email_msg.get('emailAddresses'),
                template_name=template_name,
                subject=subject,
                **email_msg)

        else:
            if any(x for x in MessageType if x.value == message_type):
                title = TitleType[MessageType(message_type).name].value

                subject = SubjectType[MessageType(message_type).name].value\
                    .format(user_first_name=email_msg.get('userFirstName'),
                            user_last_name=email_msg.get('userLastName'),
                            product_name=email_msg.get('productName'),
                            account_name=email_msg.get('orgName')
                            )
                template_name = TemplateType[
                    f'{MessageType(message_type).name}_TEMPLATE_NAME'].value
            else:
                return

            kwargs = {
                'title': title,
                'user_first_name': email_msg.get('userFirstName'),
                'user_last_name': email_msg.get('userLastName'),
                'context_url': email_msg.get('contextUrl'),
                'role': email_msg.get('role'),
                'label': email_msg.get('label'),
                'product_name': email_msg.get('productName'),
                'remarks': email_msg.get('remarks'),
                'applicationDate': email_msg.get('applicationDate')
            }

            org_id = email_msg.get('accountId')
            logo_url = email_msg.get('logo_url')
            email_dict = common_mailer.process(
                org_id=org_id,
                recipients=email_msg.get('emailAddresses'),
                template_name=template_name,
                logo_url=logo_url,
                subject=subject,
                **kwargs)

        if email_dict:
            logger.debug('Extracted email msg Recipient: %s ',
                         email_dict.get('recipients', ''))
            process_email(email_dict, FLASK_APP, token)
        else:
            # TODO probably an unhandled event.handle better
            logger.error('No email content generated')