@@ -267,6 +267,29 @@ def get_notifications_for_job(
267267 return pagination
268268
269269
270+ def get_recent_notifications_for_job (
271+ service_id , job_id , filter_dict = None , page = 1 , page_size = None
272+ ):
273+ if page_size is None :
274+ page_size = current_app .config ["PAGE_SIZE" ]
275+
276+ stmt = select (Notification ).where (
277+ Notification .service_id == service_id ,
278+ Notification .job_id == job_id ,
279+ )
280+
281+ stmt = _filter_query (stmt , filter_dict )
282+ stmt = stmt .order_by (desc (Notification .job_row_number ))
283+ results = db .session .execute (stmt ).scalars ().all ()
284+
285+ page_size = current_app .config ["PAGE_SIZE" ]
286+ offset = (page - 1 ) * page_size
287+ paginated_results = results [offset : offset + page_size ]
288+
289+ pagination = Pagination (paginated_results , page , page_size , len (results ))
290+ return pagination
291+
292+
270293def dao_get_notification_count_for_job_id (* , job_id ):
271294 stmt = select (func .count (Notification .id )).where (Notification .job_id == job_id )
272295 return db .session .execute (stmt ).scalar ()
@@ -279,6 +302,44 @@ def dao_get_notification_count_for_service(*, service_id):
279302 return db .session .execute (stmt ).scalar ()
280303
281304
305+ def dao_get_notification_count_for_service_message_ratio (service_id , current_year ):
306+ start_date = datetime (current_year , 1 , 1 )
307+ end_date = datetime (current_year + 1 , 1 , 1 )
308+ stmt1 = (
309+ select (func .count ())
310+ .select_from (Notification )
311+ .where (
312+ Notification .service_id == service_id ,
313+ Notification .status
314+ not in [
315+ NotificationStatus .CANCELLED ,
316+ NotificationStatus .CREATED ,
317+ NotificationStatus .SENDING ,
318+ ],
319+ Notification .created_at >= start_date ,
320+ Notification .created_at < end_date ,
321+ )
322+ )
323+ stmt2 = (
324+ select (func .count ())
325+ .select_from (NotificationHistory )
326+ .where (
327+ NotificationHistory .service_id == service_id ,
328+ NotificationHistory .status
329+ not in [
330+ NotificationStatus .CANCELLED ,
331+ NotificationStatus .CREATED ,
332+ NotificationStatus .SENDING ,
333+ ],
334+ NotificationHistory .created_at >= start_date ,
335+ NotificationHistory .created_at < end_date ,
336+ )
337+ )
338+ recent_count = db .session .execute (stmt1 ).scalar_one ()
339+ old_count = db .session .execute (stmt2 ).scalar_one ()
340+ return recent_count + old_count
341+
342+
282343def dao_get_failed_notification_count ():
283344 stmt = select (func .count (Notification .id )).where (
284345 Notification .status == NotificationStatus .FAILED
@@ -446,7 +507,7 @@ def insert_notification_history_delete_notifications(
446507 SELECT id, job_id, job_row_number, service_id, template_id, template_version, api_key_id,
447508 key_type, notification_type, created_at, sent_at, sent_by, updated_at, reference, billable_units,
448509 client_reference, international, phone_prefix, rate_multiplier, notification_status,
449- created_by_id, document_download_count
510+ created_by_id, document_download_count, message_cost
450511 FROM notifications
451512 WHERE service_id = :service_id
452513 AND notification_type = :notification_type
@@ -781,7 +842,6 @@ def dao_update_delivery_receipts(receipts, delivered):
781842 new_receipts .append (r )
782843
783844 receipts = new_receipts
784-
785845 id_to_carrier = {
786846 r ["notification.messageId" ]: r ["delivery.phoneCarrier" ] for r in receipts
787847 }
@@ -790,9 +850,13 @@ def dao_update_delivery_receipts(receipts, delivered):
790850 }
791851 id_to_timestamp = {r ["notification.messageId" ]: r ["@timestamp" ] for r in receipts }
792852
853+ id_to_message_cost = {
854+ r ["notification.messageId" ]: r ["delivery.priceInUSD" ] for r in receipts
855+ }
793856 status_to_update_with = NotificationStatus .DELIVERED
794857 if not delivered :
795858 status_to_update_with = NotificationStatus .FAILED
859+
796860 stmt = (
797861 update (Notification )
798862 .where (Notification .message_id .in_ (id_to_carrier .keys ()))
@@ -816,6 +880,12 @@ def dao_update_delivery_receipts(receipts, delivered):
816880 for key , value in id_to_provider_response .items ()
817881 ]
818882 ),
883+ message_cost = case (
884+ * [
885+ (Notification .message_id == key , value )
886+ for key , value in id_to_message_cost .items ()
887+ ]
888+ ),
819889 )
820890 )
821891 db .session .execute (stmt )
@@ -847,7 +917,6 @@ def dao_close_out_delivery_receipts():
847917
848918
849919def dao_batch_insert_notifications (batch ):
850-
851920 db .session .bulk_save_objects (batch )
852921 db .session .commit ()
853922 current_app .logger .info (f"Batch inserted notifications: { len (batch )} " )
0 commit comments