This commit is contained in:
Rizki
2026-04-15 14:16:38 +07:00
parent 477c225a21
commit 3a8413c9ec

View File

@ -1,7 +1,10 @@
const http = require('http'); const http = require('http');
const db = require('./config/dbproc.js'); const db = require('./config/dbproc.js');
const RECIPIENT = 'rizki.rmdhn1304@gmail.com'; const ALLOWED_EMAILS = new Set([
'david.rodriques@telin.net',
'syam.thasya@telin.net',
]);
function buildHtml(approverName, title, priorName, idxjustification, remark, status) { function buildHtml(approverName, title, priorName, idxjustification, remark, status) {
const url = status === 'RETURNED' const url = status === 'RETURNED'
@ -43,13 +46,18 @@ function logEmail(trxId, toEmail, subject, html, fromEmail, status, requestPaylo
}); });
} }
function postEmail(approverName, title, priorName, idxjustification, remark, status) { function postEmail(toEmail, approverName, title, priorName, idxjustification, remark, status) {
if (!ALLOWED_EMAILS.has(toEmail)) {
console.log('[email-notif] skipped (not in allowed list):', toEmail);
return;
}
const subject = status === 'RETURNED' const subject = status === 'RETURNED'
? `[Returned] Justification for ${title}` ? `[Returned] Justification for ${title}`
: `[Approval Required] Justification for ${title}`; : `[Approval Required] Justification for ${title}`;
const html = buildHtml(approverName, title, priorName, idxjustification, remark, status); const html = buildHtml(approverName, title, priorName, idxjustification, remark, status);
const payload = JSON.stringify({ const payload = JSON.stringify({
to: [RECIPIENT], to: [toEmail],
cc: [], cc: [],
bcc: [], bcc: [],
subject, subject,
@ -78,14 +86,14 @@ function postEmail(approverName, title, priorName, idxjustification, remark, sta
} catch (_) {} } catch (_) {}
const logStatus = res.statusCode >= 200 && res.statusCode < 300 ? 'success' : 'failed'; const logStatus = res.statusCode >= 200 && res.statusCode < 300 ? 'success' : 'failed';
console.log('[email-notif] sent, status:', res.statusCode, '| trxId:', trxId); console.log('[email-notif] sent, status:', res.statusCode, '| trxId:', trxId, '| to:', toEmail);
logEmail(trxId, RECIPIENT, subject, html, '', logStatus, payload, responseMessage); logEmail(trxId, toEmail, subject, html, '', logStatus, payload, responseMessage);
}); });
}); });
req.on('error', (err) => { req.on('error', (err) => {
console.error('[email-notif] failed:', err.message); console.error('[email-notif] failed:', err.message);
logEmail('', RECIPIENT, subject, html, '', 'failed', payload, err.message); logEmail('', toEmail, subject, html, '', 'failed', payload, err.message);
}); });
req.write(payload); req.write(payload);
@ -112,7 +120,7 @@ function notifyOnSubmit(idxjustification) {
const creatorName = rows[0].creator_name; const creatorName = rows[0].creator_name;
const qryApprover = ` const qryApprover = `
select t.nik, e.fullname select t.nik, e.fullname, e.email
from tbl_justificationttd t from tbl_justificationttd t
join dbssotcel.tbl_employee e on e.nik = t.nik join dbssotcel.tbl_employee e on e.nik = t.nik
where t.idxjustification = '${idxjustification}' and t.issigned = 0 and t.isdeleted = 0 where t.idxjustification = '${idxjustification}' and t.issigned = 0 and t.isdeleted = 0
@ -123,7 +131,7 @@ function notifyOnSubmit(idxjustification) {
console.error('[email-notif] notifyOnSubmit: no approver found', err2); console.error('[email-notif] notifyOnSubmit: no approver found', err2);
return; return;
} }
postEmail(rows2[0].fullname, title, creatorName, idxjustification, remark); postEmail(rows2[0].email, rows2[0].fullname, title, creatorName, idxjustification, remark);
}); });
}); });
} }
@ -150,7 +158,7 @@ function notifyOnSigned(idxjustification, signerNik) {
const signerName = rows2[0].fullname; const signerName = rows2[0].fullname;
const qryNext = ` const qryNext = `
select t.nik, e.fullname select t.nik, e.fullname, e.email
from tbl_justificationttd t from tbl_justificationttd t
join dbssotcel.tbl_employee e on e.nik = t.nik join dbssotcel.tbl_employee e on e.nik = t.nik
where t.idxjustification = '${idxjustification}' and t.issigned = 0 and t.isdeleted = 0 where t.idxjustification = '${idxjustification}' and t.issigned = 0 and t.isdeleted = 0
@ -161,7 +169,7 @@ function notifyOnSigned(idxjustification, signerNik) {
console.log('[email-notif] notifyOnSigned: no more approvers to notify'); console.log('[email-notif] notifyOnSigned: no more approvers to notify');
return; return;
} }
postEmail(rows3[0].fullname, title, signerName, idxjustification, remark); postEmail(rows3[0].email, rows3[0].fullname, title, signerName, idxjustification, remark);
}); });
}); });
}); });
@ -172,7 +180,7 @@ function notifyOnSigned(idxjustification, signerNik) {
*/ */
function notifyOnReturn(idxjustification, returnerNik) { function notifyOnReturn(idxjustification, returnerNik) {
const qryJustification = ` const qryJustification = `
select j.title, j.remark, e.fullname as creator_name select j.title, j.remark, e.fullname as creator_name, e.email as creator_email
from tbl_justification j from tbl_justification j
join dbssotcel.tbl_employee e on e.nik = j.iby join dbssotcel.tbl_employee e on e.nik = j.iby
where j._idx = '${idxjustification}' limit 1 where j._idx = '${idxjustification}' limit 1
@ -185,6 +193,7 @@ function notifyOnReturn(idxjustification, returnerNik) {
const title = rows[0].title; const title = rows[0].title;
const remark = rows[0].remark || ''; const remark = rows[0].remark || '';
const creatorName = rows[0].creator_name; const creatorName = rows[0].creator_name;
const creatorEmail = rows[0].creator_email;
const qryReturner = `select fullname from dbssotcel.tbl_employee where nik = '${returnerNik}' limit 1`; const qryReturner = `select fullname from dbssotcel.tbl_employee where nik = '${returnerNik}' limit 1`;
db.query(qryReturner, [], function(err2, rows2) { db.query(qryReturner, [], function(err2, rows2) {
@ -193,7 +202,7 @@ function notifyOnReturn(idxjustification, returnerNik) {
return; return;
} }
const returnerName = rows2[0].fullname; const returnerName = rows2[0].fullname;
postEmail(creatorName, title, returnerName, idxjustification, remark, 'RETURNED'); postEmail(creatorEmail, creatorName, title, returnerName, idxjustification, remark, 'RETURNED');
}); });
}); });
} }