const http = require('http'); const db = require('./config/dbproc.js'); const RECIPIENT = 'rizki.rmdhn1304@gmail.com'; function buildHtml(approverName, title, priorName) { return `

Dear ${approverName},

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

Action Required:
https://e-portal.telkomcel.tl/app/ext/eproc/

Best regards,
E-Procurement

`; } function postEmail(approverName, title, priorName) { const payload = JSON.stringify({ to: [RECIPIENT], subject: `[Approval Required] Justification for ${title}`, html: buildHtml(approverName, title, priorName) }); const req = http.request({ hostname: 'localhost', port: 7004, path: '/api/email/send', method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(payload) } }, (res) => { console.log('[email-notif] sent, status:', res.statusCode); }); req.on('error', (err) => { console.error('[email-notif] failed:', err.message); }); req.write(payload); req.end(); } /** * Notify first approver when justification is submitted. */ function notifyOnSubmit(idxjustification) { const qryJustification = ` select j.title, 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] notifyOnSubmit: failed to get justification data', err); return; } const title = rows[0].title; const creatorName = rows[0].creator_name; const qryApprover = ` select t.nik, e.fullname from tbl_justificationttd t join dbssotcel.tbl_employee e on e.nik = t.nik where t.idxjustification = '${idxjustification}' and t.category = 'APR' and t.issigned = 0 and t.isdeleted = 0 order by t._idx asc limit 1 `; db.query(qryApprover, [], function(err2, rows2) { if (err2 || rows2.length === 0) { console.error('[email-notif] notifyOnSubmit: no approver found', err2); return; } postEmail(rows2[0].fullname, title, creatorName); }); }); } /** * Notify next approver after someone signs. */ function notifyOnSigned(idxjustification, signerNik) { const qryTitle = `select title 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 qrySigner = `select fullname from dbssotcel.tbl_employee where nik = '${signerNik}' limit 1`; db.query(qrySigner, [], function(err2, rows2) { if (err2 || rows2.length === 0) { console.error('[email-notif] notifyOnSigned: failed to get signer name', err2); return; } const signerName = rows2[0].fullname; const qryNext = ` select t.nik, e.fullname from tbl_justificationttd t join dbssotcel.tbl_employee e on e.nik = t.nik where t.idxjustification = '${idxjustification}' and t.category = 'APR' and t.issigned = 0 and t.isdeleted = 0 order by t._idx asc limit 1 `; db.query(qryNext, [], function(err3, rows3) { if (err3 || rows3.length === 0) { console.log('[email-notif] notifyOnSigned: no more approvers to notify'); return; } postEmail(rows3[0].fullname, title, signerName); }); }); }); } module.exports = { notifyOnSubmit, notifyOnSigned };