import {z} from 'zod' export type ServiceChannelServicesMessagingNotificationsNotificationSubscriptionsRules = {Trades?: Array | undefined, Categories?: Array | undefined, Statuses?: Array | undefined}; export type ServiceChannelServicesMessagingNotificationsAddHttpNotificationSubscriptionRequest = {Url: string, Enabled?: boolean | undefined, Name: string, EventTypes: Array<'WorkOrderCreated' | 'WorkOrderNoteAdded' | 'WorkOrderNotePinned' | 'WorkOrderNoteUnpinned' | 'WorkOrderNoteActionResolved' | 'WorkOrderStatusChanged' | 'WorkOrderAttachmentAdded' | 'WorkOrderAttachmentDeleted' | 'WorkOrderNumberChanged' | 'WorkOrderCallerChanged' | 'WorkOrderMoeNumberChanged' | 'WorkOrderServiceNowIdChanged' | 'WorkOrderPriorityChanged' | 'WorkOrderScheduledDateChanged' | 'WorkOrderTradeChanged' | 'WorkOrderNteChanged' | 'WorkOrderCategoryChanged' | 'WorkOrderProviderChanged' | 'WorkOrderDescriptionChanged' | 'WorkOrderResolutionCreated' | 'RefrigerantWasUsedChanged' | 'WorkOrderResolutionUpdated' | 'WorkOrderResolutionCodeUpdated' | 'WorkOrderRootCauseCodeUpdated' | 'WorkOrderAssetAttached' | 'WorkOrderAssetDetached' | 'WorkOrderAssetValidated' | 'WorkOrderLabelAdded' | 'WorkOrderLabelDeleted' | 'WorkOrderWeatherTypeChanged' | 'WorkOrderWeatherTypeDeleted' | 'WorkOrderVerbalApprovalChanged' | 'WorkOrderGLCodeChanged' | 'WorkOrderAdditionalApprovalCodesChanged' | 'WorkOrderFeedbackRequested' | 'WorkOrderAssigneeChanged' | 'WorkOrderAssigneeDeleted' | 'WorkOrderProjectLink' | 'WorkOrderProjectUnlink' | 'WorkOrderRecallChanged' | 'WorkOrderRecallDeleted' | 'WorkOrderIssueChoiceChanged' | 'WorkOrderTechnicianAssigned' | 'WorkOrderTechnicianUnassigned' | 'WorkOrderTechnicianAccepted' | 'WorkOrderTechnicianDeclined' | 'WorkOrderCertifiedTechnicianAssigned' | 'WorkOrderLinksAdded' | 'WorkOrderLinksDeleted' | 'WorkOrderStarAdded' | 'WorkOrderStarRemoved' | 'WorkOrderWorkActivityAdded' | 'WorkOrderWorkActivityDeleted' | 'WorkOrderWorkActivityChanged' | 'WorkOrderCheckIn' | 'WorkOrderCheckOut' | 'BadgeScanRequired' | 'InvoiceCreated' | 'InvoiceOpen' | 'InvoiceApproved' | 'InvoiceOnHold' | 'InvoiceReviewed' | 'InvoiceRejected' | 'InvoiceApprovalCodeChanged' | 'InvoiceAdditionalApprovalCodesChanged' | 'InvoiceStarAdded' | 'InvoiceStarRemoved' | 'InvoiceDisputed' | 'InvoiceVoided' | 'InvoicePaid' | 'ProposalCreated' | 'ProposalOpen' | 'ProposalApproved' | 'ProposalOnHold' | 'ProposalRejected' | 'ProposalReviewed' | 'ProposalUserAssigned' | 'ProposalWorkOrderAssigned' | 'ProposalAttachmentAdded' | 'ProposalScheduled' | 'ProposalVoided' | 'ProposalPendingExternalApproval' | 'ProposalUpdated' | 'FbInvitationAccepted' | 'CalendarIntegrationAdded' | 'CalendarIntegrationUpdated' | 'LocationCreated' | 'LocationUpdated' | 'LocationDeleted' | 'LocationNoteHeaderCreated' | 'LocationNoteHeaderUpdated' | 'LocationNoteHeaderDeleted' | 'LocationNotesChanged' | 'SubscriberTradeCreated' | 'SubscriberTradeUpdated' | 'SubscriberTradeDeleted' | 'SubscriberPriorityCreated' | 'SubscriberPriorityUpdated' | 'SubscriberPriorityDeleted' | 'SubscriberStatusCreated' | 'SubscriberStatusUpdated' | 'SubscriberStatusDeleted' | 'SubscriberCategoryCreated' | 'SubscriberCategoryUpdated' | 'SubscriberCategoryDeleted' | 'SubscriberGlCodeCreated' | 'SubscriberGlCodeUpdated' | 'SubscriberGlCodeDeleted' | 'SubscriberAdditionalApprovalCodeCreated' | 'SubscriberAdditionalApprovalCodeUpdated' | 'SubscriberNameUpdated' | 'ProviderTradeAssigned' | 'ProviderTradeUnassigned' | 'ProviderTradeUpdated' | 'ProviderNameUpdated' | 'LeakRecordCreated' | 'LeakRecordUpdated' | 'LeakRecordDeleted' | 'CheckListResponseAdded' | 'CheckListResponseUpdated' | 'CheckListCreated' | 'CheckListUpdated' | 'CheckListDeleted' | 'IssueListAssigned' | 'AssetCreated' | 'AssetUpdated' | 'AssetAttachmentAdded' | 'AssetAttachmentDeleted' | 'AssetRefrigerantChanged'>, Rules?: ServiceChannelServicesMessagingNotificationsNotificationSubscriptionsRules | undefined}; export type CreateApiNotificationSubscriptionsHttpArgs = {body: ServiceChannelServicesMessagingNotificationsAddHttpNotificationSubscriptionRequest}; export type CreateApiNotificationSubscriptionsHttpOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const serviceChannelServicesMessagingNotificationsAddNotificationSubsciptionResponse = z.object({Id: 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 }; /** * Create a subscription * * POST /NotificationSubscriptions/Http */ export const createApiNotificationSubscriptionsHttp = async (args: CreateApiNotificationSubscriptionsHttpArgs, options?: CreateApiNotificationSubscriptionsHttpOptions) => { 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(/\/+$/, '')}/NotificationSubscriptions/Http${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(serviceChannelServicesMessagingNotificationsAddNotificationSubsciptionResponse, await res.json()) };