Update availability checker
This commit is contained in:
parent
cb93f44dbe
commit
e29320151a
15
index.js
15
index.js
|
@ -23,6 +23,11 @@ const crypto = require("crypto")
|
||||||
const app = express();
|
const app = express();
|
||||||
const port = process.env.SERVER_PORT || 3000;
|
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({
|
const pool = mariadb.createPool({
|
||||||
host: process.env.DB_HOST || '127.0.0.1',
|
host: process.env.DB_HOST || '127.0.0.1',
|
||||||
port: process.env.DB_PORT || 3306,
|
port: process.env.DB_PORT || 3306,
|
||||||
|
@ -614,10 +619,6 @@ app.get("/api/v1/directory", (req, res) => {
|
||||||
|
|
||||||
// Function to find open number blocks
|
// Function to find open number blocks
|
||||||
app.get("/api/v1/directory/openBlocks", (req, res) => {
|
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) => {
|
pool.query("SELECT block_start, block_length FROM routes").then((rows) => {
|
||||||
console.log(JSON.stringify(rows)); // for testing
|
console.log(JSON.stringify(rows)); // for testing
|
||||||
|
|
||||||
|
@ -702,8 +703,10 @@ app.get("/footer", (req, res) => {
|
||||||
app.get("/api/v1/checkAvailability/:number", (req, res) => {
|
app.get("/api/v1/checkAvailability/:number", (req, res) => {
|
||||||
// Check if the number is 7 digits
|
// Check if the number is 7 digits
|
||||||
const number = Number(req.params.number);
|
const number = Number(req.params.number);
|
||||||
if (number < 2000000 || number > 9999999) {
|
// Round to nearest 10000 so it's always NXX0000
|
||||||
res.status(400).json({ error: `Number is outside valid range` });
|
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;
|
return;
|
||||||
}
|
}
|
||||||
pool.getConnection().then(conn => {
|
pool.getConnection().then(conn => {
|
||||||
|
|
Loading…
Reference in a new issue