diff --git a/services/core-api/app/api/mines/mine/models/mine.py b/services/core-api/app/api/mines/mine/models/mine.py index 6021ef9a00..5ab1f0f6f9 100644 --- a/services/core-api/app/api/mines/mine/models/mine.py +++ b/services/core-api/app/api/mines/mine/models/mine.py @@ -1,4 +1,5 @@ import uuid +from datetime import datetime, timezone import utm from geoalchemy2 import Geometry @@ -180,9 +181,15 @@ def mine_location(self): @hybrid_property def mine_manager(self): if self.mine_party_appt: - return next( - (mpa for mpa in self.mine_party_appt if mpa.mine_party_appt_type_code == 'MMG'), - None) + today = datetime.now(timezone.utc) # To filter out previous mine managers. + for party in self.mine_party_appt: + party_end_date = None + if party.end_date: + party_end_date = datetime.strptime(str(party.end_date), '%Y-%m-%d').replace(tzinfo=timezone.utc) + if party.mine_party_appt_type_code == "MMG" and party.party.email \ + and (party_end_date == None or party_end_date > today) \ + and str(party.status).lower() != "inactive": #There are mine managers with null status + return party return None @hybrid_property 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 9b7bd65ebf..10632b483e 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 @@ -205,10 +205,8 @@ def collectRecipients(self, is_proponent): core_recipients.extend(contacts_email) # Adding mine manager's email. - if self.mine.mine_party_appt: - for party in self.mine.mine_party_appt: - if party.mine_party_appt_type_code == "MMG" and party.party.email: - ms_recipients.append(party.party.email) + if self.mine.mine_manager: + ms_recipients.append(self.mine.mine_manager.party.email) # If no core_recipients found yet if len(core_recipients) == 0: