import {z} from 'zod' export type ServiceChannelServicesMessagingSiteAuditIssueAttachmentExternal = {Id?: number | undefined, RemoteName?: string | undefined, RemoteNameResized?: string | undefined, FileName?: string | undefined, ContentUrl?: string | undefined, ContentUrlResized?: string | undefined, ContentType?: string | undefined}; export type ServiceChannelServicesMessagingSiteAuditAuditIssueExternal = {Id?: number | undefined, Description?: string | undefined, Date?: string | undefined, Attachments?: Array | undefined, Persistent?: boolean | undefined, UserName?: string | undefined}; export type ServiceChannelServicesMessagingSiteAuditQuantityExternal = {Value?: number | undefined, Type?: ('Dollar' | 'Quantity') | undefined, Unit?: string | undefined}; export type ServiceChannelServicesMessagingSiteAuditCheckListItemRequest = {Name?: string | undefined, Description?: string | undefined, Score?: number | undefined, System?: ('PassFail' | 'Numeric' | 'Weighted' | 'Percentage') | undefined, Range?: Array | undefined, PassThreshold?: number | undefined, Date?: string | undefined, Unlisted?: boolean | undefined, Issues?: Array | undefined, Quantity?: ServiceChannelServicesMessagingSiteAuditQuantityExternal | undefined, CheckListItemImpact?: string | undefined, WorkOrderIds?: Array | undefined}; export type ServiceChannelServicesMessagingSiteAuditCheckListRequest = {Name?: string | undefined, Unlisted?: boolean | undefined, SortOrder?: number | undefined, CheckListItems?: Array | undefined}; export type ServiceChannelServicesMessagingSiteAuditAuditAreaRequest = {Id?: number | undefined, Name?: string | undefined, SortOrder?: number | undefined, CheckLists?: Array | undefined}; export type ServiceChannelServicesMessagingSiteAuditAuditDataExternal = {Id?: number | undefined, StoreId?: number | undefined, TemplateTypeId?: number | undefined, StartDate?: string | undefined, FinishDate?: string | undefined, SendEmails?: boolean | undefined, Emails?: string | undefined, Areas?: Array | undefined, Language?: string | undefined, Status?: ('Undefined' | 'Open' | 'Completed') | undefined, Source?: string | undefined}; export type CreateApiSubscribersCurrentLocationsAuditInfoArgs = {body: ServiceChannelServicesMessagingSiteAuditAuditDataExternal}; export type CreateApiSubscribersCurrentLocationsAuditInfoOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const serviceChannelServicesMessagingSiteAuditSaveAuditDataResponse = z.object({Saved: z.boolean().optional(), Sent: z.string().optional(), AuditId: z.number().int().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 }; /** * Save completed audit to the database * * POST /subscribers/current/locations/auditInfo */ export const createApiSubscribersCurrentLocationsAuditInfo = async (args: CreateApiSubscribersCurrentLocationsAuditInfoArgs, options?: CreateApiSubscribersCurrentLocationsAuditInfoOptions) => { const {baseUrl = "https://sb2api.servicechannel.com/v3", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/subscribers/current/locations/auditInfo${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(serviceChannelServicesMessagingSiteAuditSaveAuditDataResponse, await res.json()) };