diff --git a/index.js b/index.js index 982ea1e..5ac5606 100644 --- a/index.js +++ b/index.js @@ -23,6 +23,11 @@ const crypto = require("crypto") const app = express(); const port = process.env.SERVER_PORT || 3000; +const invalidBlocks = [ + // Emergency number prefixes (112, 911, 999, 110, 117, 119, 113, 191, 111) + 1120000, 9110000, 9990000, 1100000, 1170000, 1190000, 1130000, 1910000, 1110000 +] + const pool = mariadb.createPool({ host: process.env.DB_HOST || '127.0.0.1', port: process.env.DB_PORT || 3306, @@ -614,10 +619,6 @@ app.get("/api/v1/directory", (req, res) => { // Function to find open number blocks app.get("/api/v1/directory/openBlocks", (req, res) => { - const invalidBlocks = [ - // Emergency number prefixes (112, 911, 999, 110, 117, 119, 113, 191, 111) - 1120000, 9110000, 9990000, 1100000, 1170000, 1190000, 1130000, 1910000, 1110000 - ] pool.query("SELECT block_start, block_length FROM routes").then((rows) => { console.log(JSON.stringify(rows)); // for testing @@ -702,8 +703,10 @@ app.get("/footer", (req, res) => { app.get("/api/v1/checkAvailability/:number", (req, res) => { // Check if the number is 7 digits const number = Number(req.params.number); - if (number < 2000000 || number > 9999999) { - res.status(400).json({ error: `Number is outside valid range` }); + // Round to nearest 10000 so it's always NXX0000 + number = Math.floor(number / 10000) * 10000; + if (!number || number < 1000000 || number > 9999999 || invalidBlocks.includes(number)) { + res.status(400).json({ error: `Number is outside valid range or is an invalid block` }); return; } pool.getConnection().then(conn => {