import {z} from 'zod' export type FieldReference = {field_id: string, source?: ('standard_field' | 'custom_property' | 'association' | 'record_display' | 'formatted_id' | 'system' | null) | undefined, label?: (string | null) | undefined}; export type SearchFilterExpression = {field: FieldReference, operator: 'equals' | 'not_equals' | 'contains' | 'does_not_contain' | 'starts_with' | 'ends_with' | 'in' | 'not_in' | 'is_empty' | 'is_not_empty' | 'greater_than' | 'greater_than_or_equal' | 'less_than' | 'less_than_or_equal' | 'between' | 'equal_or_after_today' | 'equal_or_before_today' | 'last_x_days' | 'more_than_x_days', value: unknown, selected_label?: (string | null) | undefined}; export type GoalMetricDefinition = {source: string, measure?: string | undefined, date_field?: string | undefined, filters?: Array | undefined}; export type GoalCreateRequest = {name: string, metric: 'deals_created' | 'invoice_revenue' | 'custom', amount?: ('before_tax' | 'including_tax') | undefined, currency?: (string | null) | undefined, definition?: (GoalMetricDefinition | null) | undefined, assignment?: ('company' | 'people' | 'company_and_people') | undefined, assignee_ids?: Array | undefined}; export type CreateApiV2PublicGoalsArgs = {language?: (string | null) | undefined, workspace_id?: (string | null) | undefined, 'X-Workspace-Code'?: string | undefined, body: GoalCreateRequest}; export type CreateApiV2PublicGoalsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const fieldReference = z.object({field_id: z.string(), source: z.enum(["standard_field", "custom_property", "association", "record_display", "formatted_id", "system"]).nullable().optional(), label: z.string().nullable().optional()}); export const searchFilterExpression = z.object({field: fieldReference, operator: z.enum(["equals", "not_equals", "contains", "does_not_contain", "starts_with", "ends_with", "in", "not_in", "is_empty", "is_not_empty", "greater_than", "greater_than_or_equal", "less_than", "less_than_or_equal", "between", "equal_or_after_today", "equal_or_before_today", "last_x_days", "more_than_x_days"]), value: z.unknown(), selected_label: z.string().nullable().optional()}); export const goalMetricDefinition = z.object({source: z.string().min(1).max(64), measure: z.string().min(1).max(64).optional(), date_field: z.string().min(1).max(64).optional(), filters: z.array(searchFilterExpression).optional()}); export const goalOwner = z.object({id: z.number().int(), label: z.string()}); export const goalData = z.object({id: z.string(), name: z.string(), metric: z.enum(["deals_created", "invoice_revenue", "custom"]), unit: z.enum(["count", "money", "number"]), source: z.string(), definition: goalMetricDefinition, amount: z.enum(["before_tax", "including_tax"]).nullable(), currency: z.string().nullable(), assignment: z.enum(["company", "people", "company_and_people"]), assignees: z.array(goalOwner), version: z.number().int(), archived: z.boolean(), created_at: z.string(), updated_at: z.string()}); export const paginationMeta = z.object({page: z.number().int().gte(1), page_size: z.number().int().gte(1).lte(500), total: z.number().int().gte(0), next_cursor: z.string().nullable().optional()}); export const toastMessageI18n = z.object({en: z.string(), ja: z.string()}); export const toastMessage = z.object({variant: z.enum(["success", "error", "info", "warning"]), code: z.string(), message: z.string(), i18n: toastMessageI18n}); export const envelopeMeta = z.object({ctx_id: z.string(), pagination: paginationMeta.optional(), toast: toastMessage.optional()}); export const createPublicGoal201Envelope = z.object({success: z.literal(true), data: goalData, meta: envelopeMeta}); 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 Public Goal * * POST /v2/public/goals */ export const createApiV2PublicGoals = async (args: CreateApiV2PublicGoalsArgs, options?: CreateApiV2PublicGoalsOptions) => { const {baseUrl = "https://api.sanka.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.language != null) query.append("language", String(args.language)) if (args.workspace_id != null) query.append("workspace_id", String(args.workspace_id)) if (args['X-Workspace-Code'] != null) headers.set("X-Workspace-Code", String(args['X-Workspace-Code'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/public/goals${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(createPublicGoal201Envelope, await res.json()) };