You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Global-sales/wai-server/services/outbound_messages.service.js

50 lines
1.5 KiB
JavaScript

'use strict';
const db = require('../config').database;
const { domain, name } = require('../config').server;
const { objectMapper, pick, isEmpty } = 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() || {};
};
const createOutboundMessage = async data => {
const r = await OutboundModelModel.create(data);
return r;
};
/**
*
*/
const upsertOutboundMessage = async (data, where = {}) => {
// 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 _where = isEmpty(where) ? data : where;
const [instance, created] = await OutboundModelModel.findOrCreate({ where: _where, defaults: { ...data } });
if (!created) {
await instance.update({ ...data }, { where });
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, createOutboundMessage, upsertOutboundMessage };