Skip to content

Commit

Permalink
search api updated for filter
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdulAhadArain committed Jul 26, 2023
1 parent 8ca6ea7 commit 72be3d2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/controllers/explorer.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,47 @@ export const searchData = async (
next: NextFunction,
): Promise<void> => {
try {
const queryData: any = {};
if (req.query.fromNetwork) {
queryData.networkId = req.query.fromNetwork;
}
if (req.query.toNetwork) {
queryData.remoteNetworkId = req.query.toNetwork;
}
if (req.query.fromDate && req.query.tDate) {
queryData.$and = [
{ timestamp: { $gte: req.query.fromDate } },
{ timestamp: { $lte: req.query.toDate } },
];
} else if (req.query.fromDate) {
queryData.timestamp = { $gte: req.query.fromDate };
} else if (req.query.toDate) {
queryData.timestamp = { $lte: req.query.toDate };
}
if (
Web3.utils.isHex(req.query.data as string) &&
isTransactionOrBlockHash(req.query.data as string)
) {
let hash;
hash = await transactionsService.getTransaction(req.query.data as string);
if (!hash) {
hash = await blockService.getBlockByBlockHash(req.query.data as string);
hash = await transactionsService.getTransactionByQuery({
...queryData,
hash: req.query.data,
});
if (!hash.length) {
hash = await blockService.getBlockByQuery({
...queryData,
blockHash: req.query.data,
});
}
if (!hash) {
next(new ApiError(httpStatus.BAD_REQUEST, 'Not Found'));
if (!hash.length) {
return next(new ApiError(httpStatus.BAD_REQUEST, 'Not Found'));
}
res.send(hash);
} else if (Web3.utils.isAddress(req.query.data as string)) {
const account = await accountService.getAccount(req.query.data as string);
res.send(account);
} else {
next(new ApiError(httpStatus.BAD_REQUEST, 'Not Found'));
return next(new ApiError(httpStatus.BAD_REQUEST, 'Not Found'));
}
} catch (error) {
next(error);
Expand Down
7 changes: 7 additions & 0 deletions src/services/block.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ export const getBlockByBlockHash = async (
return block;
};

export const getBlockByQuery = async (
query: Object,
): Promise<QuantumPortalMinedBlockDocument[]> => {
const block = await QuantumPortalMinedBlockModel.find(query);
return block;
};

export const getBlockTxsByBlockHash = async (
blockHash: string,
page: number,
Expand Down
7 changes: 7 additions & 0 deletions src/services/transaction.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,10 @@ export const getTransaction = async (
});
return tx;
};

export const getTransactionByQuery = async (
query: Object,
): Promise<QuantumPortalRemoteTransaction[]> => {
const tx = await QuantumPortalRemoteTransactionModel.find(query);
return tx;
};
4 changes: 4 additions & 0 deletions src/validations/explorer.validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ import Joi from 'joi';
export const searchData = {
query: Joi.object().keys({
data: Joi.string().required(),
fromNetwork: Joi.string(),
toNetwork: Joi.string(),
fromDate: Joi.number(),
toDate: Joi.number(),
}),
};

0 comments on commit 72be3d2

Please sign in to comment.