import {z} from 'zod' export type GetApiV1OrgOrgIdGoalGoalIdTargetArgs = {orgId: string, goalId: string, limit?: number | undefined, returnAccess?: string | undefined}; export type GetApiV1OrgOrgIdGoalGoalIdTargetOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const translations = z.object({values: z.record(z.string(), z.string())}); export const goalTargetValue = z.object({targetValueTypeId: z.string().optional(), value: z.union([z.string(), z.number(), z.boolean()])}); export const attribution = z.object({principalUserId: z.string().optional(), agentUserIds: z.array(z.string()).optional(), eventId: z.string().optional(), aiChatId: z.string().optional(), aiToolUseId: z.string().optional(), channel: z.enum(["WEB", "MOBILE", "SLACK", "TEAMS", "MCP"]).optional()}); export const goalTarget = z.object({id: z.string(), orgId: z.string(), goalId: z.string(), label: z.string().optional(), labelTr: translations.optional(), slug: z.string().optional(), startValue: z.union([z.string(), z.number(), z.boolean()]).optional(), targetValues: z.array(goalTargetValue).optional(), direction: z.enum(["MINIMIZE", "MAXIMIZE", "TARGET"]).optional(), type: z.enum(["MILESTONE", "MONEY", "PERCENT", "INTEGER", "DECIMAL"]), valueExpr: z.string().optional(), startDate: z.string(), endDate: z.string(), createId: z.string().optional(), createBehalfId: z.string().optional(), createAttribution: attribution.optional(), createAt: z.string().optional(), updateId: z.string().optional(), updateBehalfId: z.string().optional(), updateAttribution: attribution.optional(), updateAt: z.string().optional(), deleteId: z.string().optional(), deleteBehalfId: z.string().optional(), deleteAttribution: attribution.optional(), deleteAt: z.string().optional()}); export const accessAction = z.object({action: z.string(), fields: z.array(z.string()).optional(), types: z.array(z.string()).optional()}); export const resultsAccess = z.object({ids: z.array(z.string()).optional(), allowed: z.array(accessAction)}); export const resultsGoalTarget = z.object({data: z.array(goalTarget), next: z.string().optional(), access: z.array(resultsAccess).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 }; /** * Find goal targets for a goal * * GET /v1/org/{orgId}/goal/{goalId}/target */ export const getApiV1OrgOrgIdGoalGoalIdTarget = async (args: GetApiV1OrgOrgIdGoalGoalIdTargetArgs, options?: GetApiV1OrgOrgIdGoalGoalIdTargetOptions) => { const {baseUrl = "https://localhost", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.returnAccess != null) query.append("returnAccess", String(args.returnAccess)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/org/${encodeURIComponent(String(args.orgId))}/goal/${encodeURIComponent(String(args.goalId))}/target${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(resultsGoalTarget, await res.json()) };