import {z} from 'zod' export type PutV2JobSitesJobSiteIdAddRules = {rules: Array<{enter_action?: ('none' | 'track' | 'notify') | undefined, exit_action?: ('none' | 'track' | 'notify') | undefined, user_ids?: Array | undefined, team_ids?: Array | undefined, start_project_id?: number | undefined}>}; export type UpdateApiV2JobSitesJobSiteIdAddRulesArgs = {job_site_id: number, body: PutV2JobSitesJobSiteIdAddRules}; export type UpdateApiV2JobSitesJobSiteIdAddRulesOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const jobSiteRule = z.object({id: z.number().int(), enter_action: z.string(), exit_action: z.string(), user_ids: z.array(z.number().int()), team_ids: z.array(z.number().int()), start_project_id: z.number().int().optional(), start_task_id: z.number().int().optional()}); export const jobSite = z.object({id: z.number().int(), organization_id: z.number().int(), name: z.string(), status: z.string(), address: z.string(), latitude: z.string(), longitude: z.string(), radius: z.number().int(), created_at: z.string(), updated_at: z.string(), last_visited_at: z.string(), rules: z.array(jobSiteRule).optional()}); export const updateApiV2JobSitesJobSiteIdAddRulesResponse = z.object({job_site: jobSite.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 }; /** * Add job site rules * * PUT /v2/job_sites/{job_site_id}/add_rules */ export const updateApiV2JobSitesJobSiteIdAddRules = async (args: UpdateApiV2JobSitesJobSiteIdAddRulesArgs, options?: UpdateApiV2JobSitesJobSiteIdAddRulesOptions) => { 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/job_sites/${encodeURIComponent(String(args.job_site_id))}/add_rules${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(updateApiV2JobSitesJobSiteIdAddRulesResponse, await res.json()) };