import {z} from 'zod' export type TaskPriorityListFilter = {in?: (Array<'URGENT' | 'HIGH' | 'MEDIUM' | 'LOW'>) | undefined, notIn?: (Array<'URGENT' | 'HIGH' | 'MEDIUM' | 'LOW'>) | undefined}; export type TaskTypeListFilter = {in?: (Array<'MESSAGE_REVIEW' | 'TRANSACTION_REVIEW' | 'BANK_ERROR' | 'RULE_REVIEW' | 'CSV_IMPORT_REVIEW'>) | undefined, notIn?: (Array<'MESSAGE_REVIEW' | 'TRANSACTION_REVIEW' | 'BANK_ERROR' | 'RULE_REVIEW' | 'CSV_IMPORT_REVIEW'>) | 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 TaskFilterParam = {priority?: TaskPriorityListFilter | undefined, type?: TaskTypeListFilter | undefined, status?: EnumStatusListFilter | undefined}; export type TaskGetParam = {cursor?: (string | null) | undefined, limit?: number | undefined, filter?: TaskFilterParam | undefined}; export type CreateApiV6TasksQueryArgs = {body: TaskGetParam}; export type CreateApiV6TasksQueryOptions = {baseUrl?: string | undefined, EmbedToken?: string | undefined, fetch?: typeof fetch | undefined}; export const taskGetDto = z.object({id: z.string(), name: z.string(), description: z.string(), shortDescription: z.string(), priority: z.enum(["URGENT", "HIGH", "MEDIUM", "LOW"]), type: z.enum(["MESSAGE_REVIEW", "TRANSACTION_REVIEW", "BANK_ERROR", "RULE_REVIEW", "CSV_IMPORT_REVIEW"]), dismissable: z.enum(["DISMISSABLE", "NON_DISMISSABLE"]), status: z.enum(["ACTIVE", "INACTIVE", "REMOVED"])}); export const responseCursor = z.object({after: z.string().optional(), before: z.string().optional()}); export const paginatedResponseTaskGetDto = z.object({result: z.enum(["SUCCESS", "FAILURE"]).optional(), data: z.array(taskGetDto).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 tasks * * POST /v6/tasks/query */ export const createApiV6TasksQuery = async (args: CreateApiV6TasksQueryArgs, options?: CreateApiV6TasksQueryOptions) => { 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/tasks/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(paginatedResponseTaskGetDto, await res.json()) };