Aug 5 - Update code to follow up ST - MT14c

This commit is contained in:
2026-08-05 10:02:12 -04:00
parent be5484e1fb
commit 9bd6b364b7
6 changed files with 667 additions and 5 deletions
+32 -3
View File
@@ -595,6 +595,23 @@ def notify_by_matrix(
logger.info('MATRIX NOTIFY | event=%s | role=%s | users_found=%s',
event_type, role_key, [u.username for u in users])
# Scope the inspector role for "inspection_completed" to the inspection's
# OWN inspector — the person who did the work — not the whole inspector
# pool. Without this, switching the Inspector column on for this event
# notifies EVERY active inspector on EVERY submitted inspection, which on
# a tenant with a dozen inspectors is a mail storm and trains people to
# ignore notifications. Falls back to notifying nobody when the
# inspection cannot be resolved, rather than notifying everybody.
if role_key == 'inspector' and event_type == 'inspection_completed':
target_id = None
if inspection_id:
from app.models.inspection import Inspection
insp = db.session.get(Inspection, inspection_id)
target_id = insp.inspector_id if insp else None
users = [u for u in users if u.id == target_id] if target_id else []
logger.info('MATRIX NOTIFY | event=%s | role=inspector scoped to '
'submitting inspector_id=%s', event_type, target_id)
# Scope customer role to facility if provided
if role_key == 'customer' and facility_id:
from app.utils.notifications import notify_customers_for_facility
@@ -625,10 +642,22 @@ def notify_by_matrix(
)
notified.add(user.id)
# ── Custom email recipients ───────────────────────────────────────────
# ── Custom email recipients (global, per-event) ───────────────────────
# Deduplicated on the normalised address. The matrix stores this list as
# free text, so the same person can appear twice with different casing or
# stray whitespace ("Ops@x.com" and "ops@x.com "), which previously produced
# two identical emails. `sent_emails` is built AS WE SEND, so it reflects
# what actually went out — blank entries are skipped rather than being
# counted as sent — and is then handed to the per-contract pass below so a
# recipient listed both globally and on the contract is contacted once.
custom_emails = get_custom_emails_for(event_type)
sent_emails = set()
for email in custom_emails:
norm = (email or '').strip().lower()
if not norm or norm in sent_emails:
continue
_send_custom_email(email, title, body, link)
sent_emails.add(norm)
# ── Per-contract additional recipients (phase37) ──────────────────────
_notify_project_recipients(
@@ -641,12 +670,12 @@ def notify_by_matrix(
facility_id = facility_id,
exclude_user_ids = exclude,
already_notified = notified,
already_emailed = {e.strip().lower() for e in custom_emails},
already_emailed = sent_emails,
)
logger.info(
'MATRIX NOTIFY | event=%s | notified=%s | custom_emails=%s',
event_type, len(notified), len(custom_emails),
event_type, len(notified), len(sent_emails),
)