Skip to content

Instantly share code, notes, and snippets.

@Avi-E-Koenig
Last active May 30, 2020 10:38
Show Gist options
  • Save Avi-E-Koenig/eaf7fa2f80604da6b595ebe7309898d9 to your computer and use it in GitHub Desktop.
Save Avi-E-Koenig/eaf7fa2f80604da6b595ebe7309898d9 to your computer and use it in GitHub Desktop.
const db = require('../models');
const Logger = require('../helpers/logger.helper');
const callLifeCycle = db.call_life_cycle;
const newcall_handler = async (dataObj, insert_params, where_params) => {
const t1 = await sequelize.transaction();
try {
let query_newcall_result = await callLifeCycle.findOne({
where: where_params,
transaction: t1,
lock: t1.LOCK.UPDATE,
});
if (!query_newcall_result) {
insert_params.ivruniqueid = dataObj.ivruniqueid;
result = await callLifeCycle.create(insert_params, { transaction: t1 });
return result;
}
await transaction.commit();
result = query_newcall_result;
return result;
} catch (error) {
const rollback = await t.rollback();
Logger.error('create err', {
rollback,
where_params,
insert_params,
time: Date.now(),
error,
});
return null;
}
};
const hangup_handler = async (dataObj, insert_params, where_params) => {
const t1 = await sequelize.transaction();
try {
result = null;
let query_hangup_result = await callLifeCycle.findOne({
where: where_params,
transaction: t1,
lock: t1.LOCK.UPDATE,
});
if (
query_hangup_result &&
query_hangup_result.dataValues &&
query_hangup_result.dataValues.hangup == false
) {
insert_params.hangup = 1;
query_hangup_result = await callLifeCycle.update(
insert_params,
{
where: where_params,
transaction: t1,
},
{},
);
console.log('hangup_handler -> query_hangup_result', query_hangup_result);
where_params.hangup = 1;
result = await callLifeCycle.findOne({
where: where_params,
transaction: t1,
returning: true,
raw: true,
});
console.log(
'exports.addEntryIfNotExists -> result FOUND HANGUP FALSE',
Date.now(),
result,
);
}
await transaction.commit();
return result;
} catch (error) {
console.log('hangup_handler -> error', error);
const rollback = await t.rollback();
Logger.error('create err', {
rollback,
where_params,
insert_params,
time: Date.now(),
error,
});
return null;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment