import {z} from 'zod' export type ServiceChannelServicesMessagingAssetsWarrantyProvider = {Type?: ('Fixed' | 'PrimaryRanked' | 'FixedRanked') | undefined, Value?: number | undefined, ProviderName?: string | undefined}; export type ServiceChannelServicesMessagingAssetsWarrantyExpiration = {Type?: ('FromPurchaseDate' | 'FromInstallDate') | undefined, Period?: number | undefined}; export type ServiceChannelServicesMessagingAssetsWarrantyInfo = {NTE?: number | undefined, Priority?: string | undefined, PriorityId?: number | undefined, Category?: string | undefined, CategoryId?: number | undefined, Provider?: ServiceChannelServicesMessagingAssetsWarrantyProvider | undefined, Expiration?: ServiceChannelServicesMessagingAssetsWarrantyExpiration | undefined}; export type SystemObject = Record; export type ServiceChannelServicesMessagingEquipmentsEquipmentAttributeDescription = {EquipmentAttributeId?: number | undefined, EquipmentTypeAttributeId?: number | undefined, ValueType?: number | undefined, EquipmentId?: number | undefined, Value?: SystemObject | undefined, ValueText?: string | undefined, SelectedOptionsText?: string | undefined, Visible?: boolean | undefined, Editable?: boolean | undefined, Required?: boolean | undefined, Name?: string | undefined, Custom?: boolean | undefined}; export type ServiceChannelServicesMessagingAssetsUpdateAssetRequest = {Id?: number | undefined, NeedUpdateParentId?: boolean | undefined, ProcessDuplicatedAssetTag?: boolean | undefined, OverwriteIfEmpty?: boolean | undefined, Tag: string, ParentAssetTagId?: string | undefined, Manufacturer: string, ModelNo?: string | undefined, SerialNo?: string | undefined, LocationId?: number | undefined, Area?: string | undefined, WorkOrderArea?: string | undefined, StoreId: string, Trade?: string | undefined, TradeId?: number | undefined, Type: string, AssetTypeId?: number | undefined, BrandId?: number | undefined, Active?: boolean | undefined, InstallDate?: string | undefined, PurchaseDate?: string | undefined, ManufacturedDate?: string | undefined, Warranty?: ServiceChannelServicesMessagingAssetsWarrantyInfo | undefined, UsesRefrigerant?: number | undefined, IsLeased?: boolean | undefined, LifeExpectancy?: number | undefined, OriginalValue?: number | undefined, LeaseDate?: string | undefined, LeasePeriod?: number | undefined, Condition?: number | undefined, EnergyEfficiency?: string | undefined, Capacity?: string | undefined, HasLeakDetector?: boolean | undefined, ParentId?: number | undefined, Description?: string | undefined, HasComponents?: boolean | undefined, HasCircuits?: boolean | undefined, ClearAssetTag?: boolean | undefined, AssetTag?: string | undefined, IsInspection?: boolean | undefined, IsCarbReporting?: boolean | undefined, ApplyCustom?: boolean | undefined, CustomAttributes?: Array | undefined, AssetUpdatesStatus?: string | undefined, LocationNoteHeader?: string | undefined, LocationNoteValue?: string | undefined, Currency?: number | undefined}; export type UpdateApiAssetsArgs = {body: ServiceChannelServicesMessagingAssetsUpdateAssetRequest}; export type UpdateApiAssetsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const systemObject = z.object({}); 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 }; /** * Update an asset * * PUT /assets */ export const updateApiAssets = async (args: UpdateApiAssetsArgs, options?: UpdateApiAssetsOptions) => { 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(/\/+$/, '')}/assets${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(systemObject, await res.json()) };