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 | 515x 111x 111x 111x 111x 105x 105x 111x 111x 111x 111x 104x 111x 1x 111x 111x 515x 759x 108x 108x 108x 108x 108x 33x 13x 515x 155x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 125x 143x 143x 143x 126x 121x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 143x 5x 143x 143x 16x 127x 124x 143x 143x 167x 143x 97x 46x 46x 143x 29x 143x 143x 6x 143x 121x 57x 515x 121x 104x 47x 47x 47x 47x 33x 33x 33x 14x 17x 10x 515x 515x 47x | import { customInstance } from "@services/api";
import { buildMerchantEndpoints } from "@services/api/utils.api";
import {
blockedAndQuarantinedtnx,
encodedBlockedAndQuarantinedFilter,
} from "@shared/constants";
import { MutableRefObject } from "react";
export const prepareBasePath = (
queryParams: any,
shouldAddPageParam = true,
pageParam?: number,
) => {
const {
sorting,
path,
rowsPerPage,
queryString,
searchQuery,
filter,
max,
isQueryParam,
} = queryParams;
const sortQuery = sorting ? `sort=${sorting}` : "";
let basePath = `${path ?? ""}?${sortQuery}${
shouldAddPageParam ? `&page=${pageParam}&max=${rowsPerPage}` : ""
}`;
const appendParam = (param: string) => {
Iif (!param) return;
basePath += basePath.includes("?") ? `&${param}` : `?${param}`;
};
const filters = [];
if (queryString) filters.push(queryString);
if (filter) filters.push(filter);
if (filters.length > 0) {
appendParam(
isQueryParam ? `${filters.join("")}` : `filter=${filters.join("")}`,
);
}
if (searchQuery) {
appendParam(`q="${encodeURIComponent(searchQuery)}"`);
}
// if page param is not added we should send total amount of merchants to avoid default limit of 100
Iif (max && !shouldAddPageParam) {
appendParam(`max=${max}`);
}
return basePath;
};
export const queryBuilder = (queryParams: any) => {
return async ({
pageParam = 1,
signal,
}: {
pageParam?: number;
signal?: AbortSignal;
}) => {
const currentPage = pageParam ?? 1;
const basePath = prepareBasePath(queryParams, true, currentPage);
const pathParamId = queryParams.merchantId;
const url = pathParamId
? buildMerchantEndpoints(basePath, pathParamId)
: basePath;
const data = await customInstance({
url,
method: "GET",
signal,
});
const numberOfPages = Math.ceil(
Number(data.total ?? 0) / (queryParams?.rowsPerPage || 20),
);
return {
data: data.data,
nextCursor: numberOfPages >= currentPage + 1 ? currentPage + 1 : null,
total: data.total,
};
};
};
export type PaginationMethodType = "offset" | "cursor";
export type CursorValue = {
sortValue: any;
idValue: any;
};
export type QueryFunctionBuilderParamsType = {
sorting: string;
path: string;
rowsPerPage?: number;
queryString?: string;
searchQuery?: string;
filter?: string;
merchantId?: number | string;
paginationMethod?: PaginationMethodType;
cursorPropertyName?: string;
sortPropertyName?: string;
nextCursorValue?: MutableRefObject<CursorValue | null>;
pageRef?: MutableRefObject<number | null>;
isMembership?: boolean;
isEnterprisePortal?: boolean;
} & (
| {
paginationMethod: "cursor";
cursorPropertyName: string;
nextCursorValue: MutableRefObject<CursorValue | null>;
}
| { paginationMethod?: "offset" }
);
export const queryFunctionBuilder =
(queryParams: QueryFunctionBuilderParamsType) =>
async ({ pageParam, signal }: any) => {
const merchantId = queryParams?.merchantId;
const {
sorting,
path,
rowsPerPage = 20,
queryString,
searchQuery,
filter,
paginationMethod = "offset",
nextCursorValue,
cursorPropertyName,
sortPropertyName: sortPropertyNameParam,
isMembership,
isEnterprisePortal,
} = queryParams;
// Cursor filter must include the sort field so pagination matches sort order
const sortPropertyName =
sortPropertyNameParam ||
(paginationMethod === "cursor" && sorting
? sorting.replace(/^-/, "")
: undefined);
let sortQuery = sorting ? `sort=${sorting}` : "";
const isTransfers = path === "transfers";
const isTransactionMinimal = path === "transactions-minimal";
const transactionItemsPath = path.includes("transaction-items");
const transactionTablePath = path.includes("transaction-settlements/");
const isTransactionItemWithCustomFilter = !!filter && transactionItemsPath;
if (paginationMethod === "cursor") {
sortQuery = sorting
? `sort=${sorting},-${cursorPropertyName}`
: `-${cursorPropertyName}`;
}
const pathPageParam =
paginationMethod === "offset" ? `&page=${pageParam ?? 1}` : "";
let basePath = `${path}?${sortQuery}${pathPageParam}&max=${rowsPerPage}`;
const addCursorFilter = (currFilter?: string) => {
// Check for null/undefined explicitly to allow 0 as a valid cursor value
if (
paginationMethod !== "cursor" ||
pageParam === null ||
pageParam === undefined
)
return "";
const prefix = currFilter?.endsWith("%3B") ? "" : "%3B";
const comparisonOperator = sorting?.startsWith("-") ? "<" : ">";
const { sortValue, idValue } = pageParam;
const hasSortValue =
sortValue !== undefined && sortValue !== null && sortValue !== "";
const cursorString = `${cursorPropertyName}:<${idValue}`; // cursorPropertyName is always used with -, so the operator should be <
const dateSortHelper = dateSortKeys.includes(sortPropertyName || "")
? "d"
: "";
// String values must be quoted or the API fails to parse
const formatCursorValue = (v: unknown): string =>
typeof v === "string" ? `%22${encodeURIComponent(v)}%22` : String(v);
const encodedSortValue = hasSortValue ? formatCursorValue(sortValue) : "";
// Cursor must match sort: (sortField op value) OR (sortField = value AND id < idValue).
// API uses comma for OR, semicolon for AND; a single (a;b) is AND, so we need (clause1),(clause2).
Eif (sortPropertyName && hasSortValue) {
const clause1 = `${sortPropertyName}:${comparisonOperator}${dateSortHelper}${encodedSortValue}`;
const clause2 = `${sortPropertyName}:${dateSortHelper}${encodedSortValue}%3B${cursorString}`;
return `${prefix}((${clause1})%2C(${clause2}))`;
}
return `${prefix}(${cursorString})`;
};
if (searchQuery) {
basePath += `&q="${encodeURIComponent(searchQuery)}"`;
}
//GB-20253 for provider transfers and transaction tab we should filter out the reserve transfers
const key = (() => {
if (isTransfers) {
return "transactionTypeName";
}
if (isTransactionMinimal) return "txnType";
return "type";
})();
const providerTransferBaseFilter = isEnterprisePortal
? `(${key}:!"reserve_release"%3B${key}:!"reserve_deposit")`
: "";
//
const mergedFilter = [queryString, filter, providerTransferBaseFilter]
.filter(Boolean)
.map((f) => `${f}`)
.join("%3B");
if (mergedFilter && !isTransactionItemWithCustomFilter) {
basePath += `&filter=(${mergedFilter}${addCursorFilter(mergedFilter)})`;
} else {
Iif (queryString) {
basePath += `&filter=${queryString}${addCursorFilter(queryString)}`;
}
Iif (filter && !isTransactionItemWithCustomFilter) {
basePath += `&filter=${filter}${addCursorFilter(filter)}`;
}
}
if (!mergedFilter && !queryString && paginationMethod === "cursor") {
basePath += `&filter=${addCursorFilter()}`;
}
Iif (transactionItemsPath) {
basePath += `&filter=(${
filter ? filter + "%3B" : ""
}${blockedAndQuarantinedtnx})`;
}
if (transactionTablePath) {
basePath += `&filter=(${encodedBlockedAndQuarantinedFilter}${
isMembership ? "%3BisAutomaticallyAdded:false" : ""
})`;
}
const data = await customInstance({
url: buildMerchantEndpoints(basePath, merchantId),
method: "GET",
signal,
});
const nextCursor = getNextCursor({
data,
paginationMethod,
currentPageParam: pageParam,
rowsPerPage,
nextCursorValue,
cursorPropertyName,
sortPropertyName,
sorting,
});
return {
data: data.data,
nextCursor,
total: data.total,
};
};
const getNextCursor = ({
data,
currentPageParam = 1,
rowsPerPage,
paginationMethod,
nextCursorValue,
cursorPropertyName = "id",
sortPropertyName,
sorting,
}: {
data: any;
paginationMethod: QueryFunctionBuilderParamsType["paginationMethod"];
rowsPerPage: number;
currentPageParam: number;
nextCursorValue?: MutableRefObject<CursorValue | null>;
cursorPropertyName?: string;
sortPropertyName?: string;
sorting?: string;
}) => {
if (paginationMethod === "cursor" && nextCursorValue) {
const dataArr = data.data;
Iif (!Array.isArray(dataArr) || dataArr.length === 0) return null;
// Pick the item that is last in current sort order (primary + secondary -id tie-break)
const lastItem = getLastCursorItem({
data: dataArr,
});
const isLastPage = dataArr.length < rowsPerPage;
if (lastItem && !isLastPage) {
const cursorValue: CursorValue = {
sortValue: sortPropertyName ? lastItem[sortPropertyName] : "",
idValue: lastItem[cursorPropertyName],
};
// Update the ref for use in the component
nextCursorValue.current = cursorValue;
return cursorValue;
}
return null;
}
const numberOfPages = Math.ceil(Number(data.total ?? 0) / rowsPerPage);
return numberOfPages >= currentPageParam + 1 ? currentPageParam + 1 : null;
};
const dateSortKeys = ["createdAt", "updatedAt"];
const getLastCursorItem = ({ data }: { data: any[] }) => {
return data[data.length - 1];
};
|