diff --git a/controllers/heightController.js b/controllers/heightController.js index 329e145..06723fe 100644 --- a/controllers/heightController.js +++ b/controllers/heightController.js @@ -1,3 +1,4 @@ +// @ts-check import path from 'path'; import { fileURLToPath } from 'url'; import { processAndConvert } from '../services/fileProcessor.js'; @@ -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( @@ -42,7 +56,7 @@ export const handleHeightLogs = async (req, res) => { `; await fetchAndStoreHeightLogs({ - blockHeight: height, + blockHeight: search, inputFile, network, queryfilter, diff --git a/public/index.html b/public/index.html index 0099641..2d5ba21 100644 --- a/public/index.html +++ b/public/index.html @@ -56,17 +56,17 @@
-
- +
+
-
- +
+
-
- +
+
diff --git a/public/scripts/submitSearch.js b/public/scripts/submitSearch.js index 72940c0..b0f8c7d 100644 --- a/public/scripts/submitSearch.js +++ b/public/scripts/submitSearch.js @@ -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, }), }); @@ -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);