HDB Service uses a single consolidated cron endpoint —Documentation Index
Fetch the complete documentation index at: https://mintlify.com/GianlucaBessone/HDB-Service/llms.txt
Use this file to discover all available pages before exploring further.
/api/cron — to run all scheduled background work. Rather than maintaining multiple independent cron routes, the endpoint executes three tasks sequentially every day: it closes stale maintenance schedules from prior months, generates fresh schedules for active dispensers, and checks every open ticket for SLA breaches and near-breach conditions. Each task is wrapped in its own try/catch so that a failure in one does not block the others.
Vercel Cron Configuration
The cron schedule is declared invercel.json at the repository root:
GET /api/cron automatically every day at 2:00 AM UTC. Before executing any task, the endpoint validates the incoming Authorization header:
Authorization: Bearer <CRON_SECRET> receives a 401 Unauthorized response and no tasks are executed. See Environment Variables for details on setting CRON_SECRET.
Task A: Monthly Closure
The first task to run marks stale maintenance schedules asEXPIRED.
Any MaintenanceSchedule record whose scheduledMonth value is earlier than the current calendar month (using YYYY-MM string comparison) and whose status is still PENDING or OVERDUE is bulk-updated to EXPIRED. This prevents old unresolved schedules from polluting active reporting and dashboards.
The task returns expiredCount — the number of records updated.
Task B: Maintenance Schedule Generation
The second task keeps the maintenance schedule table current for all active dispensers. It operates in two steps:-
Mark overdue schedules — any
PENDINGschedule whosescheduledMonthis earlier than the current month is transitioned toOVERDUE. This catches dispensers that were missed before the monthly closure window. -
Upsert schedules for this month — the task fetches every
Dispenserthat is bothIN_SERVICEandactive: true, then callsupsertonMaintenanceScheduleusing the composite unique key(dispenserId, scheduledMonth). If a schedule for the current month already exists it is left unchanged; otherwise a newPENDINGschedule is created.
| Field | Meaning |
|---|---|
overdueCount | Schedules transitioned to OVERDUE |
processedCount | Total active IN_SERVICE dispensers evaluated |
createdCount | New PENDING schedules inserted this run |
Task C: SLA Breach and Near-Breach Checking
The third task audits every open ticket for SLA compliance and dispatches notifications when deadlines are breached or approaching. The logic runs as follows:-
Fetch active tickets — all tickets with
statusOPENorIN_PROGRESSare loaded, including their client’sSlaConfig(which holds per-priority deadline minutes and thenearBreachPercentthreshold). -
Deduplicate near-breach notifications — before iterating, existing
Notificationrecords of typeSLA_NEAR_BREACHare loaded for all active ticket IDs. This set is used to avoid sending duplicate near-breach alerts for the same ticket across multiple cron runs. -
Per-ticket evaluation — for each ticket:
- If
statusisOPENandslaResponseDeadlinehas passed (andslaResponseBreachedis not yet set),slaResponseBreachedis flagged for update. - If
slaResolutionDeadlinehas passed (andslaResolutionBreachedis not yet set),slaResolutionBreachedis flagged and anSLA_BREACHEDnotification is queued. - If the resolution deadline has not passed but
getSlaStatus()returnsNEAR_BREACHand no prior near-breach notification exists for that ticket, anSLA_NEAR_BREACHnotification is queued.
- If
-
Batch updates — all flag changes are executed in a single
prisma.$transaction. -
Push + in-app notifications — if any notifications were queued, all active
ADMINandSUPERVISORusers are looked up. TheironesignalPlayerIdvalues are collected and a single push is sent viasendPushNotification. In-appNotificationrows are created for every (user, notification) pair usingcreateMany.
| Field | Meaning |
|---|---|
checkedCount | Total open tickets evaluated |
updatedCount | Tickets whose SLA breach flags were updated |
notificationCount | Notifications queued (push + in-app) |
Response Format
A successful cron run returns a JSON object that consolidates the outcome of all three tasks:results will contain "success": false and an "error" field with the error message, while the other tasks continue to run and report their own outcomes.
Each sub-task catches its own errors independently, so a failure in one task does not prevent the others from running. Always inspect each key in
results individually — a top-level "success": true only means the HTTP handler itself did not crash.Manual Trigger
You can invoke the cron endpoint directly to test it or to run tasks outside of the scheduled window:your-cron-secret with the value of the CRON_SECRET environment variable. The endpoint is idempotent — running it multiple times in the same month is safe because the maintenance schedule step uses upsert and the near-breach notification step checks for existing records before creating duplicates.