import {z} from 'zod' export type DepartmentPageSelection = Record; export type TicketSource = 1 | 2 | 3 | 4 | 5 | 6; export type TicketStatus = 1 | 2 | 3 | 4; export type PriorityType = 1 | 2 | 3 | 4; export type QueryPrefix = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16; export type DataFieldFilter = {op?: QueryPrefix | undefined, val: unknown}; export type ViewType = 0 | 1; export type GetApiV1TicketsArgs = {Id?: number | undefined, DepartmentScope?: DepartmentPageSelection | undefined, PipelineId?: number | undefined, StageId?: number | undefined, StageIds?: Array | undefined, Subject?: string | undefined, Content?: string | undefined, Notes?: string | undefined, Source?: TicketSource | undefined, Status?: Array | undefined, Priority?: PriorityType | undefined, ContactId?: number | undefined, CompanyId?: number | undefined, CreatedUserId?: number | undefined, OwnerIds?: string | undefined, OwnerGroupId?: number | undefined, FollowerIds?: string | undefined, CategoryIds?: string | undefined, IsSpam?: number | undefined, FromDate?: string | undefined, ToDate?: string | undefined, FromStartDate?: string | undefined, ToStartDate?: string | undefined, FromDueDate?: string | undefined, ToDueDate?: string | undefined, FromUpdatedDate?: string | undefined, ToUpdatedDate?: string | undefined, FromClosingDate?: string | undefined, ToClosingDate?: string | undefined, FromNextSlaBreach?: string | undefined, ToNextSlaBreach?: string | undefined, FromOpenTimeSec?: number | undefined, ToOpenTimeSec?: number | undefined, FromPendingTimeSec?: number | undefined, ToPendingTimeSec?: number | undefined, SlaId?: number | undefined, MergeId?: number | undefined, IncludeMerged?: boolean | undefined, CustomFields?: ({[key: string]: Array | null}) | undefined, Q?: string | undefined, Fields?: string | undefined, MinCsatRate?: number | undefined, MaxCsatRate?: number | undefined, ChatId?: number | undefined, ItemsPerPage?: number | undefined, ViewType?: ViewType | undefined, Page?: number | undefined, ReturnTotalItems?: boolean | undefined, sortCol?: string | undefined, sortDir?: string | undefined, isExport?: boolean | undefined}; export type GetApiV1TicketsOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const ticketSource = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6)]); export const ticketStatus = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4)]); export const priorityType = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4)]); export const table = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(5), z.literal(6), z.literal(7), z.literal(10), z.literal(11), z.literal(12), z.literal(13), z.literal(14), z.literal(15), z.literal(16), z.literal(19), z.literal(20), z.literal(21), z.literal(22), z.literal(23), z.literal(25), z.literal(26), z.literal(27), z.literal(31), z.literal(32), z.literal(34), z.literal(35), z.literal(36), z.literal(37), z.literal(38), z.literal(39), z.literal(40), z.literal(41), z.literal(42), z.literal(43), z.literal(44), z.literal(45), z.literal(46), z.literal(47), z.literal(48), z.literal(50), z.literal(51), z.literal(52), z.literal(53), z.literal(55), z.literal(60), z.literal(61), z.literal(62), z.literal(63), z.literal(64), z.literal(65), z.literal(66), z.literal(67), z.literal(68), z.literal(69), z.literal(72)]); export const slaStats = z.object({id: z.number().int().optional(), departmentId: z.number().int().optional(), slaId: z.number().int().optional(), linkedType: z.number().int().optional(), linkedId: z.number().int().optional(), dueBy: z.string().nullable().optional(), resolveTime: z.string().nullable().optional(), dueEscalated: z.boolean().optional(), frDueBy: z.string().nullable().optional(), frTime: z.string().nullable().optional(), frEscalated: z.boolean().optional(), nrDueBy: z.string().nullable().optional(), nrTime: z.string().nullable().optional(), nrEscalated: z.boolean().optional(), accountId: z.number().int().optional()}); export const ticketResponseModel = z.object({id: z.number().int().nullable().optional(), subject: z.string().nullable().optional(), departmentId: z.number().int().nullable().optional(), pipelineId: z.number().int().nullable().optional(), stageId: z.number().int().nullable().optional(), orderNo: z.number().int().nullable().optional(), contactId: z.number().int().nullable().optional(), contactName: z.string().nullable().optional(), contactEmail: z.string().nullable().optional(), contactPhone: z.string().nullable().optional(), companyId: z.number().int().nullable().optional(), companyName: z.string().nullable().optional(), categoryIds: z.string().nullable().optional(), categoryNames: z.string().nullable().optional(), ownerIds: z.string().nullable().optional(), ownerGroupId: z.number().int().nullable().optional(), ownerNames: z.string().nullable().optional(), ownerPhotos: z.string().nullable().optional(), ownerColors: z.string().nullable().optional(), followerIds: z.string().nullable().optional(), startDate: z.string().nullable().optional(), dueDate: z.string().nullable().optional(), source: ticketSource.optional(), status: ticketStatus.optional(), lastStatusChangeDate: z.string().nullable().optional(), priority: priorityType.optional(), createdUserId: z.number().int().nullable().optional(), createdDate: z.string().nullable().optional(), updatedUserId: z.number().int().nullable().optional(), updatedDate: z.string().nullable().optional(), notes: z.string().nullable().optional(), linkedType: table.optional(), linkedId: z.number().int().nullable().optional(), linked: z.string().nullable().optional(), customFields: z.string().nullable().optional(), firstResponseDate: z.string().nullable().optional(), closingDate: z.string().nullable().optional(), reopenDate: z.string().nullable().optional(), resolveTimeSec: z.number().int().nullable().optional(), resolveTimeBusinessSec: z.number().int().nullable().optional(), frTimeSec: z.number().int().nullable().optional(), frTimeBusinessSec: z.number().int().nullable().optional(), openTimeSec: z.number().int().nullable().optional(), openTimeBusinessSec: z.number().int().nullable().optional(), pendingTimeSec: z.number().int().nullable().optional(), pendingTimeBusinessSec: z.number().int().nullable().optional(), isSpam: z.number().int().nullable().optional(), slaId: z.number().int().nullable().optional(), nextSlaBreach: z.string().nullable().optional(), slaStats: slaStats.optional(), highlights: z.record(z.string(), z.array(z.string()).nullable()).nullable().optional()}); export const ticketResponseModelQueryResult = z.object({totalItems: z.number().int().optional(), items: z.array(ticketResponseModel).nullable().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 }; /** * List all existing tickets * * GET /v1/tickets * * Also accepts auth: API Key */ export const getApiV1Tickets = async (args: GetApiV1TicketsArgs, options: GetApiV1TicketsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Id != null) query.append("Id", String(args.Id)) if (args.DepartmentScope != null) query.append("DepartmentScope", JSON.stringify(args.DepartmentScope)) if (args.PipelineId != null) query.append("PipelineId", String(args.PipelineId)) if (args.StageId != null) query.append("StageId", String(args.StageId)) for (const item of args.StageIds ?? []) query.append("StageIds", String(item)) if (args.Subject != null) query.append("Subject", String(args.Subject)) if (args.Content != null) query.append("Content", String(args.Content)) if (args.Notes != null) query.append("Notes", String(args.Notes)) if (args.Source != null) query.append("Source", String(args.Source)) for (const item of args.Status ?? []) query.append("Status", String(item)) if (args.Priority != null) query.append("Priority", String(args.Priority)) if (args.ContactId != null) query.append("ContactId", String(args.ContactId)) if (args.CompanyId != null) query.append("CompanyId", String(args.CompanyId)) if (args.CreatedUserId != null) query.append("CreatedUserId", String(args.CreatedUserId)) if (args.OwnerIds != null) query.append("OwnerIds", String(args.OwnerIds)) if (args.OwnerGroupId != null) query.append("OwnerGroupId", String(args.OwnerGroupId)) if (args.FollowerIds != null) query.append("FollowerIds", String(args.FollowerIds)) if (args.CategoryIds != null) query.append("CategoryIds", String(args.CategoryIds)) if (args.IsSpam != null) query.append("IsSpam", String(args.IsSpam)) if (args.FromDate != null) query.append("FromDate", String(args.FromDate)) if (args.ToDate != null) query.append("ToDate", String(args.ToDate)) if (args.FromStartDate != null) query.append("FromStartDate", String(args.FromStartDate)) if (args.ToStartDate != null) query.append("ToStartDate", String(args.ToStartDate)) if (args.FromDueDate != null) query.append("FromDueDate", String(args.FromDueDate)) if (args.ToDueDate != null) query.append("ToDueDate", String(args.ToDueDate)) if (args.FromUpdatedDate != null) query.append("FromUpdatedDate", String(args.FromUpdatedDate)) if (args.ToUpdatedDate != null) query.append("ToUpdatedDate", String(args.ToUpdatedDate)) if (args.FromClosingDate != null) query.append("FromClosingDate", String(args.FromClosingDate)) if (args.ToClosingDate != null) query.append("ToClosingDate", String(args.ToClosingDate)) if (args.FromNextSlaBreach != null) query.append("FromNextSlaBreach", String(args.FromNextSlaBreach)) if (args.ToNextSlaBreach != null) query.append("ToNextSlaBreach", String(args.ToNextSlaBreach)) if (args.FromOpenTimeSec != null) query.append("FromOpenTimeSec", String(args.FromOpenTimeSec)) if (args.ToOpenTimeSec != null) query.append("ToOpenTimeSec", String(args.ToOpenTimeSec)) if (args.FromPendingTimeSec != null) query.append("FromPendingTimeSec", String(args.FromPendingTimeSec)) if (args.ToPendingTimeSec != null) query.append("ToPendingTimeSec", String(args.ToPendingTimeSec)) if (args.SlaId != null) query.append("SlaId", String(args.SlaId)) if (args.MergeId != null) query.append("MergeId", String(args.MergeId)) if (args.IncludeMerged != null) query.append("IncludeMerged", String(args.IncludeMerged)) if (args.CustomFields != null) query.append("CustomFields", JSON.stringify(args.CustomFields)) if (args.Q != null) query.append("Q", String(args.Q)) if (args.Fields != null) query.append("Fields", String(args.Fields)) if (args.MinCsatRate != null) query.append("MinCsatRate", String(args.MinCsatRate)) if (args.MaxCsatRate != null) query.append("MaxCsatRate", String(args.MaxCsatRate)) if (args.ChatId != null) query.append("ChatId", String(args.ChatId)) if (args.ItemsPerPage != null) query.append("ItemsPerPage", String(args.ItemsPerPage)) if (args.ViewType != null) query.append("ViewType", String(args.ViewType)) if (args.Page != null) query.append("Page", String(args.Page)) if (args.ReturnTotalItems != null) query.append("ReturnTotalItems", String(args.ReturnTotalItems)) if (args.sortCol != null) query.append("sortCol", String(args.sortCol)) if (args.sortDir != null) query.append("sortDir", String(args.sortDir)) if (args.isExport != null) query.append("isExport", String(args.isExport)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/tickets${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(ticketResponseModelQueryResult, await res.json()) };