Service levels
First response, next response and resolution clocks on any record, from a policy per record type and priority, in business hours, paused while the record waits, with warnings and breaches a job records.
sla puts service-level clocks on any record in config.recordTypes: inbox conversations, tickets, incidents, work items. A policy per record type and priority says how many minutes a first response, a next response and the resolution may take, and whether only business hours count. A record's clocks start when it is created, pause while it waits on the customer, close when it is answered or resolved, and a job records the ones that warn or breach.
Reading policies and clocks needs sla.read, and changing policies needs sla.manage. Admins get both, and members get sla.read.
better-supabase add slaThe module needs tenant, permissions and subjects.
blocks: {
sla: {
on: {
ticket: { status: "status", priority: "priority", pause: ["waiting"], resolve: ["solved", "closed"] },
},
options: {
businessHours: { timeZone: "Europe/Amsterdam", start: "09:00", end: "17:00", days: [1, 2, 3, 4, 5] },
},
},
},Policies
Server code calls the sla instance on db, in the caller's organization. With gen.hooks set, better-supabase gen writes the hooks:
export const { useList: useSla } = defineDataHooks(
"sla",
(db: AppDb) => db.sla,
);import { createSla } from "better-supabase/blocks/sla";
await db.sla
.savePolicy({
subjectType: "ticket",
name: "Normal",
firstResponseMinutes: 240,
nextResponseMinutes: 120,
resolutionMinutes: 2880,
businessHours: true,
})
.orThrow();
await db.sla
.savePolicy({
subjectType: "ticket",
name: "Urgent",
priority: "urgent",
firstResponseMinutes: 30,
resolutionMinutes: 240,
warnAtPercent: 50,
})
.orThrow();"use client";
import { useSla } from "@/lib/supabase/hooks";
const rows = useSla({ limit: 50 });savePolicy acts in the block's tenant, an organization id or a function that returns the current one (createBlocks and createBlockClient pass the request's tenant); a call without one fails with invalid_request and the hint TENANT_REQUIRED. A record uses the policy for its type and priority, or the policy without a priority. One active policy covers each type and priority (SLA_POLICY_TAKEN). warnAtPercent (default 80) is when a clock warns. With businessHours, only the hours of options.businessHours count, in its time zone (UTC, 09:00 to 17:00, Monday to Friday by default); dueAt(start, minutes, businessHours) shows the result.
Clocks
Bind a record type in on and the module drives its clocks from the record's own columns:
| Change | Clocks |
|---|---|
| a record is created | first response and resolution start |
| its priority changes | the open clocks take the new policy's minutes from their start |
its status enters a pause status | every open clock pauses |
its status leaves a pause status | they resume, and the due times move by the pause |
its status enters a resolve status | every open clock is met |
its status leaves a resolve status | a new resolution clock starts |
A reply is an event only the app knows about: call record(subject, "responded") when an agent answers. It meets the first response, and with a next-response time and the record still open, it starts a next-response clock. Without a binding, call start(subject, priority) and record(subject, event) with resolved, reopened, paused and resumed yourself. The subject is a RecordRef, such as { type: "ticket", id: ticketId }; these calls take no tenant, since it comes from the record. Each clock carries its record as subject.
clocks(subject) returns a record's clocks with their state (running, paused, met, breached) and the seconds left, for a ticket's timer.
Warnings and breaches
Run check() every minute from a job with the service role. It records sla.warned once a running clock passes its warning share and sla.breached once it passes its due time, and, with the notifications module installed, notifies the record's watchers at high priority. Escalate from those events with a workflow, such as reassigning a breached ticket. A clock met after its due time records sla.completed with late: true.
Escalation ladder
A policy can carry a ladder of steps. Each step fires once per clock when the stated share of the clock's time has passed, and check() evaluates the ladder with the warnings and breaches. Shares above 100 fire after the breach.
await sla.savePolicy({
subjectType: "ticket",
name: "Default",
resolutionMinutes: 480,
escalations: [
{ at: 50, action: "notify" },
{ at: 100, action: "reassign", target: managerId },
{ at: 150, action: "page" },
],
});notify notifies the record's watchers, reassign writes target into the owner column of the record, and page records the event at urgent priority for a sink to page someone. The owner column comes from the type's binding: on: { ticket: { status: "status", owner: "owner_id" } }. Without owner, a reassign step is recorded with the result skipped. Every fired step is a row in sla_escalations and an sla.escalated event, and a step never fires twice for the same clock. A malformed ladder fails with SLA_ESCALATION_INVALID.
Events
| Event | Data |
|---|---|
sla.warned, sla.breached, sla.completed | organizationId, clockId, subjectType, subjectId, metric, dueAt, late |
sla.escalated | the clock fields and atPercent, action, target, result (sent, done, unchanged, skipped) |
Wire it to ReUI
The ReUI Support Escalations board shows each ticket's SLA timer and turns it to "Paused" while it waits on the customer: read clocks(ticket) and show the open clock's secondsLeft, or "Paused" when its state is paused.
Errors
| Hint | Cause |
|---|---|
SLA_FORBIDDEN | the caller lacks sla.manage, or can't read the record |
SLA_POLICY_TAKEN | another active policy covers the type and priority |
SLA_POLICY_INVALID | a policy needs a name, a record type and positive minutes |
SLA_POLICY_NOT_FOUND | no such policy |
SLA_SUBJECT_INVALID | the record isn't in config.recordTypes |
SLA_EVENT_INVALID | not one of the events |
SLA_HOURS_INVALID | the business hours never open |
SLA_ESCALATION_INVALID | a ladder step needs at from 1 to 500, an action, and a target for reassign |
Your own columns
Add columns to the policy table with alter table ... add column in your own schema files
(column name to SQL type), then pass a Standard Schema for them as fields.
savePolicy validates and writes them, and savePolicy return them parsed under
fields. The block also takes hooks around its methods; see
extending blocks.
import { createSla } from "better-supabase/blocks/sla";
import { z } from "zod";
const client = createSla({
transport,
fields: z.object({ priority: z.enum(["low", "high"]) }),
});Without the client
db.sla is this client. Code that does not go through bsb.db() builds it with a transport and an organization id:
import { createSla } from "better-supabase/blocks/sla";
const sla = createSla({ transport, tenant: organizationId });
await sla
.savePolicy({
subjectType: "ticket",
name: "Normal",
firstResponseMinutes: 240,
nextResponseMinutes: 120,
resolutionMinutes: 2880,
businessHours: true,
})
.orThrow();
await sla
.savePolicy({
subjectType: "ticket",
name: "Urgent",
priority: "urgent",
firstResponseMinutes: 30,
resolutionMinutes: 240,
warnAtPercent: 50,
})
.orThrow();Last updated on
Tasks
Issues and tasks on pipelines, with identifiers, parents without cycles, several assignees, priority 0 to 4, estimates, start, end and due dates, locking, projects, iterations, batched edits and a progress tree.
Deadlines
Due dates computed from approved, versioned rule sets of groups, conditions, exceptions and counts, with results that name the facts still missing instead of guessing.