import {z} from 'zod' export type CreateApiV2BillingTicketProductsAdhocArgs = {body: {name: string, description: string, quantityType: 'MANUAL' | 'DEVICE_COUNTS' | 'TIME_ENTRIES' | 'END_USER_COUNTS' | 'BACKUP_STORAGE_COUNTS' | 'SOFTWARE_LICENSE', content: {[key: string]: unknown}, ticketId: number, productId: number | null, accountId: number, taxId?: number | undefined, inheritTaxFromAgreement?: boolean | undefined, productUserRole?: ({allowAllTechnicians: boolean, userRoles: Array} | null) | undefined, integrationSettings?: {[key: string]: unknown} | undefined, group: boolean}}; export type CreateApiV2BillingTicketProductsAdhocOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2BillingTicketProductsAdhocResponse = z.object({id: z.number().int().optional(), name: z.string().optional(), description: z.string().optional(), billingStatus: z.enum(["BILLED", "PENDING", "NOT_BILLABLE", "UNASSIGNED"]).optional(), quantityType: z.enum(["MANUAL", "DEVICE_COUNTS", "TIME_ENTRIES", "END_USER_COUNTS", "BACKUP_STORAGE_COUNTS", "SOFTWARE_LICENSE"]).optional(), content: z.record(z.string(), z.unknown()).optional(), technician: z.object({firstName: z.string().optional(), lastName: z.string().optional(), id: z.number().int().optional(), email: z.string().optional(), deleted: z.boolean().optional()}).optional(), productId: z.number().int().optional(), integrationSettings: z.record(z.string(), z.unknown()).optional(), taxId: z.number().int().optional(), inheritTaxFromAgreement: z.boolean().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 }; /** * Creates an adhoc ticket product. * * POST /v2/billing/ticket-products/adhoc * * Also accepts auth: sessionKey */ export const createApiV2BillingTicketProductsAdhoc = async (args: CreateApiV2BillingTicketProductsAdhocArgs, options: CreateApiV2BillingTicketProductsAdhocOptions) => { const {baseUrl, 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(/\/+$/, '')}/v2/billing/ticket-products/adhoc${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(createApiV2BillingTicketProductsAdhocResponse, await res.json()) };