| 1 | import type { ServiceBinding } from "./clients"; |
| 2 | import type { User } from "./identity"; |
| 3 | import type { Result } from "./result"; |
| 4 | |
| 5 | export type ErasureStatus = { id: string; requested_at: string; due_at: string; status: "received" | "reviewing" | "completed" | "refused" }; |
| 6 | export type ErasureRequest = ErasureStatus & { |
| 7 | user_id: string; |
| 8 | username: string; |
| 9 | scope: string; |
| 10 | evidence: string; |
| 11 | updated_at: string; |
| 12 | updated_by: string | null; |
| 13 | closed_at: string | null; |
| 14 | reviews: ErasureReview[]; |
| 15 | }; |
| 16 | /** One entry in a case's trail: intake notes, a review, a refusal or the completion evidence. Staff only. */ |
| 17 | export type ErasureReview = { at: string; staff: string; status: string; evidence: string }; |
| 18 | export type PurgeJob = { subject_id: string; kind: string; target: string; attempts: number; retry_at: string; last_error: string | null; created_at: string }; |
| 19 | |
| 20 | export function privacyClient(service: ServiceBinding) { |
| 21 | async function rpc<T>(method: string, args: object = {}): Promise<T> { |
| 22 | const response = await service.fetch(`https://service/rpc/${method}`, { |
| 23 | method: "POST", |
| 24 | headers: { "content-type": "application/json" }, |
| 25 | body: JSON.stringify(args), |
| 26 | }); |
| 27 | if (!response.ok) throw new Error("Privacy service unavailable"); |
| 28 | return response.json() as Promise<T>; |
| 29 | } |
| 30 | return { |
| 31 | request: (user: User) => rpc<Result<ErasureStatus>>("request_erasure", { user }), |
| 32 | mine: (user: User) => rpc<ErasureStatus | null>("my_erasure", { user }), |
| 33 | intake: (username: string, received_at: string, staff: string, evidence: string) => |
| 34 | rpc<Result<boolean>>("admin_intake_erasure", { username, received_at, staff, evidence }), |
| 35 | requests: () => rpc<ErasureRequest[]>("admin_erasure_requests"), |
| 36 | pending: () => rpc<PurgeJob[]>("admin_pending_purges"), |
| 37 | review: (id: string, staff: string, status: string, evidence: string) => rpc<Result<boolean>>("admin_review_erasure", { id, staff, status, evidence }), |
| 38 | }; |
| 39 | } |