Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 | 261x 110x 13x 13x 12x 12x 12x 13x 14x 14x 14x 14x 14x 13x 13x 13x 158x 5x 4x 4x 4x 4x 4x 4x 2x 2x 2x 1x 3x 1x 2x 2x 2x 2x 279x 279x 21x 13x 53x 1x 2x 1x 34x 68x 34x 17x 17x 17x 17x 688x 51x 51x 3x 5x 4x 21x 4x 4x 4x 4x 4x 4x 4x 2x 3x 3x 3x 3x 3x 3x 7x 3x 3x 5x 5x 5x 5x 5x 5x 5x 5x 5x 3x 1x 4x 3x 5x 5x 3x 5x 3x 1x 4x 3x 3x 5x 5x 1x 1x 3x 2x 3x 3x 261x 261x 1x 1x 4x 261x 5x 5x 5x 5x 5x 5x 261x 19x 1x 1x 261x 17x 17x 1x 1x 17x 2562x 58x 57x 53x 4x 261x 261x 1319x | import {
FilterChip,
SelectorOption,
} from "@pages/ManageMoney/Modal/filter.types";
import {
FilterPagesEnum,
pendingTransactionMap,
TRANSACTION_TRANSFER,
TRANSFER_RETURN_TYPE,
} from "./constants";
import { isEmpty } from "lodash";
import { TabNames } from "@features/Merchants/MerchantsTable/hooks/useTabs";
import { blockedAndQuarantinedFilter } from "@shared/constants";
import moment from "moment";
// The "Returned" transfer status chip value (maps to the TRANSFER_RETURN_TYPE
// transaction type imported from ./constants).
const RETURNED_TRANSFER_STATUS_VALUE = "returned";
function formatDateFilter({
dateRange,
endpointName,
timezone,
}: {
dateRange: string;
endpointName: string;
timezone?: string;
}) {
if (!dateRange) return "";
const [start, end] = dateRange.includes("-")
? dateRange.split("-")
: [dateRange, dateRange]; // Treat a single date as both start and end
// Function to get Unix timestamp in seconds with 'd' prefix
const getUnixTimestamp = (date: string, endOfDay = false) => {
const timestamp = new Date(date).getTime();
Iif (isNaN(timestamp)) return null;
return `d${Math.floor(timestamp / 1000) + (endOfDay ? 86399 : 0)}`;
};
const getTimezoneStamp = (dateString: string, end = false) => {
Iif (!timezone) return getUnixTimestamp(dateString, end);
const timezoneMoment = moment.tz(dateString, "MM/DD/YYYY", timezone);
const adjustedMoment = end
? timezoneMoment.endOf("day")
: timezoneMoment.startOf("day");
const utcTimestamp = adjustedMoment.utc().unix();
return `d${utcTimestamp}`;
};
const startDate = timezone
? getTimezoneStamp(start)
: getUnixTimestamp(start);
const endDate = timezone
? getTimezoneStamp(end, true)
: getUnixTimestamp(end, true); // End of the day
return `(${endpointName}:>=${startDate});(${endpointName}:<=${endDate})`;
}
function formatInflowOutFlowFilter({
flowRange,
endpointName,
isNegative = false,
multiplyBy = 100,
}: {
flowRange?: string;
endpointName?: string;
isNegative?: boolean;
multiplyBy?: number;
}): string {
if (!flowRange || !endpointName) return "";
// Special case: If flowRange is exactly "0", return (endpointName:0)
if (flowRange === "0") return `(${endpointName}:0)`;
Iif (flowRange === "<=0") return `(${endpointName}:<=0)`;
const hasRange = flowRange.includes("-");
let [min, max] = hasRange
? flowRange.split("-").map(Number)
: [Number(flowRange), undefined];
Eif (min > 0) min *= multiplyBy;
if (max !== undefined && max > 0) max *= multiplyBy;
if (isNegative) {
min = -min;
max = max !== undefined ? -max : undefined;
// If it's a single value and isNegative, return it as an upper bound (<=)
if (max === undefined) return `(${endpointName}:<=${min.toFixed(2)})`;
// Fix: Swap only if `max` is defined, otherwise keep min as is
[min, max] = [max, min];
}
// NEW LOGIC: if there's only one value, just use "="
if (max === undefined || isNaN(max)) {
return `(${endpointName}:${min.toFixed(2)})`;
}
// Otherwise, Add conditions for min and max
const filterParts = [];
Eif (!isNaN(min)) filterParts.push(`${endpointName}:>=${min.toFixed(2)}`);
Eif (!isNaN(max)) filterParts.push(`${endpointName}:<=${max.toFixed(2)}`);
return filterParts.length ? `(${filterParts.join(";")})` : "";
}
function formatArrayFilter({
array = [],
key,
value = null,
}: {
array: string[];
key: string;
value?: string | null;
}) {
Iif (value) return `(${key}:"${value}")`;
if (!array.length) return "";
const filterParts = array.map((status) => `${key}:"${status}"`);
return `(${filterParts.join(",")})`;
}
function formatArrayNegativeFilter({
array = [],
key,
}: {
array: string[];
key: string;
}) {
if (!array.length) return "";
const filterParts = array.map((status) => `${key}:!"${status}"`);
return `(${filterParts.join(",")})`;
}
function formatObjectArrayFilter({
array,
key,
}: {
array: SelectorOption[];
key: string;
}) {
if (!array.length) return "";
const filterParts = array.map((item) =>
typeof item.value === "number"
? `${key}:${item.value}`
: `${key}:"${item.value}"`,
);
return `(${filterParts.join(",")})`;
}
function formatObjectToTupleString(obj: Record<string, unknown>) {
const entries = Object.entries(obj)
.filter(([, value]) => value !== undefined)
.map(([key, value]) => `(${key}:${value})`);
Eif (entries.length === 0) return "";
return entries.join(";");
}
function formatAccountTypeFilter({
isProviderTransfers,
isMerchantTransfers,
key = "merchAccType",
}: {
isProviderTransfers?: boolean | null;
isMerchantTransfers?: boolean | null;
key?: string;
}) {
const parts: string[] = [];
// for this filter key the BE expect:
// acqurier => 1
// enterprise => 2
// submerchant => 3
Iif (isProviderTransfers) parts.push(`${key}:2`);
Iif (isMerchantTransfers) parts.push(`${key}:3`);
Eif (!parts.length) return "";
return `(${parts.join(",")})`;
}
function joinWithSemicolon(...strings: string[]) {
return strings.filter((str) => str).join(";");
}
function joinWithColon(...strings: string[]) {
const filteredArr = strings.filter(Boolean);
Eif (filteredArr.length < 1) return "";
return `(${filteredArr.filter((str) => str).join(",")})`;
}
function filterMap({
original,
stringArr,
}: {
original?: FilterChip[];
stringArr: string[];
}) {
return original
?.filter((item) => stringArr.includes(item.value.toLocaleString()))
.map((x) => x.typeName)
.filter(Boolean) as string[];
}
function handleDisplayStatusFilter({
purchase = [],
transfer = [],
originalTransfer,
filterPage,
}: {
[TRANSACTION_TRANSFER.TRANSACTION]?: string[];
[TRANSACTION_TRANSFER.TRANSFER]?: string[];
originalTransfer?: FilterChip[];
filterPage?: FilterPagesEnum;
}) {
if (isEmpty(purchase) && isEmpty(transfer)) return "";
const purchaseFilterString = generatePurchaseFilters({
purchase,
filterPage,
});
const transferFilterString = generateTransferFilters({
transfer,
originalTransfer,
filterPage,
});
const mergedFilter = [purchaseFilterString, transferFilterString]
.filter(Boolean)
.join(",");
return blockedAndQuarantinedFilter ? `(${mergedFilter})` : mergedFilter;
}
function generatePurchaseFilters({
purchase = [],
filterPage,
}: {
purchase?: string[];
filterPage?: FilterPagesEnum;
}) {
Eif (isEmpty(purchase)) return "";
const typeStateMap: Record<string, string[]> = {};
const stateTypeMap: Record<string, string[]> = {};
const typesWithoutStates: string[] = [];
const typeAndStateFilters: string[] = [];
const coveredPairs = new Set<string>();
// Build type-state and state-type maps
purchase.forEach((item) => {
const { type, processingState } = formatItem(item);
if (processingState) {
typeStateMap[type] = typeStateMap[type] || [];
if (!typeStateMap[type].includes(processingState))
typeStateMap[type].push(processingState);
stateTypeMap[processingState] = stateTypeMap[processingState] || [];
if (!stateTypeMap[processingState].includes(type))
stateTypeMap[processingState].push(type);
} else {
if (!typesWithoutStates.includes(type)) typesWithoutStates.push(type);
}
});
// Handle pending special case
if (
filterPage === FilterPagesEnum.MANAGE_MONEY &&
purchase.includes("purchase_pending")
) {
const pendingFilter = `(type:"purchase";(${pendingTransactionMap
.map((s) => `processingState:"${s}"`)
.join(",")}))`;
typeAndStateFilters.push(pendingFilter);
pendingTransactionMap.forEach((state) =>
coveredPairs.add(`purchase:${state}`),
);
delete stateTypeMap["pending"];
}
// Step 1: State-first grouping
Object.entries(stateTypeMap).forEach(([state, types]) => {
const uncovered = types.filter((t) => !coveredPairs.has(`${t}:${state}`));
if (uncovered.length >= 2) {
typeAndStateFilters.push(formatStateGroup(state, uncovered));
uncovered.forEach((t) => coveredPairs.add(`${t}:${state}`));
}
});
// Step 2: Type-first grouping
Object.entries(typeStateMap).forEach(([type, states]) => {
const uncovered = states.filter((s) => !coveredPairs.has(`${type}:${s}`));
if (uncovered.length >= 2) {
typeAndStateFilters.push(formatTypeGroup(type, uncovered));
uncovered.forEach((s) => coveredPairs.add(`${type}:${s}`));
}
});
// Step 3: Single pairs
Object.entries(typeStateMap).forEach(([type, states]) =>
states.forEach((s) => {
const key = `${type}:${s}`;
if (!coveredPairs.has(key)) {
typeAndStateFilters.push(`(type:"${type}";processingState:"${s}")`);
coveredPairs.add(key);
}
}),
);
// Types without states
if (typesWithoutStates.length) {
typeAndStateFilters.push(
`(${typesWithoutStates.map((t) => `type:"${t}"`).join(",")})`,
);
}
const finalFilter = typeAndStateFilters.join(",");
const finalQuarantinedFilter = finalFilter.includes("authorized")
? `${finalFilter};isQuarantined:!true`
: finalFilter;
return blockedAndQuarantinedFilter
? `(${finalQuarantinedFilter})`
: finalQuarantinedFilter;
}
function generateTransferFilters({
transfer = [],
originalTransfer,
filterPage,
}: {
transfer?: string[];
originalTransfer?: FilterChip[];
filterPage?: FilterPagesEnum;
}) {
Iif (isEmpty(transfer)) return "";
// Dedicated Transfers page: filters by processingSatetName against
// transfers_view (transfer-only rows). Preserve its original behavior and map
// every selected status straight through — the transfer_return remapping
// below is scoped to the Transactions / Manage Money surfaces, which query the
// transactions view where transfer_return rows exist.
if (filterPage === FilterPagesEnum.TRANSFERS) {
return transfer.map((state) => `processingSatetName:"${state}"`).join(",");
}
const typeStateMap: Record<string, string[]> = {};
const stateTypeMap: Record<string, string[]> = {};
const coveredPairs = new Set<string>();
const typeAndStateFilters: string[] = [];
// A "Returned" transfer is not a processingState on the transfer itself; it is
// modeled as a separate `transfer_return`-type transaction (the original
// transfer keeps its own status). So this chip maps to a type-only predicate
// instead of `type:"transfer";processingState:"returned"` (which matches
// nothing on the BE). Handle it separately from the state-based statuses.
const hasReturned = transfer.includes(RETURNED_TRANSFER_STATUS_VALUE);
const stateBasedTransfers = transfer.filter(
(item) => item !== RETURNED_TRANSFER_STATUS_VALUE,
);
// Extract transfer types from filter chips if available
const transferTypesFromFilterMap = filterMap({
original: originalTransfer,
stringArr: stateBasedTransfers,
});
stateBasedTransfers.forEach((item) => {
const { type, processingState } = formatItem(item);
const finalType = processingState ? type : "transfer";
const finalState = processingState || item;
typeStateMap[finalType] = typeStateMap[finalType] || [];
Eif (!typeStateMap[finalType].includes(finalState))
typeStateMap[finalType].push(finalState);
stateTypeMap[finalState] = stateTypeMap[finalState] || [];
Eif (!stateTypeMap[finalState].includes(finalType))
stateTypeMap[finalState].push(finalType);
});
// Add extracted transfer types to the map
if (transferTypesFromFilterMap) {
transferTypesFromFilterMap.forEach((typeName) => {
// These types from filter chips need to be tracked
typeStateMap[typeName] = typeStateMap[typeName] || [];
});
}
// State-first grouping
Object.entries(stateTypeMap).forEach(([state, types]) => {
const uncovered = types.filter((t) => !coveredPairs.has(`${t}:${state}`));
Iif (uncovered.length >= 2) {
typeAndStateFilters.push(formatStateGroup(state, uncovered));
uncovered.forEach((t) => coveredPairs.add(`${t}:${state}`));
}
});
// Type-first grouping
Object.entries(typeStateMap).forEach(([type, states]) => {
const uncovered = states.filter((s) => !coveredPairs.has(`${type}:${s}`));
if (uncovered.length >= 2) {
typeAndStateFilters.push(formatTypeGroup(type, uncovered));
uncovered.forEach((s) => coveredPairs.add(`${type}:${s}`));
}
});
// Single pairs
Object.entries(typeStateMap).forEach(([type, states]) =>
states.forEach((s) => {
const key = `${type}:${s}`;
if (!coveredPairs.has(key)) {
typeAndStateFilters.push(`(type:"${type}";processingState:"${s}")`);
coveredPairs.add(key);
}
}),
);
// Returned transfers are a distinct transaction type, so OR the type-only
// predicate in alongside any state-based transfer statuses.
if (hasReturned) {
typeAndStateFilters.push(`(type:"${TRANSFER_RETURN_TYPE}")`);
}
const finalFilter = typeAndStateFilters.join(",");
return blockedAndQuarantinedFilter ? `(${finalFilter})` : finalFilter;
}
/** Groups multiple types under one state (state-first grouping) */
const formatStateGroup = (state: string, types: string[]) => {
if (!types?.length) return "";
return types.length === 1
? `(type:"${types[0]}";processingState:"${state}")`
: `((${types
.map((t) => `type:"${t}"`)
.join(",")});processingState:"${state}")`;
};
/** Groups multiple states under one type (type-first grouping) */
const formatTypeGroup = (type: string, states: string[]) => {
Iif (!states?.length) return "";
return states.length === 1
? `(type:"${type}";processingState:"${states[0]}")`
: `(type:"${type}";(${states
.map((s) => `processingState:"${s}"`)
.join(",")}))`;
};
const formatItem = (item: string) => {
Iif (item === "chargeback_reversal") return { type: item };
let parts = [item];
Iif (item.includes("_")) {
const lastIndex = item.lastIndexOf("_");
parts = [item.substring(0, lastIndex), item.substring(lastIndex + 1)];
}
const type = parts.length > 1 ? parts[0] : item;
const state = parts.length > 1 ? parts.slice(1).join("_") : undefined;
return { type, processingState: state };
};
const checkBoolean = ({
value,
endpointName,
validatorStr,
}: {
value: string;
endpointName: string;
validatorStr?: string;
}) => {
if (!value) return "";
Eif (validatorStr) {
return `(${endpointName}:${
value?.toLocaleLowerCase() === validatorStr?.toLocaleLowerCase()
})`;
}
return `(${endpointName}:${value})`;
};
const formatAssigneeFilter = ({
assignees,
tab,
}: {
assignees: SelectorOption[];
tab: string;
}) => {
let assigneeEmail = "";
switch (tab) {
case "disputes":
assigneeEmail = "defaultAssignee.email";
break;
case TabNames.kyb:
assigneeEmail = "merchantAssignees.kybAssigneeEmail";
break;
case TabNames.underwriting:
assigneeEmail = "merchantAssignees.underwritingAssigneeEmail";
break;
case TabNames.sponsor:
assigneeEmail = "merchantAssignees.sponsorAssigneeEmail";
break;
}
Eif (!assignees.length || !assigneeEmail) return "";
const filterParts = assignees.map(
(item) => `${assigneeEmail}:"${item.value}"`,
);
return `(${filterParts.join(",")})`;
};
export {
formatDateFilter,
formatInflowOutFlowFilter,
formatArrayFilter,
formatArrayNegativeFilter,
formatObjectArrayFilter,
joinWithSemicolon,
checkBoolean,
handleDisplayStatusFilter,
formatObjectToTupleString,
formatAccountTypeFilter,
formatAssigneeFilter,
joinWithColon,
};
export function countNonEmptyValues(
data: Record<string, unknown>,
excludeKeys: string[] = [],
): number {
return Object.entries(data).reduce((count, [key, value]) => {
if (excludeKeys.includes(key)) return count; // Skip excluded keys
if (Array.isArray(value)) {
return value.length > 0 ? count + 1 : count;
}
return value || typeof value === "boolean" ? count + 1 : count;
}, 0);
}
const FILTER_LABEL = "Filter";
// Applied count in parentheses — the portal-wide filter-button convention.
export const filterButtonLabel = (filtersAmount: number) =>
filtersAmount > 0 ? `Filters (${filtersAmount})` : FILTER_LABEL;
|