|
|
|
'use strict';
|
|
|
|
|
|
|
|
const db = require('../config').database;
|
|
|
|
const { domain, name } = require('../config').server;
|
|
|
|
const { objectMapper, pick } = require('../utils/commons.util');
|
|
|
|
const initModels = require('../models/init-models');
|
|
|
|
|
|
|
|
const Sequelize = db.sequelize;
|
|
|
|
const models = initModels(Sequelize);
|
|
|
|
|
|
|
|
const OutboundModelModel = models.outbound_messages;
|
|
|
|
|
|
|
|
const getOutboundMessage = async msg => {
|
|
|
|
const r = await OutboundModelModel.findOne({
|
|
|
|
where: msg,
|
|
|
|
});
|
|
|
|
return r?.toJSON() || {};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MySQL - Implemented with ON DUPLICATE KEY UPDATE
|
|
|
|
*/
|
|
|
|
const upsertOutboundMessage = async (key, data) => {
|
|
|
|
// let instance, created;
|
|
|
|
// if (key) {
|
|
|
|
// instance = await OutboundModelModel.create(data);
|
|
|
|
// created = true;
|
|
|
|
// } else {
|
|
|
|
// const [rows] = await OutboundModelModel.update(data, { where: { sn: key } });
|
|
|
|
// }
|
|
|
|
// console.log(rows);
|
|
|
|
const [instance, created] = await OutboundModelModel.findOrCreate({ where: { sn: key }, defaults: { ...data, sn: key } });
|
|
|
|
if (!created) {
|
|
|
|
await instance.update({ ...data, sn: key });
|
|
|
|
const savedI = await instance.save(); // reload
|
|
|
|
console.info('update OutboundMessage --- 2\n', savedI.toJSON());
|
|
|
|
return savedI.toJSON();
|
|
|
|
}
|
|
|
|
console.info('insert OutboundMessage\n', instance.toJSON(), created);
|
|
|
|
return instance.toJSON();
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = { getOutboundMessage, upsertOutboundMessage };
|