import {z} from 'zod' export type PutV2TimesheetsTimesheetId = {status: 'open' | 'submitted' | 'approved' | 'denied', denial_reason?: string | undefined, confirm_pending_manual_time_request_denial?: boolean | undefined, acknowledge_pending_time_off_requests?: boolean | undefined}; export type UpdateApiV2TimesheetsTimesheetIdArgs = {timesheet_id: number, body: PutV2TimesheetsTimesheetId}; export type UpdateApiV2TimesheetsTimesheetIdOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const timesheet = z.object({id: z.number().int(), paid: z.boolean(), user_id: z.number().int(), organization_id: z.number().int(), team_payment_id: z.number().int().optional(), team_invoice_id: z.number().int().optional(), status: z.string(), keyboard: z.number().int(), mouse: z.number().int(), overall: z.number().int(), tracked: z.number().int(), input_tracked: z.number().int(), start_date: z.string(), stop_date: z.string(), approved_on: z.string().optional(), submitted_on: z.string().optional(), approved_by_id: z.number().int().optional(), denial_reason: z.string().optional(), locked: z.boolean(), created_at: z.string(), updated_at: z.string()}); export const updateApiV2TimesheetsTimesheetIdResponse = z.object({timesheet: timesheet.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 }; /** * Update timesheet status * * PUT /v2/timesheets/{timesheet_id} */ export const updateApiV2TimesheetsTimesheetId = async (args: UpdateApiV2TimesheetsTimesheetIdArgs, options?: UpdateApiV2TimesheetsTimesheetIdOptions) => { 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/timesheets/${encodeURIComponent(String(args.timesheet_id))}${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(updateApiV2TimesheetsTimesheetIdResponse, await res.json()) };