@peopl-health/nexus 3.5.3 → 3.5.5
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.
|
@@ -3,11 +3,10 @@ const { Logging_ID } = require('../config/airtableConfig');
|
|
|
3
3
|
const { logger } = require('../utils/logger');
|
|
4
4
|
|
|
5
5
|
const { Message } = require('../models/messageModel');
|
|
6
|
+
const { getBug, VALID_SEVERITIES } = require('../models/bugModel');
|
|
6
7
|
|
|
7
8
|
const { addRecord, getRecordByFilter } = require('../services/airtableService');
|
|
8
9
|
|
|
9
|
-
const VALID_SEVERITIES = ['low', 'medium', 'high', 'critical'];
|
|
10
|
-
|
|
11
10
|
async function logBugReportToAirtable(reporter, whatsapp_id, description, severity, messageIds = []) {
|
|
12
11
|
try {
|
|
13
12
|
let conversation = null;
|
|
@@ -46,18 +45,68 @@ const reportBugController = async (req, res) => {
|
|
|
46
45
|
if (!reporter) return res.status(400).json({ success: false, error: 'Reporter username is required' });
|
|
47
46
|
if (!whatsapp_id) return res.status(400).json({ success: false, error: 'WhatsApp ID is required' });
|
|
48
47
|
if (!VALID_SEVERITIES.includes(severity)) {
|
|
49
|
-
return res.status(400).json({ success: false, error:
|
|
48
|
+
return res.status(400).json({ success: false, error: `Severity must be one of: ${VALID_SEVERITIES.join(', ')}` });
|
|
50
49
|
}
|
|
51
50
|
|
|
51
|
+
const Bug = getBug();
|
|
52
|
+
const bug = await Bug.create({ reporter, whatsapp_id, description, severity, messages: messages || [] });
|
|
53
|
+
|
|
52
54
|
logBugReportToAirtable(reporter, whatsapp_id, description, severity, messages).catch(err =>
|
|
53
55
|
logger.error('Background bug report logging failed:', { error: err.message })
|
|
54
56
|
);
|
|
55
57
|
|
|
56
|
-
res.status(201).json({ success: true, message: 'Bug report submitted successfully' });
|
|
58
|
+
res.status(201).json({ success: true, message: 'Bug report submitted successfully', bug });
|
|
57
59
|
} catch (error) {
|
|
58
60
|
logger.error('Error submitting bug report:', { error: error.message });
|
|
59
61
|
res.status(500).json({ success: false, error: error.message });
|
|
60
62
|
}
|
|
61
63
|
};
|
|
62
64
|
|
|
63
|
-
|
|
65
|
+
const getBugByWhatsappIdController = async (req, res) => {
|
|
66
|
+
try {
|
|
67
|
+
const { whatsapp_id } = req.params;
|
|
68
|
+
|
|
69
|
+
if (!whatsapp_id || whatsapp_id.trim().length === 0) {
|
|
70
|
+
return res.status(400).json({ success: false, error: 'WhatsApp ID is required' });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const maxLimit = 100;
|
|
74
|
+
const limit = Math.min(Math.max(parseInt(req.query.limit) || 50, 1), maxLimit);
|
|
75
|
+
const page = Math.max(parseInt(req.query.page) || 1, 1);
|
|
76
|
+
const skip = (page - 1) * limit;
|
|
77
|
+
|
|
78
|
+
const Bug = getBug();
|
|
79
|
+
const total = await Bug.countDocuments({ whatsapp_id: whatsapp_id });
|
|
80
|
+
const bugs = await Bug.find({ whatsapp_id: whatsapp_id })
|
|
81
|
+
.populate('messages')
|
|
82
|
+
.sort({ createdAt: -1 })
|
|
83
|
+
.skip(skip)
|
|
84
|
+
.limit(limit);
|
|
85
|
+
|
|
86
|
+
const bugsWithCount = bugs.map(bug => ({
|
|
87
|
+
...bug.toObject(),
|
|
88
|
+
count: bug.messages ? bug.messages.length : 0
|
|
89
|
+
}));
|
|
90
|
+
|
|
91
|
+
const totalPages = Math.ceil(total / limit);
|
|
92
|
+
|
|
93
|
+
res.status(200).json({
|
|
94
|
+
success: true,
|
|
95
|
+
whatsappId: whatsapp_id,
|
|
96
|
+
bugs: bugsWithCount,
|
|
97
|
+
pagination: {
|
|
98
|
+
page,
|
|
99
|
+
limit,
|
|
100
|
+
total,
|
|
101
|
+
totalPages,
|
|
102
|
+
hasNext: page < totalPages,
|
|
103
|
+
hasPrev: page > 1
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
} catch (error) {
|
|
107
|
+
logger.error('Error fetching bug reports:', { error: error.message });
|
|
108
|
+
res.status(500).json({ success: false, error: error.message });
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
module.exports = { reportBugController, getBugByWhatsappIdController };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { Monitoreo_ID } = require('../config/airtableConfig');
|
|
1
|
+
const { Monitoreo_ID, Symptoms_ID } = require('../config/airtableConfig');
|
|
2
2
|
|
|
3
3
|
const { logger } = require('../utils/logger');
|
|
4
4
|
|
|
@@ -23,4 +23,68 @@ const getPatientInfoController = async (req, res) => {
|
|
|
23
23
|
}
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
const getPatientTriageInfoController = async (req, res) => {
|
|
27
|
+
try {
|
|
28
|
+
const { whatsapp_id } = req.params;
|
|
29
|
+
if (!whatsapp_id) return res.status(400).json({ success: false, error: 'WhatsApp ID is required' });
|
|
30
|
+
|
|
31
|
+
const maxLimit = 100;
|
|
32
|
+
const limit = Math.min(Math.max(parseInt(req.query.limit) || 50, 1), maxLimit);
|
|
33
|
+
const page = Math.max(parseInt(req.query.page) || 1, 1);
|
|
34
|
+
const skip = (page - 1) * limit;
|
|
35
|
+
|
|
36
|
+
const records = await getRecordByFilter(Symptoms_ID, 'triaje_symptoms', `{whatsapp_id}="${whatsapp_id}"`);
|
|
37
|
+
|
|
38
|
+
if (!records || records.length === 0) {
|
|
39
|
+
return res.status(404).json({ success: false, error: 'Patient not found' });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const { startDate, endDate } = req.query;
|
|
43
|
+
|
|
44
|
+
let filtered = records;
|
|
45
|
+
if (startDate || endDate) {
|
|
46
|
+
const fromDate = startDate ? new Date(`${startDate}T00:00:00.000Z`) : null;
|
|
47
|
+
const toDate = endDate ? new Date(`${endDate}T23:59:59.999Z`) : null;
|
|
48
|
+
|
|
49
|
+
filtered = records.filter(r => {
|
|
50
|
+
const created = r?.Created ? new Date(r.Created) : null;
|
|
51
|
+
if (!created) return false;
|
|
52
|
+
if (fromDate && created < fromDate) return false;
|
|
53
|
+
if (toDate && created > toDate) return false;
|
|
54
|
+
return true;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const sorted = filtered.sort((a, b) => new Date(b?.Created || 0) - new Date(a?.Created || 0));
|
|
59
|
+
|
|
60
|
+
const paginatedRecords = sorted.slice(skip, skip + limit);
|
|
61
|
+
|
|
62
|
+
const triageRecords = paginatedRecords.map(record => ({
|
|
63
|
+
id: record.recordID,
|
|
64
|
+
created: record?.Created || null,
|
|
65
|
+
full_symptoms_metadata: record?.full_symptoms_metadata || null,
|
|
66
|
+
whatsapp_id: record?.whatsapp_id || null
|
|
67
|
+
}));
|
|
68
|
+
|
|
69
|
+
const total = filtered.length;
|
|
70
|
+
const totalPages = Math.ceil(total / limit);
|
|
71
|
+
|
|
72
|
+
res.status(200).json({
|
|
73
|
+
success: true,
|
|
74
|
+
records: triageRecords,
|
|
75
|
+
pagination: {
|
|
76
|
+
page,
|
|
77
|
+
limit,
|
|
78
|
+
total,
|
|
79
|
+
totalPages,
|
|
80
|
+
hasNext: page < totalPages,
|
|
81
|
+
hasPrev: page > 1
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
} catch (error) {
|
|
85
|
+
logger.error('Error fetching patient triage info:', { error: error.message, whatsapp_id: req.params?.whatsapp_id });
|
|
86
|
+
res.status(500).json({ success: false, error: error.message });
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
module.exports = { getPatientInfoController, getPatientTriageInfoController };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const mongoose = require('mongoose');
|
|
2
|
+
|
|
3
|
+
const { getDb, getModelDatabase } = require('../config/mongoConfig');
|
|
4
|
+
const { logger } = require('../utils/logger');
|
|
5
|
+
|
|
6
|
+
const VALID_SEVERITIES = ['low', 'medium', 'high', 'critical'];
|
|
7
|
+
|
|
8
|
+
const bugSchema = new mongoose.Schema({
|
|
9
|
+
reporter: { type: String, required: true, index: true },
|
|
10
|
+
whatsapp_id: { type: String, required: true, index: true },
|
|
11
|
+
description: { type: String, required: true },
|
|
12
|
+
severity: { type: String, required: true, enum: VALID_SEVERITIES },
|
|
13
|
+
messages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Message' }]
|
|
14
|
+
}, { timestamps: true });
|
|
15
|
+
|
|
16
|
+
const getBug = () => {
|
|
17
|
+
const dbName = getModelDatabase('Bug');
|
|
18
|
+
const db = dbName ? getDb(dbName) : mongoose;
|
|
19
|
+
logger.debug('[Bug] Using database', { dbName: dbName || 'default' });
|
|
20
|
+
return db.models.Bug || db.model('Bug', bugSchema);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
module.exports = { getBug, bugSchema, VALID_SEVERITIES };
|
package/lib/routes/index.js
CHANGED
|
@@ -22,8 +22,12 @@ const conversationRouteDefinitions = {
|
|
|
22
22
|
'POST /reply': 'getConversationReplyController',
|
|
23
23
|
'POST /send-template': 'sendTemplateToNewNumberController',
|
|
24
24
|
'POST /:phoneNumber/read': 'markMessagesAsReadController',
|
|
25
|
+
'POST /case-documentation': 'caseDocumentationController',
|
|
25
26
|
'POST /report-bug': 'reportBugController',
|
|
26
|
-
'POST /
|
|
27
|
+
'POST /bug': 'reportBugController',
|
|
28
|
+
'GET /bug/:whatsapp_id': 'getBugByWhatsappIdController',
|
|
29
|
+
'POST /interaction': 'addInteractionController',
|
|
30
|
+
'GET /interaction/:whatsapp_id': 'getInteractionsByWhatsappIdController'
|
|
27
31
|
};
|
|
28
32
|
|
|
29
33
|
const mediaRouteDefinitions = {
|
|
@@ -52,7 +56,8 @@ const interactionRouteDefinitions = {
|
|
|
52
56
|
};
|
|
53
57
|
|
|
54
58
|
const patientRouteDefinitions = {
|
|
55
|
-
'GET /:id': 'getPatientInfoController'
|
|
59
|
+
'GET /:id': 'getPatientInfoController',
|
|
60
|
+
'GET /triage/:whatsapp_id': 'getPatientTriageInfoController'
|
|
56
61
|
};
|
|
57
62
|
|
|
58
63
|
const templateRouteDefinitions = {
|
|
@@ -130,6 +135,7 @@ const builtInControllers = {
|
|
|
130
135
|
markMessagesAsReadController: conversationController.markMessagesAsReadController,
|
|
131
136
|
searchMessagesByNumberController: conversationController.searchMessagesByNumberController,
|
|
132
137
|
reportBugController: bugReportController.reportBugController,
|
|
138
|
+
getBugByWhatsappIdController: bugReportController.getBugByWhatsappIdController,
|
|
133
139
|
caseDocumentationController: caseDocumentationController.caseDocumentationController,
|
|
134
140
|
|
|
135
141
|
// Interaction controllers
|
|
@@ -156,6 +162,7 @@ const builtInControllers = {
|
|
|
156
162
|
|
|
157
163
|
// Patient controllers
|
|
158
164
|
getPatientInfoController: patientController.getPatientInfoController,
|
|
165
|
+
getPatientTriageInfoController: patientController.getPatientTriageInfoController,
|
|
159
166
|
|
|
160
167
|
// Template controllers
|
|
161
168
|
createTemplate: templateController.createTemplate,
|