|
14 | 14 |
|
15 | 15 | logger = logging.getLogger(__name__) |
16 | 16 |
|
| 17 | +DEFAULT_ALERT_PER_DAY = 100 |
| 18 | + |
17 | 19 |
|
18 | 20 | def send_alert_email_notification( |
19 | 21 | load_item: LoadItem, |
@@ -56,26 +58,33 @@ def send_alert_email_notification( |
56 | 58 | html=email_body, |
57 | 59 | mailtype=email_type, |
58 | 60 | ) |
59 | | - |
| 61 | + # Create thread for initial emails |
60 | 62 | email_log.status = AlertEmailLog.Status.SENT |
61 | 63 | email_log.email_sent_at = timezone.now() |
62 | | - email_log.save(update_fields=["status", "email_sent_at"]) |
63 | 64 |
|
64 | | - # Create thread for initial emails |
65 | 65 | if not is_reply: |
66 | | - thread = AlertEmailThread.objects.create( |
| 66 | + thread, created = AlertEmailThread.objects.get_or_create( |
67 | 67 | user=user, |
68 | 68 | parent_event_id=load_item.parent_event_id, |
69 | | - root_email_message_id=message_id, |
70 | | - root_message_sent_at=timezone.now(), |
| 69 | + defaults={ |
| 70 | + "root_email_message_id": message_id, |
| 71 | + "root_message_sent_at": timezone.now(), |
| 72 | + }, |
71 | 73 | ) |
72 | 74 | email_log.thread = thread |
73 | | - email_log.save(update_fields=["thread"]) |
74 | | - logger.info( |
75 | | - f"Alert Email thread created for user [{user.get_full_name()}] " |
76 | | - f"with parent event [{load_item.parent_event_id}]" |
77 | | - ) |
78 | | - |
| 75 | + email_log.save(update_fields=["status", "email_sent_at", "thread"]) |
| 76 | + |
| 77 | + if created: |
| 78 | + logger.info( |
| 79 | + f"Alert Email thread created for user [{user.get_full_name()}] " |
| 80 | + f"with parent event [{load_item.parent_event_id}]" |
| 81 | + ) |
| 82 | + else: |
| 83 | + logger.info( |
| 84 | + f"Existing thread found for user [{user.get_full_name()}] " f"with parent event [{load_item.parent_event_id}]" |
| 85 | + ) |
| 86 | + else: |
| 87 | + email_log.save(update_fields=["status", "email_sent_at"]) |
79 | 88 | logger.info(f"Alert email sent to [{user.get_full_name()}] for LoadItem ID [{load_item.id}]") |
80 | 89 |
|
81 | 90 | except Exception: |
@@ -141,15 +150,16 @@ def process_email_alert(load_item_id: int) -> None: |
141 | 150 | thread = existing_threads.get(user_id) |
142 | 151 | is_reply: bool = thread is not None |
143 | 152 |
|
| 153 | + # Skip duplicate emails for same item |
| 154 | + if (user_id, subscription_id) in already_sent: |
| 155 | + logger.info(f"Duplicate alert skipped for user [{user.get_full_name()}] " f"with LoadItem ID [{load_item_id}]") |
| 156 | + continue |
| 157 | + |
144 | 158 | # Skip if daily alert limit reached |
145 | 159 | sent_today: int = daily_count_map.get((user_id, subscription_id), 0) |
146 | | - if subscription.alert_per_day and sent_today >= subscription.alert_per_day: |
| 160 | + effective_limit = subscription.alert_per_day or DEFAULT_ALERT_PER_DAY |
| 161 | + if sent_today >= effective_limit: |
147 | 162 | logger.info(f"Daily alert limit reached for user [{user.get_full_name()}]") |
148 | 163 | continue |
149 | 164 |
|
150 | | - # Skip duplicate emails for same item |
151 | | - if (user_id, subscription_id) in already_sent: |
152 | | - logger.info(f"Duplicate alert skipped for user [{user.get_full_name()}] " f"with LoadItem ID [{subscription_id}]") |
153 | | - continue |
154 | | - |
155 | 165 | send_alert_email_notification(load_item=load_item, user=user, subscription=subscription, thread=thread, is_reply=is_reply) |
0 commit comments