Skip to content

Commit

Permalink
17831 - EFT Monthly Statement (#1279)
Browse files Browse the repository at this point in the history
* 17831 - EFT Monthly Statement

* 17831 - PR fixes

Fix tests (restore old factory method), fix missing recipients list

* 17831 - PR updates

* clean up comment typos
  • Loading branch information
ochiu authored Oct 13, 2023
1 parent 0333fe1 commit 8ad8dae
Show file tree
Hide file tree
Showing 8 changed files with 525 additions and 10 deletions.
8 changes: 8 additions & 0 deletions jobs/payment-jobs/flags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"flagValues": {
"string-flag": "a string value",
"bool-flag": true,
"integer-flag": 10,
"enable-eft-payment-method": true
}
}
2 changes: 1 addition & 1 deletion jobs/payment-jobs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-e git+https://github.com/bcgov/sbc-common-components.git@b93585ea3ac273b9e51c4dd5ddbc8190fd95da6a#egg=sbc_common_components&subdirectory=python
-e git+https://github.com/bcgov/sbc-pay.git@2e4373b8a9280d64737f7b6ed172831ba4e541d2#egg=pay_api&subdirectory=pay-api
-e git+https://github.com/bcgov/sbc-pay.git@90fc03c42aa6e987974398ad3dbcf35b5b21a6c5#egg=pay_api&subdirectory=pay-api
Flask-Caching==2.0.2
Flask-Migrate==2.7.0
Flask-Moment==1.0.5
Expand Down
1 change: 1 addition & 0 deletions jobs/payment-jobs/requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pytest-mock
requests
pyhamcrest
pytest-cov
Faker

# Lint and code style
flake8==5.0.4
Expand Down
27 changes: 21 additions & 6 deletions jobs/payment-jobs/tasks/statement_notification_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
from pay_api.models.statement import Statement as StatementModel
from pay_api.models.statement_recipients import StatementRecipients as StatementRecipientsModel
from pay_api.services.oauth_service import OAuthService
from pay_api.utils.enums import AuthHeaderType, ContentType, NotificationStatus
from pay_api.services import Statement as StatementService
from pay_api.services.flags import flags
from pay_api.utils.enums import AuthHeaderType, ContentType, NotificationStatus, PaymentMethod

from utils.auth import get_token

from utils.mailer import publish_statement_notification

ENV = Environment(loader=FileSystemLoader('.'), autoescape=True)

Expand Down Expand Up @@ -62,7 +64,7 @@ def send_notifications(cls):
statement.notification_status_code = NotificationStatus.PROCESSING.value
statement.notification_date = datetime.now()
statement.commit()
payment_account = PaymentAccountModel.find_by_id(statement.payment_account_id)
payment_account: PaymentAccountModel = PaymentAccountModel.find_by_id(statement.payment_account_id)
recipients = StatementRecipientsModel.find_all_recipients_for_payment_id(statement.payment_account_id)
if len(recipients) < 1:
current_app.logger.info(f'No recipients found for statement: '
Expand All @@ -78,14 +80,27 @@ def send_notifications(cls):
params['frequency'] = statement.frequency.lower()
# logic changed https://github.com/bcgov/entity/issues/4809
# params.update({'url': params['url'].replace('orgId', payment_account.auth_account_id)})

notification_success = True
eft_enabled = flags.is_on('enable-eft-payment-method', default=False)
try:
notify_response = cls.send_email(token, to_emails, template.render(params))
if not payment_account.payment_method == PaymentMethod.EFT.value:
notification_success = cls.send_email(token, to_emails, template.render(params))
elif eft_enabled: # This statement template currently only used for EFT
result = StatementService.get_summary(payment_account.auth_account_id)
notification_success = publish_statement_notification(payment_account, statement,
result['total_due'], to_emails)
else: # EFT not enabled - mark skip - shouldn't happen, but safeguard for manual data injection
statement.notification_status_code = NotificationStatus.SKIP.value
statement.notification_date = datetime.now()
statement.commit()
continue
except Exception as e: # NOQA # pylint:disable=broad-except
current_app.logger.error('<notification failed')
current_app.logger.error(e)
notify_response = False
notification_success = False

if not notify_response:
if not notification_success:
current_app.logger.error('<notification failed')
statement.notification_status_code = NotificationStatus.FAILED.value
statement.notification_date = datetime.now()
Expand Down
28 changes: 26 additions & 2 deletions jobs/payment-jobs/tests/jobs/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from pay_api.models import (
CfsAccount, DistributionCode, DistributionCodeLink, Invoice, InvoiceReference, Payment, PaymentAccount,
PaymentLineItem, Receipt, Refund, RoutingSlip, StatementSettings)
PaymentLineItem, Receipt, Refund, RoutingSlip, StatementRecipients, StatementSettings)
from pay_api.utils.enums import (
CfsAccountStatus, InvoiceReferenceStatus, InvoiceStatus, LineItemStatus, PaymentMethod, PaymentStatus,
PaymentSystem, RoutingSlipStatus)
Expand All @@ -36,6 +36,18 @@ def factory_premium_payment_account(bcol_user_id='PB25020', bcol_account_id='123
return account


def factory_statement_recipient(auth_user_id: int, first_name: str, last_name: str, email: str,
payment_account_id: int):
"""Return statement recipient model."""
return StatementRecipients(
auth_user_id=auth_user_id,
firstname=first_name,
lastname=last_name,
email=email,
payment_account_id=payment_account_id
).save()


def factory_statement_settings(pay_account_id: str, frequency='DAILY', from_date=datetime.now(),
to_date=None) -> StatementSettings:
"""Return Factory."""
Expand Down Expand Up @@ -66,7 +78,7 @@ def factory_payment(
def factory_invoice(payment_account: PaymentAccount, status_code: str = InvoiceStatus.CREATED.value,
corp_type_code='CP',
business_identifier: str = 'CP0001234',
service_fees: float = 0.0, total=0,
service_fees: float = 0.0, total=0, paid=0,
payment_method_code: str = PaymentMethod.DIRECT_PAY.value,
created_on: datetime = datetime.now(),
cfs_account_id: int = 0,
Expand All @@ -79,6 +91,7 @@ def factory_invoice(payment_account: PaymentAccount, status_code: str = InvoiceS
invoice_status_code=status_code,
payment_account_id=payment_account.id,
total=total,
paid=paid,
created_by='test',
created_on=created_on,
business_identifier=business_identifier,
Expand Down Expand Up @@ -206,6 +219,17 @@ def factory_create_eft_account(auth_account_id='1234', status=CfsAccountStatus.P
return account


def factory_create_account(auth_account_id: str = '1234', payment_method_code: str = PaymentMethod.DIRECT_PAY.value,
status: str = CfsAccountStatus.PENDING.value, statement_notification_enabled: bool = True):
"""Return payment account model."""
account = PaymentAccount(auth_account_id=auth_account_id,
payment_method=payment_method_code,
name=f'Test {auth_account_id}',
statement_notification_enabled=statement_notification_enabled).save()
CfsAccount(status=status, account_id=account.id).save()
return account


def factory_create_ejv_account(auth_account_id='1234',
client: str = '112',
resp_centre: str = '11111',
Expand Down
Loading

0 comments on commit 8ad8dae

Please sign in to comment.