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

35 lines
1.3 KiB
JavaScript

'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) => {
const defaultR = { direction: 'outbound' };
const r1 = pick(data, ['actionId', 'msgtype', 'externalId', 'id']);
const record = objectMapper(data, { from: 'froms', to: 'tos' }, false);
const byType = data.msgtype === 'text' ? { text_body: data.msgcontent.body, text_preview_url: data.msgcontent.preview_url } : {};
const toUpsert = { ...defaultR, ...r1, ...record, ...byType, sn: key, message_origin: JSON.stringify(data) };
const [instance, created] = await OutboundModelModel.upsert({ ...toUpsert }, { returning: true });
console.info('upsertOutboundMessage', { instance, created });
return instance.toJSON();
};
module.exports = { getOutboundMessage, upsertOutboundMessage };