@peopl-health/nexus 2.5.10 → 2.5.11

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.
@@ -14,6 +14,7 @@ const Monitoreo_ID = require('./runtimeConfig').get('AIRTABLE_MONITOREO_ID') ||
14
14
  const Programa_Juntas_ID = require('./runtimeConfig').get('AIRTABLE_PROGRAMA_JUNTAS_ID') || 'appKFWzkcDEWlrXBE';
15
15
  const Symptoms_ID = require('./runtimeConfig').get('AIRTABLE_SYMPTOMS_ID') || 'appQRhZlQ9tMfYZWJ';
16
16
  const Webinars_Leads_ID = require('./runtimeConfig').get('AIRTABLE_WEBINARS_LEADS_ID') || 'appzjpVXTI0TgqGPq';
17
+ const Product_ID = require('./runtimeConfig').get('AIRTABLE_PRODUCT_ID') || 'appu2YDW2pKDYLL5H';
17
18
 
18
19
  // Initialize Airtable only if API key is provided
19
20
  let airtable = null;
@@ -29,7 +30,8 @@ const BASE_MAP = {
29
30
  monitoreo: Monitoreo_ID,
30
31
  programa: Programa_Juntas_ID,
31
32
  symptoms: Symptoms_ID,
32
- webinars: Webinars_Leads_ID
33
+ webinars: Webinars_Leads_ID,
34
+ product: Product_ID
33
35
  };
34
36
 
35
37
  module.exports = {
@@ -43,7 +45,7 @@ module.exports = {
43
45
  Programa_Juntas_ID,
44
46
  Symptoms_ID,
45
47
  Webinars_Leads_ID,
46
-
48
+ Product_ID,
47
49
  // Helper function to get base by ID
48
50
  getBase: (baseKeyOrId = require('./runtimeConfig').get('AIRTABLE_BASE_ID')) => {
49
51
  if (!airtable) {
@@ -0,0 +1,56 @@
1
+ const { addRecord, getRecordByFilter } = require('../services/airtableService');
2
+ const { Product_ID } = require('../config/airtableConfig');
3
+ const { logger } = require('../utils/logger');
4
+
5
+ async function logCaseDocumentationToAirtable(reporter, whatsapp_id, tableName, dynamicFields = {}) {
6
+ try {
7
+ let patientId = null;
8
+ try {
9
+ const patientRecords = await getRecordByFilter(Product_ID, 'estado_general', `{whatsapp_id}='${whatsapp_id}'`);
10
+ if (patientRecords && patientRecords.length > 0) {
11
+ patientId = patientRecords[0].product_record_id;
12
+ }
13
+ } catch (err) {
14
+ logger.warn('Could not find patient in estado_general:', err.message);
15
+ }
16
+
17
+ const airtableData = {
18
+ reporter,
19
+ ...(patientId && { patient_id: [patientId] }),
20
+ ...dynamicFields
21
+ };
22
+
23
+ Object.keys(airtableData).forEach(key => {
24
+ if (airtableData[key] === undefined) {
25
+ delete airtableData[key];
26
+ }
27
+ });
28
+
29
+ await addRecord(Product_ID, tableName, airtableData);
30
+ logger.info(`Case documentation logged to Airtable successfully in table: ${tableName}`);
31
+ } catch (error) {
32
+ logger.error('Error logging case documentation to Airtable:', error);
33
+ throw error;
34
+ }
35
+ }
36
+
37
+ const caseDocumentationController = async (req, res) => {
38
+ try {
39
+ const { reporter, whatsapp_id, table_name, ...dynamicFields } = req.body;
40
+
41
+ if (!reporter) { return res.status(400).json({ success: false, error: 'Reporter username is required' }); }
42
+ if (!whatsapp_id) { return res.status(400).json({ success: false, error: 'WhatsApp ID is required' }); }
43
+ if (!table_name) { return res.status(400).json({ success: false, error: 'Table name is required' }); }
44
+
45
+ logCaseDocumentationToAirtable(reporter, whatsapp_id, table_name, dynamicFields).catch(err =>
46
+ logger.error('Background case documentation logging failed:', err)
47
+ );
48
+
49
+ res.status(201).json({ success: true, message: 'Case documentation submitted successfully' });
50
+ } catch (error) {
51
+ logger.error('Error submitting case documentation:', error);
52
+ res.status(500).json({ success: false, error: error.message });
53
+ }
54
+ };
55
+
56
+ module.exports = { caseDocumentationController };
@@ -23,7 +23,8 @@ const conversationRouteDefinitions = {
23
23
  'POST /reply': 'getConversationReplyController',
24
24
  'POST /send-template': 'sendTemplateToNewNumberController',
25
25
  'POST /:phoneNumber/read': 'markMessagesAsReadController',
26
- 'POST /report-bug': 'reportBugController'
26
+ 'POST /report-bug': 'reportBugController',
27
+ 'POST /case-documentation': 'caseDocumentationController'
27
28
  };
28
29
 
29
30
  const mediaRouteDefinitions = {
@@ -91,6 +92,7 @@ const createRouter = (routeDefinitions, controllers) => {
91
92
  // Import built-in controllers
92
93
  const assistantController = require('../controllers/assistantController');
93
94
  const bugReportController = require('../controllers/bugReportController');
95
+ const caseDocumentationController = require('../controllers/caseDocumentationController');
94
96
  const conversationController = require('../controllers/conversationController');
95
97
  const interactionController = require('../controllers/interactionController');
96
98
  const mediaController = require('../controllers/mediaController');
@@ -125,6 +127,7 @@ const builtInControllers = {
125
127
  sendTemplateToNewNumberController: conversationController.sendTemplateToNewNumberController,
126
128
  markMessagesAsReadController: conversationController.markMessagesAsReadController,
127
129
  reportBugController: bugReportController.reportBugController,
130
+ caseDocumentationController: caseDocumentationController.caseDocumentationController,
128
131
 
129
132
  // Interaction controllers
130
133
  addInteractionController: interactionController.addInteractionController,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "2.5.10",
3
+ "version": "2.5.11",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",