import {z} from 'zod' export type PutV2TimeOffRequestsTimeOffRequestIdStatus = {status: 'approved' | 'denied', response?: string | undefined, remove_shifts?: boolean | undefined}; export type UpdateApiV2TimeOffRequestsTimeOffRequestIdStatusArgs = {time_off_request_id: number, body: PutV2TimeOffRequestsTimeOffRequestIdStatus}; export type UpdateApiV2TimeOffRequestsTimeOffRequestIdStatusOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const timeOffRequestDay = z.object({date: z.string(), amount_used: z.number().int(), starts_at: z.string(), stops_at: z.string(), holiday: z.boolean(), paid: z.boolean()}); export const timeOffRequest = z.object({id: z.number().int(), user_id: z.number().int(), time_off_policy_id: z.number().int(), status: z.string(), all_day: z.boolean(), starts_at: z.string(), stops_at: z.string(), submitted_at: z.string().optional(), approved_at: z.string().optional(), denied_at: z.string().optional(), paid: z.boolean(), amount_used: z.number().int(), message: z.string().optional(), response: z.string().optional(), created_by_id: z.number().int(), approved_by_id: z.number().int().optional(), denied_by_id: z.number().int().optional(), team_payment_detail_id: z.number().int().optional(), team_payment_id: z.number().int().optional(), time_off_request_days: z.array(timeOffRequestDay), attachment_original_filename: z.string().optional(), attachment_url: z.string().optional(), created_at: z.string(), updated_at: z.string()}); export const timeOffBalanceAmount = z.object({current: z.number().int(), next: z.number().int().optional()}); export const timeOffBalancePreview = z.object({starting_balance: timeOffBalanceAmount, pending_approval: timeOffBalanceAmount, amount_left: timeOffBalanceAmount, amount_used: timeOffBalanceAmount, holiday_hours: timeOffBalanceAmount}); export const timeOffRequestWithBalancePreview = z.object({time_off_request: timeOffRequest, balance_preview: timeOffBalancePreview}); 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 }; /** * Update time off request status * * PUT /v2/time_off_requests/{time_off_request_id}/status */ export const updateApiV2TimeOffRequestsTimeOffRequestIdStatus = async (args: UpdateApiV2TimeOffRequestsTimeOffRequestIdStatusArgs, options?: UpdateApiV2TimeOffRequestsTimeOffRequestIdStatusOptions) => { const {baseUrl = "https://api.hubstaff.com", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/time_off_requests/${encodeURIComponent(String(args.time_off_request_id))}/status${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(timeOffRequestWithBalancePreview, await res.json()) };