Add email notifications with one-click approve/reject from email
When a user submits a credit request, an email is sent to all configured NOTIFY_TO recipients with approve buttons (5M, 10M, 20M), a custom amount option, and a reject button. Each button is a signed HMAC-SHA256 webhook URL that expires after WEBHOOK_TTL_HOURS. Clicking approve from email processes the top-up identical to the admin dashboard — balance update, history log, request status change. Double-approval protection prevents the same link from being used twice. Portal approval still works alongside email approval. New dependencies: nodemailer New files: services/email.js, routes/webhooks.js Modified: server.js, routes/requests.js, .env.example Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
a9c514d5b1
commit
81e18f8a73
7 changed files with 411 additions and 2 deletions
15
.env.example
15
.env.example
|
|
@ -1,3 +1,18 @@
|
|||
MONGO_URI=mongodb://mongodb:27017/LibreChat
|
||||
PORT=3002
|
||||
API_KEY=change-me-to-a-secure-key
|
||||
|
||||
# Webhook security
|
||||
WEBHOOK_SECRET=change-me-to-a-random-secret
|
||||
WEBHOOK_BASE_URL=https://your-domain.com/balance-manager
|
||||
WEBHOOK_TTL_HOURS=72
|
||||
|
||||
# SMTP (Mailgun or any SMTP server)
|
||||
SMTP_HOST=smtp.mailgun.org
|
||||
SMTP_PORT=587
|
||||
SMTP_USER=postmaster@mg.yourdomain.com
|
||||
SMTP_PASS=your-smtp-password
|
||||
SMTP_FROM=noreply@yourdomain.com
|
||||
|
||||
# Notification recipients (comma-separated for multiple)
|
||||
NOTIFY_TO=admin@yourdomain.com
|
||||
|
|
|
|||
12
package-lock.json
generated
12
package-lock.json
generated
|
|
@ -10,7 +10,8 @@
|
|||
"dependencies": {
|
||||
"dotenv": "^16.4.5",
|
||||
"express": "^4.21.0",
|
||||
"mongodb": "^6.12.0"
|
||||
"mongodb": "^6.12.0",
|
||||
"nodemailer": "^8.0.4"
|
||||
}
|
||||
},
|
||||
"node_modules/@mongodb-js/saslprep": {
|
||||
|
|
@ -634,6 +635,15 @@
|
|||
"node": ">= 0.6"
|
||||
}
|
||||
},
|
||||
"node_modules/nodemailer": {
|
||||
"version": "8.0.4",
|
||||
"resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-8.0.4.tgz",
|
||||
"integrity": "sha512-k+jf6N8PfQJ0Fe8ZhJlgqU5qJU44Lpvp2yvidH3vp1lPnVQMgi4yEEMPXg5eJS1gFIJTVq1NHBk7Ia9ARdSBdQ==",
|
||||
"license": "MIT-0",
|
||||
"engines": {
|
||||
"node": ">=6.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/object-inspect": {
|
||||
"version": "1.13.4",
|
||||
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.4.tgz",
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
"dependencies": {
|
||||
"dotenv": "^16.4.5",
|
||||
"express": "^4.21.0",
|
||||
"mongodb": "^6.12.0"
|
||||
"mongodb": "^6.12.0",
|
||||
"nodemailer": "^8.0.4"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ const { Router } = require('express');
|
|||
const requestService = require('../services/requests');
|
||||
const balanceService = require('../services/balance');
|
||||
const history = require('../services/history');
|
||||
const { sendApprovalEmail } = require('../services/email');
|
||||
const { MongoClient } = require('mongodb');
|
||||
|
||||
const publicRouter = Router();
|
||||
|
|
@ -19,6 +20,9 @@ publicRouter.post('/', (req, res) => {
|
|||
return res.status(400).json({ error: 'Invalid email address' });
|
||||
}
|
||||
const request = requestService.createRequest(email, omgJobNumber);
|
||||
sendApprovalEmail(request).catch(err =>
|
||||
console.error('[email] Failed to send approval notification:', err)
|
||||
);
|
||||
res.json({ success: true, id: request.id });
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
|
|
|
|||
240
routes/webhooks.js
Normal file
240
routes/webhooks.js
Normal file
|
|
@ -0,0 +1,240 @@
|
|||
const { Router } = require('express');
|
||||
const { MongoClient } = require('mongodb');
|
||||
const { verifyToken } = require('../services/email');
|
||||
const requestService = require('../services/requests');
|
||||
const balanceService = require('../services/balance');
|
||||
const history = require('../services/history');
|
||||
|
||||
const router = Router();
|
||||
|
||||
function htmlPage(title, message, success) {
|
||||
const color = success ? '#4ade80' : '#f87171';
|
||||
const icon = success ? '✓' : '✗';
|
||||
return `<!DOCTYPE html>
|
||||
<html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>${title}</title>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;600;700&display=swap" rel="stylesheet">
|
||||
<style>
|
||||
body{margin:0;padding:0;background:#000;font-family:'Montserrat',Arial,sans-serif;display:flex;align-items:center;justify-content:center;min-height:100vh;color:#f8fafc}
|
||||
.card{background:#111;border:1px solid rgba(255,196,7,0.15);border-radius:16px;padding:48px;max-width:440px;text-align:center}
|
||||
.icon{width:64px;height:64px;border-radius:50%;display:flex;align-items:center;justify-content:center;margin:0 auto 20px;font-size:32px;background:${success ? 'rgba(74,222,128,0.15)' : 'rgba(248,113,113,0.15)'}}
|
||||
h1{font-size:20px;margin:0 0 12px;color:${color}}
|
||||
p{color:#94a3b8;font-size:14px;line-height:1.6;margin:0}
|
||||
</style></head>
|
||||
<body><div class="card"><div class="icon">${icon}</div><h1>${title}</h1><p>${message}</p></div></body></html>`;
|
||||
}
|
||||
|
||||
async function getDbAndUser(email) {
|
||||
const MONGO_URI = process.env.MONGO_URI || 'mongodb://localhost:27017/LibreChat';
|
||||
const client = new MongoClient(MONGO_URI);
|
||||
await client.connect();
|
||||
const db = client.db();
|
||||
const user = await db.collection('users').findOne(
|
||||
{ email: email.toLowerCase() },
|
||||
{ projection: { _id: 1, email: 1 } },
|
||||
);
|
||||
return { client, db, user };
|
||||
}
|
||||
|
||||
router.get('/approve/:requestId', async (req, res) => {
|
||||
try {
|
||||
const { token, amount, expires } = req.query;
|
||||
|
||||
if (!verifyToken(req.params.requestId, amount, expires, token)) {
|
||||
return res.status(403).send(htmlPage('Link Expired or Invalid', 'This approval link has expired or is invalid. Please use the admin dashboard to process this request.', false));
|
||||
}
|
||||
|
||||
const requests = requestService.getAll();
|
||||
const request = requests.find(r => r.id === req.params.requestId);
|
||||
|
||||
if (!request) {
|
||||
return res.status(404).send(htmlPage('Not Found', 'This request could not be found.', false));
|
||||
}
|
||||
if (request.status !== 'pending') {
|
||||
return res.status(400).send(htmlPage('Already Processed', `This request was already ${request.status}${request.amountApproved ? ' with ' + (request.amountApproved / 1000000).toFixed(1) + 'M tokens' : ''}.`, false));
|
||||
}
|
||||
|
||||
const numAmount = parseInt(amount);
|
||||
const { client, db, user } = await getDbAndUser(request.email);
|
||||
|
||||
if (!user) {
|
||||
await client.close();
|
||||
return res.status(404).send(htmlPage('User Not Found', `No LibreChat user found with email: ${request.email}`, false));
|
||||
}
|
||||
|
||||
const result = await balanceService.addBalance(db, user._id.toString(), numAmount);
|
||||
await client.close();
|
||||
|
||||
history.log({
|
||||
email: request.email,
|
||||
action: 'approve',
|
||||
amount: numAmount,
|
||||
source: 'email',
|
||||
omgJobNumber: request.omgJobNumber,
|
||||
balanceAfter: result.tokenCredits,
|
||||
});
|
||||
|
||||
requestService.updateRequest(req.params.requestId, 'approved', numAmount);
|
||||
|
||||
const formatted = (numAmount / 1000000).toFixed(1) + 'M';
|
||||
res.send(htmlPage('Request Approved', `${formatted} tokens added to ${request.email}. New balance: ${result.tokenCredits.toLocaleString()} tokens.`, true));
|
||||
} catch (err) {
|
||||
console.error('[webhook] Approve error:', err);
|
||||
res.status(500).send(htmlPage('Error', 'Something went wrong processing this approval. Please use the admin dashboard.', false));
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/reject/:requestId', async (req, res) => {
|
||||
try {
|
||||
const { token, expires } = req.query;
|
||||
|
||||
if (!verifyToken(req.params.requestId, 'reject', expires, token)) {
|
||||
return res.status(403).send(htmlPage('Link Expired or Invalid', 'This rejection link has expired or is invalid.', false));
|
||||
}
|
||||
|
||||
const requests = requestService.getAll();
|
||||
const request = requests.find(r => r.id === req.params.requestId);
|
||||
|
||||
if (!request) {
|
||||
return res.status(404).send(htmlPage('Not Found', 'This request could not be found.', false));
|
||||
}
|
||||
if (request.status !== 'pending') {
|
||||
return res.status(400).send(htmlPage('Already Processed', `This request was already ${request.status}.`, false));
|
||||
}
|
||||
|
||||
requestService.updateRequest(req.params.requestId, 'rejected', null);
|
||||
res.send(htmlPage('Request Rejected', `Credit request from ${request.email} (${request.omgJobNumber}) has been rejected.`, true));
|
||||
} catch (err) {
|
||||
console.error('[webhook] Reject error:', err);
|
||||
res.status(500).send(htmlPage('Error', 'Something went wrong. Please use the admin dashboard.', false));
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/custom/:requestId', async (req, res) => {
|
||||
try {
|
||||
const { token, expires } = req.query;
|
||||
|
||||
if (!verifyToken(req.params.requestId, 'custom', expires, token)) {
|
||||
return res.status(403).send(htmlPage('Link Expired or Invalid', 'This link has expired or is invalid.', false));
|
||||
}
|
||||
|
||||
const requests = requestService.getAll();
|
||||
const request = requests.find(r => r.id === req.params.requestId);
|
||||
|
||||
if (!request) {
|
||||
return res.status(404).send(htmlPage('Not Found', 'This request could not be found.', false));
|
||||
}
|
||||
if (request.status !== 'pending') {
|
||||
return res.status(400).send(htmlPage('Already Processed', `This request was already ${request.status}.`, false));
|
||||
}
|
||||
|
||||
res.send(`<!DOCTYPE html>
|
||||
<html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>Custom Approve - ${request.email}</title>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600;700;800&display=swap" rel="stylesheet">
|
||||
<style>
|
||||
body{margin:0;padding:0;background:#000;font-family:'Montserrat',Arial,sans-serif;display:flex;align-items:center;justify-content:center;min-height:100vh;color:#f8fafc}
|
||||
.card{background:#111;border:1px solid rgba(255,196,7,0.15);border-radius:16px;padding:40px;width:420px;max-width:90vw}
|
||||
h2{font-size:18px;margin:0 0 4px;color:#FFC407}
|
||||
.sub{color:#94a3b8;font-size:13px;margin:0 0 24px}
|
||||
label{display:block;font-size:12px;font-weight:600;text-transform:uppercase;letter-spacing:0.5px;color:#94a3b8;margin-bottom:8px}
|
||||
input{width:100%;background:rgba(30,30,30,0.8);border:1px solid rgba(255,196,7,0.12);border-radius:10px;padding:14px 16px;color:#f8fafc;font-family:'Montserrat',sans-serif;font-size:16px;outline:none;box-sizing:border-box}
|
||||
input:focus{border-color:#FFC407}
|
||||
.presets{display:flex;gap:8px;flex-wrap:wrap;margin:12px 0 24px}
|
||||
.preset{background:rgba(30,30,30,0.8);border:1px solid rgba(255,196,7,0.12);color:#94a3b8;padding:8px 16px;border-radius:8px;cursor:pointer;font-family:'Montserrat',sans-serif;font-size:13px;font-weight:600}
|
||||
.preset:hover{border-color:#FFC407;color:#FFC407}
|
||||
.btn{width:100%;padding:14px;background:#4ade80;color:#000;border:none;border-radius:10px;font-family:'Montserrat',sans-serif;font-size:14px;font-weight:700;cursor:pointer}
|
||||
.btn:hover{background:#22c55e}
|
||||
.info{background:rgba(20,20,20,0.8);border:1px solid rgba(255,196,7,0.1);border-radius:12px;padding:16px;margin-bottom:24px}
|
||||
.info p{margin:0;font-size:13px}
|
||||
.info .label{color:#94a3b8;font-size:11px;text-transform:uppercase;letter-spacing:0.5px;margin-bottom:2px}
|
||||
.info .value{color:#f8fafc;font-weight:600}
|
||||
</style></head>
|
||||
<body>
|
||||
<div class="card">
|
||||
<h2>Custom Approve</h2>
|
||||
<p class="sub">Choose an amount to approve for this request</p>
|
||||
<div class="info">
|
||||
<p class="label">Email</p>
|
||||
<p class="value">${request.email}</p>
|
||||
<p class="label" style="margin-top:12px">OMG Job #</p>
|
||||
<p class="value">${request.omgJobNumber}</p>
|
||||
</div>
|
||||
<form method="POST" action="">
|
||||
<input type="hidden" name="token" value="${token}">
|
||||
<input type="hidden" name="expires" value="${expires}">
|
||||
<label>Amount (tokens)</label>
|
||||
<input type="number" name="amount" id="amt" placeholder="e.g. 12000000" required autofocus>
|
||||
<div class="presets">
|
||||
<span class="preset" onclick="document.getElementById('amt').value=1000000">1M</span>
|
||||
<span class="preset" onclick="document.getElementById('amt').value=5000000">5M</span>
|
||||
<span class="preset" onclick="document.getElementById('amt').value=10000000">10M</span>
|
||||
<span class="preset" onclick="document.getElementById('amt').value=12000000">12M</span>
|
||||
<span class="preset" onclick="document.getElementById('amt').value=20000000">20M</span>
|
||||
<span class="preset" onclick="document.getElementById('amt').value=50000000">50M</span>
|
||||
</div>
|
||||
<button type="submit" class="btn">Approve & Top Up</button>
|
||||
</form>
|
||||
</div>
|
||||
</body></html>`);
|
||||
} catch (err) {
|
||||
console.error('[webhook] Custom page error:', err);
|
||||
res.status(500).send(htmlPage('Error', 'Something went wrong. Please use the admin dashboard.', false));
|
||||
}
|
||||
});
|
||||
|
||||
router.use('/custom/:requestId', require('express').urlencoded({ extended: false }));
|
||||
|
||||
router.post('/custom/:requestId', async (req, res) => {
|
||||
try {
|
||||
const { token, expires, amount } = req.body;
|
||||
|
||||
if (!verifyToken(req.params.requestId, 'custom', expires, token)) {
|
||||
return res.status(403).send(htmlPage('Link Expired or Invalid', 'This link has expired or is invalid.', false));
|
||||
}
|
||||
|
||||
const numAmount = parseInt(amount);
|
||||
if (isNaN(numAmount) || numAmount <= 0) {
|
||||
return res.status(400).send(htmlPage('Invalid Amount', 'Please enter a valid positive number.', false));
|
||||
}
|
||||
|
||||
const requests = requestService.getAll();
|
||||
const request = requests.find(r => r.id === req.params.requestId);
|
||||
|
||||
if (!request) {
|
||||
return res.status(404).send(htmlPage('Not Found', 'This request could not be found.', false));
|
||||
}
|
||||
if (request.status !== 'pending') {
|
||||
return res.status(400).send(htmlPage('Already Processed', `This request was already ${request.status}.`, false));
|
||||
}
|
||||
|
||||
const { client, db, user } = await getDbAndUser(request.email);
|
||||
|
||||
if (!user) {
|
||||
await client.close();
|
||||
return res.status(404).send(htmlPage('User Not Found', `No LibreChat user found with email: ${request.email}`, false));
|
||||
}
|
||||
|
||||
const result = await balanceService.addBalance(db, user._id.toString(), numAmount);
|
||||
await client.close();
|
||||
|
||||
history.log({
|
||||
email: request.email,
|
||||
action: 'approve',
|
||||
amount: numAmount,
|
||||
source: 'email',
|
||||
omgJobNumber: request.omgJobNumber,
|
||||
balanceAfter: result.tokenCredits,
|
||||
});
|
||||
|
||||
requestService.updateRequest(req.params.requestId, 'approved', numAmount);
|
||||
|
||||
const formatted = (numAmount / 1000000).toFixed(1) + 'M';
|
||||
res.send(htmlPage('Request Approved', `${formatted} tokens added to ${request.email}. New balance: ${result.tokenCredits.toLocaleString()} tokens.`, true));
|
||||
} catch (err) {
|
||||
console.error('[webhook] Custom approve error:', err);
|
||||
res.status(500).send(htmlPage('Error', 'Something went wrong. Please use the admin dashboard.', false));
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
|
@ -36,10 +36,14 @@ app.use(express.static(path.join(__dirname, 'public')));
|
|||
|
||||
const apiRoutes = require('./routes/api');
|
||||
const requestRoutes = require('./routes/requests');
|
||||
const webhookRoutes = require('./routes/webhooks');
|
||||
|
||||
// Public: submit a credit request (no auth needed)
|
||||
app.use('/api/requests', requestRoutes.publicRouter);
|
||||
|
||||
// Public: email webhook approve/reject (secured by HMAC tokens)
|
||||
app.use('/api/webhooks', webhookRoutes);
|
||||
|
||||
// Admin: manage credit requests (auth required)
|
||||
app.use('/api/admin/requests', authMiddleware, requestRoutes.adminRouter);
|
||||
|
||||
|
|
|
|||
135
services/email.js
Normal file
135
services/email.js
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
const crypto = require('crypto');
|
||||
const nodemailer = require('nodemailer');
|
||||
|
||||
const WEBHOOK_SECRET = process.env.WEBHOOK_SECRET || 'change-me-webhook-secret';
|
||||
const WEBHOOK_BASE_URL = (process.env.WEBHOOK_BASE_URL || 'http://localhost:3002').replace(/\/$/, '');
|
||||
const WEBHOOK_TTL_HOURS = parseInt(process.env.WEBHOOK_TTL_HOURS || '72');
|
||||
const NOTIFY_TO = process.env.NOTIFY_TO || '';
|
||||
|
||||
function getTransporter() {
|
||||
return nodemailer.createTransport({
|
||||
host: process.env.SMTP_HOST || 'smtp.mailgun.org',
|
||||
port: parseInt(process.env.SMTP_PORT || '587'),
|
||||
secure: false,
|
||||
auth: {
|
||||
user: process.env.SMTP_USER,
|
||||
pass: process.env.SMTP_PASS,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function generateToken(requestId, amount, expiresAt) {
|
||||
const payload = `${requestId}|${amount}|${expiresAt}`;
|
||||
return crypto.createHmac('sha256', WEBHOOK_SECRET).update(payload).digest('hex');
|
||||
}
|
||||
|
||||
function verifyToken(requestId, amount, expires, token) {
|
||||
if (Date.now() > parseInt(expires)) return false;
|
||||
const expected = generateToken(requestId, amount, expires);
|
||||
try {
|
||||
return crypto.timingSafeEqual(Buffer.from(expected, 'hex'), Buffer.from(token, 'hex'));
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function buildWebhookUrl(path, requestId, amount, expiresAt) {
|
||||
const token = generateToken(requestId, amount, expiresAt);
|
||||
return `${WEBHOOK_BASE_URL}/api/webhooks/${path}/${requestId}?token=${token}&amount=${amount}&expires=${expiresAt}`;
|
||||
}
|
||||
|
||||
function buildEmail(request) {
|
||||
const expiresAt = Date.now() + (WEBHOOK_TTL_HOURS * 60 * 60 * 1000);
|
||||
const expiryDate = new Date(expiresAt).toLocaleDateString('en-GB', {
|
||||
day: '2-digit', month: 'short', year: 'numeric', hour: '2-digit', minute: '2-digit',
|
||||
});
|
||||
|
||||
const presets = [
|
||||
{ label: 'Approve 5M', amount: 5000000, color: '#4ade80' },
|
||||
{ label: 'Approve 10M', amount: 10000000, color: '#4ade80' },
|
||||
{ label: 'Approve 20M', amount: 20000000, color: '#4ade80' },
|
||||
];
|
||||
|
||||
const approveButtons = presets.map(p => {
|
||||
const url = buildWebhookUrl('approve', request.id, p.amount, expiresAt);
|
||||
return `<a href="${url}" style="display:inline-block;padding:12px 24px;background:${p.color};color:#000;text-decoration:none;border-radius:8px;font-weight:700;font-size:14px;font-family:'Montserrat',Arial,sans-serif;margin:4px;">${p.label}</a>`;
|
||||
}).join('\n ');
|
||||
|
||||
const customUrl = buildWebhookUrl('custom', request.id, 'custom', expiresAt);
|
||||
const rejectUrl = buildWebhookUrl('reject', request.id, 'reject', expiresAt);
|
||||
|
||||
return `<!DOCTYPE html>
|
||||
<html>
|
||||
<head><meta charset="utf-8"></head>
|
||||
<body style="margin:0;padding:0;background:#000;font-family:'Montserrat',Arial,sans-serif;">
|
||||
<table width="100%" cellpadding="0" cellspacing="0" style="background:#000;padding:40px 20px;">
|
||||
<tr>
|
||||
<td align="center">
|
||||
<table width="520" cellpadding="0" cellspacing="0" style="background:#111;border:1px solid rgba(255,196,7,0.15);border-radius:16px;overflow:hidden;">
|
||||
<tr>
|
||||
<td style="padding:32px 32px 0;text-align:center;">
|
||||
<div style="display:inline-block;width:48px;height:48px;background:#FFC407;border-radius:12px;line-height:48px;font-size:24px;margin-bottom:16px;">💳</div>
|
||||
<h1 style="color:#FFC407;font-size:22px;margin:0 0 8px;font-family:'Montserrat',Arial,sans-serif;">New Credit Request</h1>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding:24px 32px;">
|
||||
<table width="100%" cellpadding="0" cellspacing="0" style="background:rgba(20,20,20,0.8);border:1px solid rgba(255,196,7,0.1);border-radius:12px;">
|
||||
<tr>
|
||||
<td style="padding:20px;">
|
||||
<p style="color:#94a3b8;font-size:12px;text-transform:uppercase;letter-spacing:0.5px;margin:0 0 4px;">Email</p>
|
||||
<p style="color:#f8fafc;font-size:16px;font-weight:600;margin:0 0 16px;">${request.email}</p>
|
||||
<p style="color:#94a3b8;font-size:12px;text-transform:uppercase;letter-spacing:0.5px;margin:0 0 4px;">OMG Job Number</p>
|
||||
<p style="color:#f8fafc;font-size:16px;font-weight:600;margin:0;">${request.omgJobNumber}</p>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding:0 32px;text-align:center;">
|
||||
<p style="color:#94a3b8;font-size:13px;margin:0 0 16px;">Choose an action:</p>
|
||||
${approveButtons}
|
||||
<br style="line-height:8px;">
|
||||
<a href="${customUrl}" style="display:inline-block;padding:12px 24px;background:#FFC407;color:#000;text-decoration:none;border-radius:8px;font-weight:700;font-size:14px;font-family:'Montserrat',Arial,sans-serif;margin:4px;">Custom Amount</a>
|
||||
<a href="${rejectUrl}" style="display:inline-block;padding:12px 24px;background:rgba(248,113,113,0.2);color:#f87171;text-decoration:none;border-radius:8px;font-weight:700;font-size:14px;font-family:'Montserrat',Arial,sans-serif;margin:4px;border:1px solid rgba(248,113,113,0.3);">Reject</a>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding:24px 32px 32px;text-align:center;">
|
||||
<p style="color:#64748b;font-size:11px;margin:0;">These links expire on ${expiryDate}</p>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>`;
|
||||
}
|
||||
|
||||
async function sendApprovalEmail(request) {
|
||||
if (!NOTIFY_TO) {
|
||||
console.log('[email] NOTIFY_TO not configured, skipping email');
|
||||
return;
|
||||
}
|
||||
if (!process.env.SMTP_USER || !process.env.SMTP_PASS) {
|
||||
console.log('[email] SMTP credentials not configured, skipping email');
|
||||
return;
|
||||
}
|
||||
|
||||
const transporter = getTransporter();
|
||||
const html = buildEmail(request);
|
||||
const recipients = NOTIFY_TO.split(',').map(e => e.trim()).filter(Boolean);
|
||||
|
||||
await transporter.sendMail({
|
||||
from: process.env.SMTP_FROM || process.env.SMTP_USER,
|
||||
to: recipients.join(', '),
|
||||
subject: `Credit Request: ${request.email} — ${request.omgJobNumber}`,
|
||||
html,
|
||||
});
|
||||
|
||||
console.log(`[email] Approval notification sent to ${recipients.join(', ')} for request ${request.id}`);
|
||||
}
|
||||
|
||||
module.exports = { generateToken, verifyToken, sendApprovalEmail };
|
||||
Loading…
Add table
Reference in a new issue