From 433cedb7e5cb59abb63035eecdf5116e339fa763 Mon Sep 17 00:00:00 2001 From: Lei OT Date: Mon, 30 Dec 2024 10:38:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E9=80=81=E4=BF=9D=E5=AD=98=E6=B6=88?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wai-server/api/messages/message.controller.js | 34 +++++++++---------- .../config/components/database.config.js | 1 + wai-server/config/components/log4j.config.js | 6 ++-- wai-server/models/outbound_messages.js | 15 +++++--- .../services/outbound_messages.service.js | 17 ++++++---- 5 files changed, 43 insertions(+), 30 deletions(-) diff --git a/wai-server/api/messages/message.controller.js b/wai-server/api/messages/message.controller.js index f80d686..00414e3 100644 --- a/wai-server/api/messages/message.controller.js +++ b/wai-server/api/messages/message.controller.js @@ -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'); diff --git a/wai-server/config/components/database.config.js b/wai-server/config/components/database.config.js index 2f443f4..4879ab1 100644 --- a/wai-server/config/components/database.config.js +++ b/wai-server/config/components/database.config.js @@ -52,6 +52,7 @@ const DB = new Sequelize(databaseConfig.database, databaseConfig.user, databaseC supportBigNumbers: true, bigNumberStrings: true, }, + logQueryParameters: true, pool: { max: 5, diff --git a/wai-server/config/components/log4j.config.js b/wai-server/config/components/log4j.config.js index 5408ec4..45911ec 100644 --- a/wai-server/config/components/log4j.config.js +++ b/wai-server/config/components/log4j.config.js @@ -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; diff --git a/wai-server/models/outbound_messages.js b/wai-server/models/outbound_messages.js index f4d3cf7..07e4086 100644 --- a/wai-server/models/outbound_messages.js +++ b/wai-server/models/outbound_messages.js @@ -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' }], + }, ], }, ); diff --git a/wai-server/services/outbound_messages.service.js b/wai-server/services/outbound_messages.service.js index d49d2bb..aa680b1 100644 --- a/wai-server/services/outbound_messages.service.js +++ b/wai-server/services/outbound_messages.service.js @@ -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 };