import {z} from 'zod' export type CreateApiChecklistsCheckListResponseArgs = {version?: ('V1' | 'V2') | undefined, body: BodyInit}; export type CreateApiChecklistsCheckListResponseOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const serviceChannelServicesWebApiModelsResponseChecklistsChecklistResponsesChecklistQuestionOptionResponse = z.object({Id: z.number().int().optional(), Answer: z.string().optional(), Sequence: z.number().int().optional(), IsConditionalFollowUp: z.boolean().optional(), ResponseResultInFailure: z.boolean().optional()}); export const serviceChannelServicesWebApiModelsResponseChecklistsChecklistResponsesChecklistResponse = z.object({Id: z.number().int().optional(), Answer: z.string().optional(), FreeTextAnswer: z.string().optional(), EntityId: z.number().int().optional(), EntityTypeId: z.number().int().optional(), Attempt: z.number().int().optional(), ProviderId: z.number().int().optional(), TechnicianId: z.number().int().optional(), UpdatedDate: z.string().optional(), UpdatedDateStr: z.string().optional(), UpdatedDateDto: z.string().optional(), CheckListQuestionId: z.number().int().optional(), QuestionOptions: z.array(serviceChannelServicesWebApiModelsResponseChecklistsChecklistResponsesChecklistQuestionOptionResponse).optional(), CheckListQuestionOptionId: z.number().int().optional(), AssetId: z.number().int().optional(), IsPassed: z.boolean().optional(), LinkedWorkOrderId: z.number().int().optional()}); export const serviceChannelServicesWebApiModelsResponseChecklistsChecklistResponsesChecklistPictureResponse = z.object({Id: z.number().int().optional(), CheckListResponseId: z.number().int().optional(), DocumentName: z.string().optional(), DocumentPath: z.string().optional(), ThumbnailPath: z.string().optional(), HasThumbnail: z.boolean().optional(), UpdatedBy: z.number().int().optional()}); export const serviceChannelServicesWebApiModelsResponseChecklistsChecklistResponsesCreateChecklistResponseWithPicture = z.object({CheckListResponse: serviceChannelServicesWebApiModelsResponseChecklistsChecklistResponsesChecklistResponse.optional(), CheckListResponsePicture: serviceChannelServicesWebApiModelsResponseChecklistsChecklistResponsesChecklistPictureResponse.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 }; /** * Submit a response to a checklist question * * POST /checklists/CheckListResponse */ export const createApiChecklistsCheckListResponse = async (args: CreateApiChecklistsCheckListResponseArgs, options?: CreateApiChecklistsCheckListResponseOptions) => { const {baseUrl = "https://sb2api.servicechannel.com/v3", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.version != null) query.append("version", String(args.version)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/checklists/CheckListResponse${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: args.body}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(serviceChannelServicesWebApiModelsResponseChecklistsChecklistResponsesCreateChecklistResponseWithPicture, await res.json()) };