Skip to content

Commit

Permalink
Notification fix (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
NavpreetGrewal authored Jan 11, 2021
1 parent 7f2e970 commit 0f5d70b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 40 deletions.
75 changes: 74 additions & 1 deletion backend/api/services/send_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
from requests.auth import HTTPBasicAuth
from email.header import Header
from email.utils import formataddr
from api.models.user_profile import UserProfile
from api.models.notification import Notification
from api.models.credit_transfer_statuses import CreditTransferStatuses
from api.models.notification_subscription import NotificationSubscription
from api.models.organization import Organization
import requests
from django.db.models import Q

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -110,4 +116,71 @@ def send_email(recipient_email: str) -> {}:
return
except requests.exceptions.RequestException as e:
LOGGER.error("Error: {}".format(e))
return
return


def notifications_credit_transfers(transfer: object):
validation_status = transfer.status
notifications = None
if validation_status == CreditTransferStatuses.VALIDATED:
notifications = Notification.objects.values_list('id', flat=True).filter(
Q(notification_code='CREDIT_TRANSFER_RECORDED_GOVT') |
Q(notification_code='CREDIT_TRANSFER_RECORDED'))

elif validation_status == CreditTransferStatuses.RECOMMEND_APPROVAL:
notifications = Notification.objects.values_list('id', flat=True).filter(
notification_code='CREDIT_TRANSFER_RECOMMEND_APPROVAL')

elif validation_status == CreditTransferStatuses.RECOMMEND_REJECTION:
notifications = Notification.objects.values_list('id', flat=True).filter(
notification_code='CREDIT_TRANSFER_RECOMMEND_REJECT')

elif validation_status == CreditTransferStatuses.APPROVED:
notifications = Notification.objects.values_list('id', flat=True).filter(
Q(notification_code='CREDIT_TRANSFER_APPROVED') |
Q(notification_code='CREDIT_TRANSFER_APPROVED_PARTNER'))

elif validation_status == CreditTransferStatuses.DISAPPROVED:
notifications = Notification.objects.values_list('id', flat=True).filter(
notification_code='CREDIT_TRANSFER_REJECT_PARTNER')

elif validation_status == CreditTransferStatuses.RESCINDED:
notifications = Notification.objects.values_list('id', flat=True).filter(
Q(notification_code='CREDIT_TRANSFER_RESCIND') |
Q(notification_code='CREDIT_TRANSFER_RESCIND_PARTNER'))

elif validation_status == CreditTransferStatuses.RESCIND_PRE_APPROVAL:
notifications = Notification.objects.values_list('id', flat=True).filter(
notification_code='CREDIT_TRANSFER_RESCIND_PARTNER')

elif validation_status == CreditTransferStatuses.REJECTED:
notifications = Notification.objects.values_list('id', flat=True).filter(
Q(notification_code='CREDIT_TRANSFER_REJECTED_GOVT') |
Q(notification_code='CREDIT_TRANSFER_REJECTED'))

if notifications:
subscribed_users(notifications, transfer)


"""
Send email to the users based on their notification subscription
"""


def subscribed_users(notifications: list, transfer: object):
user_email = None
try:
subscribed_users = NotificationSubscription.objects.values_list('user_profile_id', flat=True).filter(notification__id__in=notifications)

if subscribed_users:
govt_org = Organization.objects.filter(is_government=True).first()
user_email = UserProfile.objects.values_list('email', flat=True).filter(
Q(organization_id__in=[transfer.debit_from_id,
transfer.credit_to_id,
govt_org.id]) &
Q(id__in=subscribed_users)).exclude(email__isnull=True).exclude(email__exact='').exclude(username=transfer.update_user)

if user_email:
send_email(list(user_email))
except Exception as e:
LOGGER.error('Unable to send email! %s', e)
42 changes: 3 additions & 39 deletions backend/api/viewsets/credit_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
from api.models.notification import Notification
from api.models.notification_subscription import NotificationSubscription
from auditable.views import AuditableMixin
from api.services.send_email import send_email
from api.services.send_email import notifications_credit_transfers
from api.services.credit_transaction import validate_transfer

LOGGER = logging.getLogger(__name__)


class CreditTransferViewset(
AuditableMixin, viewsets.GenericViewSet,
mixins.CreateModelMixin, mixins.ListModelMixin,
Expand Down Expand Up @@ -91,41 +92,4 @@ def create(self, request, *args, **kwargs):
def perform_update(self, serializer, *args, **kwargs):

transfer = serializer.save()
email = None
if transfer.status == CreditTransferStatuses.VALIDATED:
#call service that updates transactions and balance tables
validate_transfer(transfer)
notifications = Notification.objects.values_list('id', flat=True).filter(Q(notification_code='CREDIT_TRANSFER_RECORDED_GOVT') | Q(notification_code='CREDIT_TRANSFER_RECORDED'))

elif transfer.status == CreditTransferStatuses.RECOMMEND_APPROVAL:
notifications = Notification.objects.values_list('id', flat=True).filter(notification_code='CREDIT_TRANSFER_RECOMMEND_APPROVAL')

elif transfer.status == CreditTransferStatuses.RECOMMEND_REJECTION:
notifications = Notification.objects.values_list('id', flat=True).filter(notification_code='CREDIT_TRANSFER_RECOMMEND_REJECT')

elif transfer.status == CreditTransferStatuses.APPROVED:
notifications = Notification.objects.values_list('id', flat=True).filter(Q(notification_code='CREDIT_TRANSFER_APPROVED') | Q(notification_code='CREDIT_TRANSFER_APPROVED_PARTNER'))

elif transfer.status == CreditTransferStatuses.DISAPPROVED:
notifications = Notification.objects.values_list('id', flat=True).filter(notification_code='CREDIT_TRANSFER_REJECT_PARTNER')

elif transfer.status == CreditTransferStatuses.RESCINDED:
notifications = Notification.objects.values_list('id', flat=True).filter(Q(notification_code='CREDIT_TRANSFER_RESCIND') | Q(notification_code='CREDIT_TRANSFER_RESCIND_PARTNER'))

elif transfer.status == CreditTransferStatuses.RESCIND_PRE_APPROVAL:
notifications = Notification.objects.values_list('id', flat=True).filter(notification_code='CREDIT_TRANSFER_RESCIND_PARTNER')

elif transfer.status == CreditTransferStatuses.REJECTED:
notifications = Notification.objects.values_list('id', flat=True).filter(Q(notification_code='CREDIT_TRANSFER_REJECTED_GOVT') | Q(notification_code='CREDIT_TRANSFER_REJECTED'))
"""
Send email to the users based on their notification subscription for a credit transfer
"""
try:
subscribed_users = NotificationSubscription.objects.values_list('user_profile_id', flat=True).filter(notification__id__in=notifications)
if subscribed_users:
user_email = UserProfile.objects.values_list('email', flat=True).filter(id__in=subscribed_users).exclude(email__isnull=True).exclude(email__exact='')
if user_email:
send_email(list(user_email))
except Exception as e:
LOGGER.error('Email Failed! %s', e)

notifications_credit_transfers(transfer)

0 comments on commit 0f5d70b

Please sign in to comment.