Last active
May 30, 2020 10:38
-
-
Save Avi-E-Koenig/eaf7fa2f80604da6b595ebe7309898d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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