Skip to content

Commit

Permalink
account staff review fix (#1706)
Browse files Browse the repository at this point in the history
  • Loading branch information
karthik-aot authored Jun 29, 2021
1 parent 9a5e3a7 commit b32fb68
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
10 changes: 9 additions & 1 deletion auth-api/src/auth_api/models/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from sqlalchemy import Column, DateTime, ForeignKey, Integer, String
from sqlalchemy.orm import relationship

from ..utils.enums import TaskRelationshipStatus
from ..utils.enums import TaskRelationshipStatus, TaskStatus
from .base_model import BaseModel
from .db import db

Expand Down Expand Up @@ -69,3 +69,11 @@ def fetch_tasks(cls, task_type: str, task_status: str, # pylint:disable=too-man
def find_by_task_id(cls, task_id):
"""Find a task instance that matches the provided id."""
return db.session.query(Task).filter_by(id=task_id).first()

@classmethod
def find_by_task_relationship_id(cls, relationship_id: int, task_relationship_type: str,
task_status: str = TaskStatus.OPEN.value):
"""Find a task instance that related to the relationship id ( may be an ORG or a PRODUCT."""
return db.session.query(Task).filter(Task.relationship_id == relationship_id,
Task.relationship_type == task_relationship_type,
task_status == task_status).first()
20 changes: 12 additions & 8 deletions auth-api/src/auth_api/services/org.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from auth_api.models import Membership as MembershipModel
from auth_api.models import Org as OrgModel
from auth_api.models import User as UserModel
from auth_api.models import Task as TaskModel
from auth_api.models.affidavit import Affidavit as AffidavitModel
from auth_api.schemas import ContactSchema, InvitationSchema, OrgSchema
from auth_api.services.validators.access_type import validate as access_type_validate
Expand All @@ -53,7 +54,6 @@
from .task import Task as TaskService
from .validators.validator_response import ValidatorResponse


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


Expand Down Expand Up @@ -133,14 +133,14 @@ def create_org(org_info: dict, user_id, origin_url: str = None):
# Send an email to staff to remind review the pending account
is_bceid_status_handling_needed = access_type in (AccessType.EXTRA_PROVINCIAL.value,
AccessType.REGULAR_BCEID.value) \
and not AffidavitModel.find_approved_by_user_id(user_id=user_id)
and not AffidavitModel.find_approved_by_user_id(user_id=user_id)
if is_bceid_status_handling_needed:
Org._handle_bceid_status_and_notification(org)

org.commit()

if is_bceid_status_handling_needed:
Org.send_staff_review_account_reminder(org.id, origin_url)
Org.send_staff_review_account_reminder(relationship_id=org.id, origin_url=origin_url)

current_app.logger.info(f'<created_org org_id:{org.id}')

Expand Down Expand Up @@ -413,7 +413,7 @@ def update_org(self, org_info, origin_url: str = None): # pylint: disable=too-m
self._model.update_org_from_dict(camelback2snake(org_info), exclude=excluded)
if is_govm_account_creation:
# send mail after the org is committed to DB
Org.send_staff_review_account_reminder(self._model.id, origin_url)
Org.send_staff_review_account_reminder(relationship_id=self._model.id, origin_url=origin_url)

Org._create_payment_for_org(mailing_address, self._model, payment_info, False)
current_app.logger.debug('>update_org ')
Expand Down Expand Up @@ -815,26 +815,30 @@ def approve_or_reject(org_id: int, is_approved: bool, origin_url: str = None):

@staticmethod
@user_context
def send_staff_review_account_reminder(org_id, origin_url, **kwargs):
def send_staff_review_account_reminder(relationship_id, origin_url,
task_relationship_type=TaskRelationshipType.ORG.value,
**kwargs):
"""Send staff review account reminder notification."""
current_app.logger.debug('<send_staff_review_account_reminder')
recipient = current_app.config.get('STAFF_ADMIN_EMAIL')
context_path = f'review-account/{org_id}'
# Get task id that is related with the task. Task Relationship Type can be ORG or PRODUCT.
task = TaskModel.find_by_task_relationship_id(task_relationship_type=task_relationship_type,
relationship_id=relationship_id)
context_path = f'review-account/{task.id}'
app_url = '{}/{}'.format(origin_url, current_app.config.get('AUTH_WEB_TOKEN_CONFIRM_PATH'))
review_url = '{}/{}'.format(app_url, context_path)
user_from_context: UserContext = kwargs['user_context']
first_name = user_from_context.first_name
last_name = user_from_context.last_name

data = {
'accountId': org_id,
'emailAddresses': recipient,
'contextUrl': review_url,
'userFirstName': first_name,
'userLastName': last_name
}
try:
publish_to_mailer('staffReviewAccount', org_id=org_id, data=data)
publish_to_mailer('staffReviewAccount', org_id=relationship_id, data=data)
current_app.logger.debug('<send_staff_review_account_reminder')
except Exception as e: # noqa=B901
current_app.logger.error('<send_staff_review_account_reminder failed')
Expand Down
19 changes: 19 additions & 0 deletions auth-api/tests/unit/models/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,22 @@ def test_fetch_pending_tasks_descending(session): # pylint:disable=unused-argum
assert found_tasks[0].name == 'TEST 2'
assert found_tasks[1].name == 'TEST 1'
assert count == 2


def test_finding_task_by_relationship_id(session): # pylint:disable=unused-argument
"""Assert that we can fetch all tasks."""
user = factory_user_model()
task = TaskModel(name='TEST 1', date_submitted=datetime.now(),
relationship_type=TaskRelationshipType.ORG.value,
relationship_id=10, type=TaskTypePrefix.NEW_ACCOUNT_STAFF_REVIEW.value,
status=TaskStatus.OPEN.value,
related_to=user.id,
relationship_status=TaskRelationshipStatus.PENDING_STAFF_REVIEW.value)
task.save()

found_task = TaskModel.find_by_task_relationship_id(
task_relationship_type=TaskRelationshipType.ORG.value, relationship_id=10)
assert found_task
assert found_task.name == 'TEST 1'
assert found_task.relationship_id == 10
assert found_task.status == TaskStatus.OPEN.value

0 comments on commit b32fb68

Please sign in to comment.