import { mutationSpec, parseWithAccount } from '../../lib/api.ts' import { granolaRequest } from '../../lib/client.ts' import { echo, mutationMode, preview } from '../../lib/guard.ts' import { custodianInputs } from '../../lib/holds.ts' import { HOLD_ID_PATTERN } from '../../lib/validate.ts' /** * Name people on a legal hold (`POST /v1/legal-holds/{hold_id}/custodians`) by workspace member * email or `usr_…` id (≤100). Idempotent; if any entry is not an active or deactivated member the * whole request is rejected (`CUSTODIAN_NOT_IN_WORKSPACE`, all failures listed) and nothing is added. * WRITE: previews unless `confirm: true`. * * @param input.account - Account whose secret holds a Legal hold API key. Required. @param input.secretName - Secret override. * @param input.holdId - `lgh_…` id. Required. * @param input.custodians - Member emails and/or user ids. Required. * @param input.dryRun - Preview only. @param input.confirm - `true` to add. * @returns Preview `{ dryRun: true, request }`, or `{ executed: true, custodians }` * * @example * import addCustodians from 'kody:@charlesmatte/granola/legal-holds/custodians/add' * * await addCustodians({ account: 'work-legal', holdId: 'lgh_Abc123XyZ456Dq', custodians: ['person@example.com'], confirm: true }) */ export default async function addGranolaLegalHoldCustodians(input: unknown) { const p = parseWithAccount<{ holdId: string; custodians: Array; dryRun?: boolean; confirm?: boolean }>( input, { holdId: { type: 'string', required: true, pattern: HOLD_ID_PATTERN }, custodians: { type: 'stringArray', required: true, maxItems: 100 }, ...mutationSpec }, 'legal-holds/custodians/add', ) if (p.custodians.length === 0) throw new Error('legal-holds/custodians/add: "custodians" needs at least one email or usr_… id.') const body = { custodians: custodianInputs(p.custodians) } const mode = mutationMode(p) const path = `/v1/legal-holds/${p.holdId}/custodians` const target = `legal hold ${p.holdId} + ${p.custodians.length} custodian(s)` if (!mode.execute) return preview(p.acct, 'legal-holds/custodians/add', target, mode, { method: 'POST', path, body }) const res = await granolaRequest<{ custodians?: unknown }>(p.acct, { method: 'POST', path, json: body, retries: 1 }) return { dryRun: false, executed: true, echo: echo(p.acct, 'legal-holds/custodians/add', target, mode), custodians: res.data?.custodians ?? res.data } }