import {z} from 'zod' export type CreateApiPerformanceKeyPerformanceIndicatorsArgs = {body: {title: string, kpi_type: 'individual' | 'department' | 'location' | 'division' | 'company', metric: 'percentage' | 'number' | 'currency', currency_code?: string | undefined, department_id?: number | undefined, location_id?: number | undefined, division_id?: number | undefined, user_id: number, weight?: number | undefined}}; export type CreateApiPerformanceKeyPerformanceIndicatorsOptions = {baseUrl?: string | undefined, XAPIKEY?: string | undefined, fetch?: typeof fetch | undefined}; export const location = z.object({id: z.string(), name: z.string()}); export const division = z.object({id: z.number().int().optional(), name: z.string().optional()}); export const department = z.object({id: z.number().int(), name: z.string(), parent_id: z.number().int().optional(), manager_id: z.number().int().optional(), department_level_id: z.number().int().optional()}); export const keyPerformanceIndicator = z.object({id: z.number().int().optional(), title: z.string().optional(), description: z.string().optional(), weight: z.number().optional(), currency_code: z.string().optional(), metric: z.enum(["percentage", "number", "currency"]).optional(), status: z.enum(["none", "on_track", "behind", "at_risk"]).optional(), kpi_type: z.enum(["individual", "department", "location", "division", "company"]).optional(), progress_percentage: z.number().int().optional(), location: location.optional(), division: division.optional(), department: department.optional(), owner: z.object({id: z.number().int().optional(), first_name: z.string().optional(), last_name: z.string().optional(), email: z.string().optional()}).optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const createApiPerformanceKeyPerformanceIndicatorsResponse = z.object({data: keyPerformanceIndicator.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 a key performance indicator * * POST /performance/key_performance_indicators */ export const createApiPerformanceKeyPerformanceIndicators = async (args: CreateApiPerformanceKeyPerformanceIndicatorsArgs, options?: CreateApiPerformanceKeyPerformanceIndicatorsOptions) => { const {baseUrl = "https://app.peopleforce.io/api/public/v3", fetch: fetchFn = fetch, XAPIKEY} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (XAPIKEY !== undefined) headers.set("X-API-KEY", XAPIKEY) const url = `${baseUrl.replace(/\/+$/, '')}/performance/key_performance_indicators${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(createApiPerformanceKeyPerformanceIndicatorsResponse, await res.json()) };