From cdb11e18fd33bcb33da616b6a72c4192353b8ba5 Mon Sep 17 00:00:00 2001
From: Isuru Gunawardana
Date: Tue, 7 May 2024 14:31:23 -0700
Subject: [PATCH 1/3] email notification for report requests
---
.../api/mines/reports/models/mine_report.py | 40 ++++-
.../mines/reports/resources/mine_reports.py | 8 +-
.../report/ms_new_report_requested_email.html | 152 ++++++++++++++++++
3 files changed, 193 insertions(+), 7 deletions(-)
create mode 100644 services/core-api/app/templates/email/report/ms_new_report_requested_email.html
diff --git a/services/core-api/app/api/mines/reports/models/mine_report.py b/services/core-api/app/api/mines/reports/models/mine_report.py
index d71240d607..4258b95779 100644
--- a/services/core-api/app/api/mines/reports/models/mine_report.py
+++ b/services/core-api/app/api/mines/reports/models/mine_report.py
@@ -1,5 +1,5 @@
import uuid
-
+from flask import current_app
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.schema import FetchedValue
from sqlalchemy.ext.associationproxy import association_proxy
@@ -22,6 +22,7 @@
from app.api.mines.reports.models.mine_report_notification import MineReportNotification
from app.api.utils.helpers import get_current_core_or_ms_env_url
from app.api.utils.helpers import format_email_datetime_to_string
+from app.api.mines.exceptions.mine_exceptions import MineException
class MineReport(SoftDeleteMixin, AuditMixin, Base):
__tablename__ = "mine_report"
@@ -223,15 +224,15 @@ def collectRecipients(self, is_proponent):
return core_recipients, ms_recipients
def getReportSpecificEmailsByReportType(self, compliance_details):
- notificaiton_list = MineReportNotification.find_contact_by_compliance_article(compliance_details.section,
+ notification_list = MineReportNotification.find_contact_by_compliance_article(compliance_details.section,
compliance_details.sub_section, compliance_details.paragraph, compliance_details.sub_paragraph)
unique_recipients = set()
regional_email = self.mine.region.regional_contact_office.email
- for ntf in notificaiton_list:
- notifiy_email = ntf[0]
- if notifiy_email not in unique_recipients:
- unique_recipients.add(notifiy_email)
+ for ntf in notification_list:
+ notify_email = ntf[0]
+ if notify_email not in unique_recipients:
+ unique_recipients.add(notify_email)
if ntf[1] and self.mine.major_mine_ind and PERM_RECL_EMAIL not in unique_recipients:
unique_recipients.add(PERM_RECL_EMAIL)
@@ -256,6 +257,33 @@ def send_crr_report_update_email(self, is_edit):
body += f'View updates in Core: {link}
'
EmailService.send_email(subject, recipients, body)
+ def send_report_requested_email(self, report_name):
+ if self.mine.mine_manager:
+ recipients = [self.mine.mine_manager.party.email]
+ else:
+ current_app.logger.info(f"Can't find mine manager's email for the mine: {self.mine.mine_name}")
+ raise MineException(f"Couldn't send the email for the mine manager as no manager found for the mine: {self.mine.mine_name}")
+
+ subject = f'Report "{report_name}" is requested in MineSpace'
+ due_date = due_date = (self.due_date).strftime("%b %d %Y") if self.due_date else "N/A"
+ ms_url = get_current_core_or_ms_env_url("ms")
+ ms_report_page_link = f'{ms_url}/mines/{self.mine.mine_guid}/reports/{self.mine_report_guid}'
+
+ email_context = {
+ "report_request": {
+ "mine_number": self.mine.mine_no,
+ "mine_name": self.mine.mine_name,
+ "report_name": report_name,
+ "report_compliance_year": self.submission_year,
+ "report_due_date": due_date,
+ },
+ "minespace_login_link": ms_url,
+ "ms_report_page_link": ms_report_page_link
+ }
+
+ ms_email_body = open("app/templates/email/report/ms_new_report_requested_email.html", "r").read()
+ EmailService.send_template_email(subject, recipients, ms_email_body, email_context, cc=None)
+
@classmethod
def create(cls,
mine_report_definition_id,
diff --git a/services/core-api/app/api/mines/reports/resources/mine_reports.py b/services/core-api/app/api/mines/reports/resources/mine_reports.py
index 7e3a3493a3..8c01c6a535 100644
--- a/services/core-api/app/api/mines/reports/resources/mine_reports.py
+++ b/services/core-api/app/api/mines/reports/resources/mine_reports.py
@@ -1,6 +1,6 @@
import uuid
from flask_restx import Resource
-from flask import request
+from flask import request, current_app
from datetime import datetime
from werkzeug.exceptions import BadRequest, NotFound, InternalServerError
@@ -22,6 +22,8 @@
from app.api.mines.permits.permit_conditions.models.permit_condition_category import PermitConditionCategory
from app.api.utils.custom_reqparser import CustomReqparser
from app.api.mines.response_models import MINE_REPORT_MODEL
+from app.api.mines.exceptions.mine_exceptions import MineException
+
class MineReportListResource(Resource, UserMixin):
@@ -191,6 +193,10 @@ def post(self, mine_guid):
if is_report_request:
report_name = mine_report_definition.report_name if is_code_required_report else permit_condition_category.description
trigger_notification(f'A report has been requested by the ministry: {report_name}', ActivityType.report_requested, mine, 'MineReport', mine_report.mine_report_guid, None, None, ActivityRecipients.minespace_users)
+ try:
+ mine_report.send_report_requested_email(report_name)
+ except Exception as e:
+ current_app.logger.warning(f"Couldn't send the email notification for the requested report: {report_name}. {str(e)}")
return mine_report, 201
diff --git a/services/core-api/app/templates/email/report/ms_new_report_requested_email.html b/services/core-api/app/templates/email/report/ms_new_report_requested_email.html
new file mode 100644
index 0000000000..fa60060649
--- /dev/null
+++ b/services/core-api/app/templates/email/report/ms_new_report_requested_email.html
@@ -0,0 +1,152 @@
+
+
+
+
+
+
+
+
+
+
+
+ |
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ A report has been requested in MineSpace
+ |
+
+
+
+
+ To view more information about this submission please
+ log into the Minespace application.
+
+
+ Mine Name: {{ report_request.mine_name }}
+
+ Mine Number: {{report_request.mine_number}}
+
+
+ {{report_request.permit_info_label}}: {{report_request.report_name}}
+ Report Name: {{report_request.report_name}}
+
+
+ Compliance Year: {{ report_request.report_compliance_year }}
+ Due Date: {{ report_request.report_due_date }}
+
+ |
+
+
+
+
+ |
+
+
+ |
+
+
+
+
+
+
+
+
+
+
+
+ |
+ |
+
+
+
+
+
\ No newline at end of file
From 87674faf51a0cb3200f97f86f0cd5696329bd34b Mon Sep 17 00:00:00 2001
From: Isuru Gunawardana
Date: Tue, 7 May 2024 17:05:05 -0700
Subject: [PATCH 2/3] fixing permit view
---
.../api/mines/reports/models/mine_report.py | 18 ++++++++++++++++--
.../mines/reports/resources/mine_reports.py | 2 +-
.../report/ms_new_report_requested_email.html | 2 +-
3 files changed, 18 insertions(+), 4 deletions(-)
diff --git a/services/core-api/app/api/mines/reports/models/mine_report.py b/services/core-api/app/api/mines/reports/models/mine_report.py
index 4258b95779..c088ebfcfc 100644
--- a/services/core-api/app/api/mines/reports/models/mine_report.py
+++ b/services/core-api/app/api/mines/reports/models/mine_report.py
@@ -257,14 +257,26 @@ def send_crr_report_update_email(self, is_edit):
body += f'View updates in Core: {link}
'
EmailService.send_email(subject, recipients, body)
- def send_report_requested_email(self, report_name):
+ def send_report_requested_email(self, report_name, is_crr):
if self.mine.mine_manager:
recipients = [self.mine.mine_manager.party.email]
else:
current_app.logger.info(f"Can't find mine manager's email for the mine: {self.mine.mine_name}")
raise MineException(f"Couldn't send the email for the mine manager as no manager found for the mine: {self.mine.mine_name}")
- subject = f'Report "{report_name}" is requested in MineSpace'
+ if is_crr:
+ compliance_details = self.mine_report_definition.compliance_articles[0]
+ compliance_string = ComplianceArticle.get_compliance_article_string(self.mine_report_definition.compliance_articles[0])
+ report_name = f'{compliance_string} - {compliance_details.description}'
+ permit_info_value = ""
+ permit_info_label = ""
+
+ else: #PRR
+ report_name = self.report_name
+ permit_info_label = "Permit Number"
+ permit_info_value = self.permit_number + ": "
+
+ subject = "A Report is requested in MineSpace"
due_date = due_date = (self.due_date).strftime("%b %d %Y") if self.due_date else "N/A"
ms_url = get_current_core_or_ms_env_url("ms")
ms_report_page_link = f'{ms_url}/mines/{self.mine.mine_guid}/reports/{self.mine_report_guid}'
@@ -273,6 +285,8 @@ def send_report_requested_email(self, report_name):
"report_request": {
"mine_number": self.mine.mine_no,
"mine_name": self.mine.mine_name,
+ "permit_info_label": permit_info_label,
+ "permit_info_value": permit_info_value,
"report_name": report_name,
"report_compliance_year": self.submission_year,
"report_due_date": due_date,
diff --git a/services/core-api/app/api/mines/reports/resources/mine_reports.py b/services/core-api/app/api/mines/reports/resources/mine_reports.py
index 8c01c6a535..b31512f928 100644
--- a/services/core-api/app/api/mines/reports/resources/mine_reports.py
+++ b/services/core-api/app/api/mines/reports/resources/mine_reports.py
@@ -194,7 +194,7 @@ def post(self, mine_guid):
report_name = mine_report_definition.report_name if is_code_required_report else permit_condition_category.description
trigger_notification(f'A report has been requested by the ministry: {report_name}', ActivityType.report_requested, mine, 'MineReport', mine_report.mine_report_guid, None, None, ActivityRecipients.minespace_users)
try:
- mine_report.send_report_requested_email(report_name)
+ mine_report.send_report_requested_email(report_name, is_code_required_report)
except Exception as e:
current_app.logger.warning(f"Couldn't send the email notification for the requested report: {report_name}. {str(e)}")
diff --git a/services/core-api/app/templates/email/report/ms_new_report_requested_email.html b/services/core-api/app/templates/email/report/ms_new_report_requested_email.html
index fa60060649..88f87e4a01 100644
--- a/services/core-api/app/templates/email/report/ms_new_report_requested_email.html
+++ b/services/core-api/app/templates/email/report/ms_new_report_requested_email.html
@@ -90,7 +90,7 @@
Mine Number: {{report_request.mine_number}}
- {{report_request.permit_info_label}}: {{report_request.report_name}}
+ {{report_request.permit_info_label}} {{report_request.permit_info_value}}
Report Name: {{report_request.report_name}}
From 2a63a9e573ed796e7ee4780982e2e971051df2ba Mon Sep 17 00:00:00 2001
From: Isuru Gunawardana
Date: Fri, 10 May 2024 09:22:23 -0700
Subject: [PATCH 3/3] fix typo
---
services/core-api/app/api/mines/reports/models/mine_report.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/services/core-api/app/api/mines/reports/models/mine_report.py b/services/core-api/app/api/mines/reports/models/mine_report.py
index c088ebfcfc..5c64fda3fe 100644
--- a/services/core-api/app/api/mines/reports/models/mine_report.py
+++ b/services/core-api/app/api/mines/reports/models/mine_report.py
@@ -277,7 +277,7 @@ def send_report_requested_email(self, report_name, is_crr):
permit_info_value = self.permit_number + ": "
subject = "A Report is requested in MineSpace"
- due_date = due_date = (self.due_date).strftime("%b %d %Y") if self.due_date else "N/A"
+ due_date = (self.due_date).strftime("%b %d %Y") if self.due_date else "N/A"
ms_url = get_current_core_or_ms_env_url("ms")
ms_report_page_link = f'{ms_url}/mines/{self.mine.mine_guid}/reports/{self.mine_report_guid}'