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:
@@ -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