import {z} from 'zod' export type Translations = {values: {[key: string]: string}}; export type GoalTargetValue = {targetValueTypeId?: string | undefined, value: string | number | boolean}; export type CreateGoalTarget = {goalId: string, label?: string | undefined, labelTr?: Translations | undefined, slug?: string | undefined, startValue?: (string | number | boolean) | undefined, targetValues?: Array | undefined, direction?: ('MINIMIZE' | 'MAXIMIZE' | 'TARGET') | undefined, type: 'MILESTONE' | 'MONEY' | 'PERCENT' | 'INTEGER' | 'DECIMAL', valueExpr?: string | undefined, startDate: string, endDate: string}; export type CreateApiV1OrgOrgIdGoalGoalIdTargetArgs = {orgId: string, goalId: string, body: CreateGoalTarget}; export type CreateApiV1OrgOrgIdGoalGoalIdTargetOptions = {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 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 new goal target * * POST /v1/org/{orgId}/goal/{goalId}/target */ export const createApiV1OrgOrgIdGoalGoalIdTarget = async (args: CreateApiV1OrgOrgIdGoalGoalIdTargetArgs, options?: CreateApiV1OrgOrgIdGoalGoalIdTargetOptions) => { const {baseUrl = "https://localhost", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "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(goalTarget, await res.json()) };