发送保存消息

dev/supplier-email-drawer
Lei OT 9 months ago
parent 0bf953ab3a
commit 433cedb7e5

@ -3,7 +3,7 @@
const generateId = require('../../utils/generateId.util');
const { sessionStore } = require('../../core');
const { objectMapper, pick } = require('../../utils/commons.util');
const { upsertOutboundMessage } = require('../../services/outbound_messages.service');
const { createOutboundMessage } = require('../../services/outbound_messages.service');
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
@ -23,24 +23,24 @@ exports.sendText = async ctx => {
return;
}
try {
return wsToSend.sendTextMessage(to, content).then(({ messageId }) => {
// const messageId = generateId();
// wsToSend.sendTextMessage(to, content, actionId).then(({ messageId }) => {
// const messageId = generateId();
const _data = ctx.request.body;
const defaultR = { direction: 'outbound' };
const r1 = pick(_data, ['actionId', 'msgtype', 'externalId']);
r1.id = messageId;
r1.wamid = messageId;
r1.msg_status = 'accepted';
r1.createTime = Date.now();
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, message_origin: JSON.stringify(_data) };
upsertOutboundMessage(null, { ...toUpsert });
return 'Message sent successfully'; // { wsToSend, ret: 'Message sent successfully' };
});
// const sockMsg = await wsToSend.sendTextMessage(to, content, actionId);
const _data = ctx.request.body;
const defaultR = { direction: 'outbound' };
const r1 = pick(_data, ['actionId', 'msgtype', 'externalId']);
r1.id = actionId; // id not null
// r1.wamid = messageId;
r1.msg_status = 'ready';
r1.createTime = Date.now();
const record = objectMapper(_data, { from: 'from', to: 'to' }, false);
const byType = _data.msgtype === 'text' ? { text_body: _data.msgcontent.body, text_preview_url: _data.msgcontent.preview_url } : {};
const toUpsert = { ...defaultR, ...r1, ...record, ...byType, message_origin: JSON.stringify(_data) };
await createOutboundMessage({ ...toUpsert });
// return 'Message sent successfully'; // { wsToSend, ret: 'Message sent successfully' };
// });
wsToSend.sendTextMessage(to, content, actionId);
return 'Message sent successfully'; // { wsToSend, ret: 'Message sent successfully' };
} catch (error) {
console.error('Error sending message:', error);
ctx.assert(null, 500, 'Failed to send message');

@ -52,6 +52,7 @@ const DB = new Sequelize(databaseConfig.database, databaseConfig.user, databaseC
supportBigNumbers: true,
bigNumberStrings: true,
},
logQueryParameters: true,
pool: {
max: 5,

@ -39,8 +39,8 @@ log4js.configure(logConfig);
const logger = log4js.getLogger();
console.log = logger.info.bind(logger);
console.info = logger.info.bind(logger);
console.error = logger.error.bind(logger);
// console.log = logger.info.bind(logger);
// console.info = logger.info.bind(logger);
// console.error = logger.error.bind(logger);
module.exports = logger;

@ -35,18 +35,19 @@ module.exports = function(sequelize, DataTypes) {
defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
},
id: {
type: DataTypes.STRING(100),
allowNull: true,
type: DataTypes.STRING(200),
allowNull: false,
unique: 'outbound_messages_id_IDX',
},
wamid: {
type: DataTypes.STRING(200),
allowNull: true,
},
froms: {
from: {
type: DataTypes.STRING(100),
allowNull: true,
},
tos: {
to: {
type: DataTypes.STRING(100),
allowNull: true,
},
@ -131,6 +132,12 @@ module.exports = function(sequelize, DataTypes) {
using: 'BTREE',
fields: [{ name: 'sn' }],
},
{
name: 'outbound_messages_id_IDX',
unique: true,
using: 'BTREE',
fields: [{ name: 'id' }],
},
],
},
);

@ -2,7 +2,7 @@
const db = require('../config').database;
const { domain, name } = require('../config').server;
const { objectMapper, pick } = require('../utils/commons.util');
const { objectMapper, pick, isEmpty } = require('../utils/commons.util');
const initModels = require('../models/init-models');
const Sequelize = db.sequelize;
@ -17,10 +17,14 @@ const getOutboundMessage = async msg => {
return r?.toJSON() || {};
};
const createOutboundMessage = async (data) => {
return await OutboundModelModel.create(data);
}
/**
* MySQL - Implemented with ON DUPLICATE KEY UPDATE
*
*/
const upsertOutboundMessage = async (key, data) => {
const upsertOutboundMessage = async (data, where = {}) => {
// let instance, created;
// if (key) {
// instance = await OutboundModelModel.create(data);
@ -29,9 +33,10 @@ const upsertOutboundMessage = async (key, data) => {
// 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 } });
const _where = isEmpty(where) ? data : where;
const [instance, created] = await OutboundModelModel.findOrCreate({ where: _where, defaults: { ...data } });
if (!created) {
await instance.update({ ...data, sn: key });
await instance.update({ ...data, }, { where });
const savedI = await instance.save(); // reload
console.info('update OutboundMessage --- 2\n', savedI.toJSON());
return savedI.toJSON();
@ -40,4 +45,4 @@ const upsertOutboundMessage = async (key, data) => {
return instance.toJSON();
};
module.exports = { getOutboundMessage, upsertOutboundMessage };
module.exports = { getOutboundMessage, createOutboundMessage, upsertOutboundMessage };

Loading…
Cancel
Save