import {z} from 'zod' export type UseFrenchSchoolAction = {action: 'use_french_school', french_school_id: string}; export type CreateFrenchSchoolAction = {action: 'create_french_school', name: string, city: string, uai?: (string | null) | undefined, kind?: ('france' | 'abroad') | undefined, country?: (string | null) | undefined, create_b2b_school?: boolean | undefined, email_domain?: (string | null) | undefined}; export type CreateB2BSchoolAction = {action: 'create_b2b_school', name?: (string | null) | undefined, email_domain?: (string | null) | undefined, contact_email?: (string | null) | undefined, contact_name?: (string | null) | undefined}; export type AttachToSchoolAction = {action: 'attach_to_school', school_id: string}; export type IgnoreAction = {action: 'ignore'}; export type CreateApiAdminTeacherApprovalsTeacherIdAttachArgs = {teacher_id: string, body: UseFrenchSchoolAction | CreateFrenchSchoolAction | CreateB2BSchoolAction | AttachToSchoolAction | IgnoreAction}; export type CreateApiAdminTeacherApprovalsTeacherIdAttachOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const schoolSummary = z.object({id: z.string(), name: z.string()}); export const frenchSchoolSummary = z.object({id: z.string(), uai: z.string(), name: z.string(), city: z.string(), postal_code: z.string(), kind: z.enum(["france", "abroad"]), country: z.string()}); export const attachResponse = z.object({status: z.enum(["attached", "no_b2b", "pending_review", "ignored", "none"]), school: schoolSummary.nullable().optional(), french_school: frenchSchoolSummary.nullable().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Attach Teacher * * POST /admin/teacher-approvals/{teacher_id}/attach */ export const createApiAdminTeacherApprovalsTeacherIdAttach = async (args: CreateApiAdminTeacherApprovalsTeacherIdAttachArgs, options: CreateApiAdminTeacherApprovalsTeacherIdAttachOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/admin/teacher-approvals/${encodeURIComponent(String(args.teacher_id))}/attach${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(attachResponse, await res.json()) };