import {z} from 'zod' export type TemplateRulesSort = {orderBy?: ('LAST_APPLIED_DATE' | 'MATCH_COUNT' | 'STATUS') | undefined, direction?: ('ASC' | 'DESC') | undefined}; export type EnumStatusListFilter = {in?: (Array<'ACTIVE' | 'INACTIVE' | 'REMOVED'>) | undefined, notIn?: (Array<'ACTIVE' | 'INACTIVE' | 'REMOVED'>) | undefined, equal?: ('ACTIVE' | 'INACTIVE' | 'REMOVED') | undefined, notEqual?: ('ACTIVE' | 'INACTIVE' | 'REMOVED') | undefined, greaterThan?: ('ACTIVE' | 'INACTIVE' | 'REMOVED') | undefined, greaterThanEqual?: ('ACTIVE' | 'INACTIVE' | 'REMOVED') | undefined, lessThan?: ('ACTIVE' | 'INACTIVE' | 'REMOVED') | undefined, lessThanEqual?: ('ACTIVE' | 'INACTIVE' | 'REMOVED') | undefined}; export type StringFilter = {equal?: string | undefined, notEqual?: string | undefined, greaterThan?: string | undefined, greaterThanEqual?: string | undefined, lessThan?: string | undefined, lessThanEqual?: string | undefined, contain?: string | undefined}; export type AmountFilter = {equal?: number | undefined, notEqual?: number | undefined, greaterThan?: number | undefined, greaterThanEqual?: number | undefined, lessThan?: number | undefined, lessThanEqual?: number | undefined}; export type BusinessTypeIds = {in?: Array | undefined}; export type BusinessTypeFilter = {id?: BusinessTypeIds | undefined}; export type TemplateGlAccountIds = {in?: Array | undefined}; export type TemplateGlAccountFilter = {id?: TemplateGlAccountIds | undefined}; export type GlobalVendorNameFilter = {equal?: string | undefined, notEqual?: string | undefined}; export type TemplateRuleConditionFilterParam = {description?: StringFilter | undefined, amount?: AmountFilter | undefined, businessType?: BusinessTypeFilter | undefined, templateGlAccount?: TemplateGlAccountFilter | undefined, vendor?: GlobalVendorNameFilter | undefined}; export type ReviewStatusListFilter = {in?: (Array<'RECONCILIATION_DISCREPANCY' | 'USER_REVIEW_REQUIRED' | 'ACCOUNTANT_REVIEW_REQUIRED' | 'PARTNER_REVIEW_REQUIRED' | 'AWAITING_MATCH' | 'STAGED' | 'RECONCILED'>) | undefined, notIn?: (Array<'RECONCILIATION_DISCREPANCY' | 'USER_REVIEW_REQUIRED' | 'ACCOUNTANT_REVIEW_REQUIRED' | 'PARTNER_REVIEW_REQUIRED' | 'AWAITING_MATCH' | 'STAGED' | 'RECONCILED'>) | undefined, equal?: ('RECONCILIATION_DISCREPANCY' | 'USER_REVIEW_REQUIRED' | 'ACCOUNTANT_REVIEW_REQUIRED' | 'PARTNER_REVIEW_REQUIRED' | 'AWAITING_MATCH' | 'STAGED' | 'RECONCILED') | undefined, notEqual?: ('RECONCILIATION_DISCREPANCY' | 'USER_REVIEW_REQUIRED' | 'ACCOUNTANT_REVIEW_REQUIRED' | 'PARTNER_REVIEW_REQUIRED' | 'AWAITING_MATCH' | 'STAGED' | 'RECONCILED') | undefined, greaterThan?: ('RECONCILIATION_DISCREPANCY' | 'USER_REVIEW_REQUIRED' | 'ACCOUNTANT_REVIEW_REQUIRED' | 'PARTNER_REVIEW_REQUIRED' | 'AWAITING_MATCH' | 'STAGED' | 'RECONCILED') | undefined, greaterThanEqual?: ('RECONCILIATION_DISCREPANCY' | 'USER_REVIEW_REQUIRED' | 'ACCOUNTANT_REVIEW_REQUIRED' | 'PARTNER_REVIEW_REQUIRED' | 'AWAITING_MATCH' | 'STAGED' | 'RECONCILED') | undefined, lessThan?: ('RECONCILIATION_DISCREPANCY' | 'USER_REVIEW_REQUIRED' | 'ACCOUNTANT_REVIEW_REQUIRED' | 'PARTNER_REVIEW_REQUIRED' | 'AWAITING_MATCH' | 'STAGED' | 'RECONCILED') | undefined, lessThanEqual?: ('RECONCILIATION_DISCREPANCY' | 'USER_REVIEW_REQUIRED' | 'ACCOUNTANT_REVIEW_REQUIRED' | 'PARTNER_REVIEW_REQUIRED' | 'AWAITING_MATCH' | 'STAGED' | 'RECONCILED') | undefined}; export type TemplateRuleActionFilterParam = {reviewStatus?: ReviewStatusListFilter | undefined, templateGlAccount?: TemplateGlAccountFilter | undefined, vendor?: GlobalVendorNameFilter | undefined}; export type DateTimeFilter = {equal?: string | undefined, notEqual?: string | undefined, greaterThan?: string | undefined, greaterThanEqual?: string | undefined, lessThan?: string | undefined, lessThanEqual?: string | undefined}; export type TemplateRulesFilterParam = {status?: EnumStatusListFilter | undefined, filter?: TemplateRuleConditionFilterParam | undefined, update?: TemplateRuleActionFilterParam | undefined, lastAppliedDate?: DateTimeFilter | undefined, matchCount?: AmountFilter | undefined}; export type TemplateRulesGetParam = {cursor?: (string | null) | undefined, limit?: number | undefined, sort?: TemplateRulesSort | undefined, filter?: TemplateRulesFilterParam | undefined}; export type CreateApiV6TemplatesRulesQueryArgs = {body: TemplateRulesGetParam}; export type CreateApiV6TemplatesRulesQueryOptions = {baseUrl?: string | undefined, PartnerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const amountCondition = z.object({equal: z.number().int().optional(), greaterThan: z.number().int().optional(), greaterThanEqual: z.number().int().optional(), lessThan: z.number().int().optional(), lessThanEqual: z.number().int().optional()}); export const descriptionCondition = z.object({equal: z.string().optional(), contain: z.string().optional()}); export const idCondition = z.object({equal: z.string().optional()}); export const businessTypeCondition = z.object({id: idCondition.optional()}); export const templateGlAccountCondition = z.object({id: idCondition.optional()}); export const templateVendorCondition = z.object({name: z.string()}); export const filter = z.object({amount: amountCondition.optional(), description: descriptionCondition.optional(), businessType: businessTypeCondition.optional(), templateGlAccount: templateGlAccountCondition.optional(), vendorName: templateVendorCondition.optional()}); export const update = z.object({reviewStatus: z.enum(["RECONCILIATION_DISCREPANCY", "USER_REVIEW_REQUIRED", "ACCOUNTANT_REVIEW_REQUIRED", "PARTNER_REVIEW_REQUIRED", "AWAITING_MATCH", "STAGED", "RECONCILED"]).optional(), templateGlAccountId: z.string().optional(), vendorName: z.string().optional()}); export const jsonNode = z.object({empty: z.boolean().optional(), array: z.boolean().optional(), null: z.boolean().optional(), object: z.boolean().optional(), float: z.boolean().optional(), string: z.boolean().optional(), number: z.boolean().optional(), missingNode: z.boolean().optional(), valueNode: z.boolean().optional(), nodeType: z.enum(["ARRAY", "BINARY", "BOOLEAN", "MISSING", "NULL", "NUMBER", "OBJECT", "POJO", "STRING"]).optional(), pojo: z.boolean().optional(), integralNumber: z.boolean().optional(), floatingPointNumber: z.boolean().optional(), short: z.boolean().optional(), int: z.boolean().optional(), long: z.boolean().optional(), double: z.boolean().optional(), bigDecimal: z.boolean().optional(), bigInteger: z.boolean().optional(), textual: z.boolean().optional(), boolean: z.boolean().optional(), binary: z.boolean().optional(), container: z.boolean().optional(), embeddedValue: z.boolean().optional()}).nullable(); export const templateRuleGetDto = z.object({id: z.string(), status: z.enum(["ACTIVE", "INACTIVE", "REMOVED"]), filter: filter, update: update, customData: jsonNode.optional()}); export const responseCursor = z.object({after: z.string().optional(), before: z.string().optional()}); export const paginatedResponseTemplateRuleGetDto = z.object({result: z.enum(["SUCCESS", "FAILURE"]).optional(), data: z.array(templateRuleGetDto).optional(), cursor: responseCursor.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 }; /** * Query template rules * * POST /v6/templates/rules/query */ export const createApiV6TemplatesRulesQuery = async (args: CreateApiV6TemplatesRulesQueryArgs, options?: CreateApiV6TemplatesRulesQueryOptions) => { const {baseUrl = "https://sandbox.tight.com", fetch: fetchFn = fetch, PartnerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (PartnerAuth !== undefined) headers.set('authorization', `Bearer ${PartnerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v6/templates/rules/query${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(paginatedResponseTemplateRuleGetDto, await res.json()) };