feat(repairs): automatic repair status emails

This commit is contained in:
Schubert Ferenc 2026-07-04 22:58:47 +02:00
parent 3ed6596bf0
commit c58e212e3a
21 changed files with 525 additions and 36 deletions

View file

@ -301,7 +301,9 @@ class RepairRepository:
channel: str,
recipient: str,
subject: str,
template: str,
status: str,
success: bool,
error_message: str | None = None,
sent_at: datetime | None = None,
) -> RepairNotificationEvent:
@ -311,7 +313,9 @@ class RepairRepository:
channel=channel,
recipient=recipient,
subject=subject,
template=template,
status=status,
success=success,
error_message=error_message,
sent_at=sent_at,
)
@ -320,6 +324,33 @@ class RepairRepository:
db.refresh(event)
return event
@staticmethod
def count_status_mails_sent_today(db: Session) -> int:
today = datetime.now(UTC).date()
return db.scalar(
select(func.count(RepairNotificationEvent.id))
.where(RepairNotificationEvent.event_type == "repair_status_mail")
.where(RepairNotificationEvent.success.is_(True))
.where(func.date(RepairNotificationEvent.created_at) == today)
) or 0
@staticmethod
def count_failed_status_mails(db: Session) -> int:
return db.scalar(
select(func.count(RepairNotificationEvent.id))
.where(RepairNotificationEvent.event_type == "repair_status_mail")
.where(RepairNotificationEvent.success.is_(False))
.where(RepairNotificationEvent.status.in_(["failed", "skipped"]))
) or 0
@staticmethod
def count_open_repairs_without_customer_email(db: Session) -> int:
return db.scalar(
select(func.count(Repair.id))
.where(~Repair.status.in_(["completed", "cancelled"]))
.where((Repair.customer_email == "") | Repair.customer_email.is_(None))
) or 0
@staticmethod
def _payload_data(payload: RepairCreate | RepairUpdate) -> dict:
data = payload.model_dump()