@peopl-health/nexus 3.5.3 → 3.5.4
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 };
|
|
@@ -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 = {
|
|
@@ -130,6 +134,7 @@ const builtInControllers = {
|
|
|
130
134
|
markMessagesAsReadController: conversationController.markMessagesAsReadController,
|
|
131
135
|
searchMessagesByNumberController: conversationController.searchMessagesByNumberController,
|
|
132
136
|
reportBugController: bugReportController.reportBugController,
|
|
137
|
+
getBugByWhatsappIdController: bugReportController.getBugByWhatsappIdController,
|
|
133
138
|
caseDocumentationController: caseDocumentationController.caseDocumentationController,
|
|
134
139
|
|
|
135
140
|
// Interaction controllers
|