import {z} from 'zod' export type GetApiWorkordersWorkorderIdPartsUsageArgs = {workorderId: number}; export type GetApiWorkordersWorkorderIdPartsUsageOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const serviceChannelServicesMessagingInventoryInventoryLocation = z.object({Id: z.number().int().optional(), LocationId: z.number().int().optional(), PartId: z.number().int().optional(), Qty: z.number().optional(), UnitPrice: z.number().optional(), LocationName: z.string().optional(), StoreId: z.string().optional(), LocationGuid: z.string().optional()}); export const serviceChannelServicesMessagingRefrigerantTrackingWorkOrderPartRefrigerantResponse = z.object({Id: z.number().int().optional(), AssetId: z.number().int().optional(), TagId: z.string().optional(), RecId: z.number().int().optional(), CertifiedTechnicianId: z.number().int().optional(), Quantity: z.number().optional(), QuantityMetrics: z.number().optional(), QuantityStr: z.string().optional(), UnitCost: z.number().optional(), IsRefrigerant: z.boolean().optional(), IsInventoryPart: z.boolean().optional(), InventorySource: z.string().optional(), UseDate: z.string().optional(), ReasonCode: z.number().int().optional(), ReasonCodeId: z.number().int().optional(), PartNumber: z.string().optional(), Description: z.string().optional(), WOStatus: z.string().optional(), ProblemType: z.string().optional(), InventoryLocationId: z.number().int().optional(), InventoryLocationGuid: z.string().optional(), InventoryLocationName: z.string().optional(), InventoryPartId: z.number().int().optional(), PartName: z.string().optional(), PartGroup: z.string().optional(), SupplierName: z.string().optional(), RefrigerantChargeOptionsId: z.number().int().optional(), RefrigerantChargeVendor: z.string().optional(), ReasonCodeStr: z.string().optional(), RefrigerantUsePurpose: z.number().int().optional(), RefrigerantTypeName: z.string().optional(), RefrigerantTypeId: z.number().int().optional(), IsSupplierPart: z.boolean().optional(), IsConnectorWOPart: z.boolean().optional(), SubscriberTrackingNumber: z.number().int().optional(), SubscriberAssetId: z.number().int().optional(), CreatedByUserId: z.string().optional(), ModifiedByUserId: z.string().optional(), IsRetrofit: z.string().optional(), RefrigerantPartNumber: z.string().optional(), InventoryLocations: z.array(serviceChannelServicesMessagingInventoryInventoryLocation).optional(), SupplierPartId: z.string().optional(), LeakRate: z.number().optional(), RefrigerantSourceTypeId: z.number().int().optional()}); export const getApiWorkordersWorkorderIdPartsUsageResponse = z.array(serviceChannelServicesMessagingRefrigerantTrackingWorkOrderPartRefrigerantResponse); 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 }; /** * Retrieve work order parts usage * * GET /workorders/{workorderId}/partsUsage */ export const getApiWorkordersWorkorderIdPartsUsage = async (args: GetApiWorkordersWorkorderIdPartsUsageArgs, options?: GetApiWorkordersWorkorderIdPartsUsageOptions) => { 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 (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/workorders/${encodeURIComponent(String(args.workorderId))}/partsUsage${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiWorkordersWorkorderIdPartsUsageResponse, await res.json()) };