import {z} from 'zod' export type ClockEntrySchema = {employeeId: number, date: string, start: string, end: string, id?: number | undefined, projectId?: number | undefined, taskId?: number | undefined, note?: string | undefined, breakId?: (string | null) | undefined}; export type ClockEntriesSchema = {entries: Array}; export type CreateApiApiV1TimeTrackingClockEntriesStoreArgs = {body: ClockEntriesSchema}; export type CreateApiApiV1TimeTrackingClockEntriesStoreOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const projectInfoApiTransformer = z.object({project: z.object({id: z.number().int().optional(), name: z.string().optional()}).optional(), task: z.object({id: z.number().int().optional(), name: z.string().optional()}).nullable().optional()}); export const timesheetEntryInfoApiTransformer = z.object({id: z.number().int().optional(), employeeId: z.number().int().optional(), type: z.string().optional(), date: z.string().optional(), start: z.string().nullable().optional(), end: z.string().nullable().optional(), timezone: z.string().nullable().optional(), hours: z.number().nullable().optional(), note: z.string().nullable().optional(), projectInfo: projectInfoApiTransformer.nullable().optional(), breakInfo: z.object({id: z.number().int().optional(), name: z.string().optional()}).nullable().optional()}); export const timesheetEntryInfoCollectionApiTransformer = z.array(timesheetEntryInfoApiTransformer); 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 or Update Timesheet Clock Entries * * POST /api/v1/time_tracking/clock_entries/store * * Also accepts auth: oauth */ export const createApiApiV1TimeTrackingClockEntriesStore = async (args: CreateApiApiV1TimeTrackingClockEntriesStoreArgs, options?: CreateApiApiV1TimeTrackingClockEntriesStoreOptions) => { 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/store${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(timesheetEntryInfoCollectionApiTransformer, await res.json()) };