import {z} from 'zod' export type ClockEntryClockEntryLocationInputV1 = {latitude: number, longitude: number, accuracy?: (number | null) | undefined, address?: (string | null) | undefined}; export type ClockEntryCreateClockEntryV1 = {employeeId: number, start: string, end: string, timezone: string, note?: (string | null) | undefined, projectId?: (number | null) | undefined, taskId?: (number | null) | undefined, clockInLocation?: (ClockEntryClockEntryLocationInputV1 | null) | undefined, clockOutLocation?: (ClockEntryClockEntryLocationInputV1 | null) | undefined}; export type CreateApiApiV1TimeTrackingClockEntriesArgs = {body: ClockEntryCreateClockEntryV1}; export type CreateApiApiV1TimeTrackingClockEntriesOptions = {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 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 Clock Entry * * POST /api/v1/time-tracking/clock-entries * * Also accepts auth: oauth */ export const createApiApiV1TimeTrackingClockEntries = async (args: CreateApiApiV1TimeTrackingClockEntriesArgs, options?: CreateApiApiV1TimeTrackingClockEntriesOptions) => { const {baseUrl = "https://companySubDomain.bamboohr.com", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "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(timeTrackingClockEntryV1, await res.json()) };