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:
13
package-lock.json
generated
13
package-lock.json
generated
@@ -9,6 +9,7 @@
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"dotenv": "^17.4.2",
|
||||
"express": "^4.18.2",
|
||||
"socket.io": "^4.7.4",
|
||||
"sqlite3": "^6.0.1"
|
||||
@@ -328,6 +329,18 @@
|
||||
"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": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz",
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"dotenv": "^17.4.2",
|
||||
"express": "^4.18.2",
|
||||
"socket.io": "^4.7.4",
|
||||
"sqlite3": "^6.0.1"
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user