import {z} from 'zod' export type CustomerWords = {text: string, source_id?: (string | null) | undefined, verbatim?: boolean | undefined}; export type AppliesTo = {all_sites?: boolean | undefined, site_ids?: Array | undefined}; export type InNash = 'must' | 'aim_for' | 'report' | 'not_in_nash'; export type ValueScope = {all_sites?: boolean | undefined, site_id?: (string | null) | undefined}; export type ValueUnit = 'h' | 'min' | 'stops' | 'cube' | 'percent' | 'count' | 'trucks' | 'time_of_day' | 'window'; export type NashValue = {amount?: (number | number | null) | undefined, unit?: (ValueUnit | null) | undefined, text?: (string | null) | undefined}; export type AuthoredValueStatus = 'assumed' | 'needs_answer'; export type RuleValueContent = {scope: ValueScope, customer_value?: (string | null) | undefined, nash_value?: (NashValue | null) | undefined, status: AuthoredValueStatus, status_reason?: (string | null) | undefined, source_ids?: Array | undefined}; export type SourceKind = 'sheet_cells' | 'file_columns' | 'document' | 'call' | 'written_answer' | 'nash_reading'; export type CellExcerpt = {ref: string, value: string}; export type RuleSource = {id: string, kind: SourceKind, label?: (string | null) | undefined, file_name?: (string | null) | undefined, sheet?: (string | null) | undefined, cells?: Array | undefined, columns?: Array | undefined, excerpt?: Array | undefined, locator?: (string | null) | undefined, url?: (string | null) | undefined, title?: (string | null) | undefined, date?: (string | null) | undefined, author?: (string | null) | undefined, verbatim?: boolean | undefined, basis?: (string | null) | undefined}; export type RulePatch = {name?: (string | null) | undefined, customer_words?: (CustomerWords | null) | undefined, applies_to?: (AppliesTo | null) | undefined, in_nash?: (InNash | null) | undefined, how?: (string | null) | undefined, values?: (Array | null) | undefined, sources?: (Array | null) | undefined, key?: (string | null) | undefined, aliases?: (Array | null) | undefined}; export type PatchApiV1RuleBooksRuleBookIdDraftRulesRuleArgs = {rule_book_id: string, rule: string, body: RulePatch}; export type PatchApiV1RuleBooksRuleBookIdDraftRulesRuleOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const versionState = z.enum(["draft", "published", "superseded", "discarded"]); export const versionRef = z.object({id: z.string(), number: z.number().int(), state: versionState}); export const customerWords = z.object({text: z.string().min(1).max(2000), source_id: z.string().nullable().optional(), verbatim: z.boolean().optional()}); export const appliesTo = z.object({all_sites: z.boolean().optional(), site_ids: z.array(z.string().min(1).max(64)).optional()}); export const inNash = z.enum(["must", "aim_for", "report", "not_in_nash"]); export const valueScope = z.object({all_sites: z.boolean().optional(), site_id: z.string().min(1).max(64).nullable().optional()}); export const valueUnit = z.enum(["h", "min", "stops", "cube", "percent", "count", "trucks", "time_of_day", "window"]); export const nashValue = z.object({amount: z.union([z.number().int(), z.number()]).nullable().optional(), unit: valueUnit.nullable().optional(), text: z.string().min(1).max(200).nullable().optional()}); export const ruleStatus = z.enum(["confirmed", "assumed", "needs_answer"]); export const authoredValueStatus = z.enum(["assumed", "needs_answer"]); export const ruleValueRead = z.object({scope: valueScope, customer_value: z.string().min(1).max(2000).nullable(), nash_value: nashValue.nullable(), status: ruleStatus, authored_status: authoredValueStatus, status_reason: z.string().min(1).max(2000).nullable(), source_ids: z.array(z.string())}); export const sourceKind = z.enum(["sheet_cells", "file_columns", "document", "call", "written_answer", "nash_reading"]); export const cellExcerpt = z.object({ref: z.string(), value: z.string().min(1).max(2000)}); export const ruleSourceRead = z.object({id: z.string(), kind: sourceKind, label: z.string().min(1).max(200).nullable(), file_name: z.string().min(1).max(200).nullable(), sheet: z.string().min(1).max(200).nullable(), cells: z.array(z.string()), columns: z.array(z.string().min(1).max(200)), excerpt: z.array(cellExcerpt), locator: z.string().min(1).max(200).nullable(), url: z.string().max(2000).nullable(), title: z.string().min(1).max(200).nullable(), date: z.string().nullable(), author: z.string().min(1).max(200).nullable(), verbatim: z.boolean(), basis: z.string().min(1).max(2000).nullable(), verified: z.boolean()}); export const ruleStatusRead = z.object({state: ruleStatus, reason: z.string().nullable()}); export const confirmedBy = z.object({name: z.string().min(1).max(200), role: z.string().min(1).max(200).nullable().optional(), user_id: z.string().nullable().optional()}); export const confirmationChannel = z.enum(["call", "email", "written_answer", "meeting", "portal"]); export const actorType = z.enum(["person", "agent", "integration"]); export const actor = z.object({type: actorType, id: z.string(), display: z.string()}); export const actorAt = z.object({actor: actor, at: z.string()}); export const confirmationRead = z.object({id: z.string(), rule_id: z.string(), revision_id: z.string(), confirmed_by: confirmedBy, channel: confirmationChannel, confirmed_at: z.string(), source_url: z.string().nullable(), source_missing: z.boolean(), note: z.string().nullable(), recorded: actorAt}); export const ruleRead = z.object({id: z.string(), key: z.string(), aliases: z.array(z.string()), rule_book_id: z.string(), version: versionRef, revision_id: z.string(), revision_number: z.number().int(), added_in_version: z.number().int().nullable(), revised_in_version: z.number().int().nullable(), name: z.string().min(1).max(200), customer_words: customerWords.nullable(), applies_to: appliesTo, in_nash: inNash, how: z.string().min(1).max(2000).nullable(), values: z.array(ruleValueRead), sources: z.array(ruleSourceRead), status: ruleStatusRead, confirmations: z.array(confirmationRead), created: actorAt, updated: actorAt, etag: z.string()}); export const draftSummary = z.object({id: z.string(), number: z.number().int(), etag: z.string(), opened: actorAt, parent_version_number: z.number().int().nullable()}); export const ruleWriteResult = z.object({rule: ruleRead.nullable(), draft: draftSummary.nullable(), draft_opened: z.boolean()}); 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 }; /** * Edit Rule * * PATCH /v1/rule-books/{rule_book_id}/draft/rules/{rule} */ export const patchApiV1RuleBooksRuleBookIdDraftRulesRule = async (args: PatchApiV1RuleBooksRuleBookIdDraftRulesRuleArgs, options?: PatchApiV1RuleBooksRuleBookIdDraftRulesRuleOptions) => { const {baseUrl = "https://api.sandbox.usenash.com", 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/rule-books/${encodeURIComponent(String(args.rule_book_id))}/draft/rules/${encodeURIComponent(String(args.rule))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(ruleWriteResult, await res.json()) };