mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-06 19:16:27 +00:00
Simplify Load Balance transactions with async await
This commit is contained in:
@@ -344,128 +344,99 @@ function findReservationDetails(reservationNumbers) {
|
||||
}
|
||||
|
||||
// INSERT LOADING BALANCE
|
||||
function mapLoadBalanceDetails(rows, loadBalanceId, amountField = 'i_amount') {
|
||||
return rows.map((row) => ({
|
||||
i_load_balance: loadBalanceId,
|
||||
i_reservation_balance: row.i_reservation_balance,
|
||||
c_reservation_no: row.c_reservation_no,
|
||||
i_amount: row[amountField],
|
||||
i_issuer: row.i_issuer,
|
||||
i_issuer_code: row.i_issuer_code,
|
||||
i_issuer_name: row.i_issuer_name,
|
||||
i_issuer_fee: row.i_issuer_fee,
|
||||
i_channel: row.i_channel,
|
||||
i_channel_code: row.i_channel_code,
|
||||
i_channel_name: row.i_channel_name,
|
||||
i_channel_fee: row.i_channel_fee,
|
||||
}));
|
||||
}
|
||||
|
||||
function getInsertedLoadBalanceId(row) {
|
||||
const id = typeof row === 'object' && row !== null ? row.i_load_balance : row;
|
||||
if (!id) throw new Error('Database did not return a load balance ID');
|
||||
return id;
|
||||
}
|
||||
|
||||
async function replaceFlagCount(trx, now, cardNumber, uid, balance, flag) {
|
||||
await trx('trx.t_d_flag_counting')
|
||||
.where('c_card_number', cardNumber)
|
||||
.whereRaw('UPPER(c_uid) = ?', [uid])
|
||||
.del();
|
||||
|
||||
return trx('trx.t_d_flag_counting').insert({
|
||||
d_request: now,
|
||||
c_card_number: cardNumber,
|
||||
c_uid: uid,
|
||||
i_balance: balance,
|
||||
c_flag: flag,
|
||||
});
|
||||
}
|
||||
|
||||
function logTransactionFailure(operation, error) {
|
||||
const sqlState = typeof error?.code === 'string' && /^[A-Z0-9]{5}$/.test(error.code)
|
||||
? error.code
|
||||
: 'UNKNOWN';
|
||||
winston.logger.error('Load balance transaction failed | operation=' + operation + ' sqlstate=' + sqlState);
|
||||
}
|
||||
|
||||
const insertLoadingBalance = async (
|
||||
data,
|
||||
detail,
|
||||
reservationNumber,
|
||||
now,
|
||||
writeStatus,
|
||||
_writeStatus,
|
||||
cardNumber,
|
||||
uid,
|
||||
balance,
|
||||
flag
|
||||
) => {
|
||||
return await knex
|
||||
.transaction((trx) => {
|
||||
// Mengembalikan chain memastikan kegagalan SQL memicu rollback otomatis.
|
||||
return knex('trx.t_d_load_balance')
|
||||
.transacting(trx)
|
||||
.insert(data)
|
||||
.returning('i_load_balance')
|
||||
.then((resp) => {
|
||||
let id = typeof resp[0] === 'object' ? resp[0].i_load_balance : resp[0];
|
||||
let loadDetail = [];
|
||||
detail.forEach((element) => {
|
||||
loadDetail.push({
|
||||
i_load_balance: id ? id : id.i_load_balance,
|
||||
i_reservation_balance: element.i_reservation_balance,
|
||||
c_reservation_no: element.c_reservation_no,
|
||||
i_amount: element.i_amount,
|
||||
i_issuer: element.i_issuer,
|
||||
i_issuer_code: element.i_issuer_code,
|
||||
i_issuer_name: element.i_issuer_name,
|
||||
i_issuer_fee: element.i_issuer_fee,
|
||||
i_channel: element.i_channel,
|
||||
i_channel_code: element.i_channel_code,
|
||||
i_channel_name: element.i_channel_name,
|
||||
i_channel_fee: element.i_channel_fee,
|
||||
});
|
||||
});
|
||||
try {
|
||||
// Header, detail, riwayat, reservasi, dan flag harus tersimpan dalam satu transaksi.
|
||||
const result = await knex.transaction(async (trx) => {
|
||||
const [header] = await trx('trx.t_d_load_balance').insert(data).returning('i_load_balance');
|
||||
await trx('trx.t_d_load_balance_detail').insert(
|
||||
mapLoadBalanceDetails(detail, getInsertedLoadBalanceId(header))
|
||||
);
|
||||
|
||||
// insert detail load balance
|
||||
return knex('trx.t_d_load_balance_detail')
|
||||
.transacting(trx)
|
||||
.insert(loadDetail);
|
||||
})
|
||||
.then(() => {
|
||||
// insert load balance history
|
||||
return knex('trx.t_d_load_balance_history')
|
||||
.transacting(trx)
|
||||
.returning('i_load_balance')
|
||||
.insert(data);
|
||||
})
|
||||
.then((history) => {
|
||||
let id = typeof history[0] === 'object' ? history[0].i_load_balance : history[0];
|
||||
let loadDetail = [];
|
||||
detail.forEach((element) => {
|
||||
loadDetail.push({
|
||||
i_load_balance: id ? id : id.i_load_balance,
|
||||
i_reservation_balance: element.i_reservation_balance,
|
||||
c_reservation_no: element.c_reservation_no,
|
||||
i_amount: element.i_amount,
|
||||
i_issuer: element.i_issuer,
|
||||
i_issuer_code: element.i_issuer_code,
|
||||
i_issuer_name: element.i_issuer_name,
|
||||
i_issuer_fee: element.i_issuer_fee,
|
||||
i_channel: element.i_channel,
|
||||
i_channel_code: element.i_channel_code,
|
||||
i_channel_name: element.i_channel_name,
|
||||
i_channel_fee: element.i_channel_fee,
|
||||
});
|
||||
});
|
||||
const [history] = await trx('trx.t_d_load_balance_history').insert(data).returning('i_load_balance');
|
||||
await trx('trx.t_d_load_balance_history_detail').insert(
|
||||
mapLoadBalanceDetails(detail, getInsertedLoadBalanceId(history))
|
||||
);
|
||||
|
||||
// insert detail load balance
|
||||
return knex('trx.t_d_load_balance_history_detail')
|
||||
.transacting(trx)
|
||||
.insert(loadDetail);
|
||||
await trx('trx.t_d_reservation_balance')
|
||||
.update({
|
||||
c_status: 'I',
|
||||
c_status_name: 'IN PROGRESS',
|
||||
d_load_balance: now,
|
||||
c_load_balance_no: data.c_load_balance_no,
|
||||
c_flag_code: 'FW',
|
||||
c_flag: flag,
|
||||
d_modify: now,
|
||||
i_modify: null,
|
||||
c_modify_username: null,
|
||||
c_modify_fullname: null,
|
||||
})
|
||||
.then(() => {
|
||||
// update reservation
|
||||
return knex('trx.t_d_reservation_balance')
|
||||
.transacting(trx)
|
||||
.update({
|
||||
c_status: 'I',
|
||||
c_status_name: 'IN PROGRESS',
|
||||
d_load_balance: now,
|
||||
c_load_balance_no: data.c_load_balance_no,
|
||||
c_flag_code: 'FW',
|
||||
c_flag: flag,
|
||||
d_modify: now,
|
||||
i_modify: null,
|
||||
c_modify_username: null,
|
||||
c_modify_fullname: null,
|
||||
})
|
||||
.whereIn('c_reservation_no', reservationNumber);
|
||||
})
|
||||
.then(() => {
|
||||
// delete flag counting
|
||||
return knex('trx.t_d_flag_counting')
|
||||
.transacting(trx)
|
||||
.where('c_card_number', cardNumber)
|
||||
.whereRaw('UPPER(c_uid) = ?', [uid])
|
||||
.del();
|
||||
})
|
||||
.then(() => {
|
||||
// insert flag counting
|
||||
return knex('trx.t_d_flag_counting').transacting(trx).insert({
|
||||
d_request: now,
|
||||
c_card_number: cardNumber,
|
||||
c_uid: uid,
|
||||
i_balance: balance,
|
||||
c_flag: flag,
|
||||
});
|
||||
});
|
||||
})
|
||||
.then((result) => {
|
||||
return result.rowCount;
|
||||
})
|
||||
.catch((error) => {
|
||||
winston.logger.error(`Load balance insert rolled back | sqlstate=${error.code || 'UNKNOWN'}`);
|
||||
return error.message;
|
||||
.whereIn('c_reservation_no', reservationNumber);
|
||||
|
||||
return replaceFlagCount(trx, now, cardNumber, uid, balance, flag);
|
||||
});
|
||||
return result.rowCount;
|
||||
} catch (error) {
|
||||
logTransactionFailure('insert', error);
|
||||
return error.message;
|
||||
}
|
||||
};
|
||||
|
||||
// UPDATE LOADING BALANCE
|
||||
const updateLoadingBalance = async (
|
||||
data,
|
||||
now,
|
||||
@@ -476,67 +447,32 @@ const updateLoadingBalance = async (
|
||||
flag,
|
||||
loadBalanceNumber
|
||||
) => {
|
||||
await knex
|
||||
.transaction((trx) => {
|
||||
knex('trx.t_d_load_balance')
|
||||
.transacting(trx)
|
||||
.update(data)
|
||||
.where('c_load_balance_no', loadBalanceNumber)
|
||||
.then(() => {
|
||||
// insert load balance history
|
||||
return knex('trx.t_d_load_balance_history')
|
||||
.transacting(trx)
|
||||
.insert(data);
|
||||
try {
|
||||
await knex.transaction(async (trx) => {
|
||||
await trx('trx.t_d_load_balance').update(data).where('c_load_balance_no', loadBalanceNumber);
|
||||
await trx('trx.t_d_load_balance_history').insert(data);
|
||||
|
||||
await trx('trx.t_d_reservation_balance')
|
||||
.update({
|
||||
c_flag_code: writeStatus ? 'FW' : null,
|
||||
c_status: writeStatus ? 'I' : 'A',
|
||||
c_status_name: writeStatus ? 'IN PROGRESS' : 'LOADED',
|
||||
d_load_balance: now,
|
||||
c_load_balance_no: loadBalanceNumber,
|
||||
d_modify: now,
|
||||
i_modify: null,
|
||||
c_modify_username: null,
|
||||
c_modify_fullname: null,
|
||||
})
|
||||
.then(() => {
|
||||
// update reservation
|
||||
return knex('trx.t_d_reservation_balance')
|
||||
.transacting(trx)
|
||||
.update({
|
||||
c_flag_code: writeStatus ? 'FW' : null,
|
||||
c_status: writeStatus ? 'I' : 'A',
|
||||
c_status_name: writeStatus ? 'IN PROGRESS' : 'LOADED',
|
||||
d_load_balance: now,
|
||||
c_load_balance_no: loadBalanceNumber,
|
||||
d_modify: now,
|
||||
i_modify: null,
|
||||
c_modify_username: null,
|
||||
c_modify_fullname: null,
|
||||
})
|
||||
.where('c_load_balance_no', loadBalanceNumber);
|
||||
})
|
||||
.then(() => {
|
||||
// delete flag counting
|
||||
return knex('trx.t_d_flag_counting')
|
||||
.transacting(trx)
|
||||
.where('c_card_number', cardNumber)
|
||||
.whereRaw('UPPER(c_uid) = ?', [uid])
|
||||
.del();
|
||||
})
|
||||
.then(() => {
|
||||
// insert flag counting
|
||||
return knex('trx.t_d_flag_counting').transacting(trx).insert({
|
||||
d_request: now,
|
||||
c_card_number: cardNumber,
|
||||
c_uid: uid,
|
||||
i_balance: balance,
|
||||
c_flag: flag,
|
||||
});
|
||||
})
|
||||
.then(trx.commit)
|
||||
.catch(trx.rollback);
|
||||
})
|
||||
.then((result) => {
|
||||
// console.log(result);
|
||||
return result;
|
||||
})
|
||||
.catch((error) => {
|
||||
// console.log(error.message);
|
||||
return error.message;
|
||||
.where('c_load_balance_no', loadBalanceNumber);
|
||||
|
||||
await replaceFlagCount(trx, now, cardNumber, uid, balance, flag);
|
||||
});
|
||||
} catch (error) {
|
||||
logTransactionFailure('update', error);
|
||||
}
|
||||
};
|
||||
|
||||
// INSERT LOADING BALANCE HISTORY
|
||||
const insertLoadingBalanceHistoryOnly = async (
|
||||
data,
|
||||
detail,
|
||||
@@ -548,83 +484,30 @@ const insertLoadingBalanceHistoryOnly = async (
|
||||
balance,
|
||||
flag
|
||||
) => {
|
||||
await knex
|
||||
.transaction((trx) => {
|
||||
knex('trx.t_d_load_balance_history')
|
||||
.transacting(trx)
|
||||
.insert(data)
|
||||
.returning('i_load_balance')
|
||||
.then((resp) => {
|
||||
let id = resp[0];
|
||||
let loadDetail = [];
|
||||
detail.forEach((element) => {
|
||||
loadDetail.push({
|
||||
i_load_balance: id.i_load_balance,
|
||||
i_reservation_balance: element.i_reservation_balance,
|
||||
c_reservation_no: element.c_reservation_no,
|
||||
i_amount: element.i_amount,
|
||||
i_issuer: element.i_issuer,
|
||||
i_issuer_code: element.i_issuer_code,
|
||||
i_issuer_name: element.i_issuer_name,
|
||||
i_issuer_fee: element.i_issuer_fee,
|
||||
i_channel: element.i_channel,
|
||||
i_channel_code: element.i_channel_code,
|
||||
i_channel_name: element.i_channel_name,
|
||||
i_channel_fee: element.i_channel_fee,
|
||||
});
|
||||
});
|
||||
try {
|
||||
await knex.transaction(async (trx) => {
|
||||
const [history] = await trx('trx.t_d_load_balance_history').insert(data).returning('i_load_balance');
|
||||
await trx('trx.t_d_load_balance_history_detail').insert(
|
||||
mapLoadBalanceDetails(detail, getInsertedLoadBalanceId(history))
|
||||
);
|
||||
|
||||
// insert detail load balance
|
||||
return knex('trx.t_d_load_balance_history_detail')
|
||||
.transacting(trx)
|
||||
.insert(loadDetail);
|
||||
await trx('trx.t_d_reservation_balance')
|
||||
.update({
|
||||
c_flag_code: writeStatus ? 'FW' : null,
|
||||
c_status: writeStatus ? 'I' : 'A',
|
||||
c_status_name: writeStatus ? 'IN PROGRESS' : 'LOADED',
|
||||
d_modify: now,
|
||||
i_modify: null,
|
||||
c_modify_username: null,
|
||||
c_modify_fullname: null,
|
||||
})
|
||||
.then(() => {
|
||||
// update reservation
|
||||
return knex('trx.t_d_reservation_balance')
|
||||
.transacting(trx)
|
||||
.update({
|
||||
c_flag_code: writeStatus ? 'FW' : null,
|
||||
c_status: writeStatus ? 'I' : 'A',
|
||||
c_status_name: writeStatus ? 'IN PROGRESS' : 'LOADED',
|
||||
// d_load_balance: now,
|
||||
// c_load_balance_no: data.c_load_balance_no,
|
||||
d_modify: now,
|
||||
i_modify: null,
|
||||
c_modify_username: null,
|
||||
c_modify_fullname: null,
|
||||
})
|
||||
.whereIn('c_reservation_no', reservationNumber);
|
||||
})
|
||||
.then(() => {
|
||||
// delete flag counting
|
||||
return knex('trx.t_d_flag_counting')
|
||||
.transacting(trx)
|
||||
.where('c_card_number', cardNumber)
|
||||
.whereRaw('UPPER(c_uid) = ?', [uid])
|
||||
.del();
|
||||
})
|
||||
.then(() => {
|
||||
// insert flag counting
|
||||
return knex('trx.t_d_flag_counting').transacting(trx).insert({
|
||||
d_request: now,
|
||||
c_card_number: cardNumber,
|
||||
c_uid: uid,
|
||||
i_balance: balance,
|
||||
c_flag: flag,
|
||||
});
|
||||
})
|
||||
.then(trx.commit)
|
||||
.catch(trx.rollback);
|
||||
})
|
||||
.then((result) => {
|
||||
// console.log(result);
|
||||
return result;
|
||||
})
|
||||
.catch((error) => {
|
||||
// console.log(error.message);
|
||||
return error.message;
|
||||
.whereIn('c_reservation_no', reservationNumber);
|
||||
|
||||
await replaceFlagCount(trx, now, cardNumber, uid, balance, flag);
|
||||
});
|
||||
} catch (error) {
|
||||
logTransactionFailure('insert_history', error);
|
||||
}
|
||||
};
|
||||
|
||||
// FIND LOAD BALANCE
|
||||
@@ -655,123 +538,40 @@ const checkFlag = (cardNumber, uid) => {
|
||||
};
|
||||
|
||||
// UPDATE RESERVATION AND LOADING BALANCE CASE FAILED WRITE BEFORE
|
||||
const updateReservationAndLoadingBalance = async (
|
||||
reservationNumber,
|
||||
data,
|
||||
detail,
|
||||
now,
|
||||
flag
|
||||
) => {
|
||||
return await knex
|
||||
.transaction((trx) => {
|
||||
knex('trx.t_d_load_balance')
|
||||
.transacting(trx)
|
||||
.update(data)
|
||||
.where('c_load_balance_no', data.c_load_balance_no)
|
||||
.then(() => {
|
||||
// insert load balance history
|
||||
// winston.logger.debug(
|
||||
// `${reservationNumber} proses insert t_d_load_balance_history updateReservationAndLoadingBalance`
|
||||
// );
|
||||
return knex('trx.t_d_load_balance_history')
|
||||
.transacting(trx)
|
||||
.returning('i_load_balance')
|
||||
.insert(data);
|
||||
})
|
||||
.then((history) => {
|
||||
// winston.logger.debug(
|
||||
// `${JSON.stringify(history)} history t_d_load_balance_history_detail`
|
||||
// );
|
||||
// winston.logger.debug(
|
||||
// `${JSON.stringify(
|
||||
// history[0]
|
||||
// )} id t_d_load_balance_history_detail before parseint`
|
||||
// );
|
||||
let id = history[0];
|
||||
// winston.logger.debug(
|
||||
// `${id} id t_d_load_balance_history_detail id only`
|
||||
// );
|
||||
// winston.logger.debug(
|
||||
// `${id.i_load_balance} id t_d_load_balance_history_detail id.i_load_balance`
|
||||
// );
|
||||
let loadDetail = [];
|
||||
detail.forEach((element) => {
|
||||
loadDetail.push({
|
||||
i_load_balance: id.i_load_balance,
|
||||
i_reservation_balance: element.i_reservation_balance,
|
||||
c_reservation_no: element.c_reservation_no,
|
||||
i_amount: element.i_amount,
|
||||
i_issuer: element.i_issuer,
|
||||
i_issuer_code: element.i_issuer_code,
|
||||
i_issuer_name: element.i_issuer_name,
|
||||
i_issuer_fee: element.i_issuer_fee,
|
||||
i_channel: element.i_channel,
|
||||
i_channel_code: element.i_channel_code,
|
||||
i_channel_name: element.i_channel_name,
|
||||
i_channel_fee: element.i_channel_fee,
|
||||
});
|
||||
});
|
||||
|
||||
// winston.logger.debug(
|
||||
// `${reservationNumber} proses insert t_d_load_balance_history_detail updateReservationAndLoadingBalance: ${JSON.stringify(
|
||||
// loadDetail
|
||||
// )}`
|
||||
// );
|
||||
// insert detail load balance
|
||||
return knex('trx.t_d_load_balance_history_detail')
|
||||
.transacting(trx)
|
||||
.insert(loadDetail);
|
||||
})
|
||||
.then(() => {
|
||||
// update reservation
|
||||
// winston.logger.debug(
|
||||
// `${reservationNumber} proses update t_d_reservation_balance updateReservationAndLoadingBalance`
|
||||
// );
|
||||
return knex('trx.t_d_reservation_balance')
|
||||
.transacting(trx)
|
||||
.update({
|
||||
d_load_balance: data.d_load_balance,
|
||||
c_load_balance_no: data.c_load_balance_no,
|
||||
c_flag: flag,
|
||||
c_flag_code: null,
|
||||
c_status: 'A',
|
||||
c_status_name: 'LOADED',
|
||||
d_modify: now,
|
||||
i_modify: null,
|
||||
c_modify_username: null,
|
||||
c_modify_fullname: null,
|
||||
})
|
||||
.whereIn('c_reservation_no', reservationNumber);
|
||||
})
|
||||
.then(() => {
|
||||
// winston.logger.debug(`${reservationNumber} commit transaction`);
|
||||
return trx.commit();
|
||||
})
|
||||
.catch((error) => {
|
||||
winston.logger.debug(
|
||||
`${reservationNumber} rollback transaction: ${error.message}`
|
||||
);
|
||||
trx.rollback(); // Rollback transaction
|
||||
// throw error; // Rethrow error to outer catch
|
||||
});
|
||||
})
|
||||
.then((result) => {
|
||||
// winston.logger.debug(
|
||||
// `${reservationNumber} result success ${JSON.stringify(result)}}`
|
||||
// );
|
||||
// console.log(result);
|
||||
return true;
|
||||
})
|
||||
.catch((error) => {
|
||||
winston.logger.debug(
|
||||
`${reservationNumber} result error: ${error.message}`
|
||||
const updateReservationAndLoadingBalance = async (reservationNumber, data, detail, now, flag) => {
|
||||
try {
|
||||
await knex.transaction(async (trx) => {
|
||||
await trx('trx.t_d_load_balance').update(data).where('c_load_balance_no', data.c_load_balance_no);
|
||||
const [history] = await trx('trx.t_d_load_balance_history').insert(data).returning('i_load_balance');
|
||||
await trx('trx.t_d_load_balance_history_detail').insert(
|
||||
mapLoadBalanceDetails(detail, getInsertedLoadBalanceId(history))
|
||||
);
|
||||
// console.log(error.message);
|
||||
return false;
|
||||
await markReservationsLoaded(trx, reservationNumber, data, now, flag);
|
||||
});
|
||||
return true;
|
||||
} catch (error) {
|
||||
logTransactionFailure('update_reservations_and_balance', error);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
function markReservationsLoaded(trx, reservationNumbers, data, now, flag) {
|
||||
return trx('trx.t_d_reservation_balance')
|
||||
.update({
|
||||
d_load_balance: data.d_load_balance,
|
||||
c_load_balance_no: data.c_load_balance_no,
|
||||
c_flag: flag,
|
||||
c_flag_code: null,
|
||||
c_status: 'A',
|
||||
c_status_name: 'LOADED',
|
||||
d_modify: now,
|
||||
i_modify: null,
|
||||
c_modify_username: null,
|
||||
c_modify_fullname: null,
|
||||
})
|
||||
.whereIn('c_reservation_no', reservationNumbers);
|
||||
}
|
||||
|
||||
const getLastReservationFlag = (reservationNumber) => {
|
||||
let result = knex('trx.t_d_reservation_balance')
|
||||
.whereIn('c_reservation_no', reservationNumber)
|
||||
@@ -821,34 +621,11 @@ const updateStatusReservation = (reservationNumber, now, flag) => {
|
||||
|
||||
// update flag
|
||||
const updateFlag = async (now, cardNumber, uid, balance, flag) => {
|
||||
await knex
|
||||
.transaction((trx) => {
|
||||
knex('trx.t_d_flag_counting')
|
||||
.transacting(trx)
|
||||
.where('c_card_number', cardNumber)
|
||||
.whereRaw('UPPER(c_uid) = ?', [uid])
|
||||
.del()
|
||||
.then(() => {
|
||||
// insert flag counting
|
||||
return knex('trx.t_d_flag_counting').transacting(trx).insert({
|
||||
d_request: now,
|
||||
c_card_number: cardNumber,
|
||||
c_uid: uid,
|
||||
i_balance: balance,
|
||||
c_flag: flag,
|
||||
});
|
||||
})
|
||||
.then(trx.commit)
|
||||
.catch(trx.rollback);
|
||||
})
|
||||
.then((result) => {
|
||||
// console.log(result);
|
||||
return result;
|
||||
})
|
||||
.catch((error) => {
|
||||
// console.log(error.message);
|
||||
return error.message;
|
||||
});
|
||||
try {
|
||||
await knex.transaction((trx) => replaceFlagCount(trx, now, cardNumber, uid, balance, flag));
|
||||
} catch (error) {
|
||||
logTransactionFailure('replace_flag', error);
|
||||
}
|
||||
};
|
||||
|
||||
// GET ACCUMULATION BALANCE IN THIS MONTH FROM CARD HISTORY
|
||||
@@ -1078,66 +855,21 @@ const updateReservationAndInsertLoadingBalance = async (
|
||||
now,
|
||||
flag
|
||||
) => {
|
||||
return await knex
|
||||
.transaction((trx) => {
|
||||
knex('trx.t_d_load_balance')
|
||||
.transacting(trx)
|
||||
try {
|
||||
// Mode pemulihan memakai nominal reservasi sebagai nominal detail saldo.
|
||||
return await knex.transaction(async (trx) => {
|
||||
const [header] = await trx('trx.t_d_load_balance')
|
||||
.insert(dataLoadBalance)
|
||||
.returning('i_load_balance')
|
||||
.then((detail) => {
|
||||
let id = detail[0];
|
||||
let loadDetail = [];
|
||||
detailLoadBalance.forEach((element) => {
|
||||
loadDetail.push({
|
||||
i_load_balance: id.i_load_balance,
|
||||
i_reservation_balance: element.i_reservation_balance,
|
||||
c_reservation_no: element.c_reservation_no,
|
||||
i_amount: element.i_reservation_amount,
|
||||
i_issuer: element.i_issuer,
|
||||
i_issuer_code: element.i_issuer_code,
|
||||
i_issuer_name: element.i_issuer_name,
|
||||
i_issuer_fee: element.i_issuer_fee,
|
||||
i_channel: element.i_channel,
|
||||
i_channel_code: element.i_channel_code,
|
||||
i_channel_name: element.i_channel_name,
|
||||
i_channel_fee: element.i_channel_fee,
|
||||
});
|
||||
});
|
||||
|
||||
// insert detail load balance
|
||||
return knex('trx.t_d_load_balance_detail')
|
||||
.transacting(trx)
|
||||
.insert(loadDetail);
|
||||
})
|
||||
.then(() => {
|
||||
// update reservation
|
||||
return knex('trx.t_d_reservation_balance')
|
||||
.transacting(trx)
|
||||
.update({
|
||||
d_load_balance: dataLoadBalance.d_load_balance,
|
||||
c_load_balance_no: dataLoadBalance.c_load_balance_no,
|
||||
c_flag: flag,
|
||||
c_flag_code: null,
|
||||
c_status: 'A',
|
||||
c_status_name: 'LOADED',
|
||||
d_modify: now,
|
||||
i_modify: null,
|
||||
c_modify_username: null,
|
||||
c_modify_fullname: null,
|
||||
})
|
||||
.whereIn('c_reservation_no', reservationNumber);
|
||||
})
|
||||
.then(() => trx.commit())
|
||||
.catch(trx.rollback);
|
||||
})
|
||||
.then((result) => {
|
||||
// console.log(result);
|
||||
return result.rowCount;
|
||||
})
|
||||
.catch((error) => {
|
||||
// console.log(error.message);
|
||||
return error.message;
|
||||
.returning('i_load_balance');
|
||||
await trx('trx.t_d_load_balance_detail').insert(
|
||||
mapLoadBalanceDetails(detailLoadBalance, getInsertedLoadBalanceId(header), 'i_reservation_amount')
|
||||
);
|
||||
return markReservationsLoaded(trx, reservationNumber, dataLoadBalance, now, flag);
|
||||
});
|
||||
} catch (error) {
|
||||
logTransactionFailure('insert_balance_from_reservations', error);
|
||||
return error.message;
|
||||
}
|
||||
};
|
||||
|
||||
// GET TOPUP PARTIAL STATUS FROM GLOBAL PARAMETER
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
jest.mock('../../infrastructure/database/knex', () => ({ transaction: jest.fn() }));
|
||||
jest.mock('../helpers/winston.logger', () => ({ logger: { error: jest.fn() } }));
|
||||
|
||||
const db = require('../../infrastructure/database/knex');
|
||||
const { logger } = require('../helpers/winston.logger');
|
||||
const model = require('../modules/loadbalance/loadbalance.model');
|
||||
|
||||
const header = { c_load_balance_no: 'LBL1', d_load_balance: '2026-11-24' };
|
||||
const detail = {
|
||||
i_reservation_balance: 7,
|
||||
c_reservation_no: 'RBL1',
|
||||
i_amount: 20000,
|
||||
i_reservation_amount: 10000,
|
||||
};
|
||||
|
||||
function captureTransaction(failTable) {
|
||||
const writes = [];
|
||||
const failure = Object.assign(new Error('fixture write failure'), { code: '23514' });
|
||||
const trx = jest.fn((table) => {
|
||||
const query = {};
|
||||
|
||||
for (const method of ['insert', 'update', 'del', 'returning', 'where', 'whereIn', 'whereRaw']) {
|
||||
query[method] = (...args) => {
|
||||
writes.push([table, method, ...args]);
|
||||
return query;
|
||||
};
|
||||
}
|
||||
|
||||
query.then = (resolve, reject) => {
|
||||
if (table === failTable) return Promise.reject(failure).then(resolve, reject);
|
||||
const result = table === 'trx.t_d_load_balance' ? [{ i_load_balance: 10 }]
|
||||
: table === 'trx.t_d_load_balance_history' ? [{ i_load_balance: 20 }]
|
||||
: table === 'trx.t_d_reservation_balance' ? 1
|
||||
: { rowCount: 1 };
|
||||
return Promise.resolve(result).then(resolve, reject);
|
||||
};
|
||||
|
||||
return query;
|
||||
});
|
||||
|
||||
db.transaction.mockImplementation((callback) => callback(trx));
|
||||
return { trx, writes };
|
||||
}
|
||||
|
||||
beforeEach(() => jest.clearAllMocks());
|
||||
|
||||
test('simpan saldo memakai ID header dan riwayat masing-masing dalam satu transaksi', async () => {
|
||||
const { trx, writes } = captureTransaction();
|
||||
|
||||
await expect(model.insertLoadingBalance(
|
||||
header, [detail], ['RBL1'], '2026-11-24', false, 'card', 'UID', 20000, 1
|
||||
)).resolves.toBe(1);
|
||||
|
||||
expect(db.transaction).toHaveBeenCalledTimes(1);
|
||||
expect(trx.mock.calls.flat()).toEqual([
|
||||
'trx.t_d_load_balance', 'trx.t_d_load_balance_detail',
|
||||
'trx.t_d_load_balance_history', 'trx.t_d_load_balance_history_detail',
|
||||
'trx.t_d_reservation_balance', 'trx.t_d_flag_counting', 'trx.t_d_flag_counting',
|
||||
]);
|
||||
expect(writes).toContainEqual([
|
||||
'trx.t_d_load_balance_detail', 'insert', [expect.objectContaining({ i_load_balance: 10, i_amount: 20000 })],
|
||||
]);
|
||||
expect(writes).toContainEqual([
|
||||
'trx.t_d_load_balance_history_detail', 'insert', [expect.objectContaining({ i_load_balance: 20, i_amount: 20000 })],
|
||||
]);
|
||||
});
|
||||
|
||||
test('kegagalan detail menggagalkan callback transaksi sebelum status reservasi berubah', async () => {
|
||||
const { trx } = captureTransaction('trx.t_d_load_balance_history_detail');
|
||||
|
||||
await expect(model.updateReservationAndLoadingBalance(
|
||||
['RBL1'], header, [detail], '2026-11-24', 1
|
||||
)).resolves.toBe(false);
|
||||
|
||||
expect(trx).not.toHaveBeenCalledWith('trx.t_d_reservation_balance');
|
||||
await expect(db.transaction.mock.results[0].value).rejects.toMatchObject({ code: '23514' });
|
||||
expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('sqlstate=23514'));
|
||||
});
|
||||
|
||||
test('pemulihan saldo memakai nominal reservasi dan mengembalikan jumlah reservasi diperbarui', async () => {
|
||||
const { writes } = captureTransaction();
|
||||
|
||||
await expect(model.updateReservationAndInsertLoadingBalance(
|
||||
['RBL1'], header, [detail], '2026-11-24', 1
|
||||
)).resolves.toBe(1);
|
||||
|
||||
expect(writes).toContainEqual([
|
||||
'trx.t_d_load_balance_detail', 'insert', [expect.objectContaining({ i_amount: 10000 })],
|
||||
]);
|
||||
expect(writes).toContainEqual([
|
||||
'trx.t_d_reservation_balance', 'update', expect.objectContaining({ c_status: 'A', c_status_name: 'LOADED' }),
|
||||
]);
|
||||
});
|
||||
Reference in New Issue
Block a user