/** * Setup rows of the shell suites, written as postgres in an account's * workspace of the isolated stack: the subject of a test goes through the * page, its surroundings through here. Values are fictitious literals; each * one is quoted as an SQL literal, never pasted raw. */ import { sql } from '../../../../packages/supabase/tests/sandbox/stack' import { workspaceOf, type SandboxAccount } from './sandbox' /** A text as an SQL literal ('' doubles a quote), or null. */ export const literal = (value: string | null) => value === null ? 'null' : `'${value.replaceAll("'", "''")}'` export interface ClientSeed { kind?: 'person' | 'organization' city?: string | null phone?: string | null email?: string | null addressLine1?: string | null } export async function seedClient( account: SandboxAccount, displayName: string, seed: ClientSeed = {}, ): Promise { const ws = await workspaceOf(account) return sql(`insert into public.clients (workspace_id, kind, display_name, city, phone, email, address_line1, create_request_id, create_request_digest) values (${literal(ws)}, ${literal(seed.kind ?? 'person')}, ${literal(displayName)}, ${literal(seed.city ?? null)}, ${literal(seed.phone ?? null)}, ${literal(seed.email ?? null)}, ${literal(seed.addressLine1 ?? null)}, gen_random_uuid(), '\\x00') returning id`) } export async function seedAnimal( account: SandboxAccount, name: string, speciesId = 'equine', sex: 'male' | 'female' | 'unknown' = 'female', ): Promise { const ws = await workspaceOf(account) return sql(`insert into public.animals (workspace_id, name, species_id, sex, create_request_id, create_request_digest) values (${literal(ws)}, ${literal(name)}, ${literal(speciesId)}, ${literal(sex)}, gen_random_uuid(), '\\x00') returning id`) } export async function seedRelation( account: SandboxAccount, animalId: string, clientId: string, role: 'owner' | 'keeper' | 'contact', startedOn: string | null = null, ): Promise { const ws = await workspaceOf(account) return sql(`insert into public.animal_client_relations (workspace_id, animal_id, client_id, relation_role, started_on, create_request_id, create_request_digest) values (${literal(ws)}, ${literal(animalId)}, ${literal(clientId)}, ${literal(role)}, ${literal(startedOn)}, gen_random_uuid(), '\\x00') returning id`) }