import {z} from 'zod' export type GetApiApiV1TimeTrackingClockEntriesArgs = {filter?: string | undefined, sort?: string | undefined, page?: number | undefined, pageSize?: number | undefined}; export type GetApiApiV1TimeTrackingClockEntriesOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const timeTrackingClockEntryLocationV1 = z.object({latitude: z.number().optional(), longitude: z.number().optional(), accuracy: z.number().nullable().optional(), address: z.string().nullable().optional()}); export const timeTrackingClockEntryV1 = z.object({id: z.number().int().optional(), employeeId: z.number().int().optional(), timesheetId: z.number().int().optional(), date: z.string().optional(), start: z.string().optional(), end: z.string().nullable().optional(), timezone: z.string().optional(), hours: z.number().optional(), note: z.string().nullable().optional(), projectId: z.number().int().nullable().optional(), taskId: z.number().int().nullable().optional(), clockInLocation: timeTrackingClockEntryLocationV1.nullable().optional(), clockOutLocation: timeTrackingClockEntryLocationV1.nullable().optional(), schedulingShiftId: z.string().nullable().optional(), startSource: z.enum(["USER", "SYSTEM"]).nullable().optional(), endSource: z.enum(["USER", "SYSTEM"]).nullable().optional(), clockedInBy: z.number().int().nullable().optional(), clockedOutBy: z.number().int().nullable().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const timeTrackingClockEntryCollectionV1 = z.array(timeTrackingClockEntryV1); export const getApiApiV1TimeTrackingClockEntriesResponse = z.object({meta: z.object({totalItems: z.number().int(), totalPages: z.number().int(), page: z.number().int(), pageSize: z.number().int()}).optional(), _links: z.object({prev: z.object({href: z.string().optional()}).optional(), next: z.object({href: z.string().optional()}).optional()}).optional(), data: timeTrackingClockEntryCollectionV1.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 Clock Entries * * GET /api/v1/time-tracking/clock-entries * * Also accepts auth: oauth */ export const getApiApiV1TimeTrackingClockEntries = async (args: GetApiApiV1TimeTrackingClockEntriesArgs, options?: GetApiApiV1TimeTrackingClockEntriesOptions) => { const {baseUrl = "https://companySubDomain.bamboohr.com", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.filter != null) query.append("filter", String(args.filter)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/time-tracking/clock-entries${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(getApiApiV1TimeTrackingClockEntriesResponse, await res.json()) };