new funtion helper for scheduler

This commit is contained in:
WildanFahri
2026-04-26 16:09:29 +07:00
parent d84bc6941e
commit fef18ff448
+153 -25
View File
@@ -1,4 +1,6 @@
const CryptoJS = require('crypto-js');
const { randomUUID } = require('crypto');
const knex = require('../../../infrastructure/database/knex');
// ENCRYPT TEXT
const encryptText = (text) => {
@@ -64,41 +66,35 @@ function generateXMLRequest(urn, headers, items) {
xmlItem += `<item>
<!--Optional:-->
<ITEMNO_ACC>${String(index + 1).padStart(
10,
'0'
)}</ITEMNO_ACC>
10,
'0'
)}</ITEMNO_ACC>
<!--Optional:-->
<CUSTOMER>${
item.c_account_code_topup
? item.c_account_code_topup
: ''
}</CUSTOMER>
<CUSTOMER>${item.c_account_code_topup
? item.c_account_code_topup
: ''
}</CUSTOMER>
<!--Optional:-->
<GL_ACCOUNT>${
item.c_account_topup ? item.c_account_topup : ''
}</GL_ACCOUNT>
<GL_ACCOUNT>${item.c_account_topup ? item.c_account_topup : ''
}</GL_ACCOUNT>
<!--Optional:-->
<BUS_AREA>${item.ba ? item.ba : ''}</BUS_AREA>
<!--Optional:-->
<AMT_DOCCUR>${
item.amount ? item.amount : ''
}</AMT_DOCCUR>
<AMT_DOCCUR>${item.amount ? item.amount : ''
}</AMT_DOCCUR>
<!--Optional:-->
<BLINE_DATE>${
item.posting_date ? item.posting_date : ''
}</BLINE_DATE>
<BLINE_DATE>${item.posting_date ? item.posting_date : ''
}</BLINE_DATE>
<!--Optional:-->
<COSTCENTER></COSTCENTER>
<!--Optional:-->
<PROFITCENTER></PROFITCENTER>
<!--Optional:-->
<ALLOC_NMBR>${
item.c_invoice_no ? item.c_invoice_no : ''
}</ALLOC_NMBR>
<ALLOC_NMBR>${item.c_invoice_no ? item.c_invoice_no : ''
}</ALLOC_NMBR>
<!--Optional:-->
<ITEM_TEXT>${
item.item_text ? item.item_text : ''
}</ITEM_TEXT>
<ITEM_TEXT>${item.item_text ? item.item_text : ''
}</ITEM_TEXT>
<!--Optional:-->
<DE_CRE_IND>${item.de_cre}</DE_CRE_IND>
</item>`;
@@ -286,7 +282,7 @@ function getXMLData(response) {
resSAP.message =
'[SAP] ' +
soapBody['ZFMCMS_FI_001.Response']['C_T_RETURN']['item'][
'MESSAGE_1'
'MESSAGE_1'
];
} else {
// resSAP.data.ITEM = soapBody['ZFMCMS_FI_001.Response']['C_T_RETURN']['item']
@@ -298,7 +294,7 @@ function getXMLData(response) {
let message =
'[SAP] ' +
soapBody['ZFMCMS_FI_001.Response']['C_T_RETURN']['item'][
indexMessage
indexMessage
]['MESSAGE_1'];
resSAP.message = message;
@@ -313,6 +309,136 @@ function getXMLData(response) {
return resSAP;
}
async function startScheduler(schedulerCode) {
return await knex.transaction(async (trx) => {
const job = await knex('log.t_monitoring_scheduler_history')
.transacting(trx)
.where('c_scheduler_code', schedulerCode)
.where('c_status', 'idle')
.whereRaw(`
d_run_datetime BETWEEN NOW() - INTERVAL '3 minutes'
AND NOW() + INTERVAL '1 minute'
`)
.orderBy('d_run_datetime', 'desc')
.forUpdate()
.first();
let result;
if (!job) {
const master = await knex('log.t_monitoring_master_scheduler_name as mn')
.join('log.t_monitoring_master_scheduler_time as mt', 'mn.i_scheduler_name', 'mt.i_scheduler_name')
.transacting(trx)
.select(
'mn.i_scheduler_name',
'mn.c_scheduler_name',
'mn.c_scheduler_code',
'mn.c_category',
'mt.i_scheduler_time'
)
.where('mn.c_scheduler_code', schedulerCode)
.first();
if (!master) {
throw new Error('Master scheduler not found');
}
const token = randomUUID();
const inserted = await knex('log.t_monitoring_scheduler_history')
.transacting(trx)
.insert({
i_scheduler_name: master.i_scheduler_name,
i_scheduler_time: master.i_scheduler_time,
c_scheduler_name: master.c_scheduler_name,
d_run_datetime: knex.fn.now(),
c_status: 'processing',
d_started_at: knex.fn.now(),
c_execution_token: token,
c_category: master.c_category,
c_type: 'AUTO',
d_created: knex.fn.now(),
i_create: 0,
c_create_username: 'system',
c_create_fullname: 'system',
c_scheduler_code: master.c_scheduler_code
})
.returning('*');
result = inserted[0];
} else {
const token = randomUUID();
const updated = await knex('log.t_monitoring_scheduler_history')
.transacting(trx)
.where('i_scheduler_history', job.i_scheduler_history)
.update({
c_status: 'processing',
d_started_at: knex.fn.now(),
c_execution_token: token
})
.returning('*');
result = updated[0];
}
return result;
});
}
async function finishScheduler(token = null, schedulerCode = null) {
return await knex.transaction(async (trx) => {
let result = null;
if (token) {
const updated = await knex('log.t_monitoring_scheduler_history')
.transacting(trx)
.where('c_execution_token', token)
.where('c_status', 'processing')
.update({
c_status: 'finished',
d_finished_at: knex.fn.now()
})
.returning('*');
if (updated.length > 0) {
result = updated[0];
}
}
if (!result && schedulerCode) {
const job = await knex('log.t_monitoring_scheduler_history')
.transacting(trx)
.where('c_scheduler_code', schedulerCode)
.where('c_status', 'processing')
.whereNotNull('d_started_at')
.whereRaw(`d_started_at >= NOW() - INTERVAL '5 minutes'`)
.orderBy('d_started_at', 'desc')
.forUpdate()
.first();
if (job) {
const updated = await knex('log.t_monitoring_scheduler_history')
.transacting(trx)
.where('i_scheduler_history', job.i_scheduler_history)
.update({
c_status: 'finished',
d_finished_at: knex.fn.now()
})
.returning('*');
result = updated[0];
}
}
return result;
});
}
module.exports = {
encryptText,
decryptText,
@@ -322,4 +448,6 @@ module.exports = {
generateXMLRequest,
postingSAP,
getXMLData,
startScheduler,
finishScheduler,
};