|
27 | 27 | from app.dao.dao_utils import autocommit |
28 | 28 | from app.dao.inbound_sms_dao import Pagination |
29 | 29 | from app.enums import KeyType, NotificationStatus, NotificationType |
30 | | -from app.models import FactNotificationStatus, Notification, NotificationHistory |
| 30 | +from app.models import ( |
| 31 | + FactNotificationStatus, |
| 32 | + Notification, |
| 33 | + NotificationHistory, |
| 34 | + Template, |
| 35 | +) |
31 | 36 | from app.utils import ( |
32 | 37 | escape_special_characters, |
33 | 38 | get_midnight_in_utc, |
@@ -340,6 +345,86 @@ def dao_get_notification_count_for_service_message_ratio(service_id, current_yea |
340 | 345 | return recent_count + old_count |
341 | 346 |
|
342 | 347 |
|
| 348 | +def dao_get_notification_counts_per_service(service_ids, current_year): |
| 349 | + """ |
| 350 | + Get notification counts for multiple services in a single organization. |
| 351 | + """ |
| 352 | + if not service_ids: |
| 353 | + return {} |
| 354 | + |
| 355 | + start_date = datetime(current_year, 6, 16) |
| 356 | + end_date = datetime(current_year + 1, 6, 16) |
| 357 | + |
| 358 | + stmt1 = ( |
| 359 | + select(Notification.service_id, func.count().label("count")) |
| 360 | + .where( |
| 361 | + Notification.service_id.in_(service_ids), |
| 362 | + Notification.status |
| 363 | + not in [ |
| 364 | + NotificationStatus.CANCELLED, |
| 365 | + NotificationStatus.CREATED, |
| 366 | + NotificationStatus.SENDING, |
| 367 | + ], |
| 368 | + Notification.created_at >= start_date, |
| 369 | + Notification.created_at < end_date, |
| 370 | + ) |
| 371 | + .group_by(Notification.service_id) |
| 372 | + ) |
| 373 | + |
| 374 | + stmt2 = ( |
| 375 | + select(NotificationHistory.service_id, func.count().label("count")) |
| 376 | + .where( |
| 377 | + NotificationHistory.service_id.in_(service_ids), |
| 378 | + NotificationHistory.status |
| 379 | + not in [ |
| 380 | + NotificationStatus.CANCELLED, |
| 381 | + NotificationStatus.CREATED, |
| 382 | + NotificationStatus.SENDING, |
| 383 | + ], |
| 384 | + NotificationHistory.created_at >= start_date, |
| 385 | + NotificationHistory.created_at < end_date, |
| 386 | + ) |
| 387 | + .group_by(NotificationHistory.service_id) |
| 388 | + ) |
| 389 | + |
| 390 | + result_dict = {} |
| 391 | + |
| 392 | + recent_results = db.session.execute(stmt1).all() |
| 393 | + for service_id, count in recent_results: |
| 394 | + result_dict[service_id] = count |
| 395 | + |
| 396 | + history_results = db.session.execute(stmt2).all() |
| 397 | + for service_id, count in history_results: |
| 398 | + result_dict[service_id] = result_dict.get(service_id, 0) + count |
| 399 | + |
| 400 | + return result_dict |
| 401 | + |
| 402 | + |
| 403 | +def dao_get_recent_sms_template_per_service(service_ids): |
| 404 | + |
| 405 | + if not service_ids: |
| 406 | + return {} |
| 407 | + |
| 408 | + stmt = ( |
| 409 | + select( |
| 410 | + Notification.service_id, |
| 411 | + Template.name.label("template_name"), |
| 412 | + ) |
| 413 | + .join(Template, Template.id == Notification.template_id) |
| 414 | + .where( |
| 415 | + Notification.service_id.in_(service_ids), |
| 416 | + Notification.notification_type == NotificationType.SMS, |
| 417 | + Notification.key_type != KeyType.TEST, |
| 418 | + ) |
| 419 | + .distinct(Notification.service_id) |
| 420 | + .order_by(Notification.service_id, desc(Notification.created_at)) |
| 421 | + ) |
| 422 | + |
| 423 | + results = db.session.execute(stmt).all() |
| 424 | + |
| 425 | + return {service_id: template_name for service_id, template_name in results} |
| 426 | + |
| 427 | + |
343 | 428 | def dao_get_failed_notification_count(): |
344 | 429 | stmt = select(func.count(Notification.id)).where( |
345 | 430 | Notification.status == NotificationStatus.FAILED |
|
0 commit comments