import {z} from 'zod' export type CreateNoteLinkDto = {entityType: 'candidate' | 'vacancy' | 'client' | 'application' | 'clientContact' | 'interview' | 'opportunity', entityId: string}; export type CreateNoteDto = {title: string | null, content: string, source?: ('phone' | 'phoneInbound' | 'phoneOutbound' | 'inPerson' | 'onlineMeeting' | 'callAttempted') | undefined, links?: Array | undefined, meetingId?: string | undefined, labelIds?: Array | undefined}; export type CreateApiNotesArgs = {body: CreateNoteDto}; export type CreateApiNotesOptions = {baseUrl: string, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const dateISO = z.string(); export const userBadgeDto = z.object({id: z.string(), name: z.string(), avatarUrl: z.string().nullable(), deactivatedAt: dateISO.nullable()}); export const noteEditorDto = z.object({editedBy: userBadgeDto, editedAt: dateISO}); export const clientBadgeDto = z.object({id: z.string(), name: z.string(), logoUrl: z.string().nullable()}); export const companyBadgeDto = z.object({id: z.string(), name: z.string(), logoUrl: z.string().nullable()}); export const noteLinkDto = z.union([z.object({id: z.string(), noteId: z.string(), entityType: z.literal("candidate"), candidate: z.object({id: z.string(), firstName: z.string(), lastName: z.string(), middleName: z.string().nullable(), secondLastName: z.string().nullable(), avatarUrl: z.string().nullable()})}), z.object({id: z.string(), noteId: z.string(), entityType: z.literal("vacancy"), vacancy: z.object({id: z.string(), name: z.string()}), client: clientBadgeDto}), z.object({id: z.string(), noteId: z.string(), entityType: z.literal("client"), client: clientBadgeDto}), z.object({id: z.string(), noteId: z.string(), entityType: z.literal("application"), applicationId: z.string()}), z.object({id: z.string(), noteId: z.string(), entityType: z.literal("clientContact"), clientContact: z.object({id: z.string(), firstName: z.string(), lastName: z.string(), middleName: z.string().nullable(), secondLastName: z.string().nullable(), avatarUrl: z.string().nullable(), candidateId: z.string(), companyId: z.string(), companyName: z.string(), companyLogoUrl: z.string().nullable()})}), z.object({id: z.string(), noteId: z.string(), entityType: z.literal("interview"), interviewId: z.string()}), z.object({id: z.string(), noteId: z.string(), entityType: z.literal("opportunity"), opportunity: z.object({id: z.string(), name: z.string(), company: companyBadgeDto})})]); export const meetingProvider = z.enum(["googleMeet", "zoom", "microsoftTeams", "audioFile", "null"]).nullable(); export const noteLinkedMeetingDto = z.object({id: z.string(), meetingProvider: meetingProvider.nullable(), title: z.string(), startTime: dateISO, endTime: dateISO, hasTranscript: z.boolean()}); export const telecomProvider = z.enum(["TELNYX", "AIRCALL", "RINGCENTRAL", "RINGOVER", "VOIPPLAY", "DIALPAD"]); export const noteLinkedCallDto = z.object({id: z.string(), provider: telecomProvider, startTime: dateISO, endTime: dateISO, hasTranscript: z.boolean()}); export const noteLinkedAudioRecordingDto = z.object({id: z.string(), startTime: dateISO, endTime: dateISO, hasTranscript: z.boolean()}); export const noteDtoSource = z.enum(["phone", "phoneInbound", "phoneOutbound", "inPerson", "onlineMeeting", "callAttempted", "null"]).nullable(); export const noteDto = z.object({id: z.string(), title: z.string().nullable(), content: z.string(), createdAt: dateISO, lastEditedAt: dateISO, createdBy: userBadgeDto.nullable(), editors: z.array(noteEditorDto), links: z.array(noteLinkDto), meeting: noteLinkedMeetingDto.nullable(), call: noteLinkedCallDto.nullable(), audioRecording: noteLinkedAudioRecordingDto.nullable(), type: z.enum(["manual", "aiSummary"]), source: noteDtoSource, pinned: z.boolean(), shouldPoll: z.boolean(), labels: z.array(z.object({id: z.string(), name: z.string(), colorHex: z.string()}))}); 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 }; /** * Create a note * * POST /notes */ export const createApiNotes = async (args: CreateApiNotesArgs, options: CreateApiNotesOptions) => { const {baseUrl, fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/notes${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(noteDto, await res.json()) };