@peopl-health/nexus 3.8.26 → 3.8.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -714,7 +714,7 @@ const startConversationController = async (req, res) => {
|
|
|
714
714
|
});
|
|
715
715
|
} catch (error) {
|
|
716
716
|
logger.error('[StartConversation] Error', { error: error.message });
|
|
717
|
-
res.status(500).json({
|
|
717
|
+
res.status(error.statusCode || 500).json({
|
|
718
718
|
success: false,
|
|
719
719
|
error: error.message || 'Failed to start conversation'
|
|
720
720
|
});
|
|
@@ -7,6 +7,8 @@ const { logger } = require('../utils/logger');
|
|
|
7
7
|
|
|
8
8
|
const { getRecordByFilter, updateRecordByFilter } = require('../services/airtableService');
|
|
9
9
|
|
|
10
|
+
const { Thread } = require('./threadModel');
|
|
11
|
+
|
|
10
12
|
const messageSchema = new mongoose.Schema({
|
|
11
13
|
raw: { type: Object, default: null },
|
|
12
14
|
body: { type: String, required: true },
|
|
@@ -152,8 +154,18 @@ async function insertMessage(values) {
|
|
|
152
154
|
.filter(([k, v]) => v !== undefined && !k.startsWith('delivery_'))
|
|
153
155
|
);
|
|
154
156
|
messageData.clinical_context = clinicalContext;
|
|
155
|
-
|
|
156
|
-
|
|
157
|
+
let patientName = nombreWhatsapp;
|
|
158
|
+
if (!patientName && values.from_me === false) {
|
|
159
|
+
try {
|
|
160
|
+
const thread = await Thread.findOne({ code: values.numero }).select('nombre').lean();
|
|
161
|
+
patientName = thread?.nombre;
|
|
162
|
+
} catch (error) {
|
|
163
|
+
logger.error('Error fetching patient name from Thread:', error);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (patientName && values.from_me === false) {
|
|
167
|
+
messageData.nombre_whatsapp = patientName;
|
|
168
|
+
values.nombre_whatsapp = patientName;
|
|
157
169
|
}
|
|
158
170
|
if (!messageData.statusInfo && values.delivery_status) {
|
|
159
171
|
messageData.statusInfo = {
|
|
@@ -170,17 +170,13 @@ const processConversations = async (conversations, nameMap, unreadMap) => {
|
|
|
170
170
|
};
|
|
171
171
|
|
|
172
172
|
const startConversation = async (phoneNumber, message, name) => {
|
|
173
|
-
const thread = await Thread.findOneAndUpdate(
|
|
174
|
-
{ code: phoneNumber },
|
|
175
|
-
{
|
|
176
|
-
code: phoneNumber,
|
|
177
|
-
...(name && { nombre: name }),
|
|
178
|
-
active: true
|
|
179
|
-
},
|
|
180
|
-
{ upsert: true, new: true }
|
|
181
|
-
);
|
|
182
173
|
|
|
183
|
-
|
|
174
|
+
const existing = await Thread.findOne({ code: phoneNumber });
|
|
175
|
+
if (existing) {
|
|
176
|
+
const err = new Error('Conversation already exists');
|
|
177
|
+
err.statusCode = 409;
|
|
178
|
+
throw err;
|
|
179
|
+
}
|
|
184
180
|
|
|
185
181
|
const provider = requireProvider();
|
|
186
182
|
const templateName = `auto_${Date.now()}_${Math.floor(Math.random() * 1000)}`;
|
|
@@ -237,6 +233,12 @@ const startConversation = async (phoneNumber, message, name) => {
|
|
|
237
233
|
label: '[StartConversation]',
|
|
238
234
|
logContext: { phoneNumber },
|
|
239
235
|
onApproved: async (prov) => {
|
|
236
|
+
const thread = await Thread.findOneAndUpdate(
|
|
237
|
+
{ code: phoneNumber },
|
|
238
|
+
{ code: phoneNumber, ...(name && { nombre: name }), active: true },
|
|
239
|
+
{ upsert: true, new: true }
|
|
240
|
+
);
|
|
241
|
+
logger.info('[StartConversation] Thread ready', { phoneNumber, threadId: thread._id });
|
|
240
242
|
await sendMessage({ code: phoneNumber, contentSid: twilioContent.sid, variables: {} });
|
|
241
243
|
logger.info('[StartConversation] Template sent successfully', { phoneNumber, templateSid: twilioContent.sid });
|
|
242
244
|
await TemplateModel.updateOne({ sid: twilioContent.sid }, { status: 'APPROVED' });
|
|
@@ -260,7 +262,6 @@ const startConversation = async (phoneNumber, message, name) => {
|
|
|
260
262
|
|
|
261
263
|
return {
|
|
262
264
|
phoneNumber,
|
|
263
|
-
threadId: thread._id,
|
|
264
265
|
contentSid: twilioContent.sid,
|
|
265
266
|
approvalStatus: approvalResponse.status || 'PENDING'
|
|
266
267
|
};
|