Skip to content

Commit

Permalink
chore: validate search strategies on frontend and backend
Browse files Browse the repository at this point in the history
  • Loading branch information
rabi-siddique committed Nov 22, 2024
1 parent 89930b8 commit 3169236
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
26 changes: 20 additions & 6 deletions controllers/heightController.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
import path from 'path';
import { fileURLToPath } from 'url';
import { processAndConvert } from '../services/fileProcessor.js';
Expand All @@ -9,16 +10,29 @@ const __dirname = path.dirname(__filename);

const uploadDir = 'uploads';

const validStrategies = new Set(['blockHeight', 'txHash', 'searchTerm']);
const isValidStrategy = (strategy) => {
return validStrategies.has(strategy);
};

export const handleHeightLogs = async (req, res) => {
const { height, network } = req.body;
if (!height) {
return res.status(400).json({ message: 'Height is required.' });
const { search, network, strategy } = req.body;
console.log(req.body);

if (!isValidStrategy(strategy)) {
return res.status(400).json({
message: 'Invalid strategy selected',
});
}

if (!search) {
return res.status(400).json({ message: 'Search input is required.' });
}
if (!network || !networks[network]) {
return res.status(400).json({ error: 'Bad Request: Network not found' });
return res.status(400).json({ message: 'Bad Request: Network not found' });
}

console.log(`height:${height} AND AGORIC_NET:${network}`);
console.log(`SearchInput:${search} AND AGORIC_NET:${network}`);

const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9);
const inputFile = path.join(
Expand All @@ -42,7 +56,7 @@ export const handleHeightLogs = async (req, res) => {
`;

await fetchAndStoreHeightLogs({
blockHeight: height,
blockHeight: search,
inputFile,
network,
queryfilter,
Expand Down
12 changes: 6 additions & 6 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@
</div>

<div id="searchFormBottom">
<div class="searchInput" id="blockHeightInput">
<input type="number" id="blockHeight" name="blockHeight" required>
<div id="blockHeightInput">
<input class="searchInput" type="number" id="blockHeight" name="blockHeight">
<div class="spinner"></div>
</div>

<div class="searchInput" id="txHashInput">
<input type="text" id="txHash" name="txHash" required>
<div id="txHashInput">
<input class="searchInput" type="text" id="txHash" name="txHash">
</div>

<div class="searchInput" id="searchTermInput">
<input type="text" id="searchTerm" name="searchTerm" required>
<div id="searchTermInput">
<input class="searchInput" type="text" id="searchTerm" name="searchTerm">
</div>

<div id="spinnerSearchForm" class="spinner"></div>
Expand Down
26 changes: 20 additions & 6 deletions public/scripts/submitSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,40 @@ searchTypeSelect.addEventListener('change', () => {
}
});

const validStrategies = new Set(['blockHeight', 'txHash', 'searchTerm']);

const isValidStrategy = (strategy) => {
if (!validStrategies.has(strategy)) {
return false;
}
return true;
};

document.getElementById('searchForm').addEventListener('submit', async (e) => {
e.preventDefault();

const searchInputValue = document.querySelector('.searchInput').value;
const searchStrategy = document.getElementById('searchType').value;
const search = document.querySelector('.searchInput').value;
const strategy = document.getElementById('searchType').value;
const spinner = document.getElementById('spinnerSearchForm');
const submitButton = document.getElementById('submitSearch');
const submitButton = document.getElementById('submitSearchButton');
const network = document.getElementById('networkSelect').value;

spinner.style.display = 'inline-block';
submitButton.style.visibility = 'hidden';
try {
if (!isValidStrategy(strategy)) {
throw new Error(`Invalid strategy selected: ${strategy}`);
}

const response = await fetch('/submit-height', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
searchInputValue,
search,
network,
strategy: searchStrategy,
strategy,
}),
});

Expand All @@ -47,7 +60,8 @@ document.getElementById('searchForm').addEventListener('submit', async (e) => {
svgElement.src = url;
svgElement.style.display = 'inline-block';
} else {
console.error('Failed to upload file');
let parsedResponse = await response.json();
console.error('Failed to upload file:', parsedResponse.message);
}
} catch (error) {
console.error('Error:', error);
Expand Down

0 comments on commit 3169236

Please sign in to comment.