From b71248458c9a59ae1a41f9c05e4f05e803351c19 Mon Sep 17 00:00:00 2001 From: Rizki Date: Fri, 10 Apr 2026 15:41:11 +0700 Subject: [PATCH] update --- adapter/justificationadapter.js | 4 ++- emailnotif.js | 60 +++++++++++++++++++++++++++------ 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/adapter/justificationadapter.js b/adapter/justificationadapter.js index 9579af4..ac4bac9 100644 --- a/adapter/justificationadapter.js +++ b/adapter/justificationadapter.js @@ -390,7 +390,9 @@ class JustificationAdapter extends Adapter{ // apires.meta.message = "Record Not Found"; // } callback(null, apires); - if (parseInt(status) !== -1) { + if (parseInt(status) === -1) { + emailNotif.notifyOnReturn(idxjustification, nik); + } else { emailNotif.notifyOnSubmit(idxjustification); } } diff --git a/emailnotif.js b/emailnotif.js index 071a851..651b3e8 100644 --- a/emailnotif.js +++ b/emailnotif.js @@ -3,8 +3,10 @@ const db = require('./config/dbproc.js'); const RECIPIENT = 'rizki.rmdhn1304@gmail.com'; -function buildHtml(approverName, title, priorName, idxjustification) { +function buildHtml(approverName, title, priorName, idxjustification, remark, status) { const url = `https://e-portal.telkomcel.tl/app/ext/eproc/circulation/${idxjustification}`; + const statusLabel = status || 'READY FOR APPROVAL'; + const notesLine = remark ? `
  • Notes: ${remark}
  • \n` : ''; return `

    Dear ${approverName},

    A new workflow task requires your review and approval. Please find the details regarding this justification below:

    @@ -12,8 +14,8 @@ function buildHtml(approverName, title, priorName, idxjustification) {

    Action Required:
    @@ -29,13 +31,16 @@ Procurement Division
    Finance Business Partner Unit

    `; } -function postEmail(approverName, title, priorName, idxjustification) { +function postEmail(approverName, title, priorName, idxjustification, remark, status) { + const subject = status === 'RETURNED' + ? `[Returned] Justification for ${title}` + : `[Approval Required] Justification for ${title}`; const payload = JSON.stringify({ to: [RECIPIENT], cc: [], bcc: [], - subject: `[Approval Required] Justification for ${title}`, - html: buildHtml(approverName, title, priorName, idxjustification) + subject, + html: buildHtml(approverName, title, priorName, idxjustification, remark, status) }); const req = http.request({ @@ -64,7 +69,7 @@ function postEmail(approverName, title, priorName, idxjustification) { */ function notifyOnSubmit(idxjustification) { const qryJustification = ` - select j.title, e.fullname as creator_name + select j.title, j.remark, e.fullname as creator_name from tbl_justification j join dbssotcel.tbl_employee e on e.nik = j.iby where j._idx = '${idxjustification}' limit 1 @@ -75,6 +80,7 @@ function notifyOnSubmit(idxjustification) { return; } const title = rows[0].title; + const remark = rows[0].remark || ''; const creatorName = rows[0].creator_name; const qryApprover = ` @@ -89,7 +95,7 @@ function notifyOnSubmit(idxjustification) { console.error('[email-notif] notifyOnSubmit: no approver found', err2); return; } - postEmail(rows2[0].fullname, title, creatorName, idxjustification); + postEmail(rows2[0].fullname, title, creatorName, idxjustification, remark); }); }); } @@ -98,13 +104,14 @@ function notifyOnSubmit(idxjustification) { * Notify next approver after someone signs. */ function notifyOnSigned(idxjustification, signerNik) { - const qryTitle = `select title from tbl_justification where _idx = '${idxjustification}' limit 1`; + const qryTitle = `select title, remark from tbl_justification where _idx = '${idxjustification}' limit 1`; db.query(qryTitle, [], function(err, rows) { if (err || rows.length === 0) { console.error('[email-notif] notifyOnSigned: failed to get justification title', err); return; } const title = rows[0].title; + const remark = rows[0].remark || ''; const qrySigner = `select fullname from dbssotcel.tbl_employee where nik = '${signerNik}' limit 1`; db.query(qrySigner, [], function(err2, rows2) { @@ -126,10 +133,41 @@ function notifyOnSigned(idxjustification, signerNik) { console.log('[email-notif] notifyOnSigned: no more approvers to notify'); return; } - postEmail(rows3[0].fullname, title, signerName, idxjustification); + postEmail(rows3[0].fullname, title, signerName, idxjustification, remark); }); }); }); } -module.exports = { notifyOnSubmit, notifyOnSigned }; +/** + * Notify creator when justification is returned. + */ +function notifyOnReturn(idxjustification, returnerNik) { + const qryJustification = ` + select j.title, j.remark, e.fullname as creator_name + from tbl_justification j + join dbssotcel.tbl_employee e on e.nik = j.iby + where j._idx = '${idxjustification}' limit 1 + `; + db.query(qryJustification, [], function(err, rows) { + if (err || rows.length === 0) { + console.error('[email-notif] notifyOnReturn: failed to get justification data', err); + return; + } + const title = rows[0].title; + const remark = rows[0].remark || ''; + const creatorName = rows[0].creator_name; + + const qryReturner = `select fullname from dbssotcel.tbl_employee where nik = '${returnerNik}' limit 1`; + db.query(qryReturner, [], function(err2, rows2) { + if (err2 || rows2.length === 0) { + console.error('[email-notif] notifyOnReturn: failed to get returner name', err2); + return; + } + const returnerName = rows2[0].fullname; + postEmail(creatorName, title, returnerName, idxjustification, remark, 'RETURNED'); + }); + }); +} + +module.exports = { notifyOnSubmit, notifyOnSigned, notifyOnReturn };