Files
DEMO-AGENT/review_agent/application_form_fill/services/notifier.py

56 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from __future__ import annotations
from django.utils import timezone
from review_agent.models import (
ApplicationFormFillBatch,
ApplicationFormFillNotificationRecord,
ExportedSummaryFile,
)
from review_agent.notifications.dispatcher import dispatch_workflow_notification
from review_agent.notifications.workflow_adapters import build_application_form_fill_context
def notify_completion(
batch: ApplicationFormFillBatch,
exports: list[ExportedSummaryFile],
*,
fail: bool = False,
) -> ApplicationFormFillNotificationRecord:
export_ids = [export.pk for export in exports]
message_summary = (
f"自动填表批次 {batch.batch_no} 已完成,"
f"模板 {', '.join(batch.selected_templates or []) or '未识别'}"
f"冲突字段 {len(batch.conflict_summary or [])} 个。"
)
if fail:
return ApplicationFormFillNotificationRecord.objects.create(
batch=batch,
recipient=batch.user,
channel=ApplicationFormFillNotificationRecord.Channel.MOCK,
template_codes=batch.selected_templates,
export_ids=export_ids,
message_summary=message_summary,
send_status=ApplicationFormFillNotificationRecord.SendStatus.FAILED,
retry_count=1,
error_message="mock notification failed",
)
unified_error = ""
try:
unified_record = dispatch_workflow_notification(build_application_form_fill_context(batch))
if unified_record.send_status == unified_record.SendStatus.FAILED:
unified_error = unified_record.error_message
except Exception as exc:
unified_error = str(exc)
return ApplicationFormFillNotificationRecord.objects.create(
batch=batch,
recipient=batch.user,
channel=ApplicationFormFillNotificationRecord.Channel.MOCK,
template_codes=batch.selected_templates,
export_ids=export_ids,
message_summary=message_summary,
send_status=ApplicationFormFillNotificationRecord.SendStatus.SUCCESS,
error_message=unified_error,
sent_at=timezone.now(),
)