import {z} from 'zod' export type GetApiV1ReportTicketTimeentriesArgs = {FromDate?: string | undefined, ToDate?: string | undefined, TicketSubject?: string | undefined, AgentName?: string | undefined, AgentId?: number | undefined, TicketPriority?: string | undefined, TicketStatus?: string | undefined, pipelineIds?: string | undefined}; export type GetApiV1ReportTicketTimeentriesOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; 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 reportTicketTimeEntryModel = z.object({id: z.number().int().optional(), agentId: z.number().int().optional(), ticketId: z.number().int().optional(), companyId: z.number().int().nullable().optional(), ticketSubject: z.string().nullable().optional(), ticketStatus: ticketStatus.optional(), ticketPriority: priorityType.optional(), agentName: z.string().nullable().optional(), companyName: z.string().nullable().optional(), timeSpent: z.number().int().optional(), note: z.string().nullable().optional(), createdAt: z.string().optional()}); export const reportTicketTimeEntryModelQueryResult = z.object({totalItems: z.number().int().optional(), items: z.array(reportTicketTimeEntryModel).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 }; /** * GET /v1/report/ticket/timeentries * * Also accepts auth: API Key */ export const getApiV1ReportTicketTimeentries = async (args: GetApiV1ReportTicketTimeentriesArgs, options: GetApiV1ReportTicketTimeentriesOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.FromDate != null) query.append("FromDate", String(args.FromDate)) if (args.ToDate != null) query.append("ToDate", String(args.ToDate)) if (args.TicketSubject != null) query.append("TicketSubject", String(args.TicketSubject)) if (args.AgentName != null) query.append("AgentName", String(args.AgentName)) if (args.AgentId != null) query.append("AgentId", String(args.AgentId)) if (args.TicketPriority != null) query.append("TicketPriority", String(args.TicketPriority)) if (args.TicketStatus != null) query.append("TicketStatus", String(args.TicketStatus)) if (args.pipelineIds != null) query.append("pipelineIds", String(args.pipelineIds)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/report/ticket/timeentries${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(reportTicketTimeEntryModelQueryResult, await res.json()) };