import {z} from 'zod' export type EntityReference = {entityId?: string | undefined, entityName?: string | undefined}; export type OnlyId = {id?: string | undefined}; export type CustomAttribute = {attributeDefinitionId?: string | undefined, booleanValue?: boolean | undefined, dateValue?: number | undefined, entityId?: string | undefined, entityReferences?: Array | undefined, numberValue?: unknown | undefined, selectedValueId?: string | undefined, selectedValues?: Array | undefined, stringValue?: string | undefined}; export type TimeRecord = {id?: string | undefined, version?: string | undefined, articleId?: string | undefined, billable?: boolean | undefined, billableDurationSeconds?: number | undefined, billableInvoiceStatus?: ('INVOICED' | 'NOT_INVOICED' | 'PARTLY_INVOICED') | undefined, createdDate?: number | undefined, customAttributes?: Array | undefined, customerId?: string | undefined, description?: string | undefined, durationSeconds: number, externalConnectionSourceId?: string | undefined, hourlyRate?: unknown | undefined, lastModifiedDate?: number | undefined, performanceRecordedStatus?: ('NOT_PERFORMANCE_RECORDABLE' | 'ON_PERFORMANCE_RECORD' | 'PERFORMANCE_RECORDABLE' | 'PERFORMANCE_RECORDED') | undefined, placeOfServiceId?: string | undefined, printOnPerformanceRecord?: boolean | undefined, projectId?: string | undefined, projectTaskId?: string | undefined, salesInvoiceId?: string | undefined, salesOrderId?: string | undefined, salesOrderTicketId?: string | undefined, serviceQuotaId?: string | undefined, startDate: number, taskId?: string | undefined, ticketId?: string | undefined, timeRecordSource?: 'TEAMVIEWER' | undefined, userId?: string | undefined}; export type CreateApiTimeRecordArgs = {dryRun?: boolean | undefined, body: TimeRecord}; export type CreateApiTimeRecordOptions = {baseUrl: string, APIToken?: string | undefined, fetch?: typeof fetch | undefined}; export const entityReference = z.object({entityId: z.string().optional(), entityName: z.string().optional()}); export const onlyId = z.object({id: z.string().optional()}); export const customAttribute = z.object({attributeDefinitionId: z.string().optional(), booleanValue: z.boolean().optional(), dateValue: z.number().int().optional(), entityId: z.string().optional(), entityReferences: z.array(entityReference).optional(), numberValue: z.string().optional(), selectedValueId: z.string().optional(), selectedValues: z.array(onlyId).optional(), stringValue: z.string().optional()}); export const timeRecord = z.object({id: z.string().optional(), version: z.string().optional(), articleId: z.string().optional(), billable: z.boolean().optional(), billableDurationSeconds: z.number().int().optional(), billableInvoiceStatus: z.enum(["INVOICED", "NOT_INVOICED", "PARTLY_INVOICED"]).optional(), createdDate: z.number().int().optional(), customAttributes: z.array(customAttribute).optional(), customerId: z.string().optional(), description: z.string().optional(), durationSeconds: z.number().int(), externalConnectionSourceId: z.string().optional(), hourlyRate: z.string().optional(), lastModifiedDate: z.number().int().optional(), performanceRecordedStatus: z.enum(["NOT_PERFORMANCE_RECORDABLE", "ON_PERFORMANCE_RECORD", "PERFORMANCE_RECORDABLE", "PERFORMANCE_RECORDED"]).optional(), placeOfServiceId: z.string().optional(), printOnPerformanceRecord: z.boolean().optional(), projectId: z.string().optional(), projectTaskId: z.string().optional(), salesInvoiceId: z.string().optional(), salesOrderId: z.string().optional(), salesOrderTicketId: z.string().optional(), serviceQuotaId: z.string().optional(), startDate: z.number().int(), taskId: z.string().optional(), ticketId: z.string().optional(), timeRecordSource: z.literal("TEAMVIEWER").optional(), userId: z.string().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 timeRecord * * POST /timeRecord */ export const createApiTimeRecord = async (args: CreateApiTimeRecordArgs, options: CreateApiTimeRecordOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.dryRun != null) query.append("dryRun", String(args.dryRun)) if (APIToken !== undefined) headers.set("AuthenticationToken", APIToken) const url = `${baseUrl.replace(/\/+$/, '')}/timeRecord${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(timeRecord, await res.json()) };