From e424b80a661cf9310829e4a7c0e7614f63803a8d Mon Sep 17 00:00:00 2001 From: surbhit14 Date: Fri, 22 Nov 2024 16:17:18 +0530 Subject: [PATCH 1/2] Fix the pagination issue --- .../routes/operators/operatorController.ts | 45 +++++++------------ 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/packages/api/src/routes/operators/operatorController.ts b/packages/api/src/routes/operators/operatorController.ts index dc178cb5..c1683e95 100644 --- a/packages/api/src/routes/operators/operatorController.ts +++ b/packages/api/src/routes/operators/operatorController.ts @@ -445,35 +445,31 @@ export async function getOperatorEvents(req: Request, res: Response) { } } - let eventRecords: EventRecord[] = [] - let eventCount = 0 - const eventTypesToFetch = type ? [type] : strategyAddress ? ['SHARES_INCREASED', 'SHARES_DECREASED'] : ['SHARES_INCREASED', 'SHARES_DECREASED', 'DELEGATION', 'UNDELEGATION'] - const fetchEventsForTypes = async (types: string[]) => { - const results = await Promise.all( - types.map((eventType) => - fetchAndMapEvents(eventType, baseFilterQuery, withTokenData, withEthValue, skip, take) + const allEvents = ( + await Promise.all( + eventTypesToFetch.map((eventType) => + fetchAndMapEvents(eventType, baseFilterQuery, withTokenData, withEthValue) ) ) - return results - } + ).flatMap((result) => result.eventRecords) - const results = await fetchEventsForTypes(eventTypesToFetch) - - eventRecords = results.flatMap((result) => result.eventRecords) - eventRecords = eventRecords - .sort((a, b) => (b.blockNumber > a.blockNumber ? 1 : -1)) - .slice(0, take) + const sortedEvents = allEvents.sort((a, b) => { + if (b.blockNumber > a.blockNumber) return 1 + if (b.blockNumber < a.blockNumber) return -1 + return 0 + }) - eventCount = results.reduce((acc, result) => acc + result.eventCount, 0) + const paginatedEvents = sortedEvents.slice(skip, skip + take) + const eventCount = allEvents.length const response = { - data: eventRecords, + data: paginatedEvents, meta: { total: eventCount, skip, @@ -686,10 +682,8 @@ async function fetchAndMapEvents( eventType: string, baseFilterQuery: any, withTokenData: boolean, - withEthValue: boolean, - skip: number, - take: number -): Promise<{ eventRecords: EventRecord[]; eventCount: number }> { + withEthValue: boolean +): Promise<{ eventRecords: EventRecord[] }> { const modelName = (() => { switch (eventType) { case 'SHARES_INCREASED': @@ -707,14 +701,8 @@ async function fetchAndMapEvents( const model = prisma[modelName] as any - const eventCount = await model.count({ - where: baseFilterQuery - }) - const eventLogs = await model.findMany({ where: baseFilterQuery, - skip, - take, orderBy: { blockNumber: 'desc' } }) @@ -779,7 +767,6 @@ async function fetchAndMapEvents( ) return { - eventRecords, - eventCount + eventRecords } } From acc65d3a1117265a699c4dfed7bdd9ae29be1525 Mon Sep 17 00:00:00 2001 From: surbhit14 Date: Fri, 22 Nov 2024 16:27:16 +0530 Subject: [PATCH 2/2] Rename function to getOperatorDelegationEvents --- packages/api/src/routes/operators/operatorController.ts | 6 +++--- packages/api/src/routes/operators/operatorRoutes.ts | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/api/src/routes/operators/operatorController.ts b/packages/api/src/routes/operators/operatorController.ts index c1683e95..56df0b6b 100644 --- a/packages/api/src/routes/operators/operatorController.ts +++ b/packages/api/src/routes/operators/operatorController.ts @@ -389,13 +389,13 @@ export async function getOperatorRewards(req: Request, res: Response) { } /** - * Function for route /operators/:address/events - * Fetches and returns a list of events for a specific operator with optional filters + * Function for route /operators/:address/events/delegation + * Fetches and returns a list of delegation-related events for a specific operator with optional filters * * @param req * @param res */ -export async function getOperatorEvents(req: Request, res: Response) { +export async function getOperatorDelegationEvents(req: Request, res: Response) { const result = OperatorEventQuerySchema.and(PaginationQuerySchema).safeParse(req.query) if (!result.success) { return handleAndReturnErrorResponse(req, res, result.error) diff --git a/packages/api/src/routes/operators/operatorRoutes.ts b/packages/api/src/routes/operators/operatorRoutes.ts index 43053c45..3f0c9dca 100644 --- a/packages/api/src/routes/operators/operatorRoutes.ts +++ b/packages/api/src/routes/operators/operatorRoutes.ts @@ -4,7 +4,7 @@ import { getOperator, getAllOperatorAddresses, getOperatorRewards, - getOperatorEvents, + getOperatorDelegationEvents, invalidateMetadata } from './operatorController' import { authenticateJWT } from '../../utils/jwtUtils' @@ -109,7 +109,7 @@ router.get('/:address', routeCache.cacheSeconds(120), getOperator) router.get('/:address/rewards', routeCache.cacheSeconds(120), getOperatorRewards) -router.get('/:address/events/delegation', routeCache.cacheSeconds(120), getOperatorEvents) +router.get('/:address/events/delegation', routeCache.cacheSeconds(120), getOperatorDelegationEvents) // Protected routes router.get(