import {z} from 'zod' export type ServiceChannelServicesMessagingWorkRequestsSearchWorkRequestRequest = {Ids?: Array | undefined, SubscriberIds?: Array | undefined, ProviderIds?: Array | undefined, LocationIds?: Array | undefined, CategoryIds?: Array | undefined, StatusIds?: Array | undefined, ExtendedStatusIds?: Array | undefined, PriorityIds?: Array | undefined, TradeIds?: Array | undefined, ClosedReasonIds?: Array | undefined, EventIds?: Array | undefined, WorkOrderIds?: Array | undefined, AssetIds?: Array | undefined, CallDateFrom?: string | undefined, CallDateTo?: string | undefined, ScheduledDateFrom?: string | undefined, ScheduledDateTo?: string | undefined, ClosedDateFrom?: string | undefined, ClosedDateTo?: string | undefined, SortField?: ('Id' | 'CallDate' | 'ScheduledDate' | 'ClosedDate') | undefined, SortDirection?: ('Asc' | 'Desc') | undefined, Page?: number | undefined, PageSize?: number | undefined}; export type CreateApiWorkrequestsSearchArgs = {body: ServiceChannelServicesMessagingWorkRequestsSearchWorkRequestRequest}; export type CreateApiWorkrequestsSearchOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const serviceChannelServicesMessagingWorkRequestsWorkRequestNte = z.object({Value: z.number().optional(), CurrencyId: z.number().int().optional(), CurrencyAlphabeticCode: z.string().optional()}); export const serviceChannelServicesMessagingWorkRequestsWorkRequestIssue = z.object({AreaId: z.number().int().optional(), AreaName: z.string().optional(), ProblemType: z.string().optional(), Equipment: z.string().optional(), ProblemCode: z.string().optional(), SetupId: z.number().int().optional()}); export const serviceChannelServicesMessagingWorkRequestsWorkRequest = z.object({Id: z.number().int().optional(), SubscriberId: z.number().int().optional(), SubscriberShortName: z.string().optional(), ProviderId: z.number().int().optional(), ProviderShortName: z.string().optional(), LocationId: z.number().int().optional(), LocationStoreId: z.string().optional(), EventId: z.string().optional(), StatusId: z.number().int().optional(), StatusName: z.string().optional(), ExtendedStatusId: z.number().int().optional(), ExtendedStatusName: z.string().optional(), ClosedReasonId: z.number().int().optional(), ClosedReasonName: z.string().optional(), Description: z.string().optional(), TradeId: z.number().int().optional(), TradeName: z.string().optional(), CategoryId: z.number().int().optional(), CategoryName: z.string().optional(), PriorityId: z.number().int().optional(), PriorityName: z.string().optional(), Caller: z.string().optional(), CallDate: z.string().optional(), ClosedDate: z.string().optional(), ScheduledDate: z.string().optional(), WorkOrderId: z.number().int().optional(), AssetId: z.number().int().optional(), Nte: serviceChannelServicesMessagingWorkRequestsWorkRequestNte.optional(), Issue: serviceChannelServicesMessagingWorkRequestsWorkRequestIssue.optional()}); export const createApiWorkrequestsSearchResponse = z.array(serviceChannelServicesMessagingWorkRequestsWorkRequest); 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 }; /** * Performs search of work requests by specified filter expression. * One page contains 50 items by default ("PageSize"=50). * To get next 50 items use the “Page” parameter (for example Page=2 and so on). * * POST /workrequests/search */ export const createApiWorkrequestsSearch = async (args: CreateApiWorkrequestsSearchArgs, options?: CreateApiWorkrequestsSearchOptions) => { 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(/\/+$/, '')}/workrequests/search${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(createApiWorkrequestsSearchResponse, await res.json()) };