Add dotenv support and bulk SMS endpoint

- Integrated dotenv for environment variable management.
- Updated queueWorker to use WORKER_INTERVAL from .env or default to 5 seconds.
- Added a new bulk SMS endpoint to queue multiple messages in a single request, with validation and transaction handling.
- Updated package.json and package-lock.json to include dotenv dependency.
This commit is contained in:
Mekan1206
2026-06-04 19:34:06 +05:00
parent 87b3715774
commit 1907be7c6f
6 changed files with 59 additions and 1 deletions

1
.env Normal file
View File

@@ -0,0 +1 @@
WORKER_INTERVAL=5000

13
package-lock.json generated
View File

@@ -9,6 +9,7 @@
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"dotenv": "^17.4.2",
"express": "^4.18.2", "express": "^4.18.2",
"socket.io": "^4.7.4", "socket.io": "^4.7.4",
"sqlite3": "^6.0.1" "sqlite3": "^6.0.1"
@@ -328,6 +329,18 @@
"node": ">=8" "node": ">=8"
} }
}, },
"node_modules/dotenv": {
"version": "17.4.2",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-17.4.2.tgz",
"integrity": "sha512-nI4U3TottKAcAD9LLud4Cb7b2QztQMUEfHbvhTH09bqXTxnSie8WnjPALV/WMCrJZ6UV/qHJ6L03OqO3LcdYZw==",
"license": "BSD-2-Clause",
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://dotenvx.com"
}
},
"node_modules/dunder-proto": { "node_modules/dunder-proto": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz",

View File

@@ -10,6 +10,7 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"dotenv": "^17.4.2",
"express": "^4.18.2", "express": "^4.18.2",
"socket.io": "^4.7.4", "socket.io": "^4.7.4",
"sqlite3": "^6.0.1" "sqlite3": "^6.0.1"

View File

@@ -45,7 +45,7 @@ function startQueueWorker(io) {
}); });
} }
}); });
}, 5000); // Check every 5 seconds }, parseInt(process.env.WORKER_INTERVAL) || 5000); // Check based on interval from .env or default to 5 seconds
} }
module.exports = startQueueWorker; module.exports = startQueueWorker;

View File

@@ -44,6 +44,48 @@ module.exports = function (io) {
}); });
}); });
// Bulk endpoint to queue multiple SMS
router.post('/sms/bulk', (req, res) => {
const { messages } = req.body;
if (!Array.isArray(messages) || messages.length === 0) {
return res.status(400).json({ error: 'messages array is required and cannot be empty.' });
}
// Validate all messages first
for (const msg of messages) {
if (!msg.phone || !msg.message) {
return res.status(400).json({ error: 'Each message must have a phone and message.' });
}
}
db.serialize(() => {
db.run("BEGIN TRANSACTION");
const stmt = db.prepare("INSERT INTO sms_queue (phone, message) VALUES (?, ?)");
for (const msg of messages) {
stmt.run([msg.phone, msg.message], function(err) {
if (err) {
console.error('Error inserting into sms_queue during bulk operation:', err);
}
});
}
stmt.finalize();
db.run("COMMIT", (err) => {
if (err) {
console.error('Error committing bulk SMS transaction:', err);
return res.status(500).json({ error: 'Failed to queue bulk SMS.' });
}
return res.status(202).json({
message: `Successfully queued ${messages.length} SMS messages.`
});
});
});
});
return router; return router;
}; };

View File

@@ -1,3 +1,4 @@
require('dotenv').config();
const express = require('express'); const express = require('express');
const http = require('http'); const http = require('http');
const socketIo = require('socket.io'); const socketIo = require('socket.io');