Skip to content

Commit

Permalink
Merge pull request #42 from MHaris-Ferrum/develop
Browse files Browse the repository at this point in the history
explore api fixed for block types
  • Loading branch information
AbdulAhadArain authored Sep 11, 2024
2 parents 33f9c6a + 796e51d commit c8d992f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 51 deletions.
47 changes: 46 additions & 1 deletion src/controllers/explorer.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Web3 from 'web3';
import { transactionsService, accountService, blockService } from '../services';
import { Request, Response, NextFunction } from 'express';
import { QuantumPortalTransactionModel } from '../models';

export const searchData = async (
req: Request,
Expand Down Expand Up @@ -105,7 +106,51 @@ export const searchData = async (
queryData,
);
} else if (type === 'blocks') {
response = await blockService.getAllBlocks(page, limit, req.query);
const blockQuery: any = {};
if (req.query.fromDate && req.query.toDate) {
blockQuery.$and = [
{ timestamp: { $gte: req.query.fromDate } },
{ timestamp: { $lte: req.query.toDate } },
];
} else if (req.query.fromDate) {
blockQuery.timestamp = req.query.fromDate;
} else if (req.query.toDate) {
blockQuery.timestamp = req.query.toDate;
}
const skip = (page - 1) * limit;
console.log({
...blockQuery,
$and: [
{ method: 'finalize' },
{ 'decodedInput.parameters.name': 'remoteChainId' },
{ 'decodedInput.parameters.value': req.query.fromNetwork },
],
});
const txs = await QuantumPortalTransactionModel.aggregate([
{
$match: {
...blockQuery,

$and: [
{ method: 'finalize' },
{ 'decodedInput.parameters.name': 'remoteChainId' },
{ 'decodedInput.parameters.value': req.query.fromNetwork },
],
},
}, // Match the initial query
{ $skip: skip }, // Pagination: skip the records for the previous pages
{ $limit: limit }, // Pagination: limit the results to the desired page size
]);

// Return only the unique block numbers

// console.log('txs', txs);
const blocksList = txs.map((tx: any) => tx.block);
// console.log('blocksList', blocksList);
response = await blockService.getAllBlocks(page, limit, {
number: { $in: blocksList },
});
// response = await blockService.getAllBlocks(page, limit, req.query);
}
}
res.send({ response });
Expand Down
52 changes: 2 additions & 50 deletions src/services/block.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,57 +83,9 @@ export const getAllBlocks = async (
limit: number,
queryData: any,
): Promise<any> => {
let aggregate: any = [];
const query: any = {};
if (queryData.fromDate && queryData.toDate) {
query.$and = [
{ timestamp: { $gte: queryData.fromDate } },
{ timestamp: { $lte: queryData.toDate } },
];
} else if (queryData.fromDate) {
query.timestamp = queryData.fromDate;
} else if (queryData.toDate) {
query.timestamp = queryData.toDate;
}
if (Object.keys(queryData).length) {
aggregate.push({
$match: {
...query,
},
});
}
if (queryData.fromNetwork) {
aggregate.push({
$lookup: {
from: 'quantumportaltransactions',
localField: 'number',
foreignField: 'block',
pipeline: [
{
$match: {
$and: [
{ method: 'finalize' },
{ 'decodedInput.parameters.value': queryData.fromNetwork },
{ 'decodedInput.parameters.name': 'remoteChainId' },
],
},
},
],
as: 'result',
},
});
}
if (queryData?.page && queryData?.limit) {
aggregate.push({
$skip: (queryData.page - 1) * queryData.limit,
});
aggregate.push({
$limit: queryData.limit,
});
}
const docsPromise = await QuantumPortalBlockModel.aggregate(aggregate);
const docsPromise = await QuantumPortalBlockModel.find(queryData);

const countPromise = QuantumPortalBlockModel.countDocuments(aggregate).exec();
const countPromise = QuantumPortalBlockModel.countDocuments(queryData).exec();

const [totalResults, results] = await Promise.all([
countPromise,
Expand Down

0 comments on commit c8d992f

Please sign in to comment.