import {z} from 'zod' export type TransactionRulesSort = {orderBy?: ('LAST_APPLIED_DATE' | 'MATCH_COUNT') | 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 VendorIds = {in?: Array | undefined}; export type VendorFilter = {id?: VendorIds | undefined}; export type BankAccountIds = {in?: Array | undefined, notIn?: Array | undefined}; export type BankAccountFilter = {id?: BankAccountIds | undefined}; export type TransactionRuleConditionFilterParam = {description?: StringFilter | undefined, amount?: AmountFilter | undefined, vendorId?: VendorFilter | undefined, bankAccountId?: BankAccountFilter | undefined}; export type GlAccountIds = {in?: Array | undefined}; export type GlAccountFilter = {id?: GlAccountIds | undefined}; export type CustomerIds = {in?: Array | undefined}; export type CustomerFilter = {id?: CustomerIds | 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 DimensionValueIds = {in?: Array | undefined}; export type DimensionValueFilter = {id?: DimensionValueIds | undefined}; export type TransactionRuleActionFilterParam = {glAccountId?: GlAccountFilter | undefined, vendorId?: VendorFilter | undefined, customerId?: CustomerFilter | undefined, reviewStatus?: ReviewStatusListFilter | undefined, dimensionValueId?: DimensionValueFilter | undefined}; export type DateTimeFilter = {equal?: string | undefined, notEqual?: string | undefined, greaterThan?: string | undefined, greaterThanEqual?: string | undefined, lessThan?: string | undefined, lessThanEqual?: string | undefined}; export type TransactionRulesFilterParam = {status?: EnumStatusListFilter | undefined, filter?: TransactionRuleConditionFilterParam | undefined, update?: TransactionRuleActionFilterParam | undefined, lastAppliedDate?: DateTimeFilter | undefined, matchCount?: AmountFilter | undefined}; export type TransactionRulesGetParam = {cursor?: (string | null) | undefined, limit?: number | undefined, sort?: TransactionRulesSort | undefined, filter?: TransactionRulesFilterParam | undefined}; export type CreateApiV6BanksTransactionsRulesQueryArgs = {body: TransactionRulesGetParam}; export type CreateApiV6BanksTransactionsRulesQueryOptions = {baseUrl?: string | undefined, EmbedToken?: string | undefined, fetch?: typeof fetch | undefined}; export const descriptionCondition = z.object({equal: z.string().optional(), contain: z.string().optional()}); 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 idCondition = z.object({equal: z.string().optional()}); export const vendorCondition = z.object({id: idCondition.optional()}); export const glAccountCondition = z.object({id: idCondition.optional()}); export const bankAccountCondition = z.object({id: idCondition.optional()}); export const transactionRuleFilterDto = z.object({description: descriptionCondition.optional(), amount: amountCondition.optional(), vendor: vendorCondition.optional(), glAccount: glAccountCondition.optional(), bankAccount: bankAccountCondition.optional()}).nullable(); export const glAccountExpanded = z.object({id: z.string(), name: z.string().optional(), accountNo: z.string().optional(), type: z.enum(["ASSET", "LIABILITY", "EQUITY", "INCOME", "COGS", "EXPENSE", "OTHER_INCOME", "OTHER_EXPENSE", "SUSPENSE"]).optional(), iconUrl: z.string().optional(), displayName: z.string().optional()}); export const vendorExpanded = z.object({id: z.string(), name: z.string().optional()}).nullable(); export const customerExpanded = z.object({id: z.string(), name: z.string().optional(), email: z.string().optional()}).nullable(); export const dimensionExpanded = z.object({id: z.string(), name: z.string().optional(), requiredness: z.enum(["REQUIRED", "NOT_REQUIRED"]).optional()}); export const childDimensionValue: z.ZodType = z.object({id: z.string(), dimension: dimensionExpanded.optional(), name: z.string().optional(), child: z.lazy(() => childDimensionValue.optional())}); export const dimensionValueExpanded = z.object({id: z.string(), dimension: dimensionExpanded.optional(), name: z.string().optional(), child: childDimensionValue.optional()}); export const transactionRuleUpdateGetDto = z.object({glAccount: glAccountExpanded.nullable(), vendor: vendorExpanded.nullable(), customer: customerExpanded.nullable(), reviewStatus: z.enum(["RECONCILIATION_DISCREPANCY", "USER_REVIEW_REQUIRED", "ACCOUNTANT_REVIEW_REQUIRED", "PARTNER_REVIEW_REQUIRED", "AWAITING_MATCH", "STAGED", "RECONCILED"]).nullable(), dimensionValues: z.array(dimensionValueExpanded)}).nullable(); 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 transactionRuleGetDto = z.object({id: z.string(), status: z.enum(["ACTIVE", "INACTIVE", "REMOVED"]), filter: transactionRuleFilterDto, update: transactionRuleUpdateGetDto, matchCount: z.number().int(), lastAppliedDateTime: z.string(), filterDisplayName: z.string(), updateDisplayName: z.string(), customData: jsonNode.optional()}); export const responseCursor = z.object({after: z.string().optional(), before: z.string().optional()}); export const paginatedResponseTransactionRuleGetDto = z.object({result: z.enum(["SUCCESS", "FAILURE"]).optional(), data: z.array(transactionRuleGetDto).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 transaction rules * * POST /v6/banks/transactions/rules/query */ export const createApiV6BanksTransactionsRulesQuery = async (args: CreateApiV6BanksTransactionsRulesQueryArgs, options?: CreateApiV6BanksTransactionsRulesQueryOptions) => { const {baseUrl = "https://sandbox.tight.com", fetch: fetchFn = fetch, EmbedToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (EmbedToken !== undefined) headers.set('authorization', `Bearer ${EmbedToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v6/banks/transactions/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(paginatedResponseTransactionRuleGetDto, await res.json()) };