Skip to content

The open-source AI receptionist for businesses. Answers every call, books the appointment and hands over to a person when needed. You own the code and the data.

1.4 KB
Raw Blame
// Who hears of a change (LIN-900): not the admin who made it in their own session, as long as another admin does.
// Its own file without the database, so the rule is tested on its own (admin-alert-actor.test.ts).
import type { AlertActor } from './emails/alerts.ts'

/** The admins to tell, without the one who made the change, unless nobody else would hear of it (alertInstanceAdmins). */
export function withoutActor(admins: string[], actor: AlertActor | undefined): string[] {
  const own = actor && !actor.operator ? actor.email?.toLowerCase() : undefined
  if (!own) return admins
  const others = admins.filter(a => a.toLowerCase() !== own)
  return others.length ? others : admins
}

/**
 * The actor to leave out of an alert (alertInstanceAdmins `actor`): only a person signed in to their own session, with
 * no key involved (an allowlist: a new kind of actor alerts everyone until it is added here). A key (API, personal, an
 * agent's setup key) or an impersonation acts in their name without being them, so they hear of it too.
 */
export function ownChange(
  actor: {
    sessionId?: string
    setupKeyId?: string
    apiKeyId?: string
    personalKeyId?: string
    impersonation?: unknown
  },
  who: AlertActor,
): AlertActor | undefined {
  const ownSession = Boolean(actor.sessionId) && !actor.setupKeyId && !actor.apiKeyId && !actor.personalKeyId
  return ownSession && !actor.impersonation ? who : undefined
}