@peopl-health/nexus 6.4.18 → 6.4.20

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.
@@ -91,7 +91,7 @@ async function generatePresignedUrl(bucketName, key, expiration = 3000) {
91
91
 
92
92
  try {
93
93
  const url = await s3.getSignedUrlPromise('getObject', params);
94
- logger.info(`Presigned URL generated: ${url}`);
94
+ logger.info('Presigned URL generated');
95
95
  return url;
96
96
  } catch (error) {
97
97
  logger.error(`Error generating presigned URL: ${error.message}`);
@@ -38,21 +38,6 @@ const storeFlowConditionalRules = async (flowId, contentSid, flowFirstPageId, ru
38
38
  }
39
39
  };
40
40
 
41
- const getAnyFlowConditionalRules = async () => {
42
- const iter = flowConditionalRules.values().next();
43
- if (!iter.done) return iter.value;
44
- try {
45
- const doc = await FlowRouting.findOne({}, {}, { sort: { updatedAt: -1 } }).lean();
46
- if (doc) {
47
- flowConditionalRules.set(doc.flowId, doc);
48
- return doc;
49
- }
50
- } catch (err) {
51
- console.warn('[FlowRouting] Failed to load latest rules', err.message);
52
- }
53
- return null;
54
- };
55
-
56
41
  const getFlowConditionalRules = async (flowId) => {
57
42
  if (!flowId) return null;
58
43
  if (flowConditionalRules.has(flowId)) return flowConditionalRules.get(flowId);
@@ -68,35 +53,13 @@ const getFlowConditionalRules = async (flowId) => {
68
53
  return null;
69
54
  };
70
55
 
71
- const getFlowConditionalRulesByToken = async (flowToken) => {
72
- if (!flowToken) return null;
73
- for (const value of flowConditionalRules.values()) {
74
- if (value.flowToken === flowToken) return value;
75
- }
76
- try {
77
- const doc = await FlowRouting.findOne({ flowToken }).lean();
78
- if (doc) {
79
- flowConditionalRules.set(doc.flowId, doc);
80
- return doc;
81
- }
82
- } catch (err) {
83
- console.warn('[FlowRouting] Failed to load rules by flowToken', err.message);
84
- }
85
- return null;
86
- };
87
-
88
- const attachFlowToken = async (flowId, flowToken) => {
89
- if (!flowId || !flowToken) return;
90
- const cached = flowConditionalRules.get(flowId);
91
- if (cached) {
92
- cached.flowToken = flowToken;
93
- flowConditionalRules.set(flowId, cached);
94
- }
95
- try {
96
- await FlowRouting.findOneAndUpdate({ flowId }, { flowToken }, { new: true });
97
- } catch (err) {
98
- console.warn('[FlowRouting] Failed to attach flowToken', err.message);
99
- }
56
+ // Tokens are minted as `<flowId>:<uuid>` so the answer routes back to its own flow without a lookup table.
57
+ const FLOW_TOKEN_SEPARATOR = ':';
58
+ const buildFlowToken = (flowId) => `${flowId}${FLOW_TOKEN_SEPARATOR}${uuidv4()}`;
59
+ const flowIdFromToken = (flowToken) => {
60
+ if (typeof flowToken !== 'string') return null;
61
+ const index = flowToken.indexOf(FLOW_TOKEN_SEPARATOR);
62
+ return index > 0 ? flowToken.slice(0, index) : null;
100
63
  };
101
64
 
102
65
  const deleteFlowConditionalRules = (contentSid) => {
@@ -152,7 +115,7 @@ const ensureFlowTokenInVariables = async (contentSid, existingVariables = null)
152
115
  const flowRouting = await FlowRouting.findOne({ contentSid }).lean();
153
116
  if (!flowRouting) return existingVariables;
154
117
 
155
- const flowToken = uuidv4();
118
+ const flowToken = buildFlowToken(flowRouting.flowId);
156
119
  logger.info('[ensureFlowTokenInVariables] Meta Flow template detected', {
157
120
  contentSid,
158
121
  flowId: flowRouting.flowId,
@@ -170,10 +133,8 @@ const ensureFlowTokenInVariables = async (contentSid, existingVariables = null)
170
133
 
171
134
  module.exports = {
172
135
  storeFlowConditionalRules,
173
- getAnyFlowConditionalRules,
174
136
  getFlowConditionalRules,
175
- getFlowConditionalRulesByToken,
176
- attachFlowToken,
137
+ flowIdFromToken,
177
138
  deleteFlowConditionalRules,
178
139
  buildFlowTemplatePayload,
179
140
  generateUniqueFriendlyName,
@@ -15,47 +15,23 @@ const {
15
15
  } = require('../helpers/flowDataExchangeHelper');
16
16
  const {
17
17
  getFlowConditionalRules,
18
- getFlowConditionalRulesByToken,
19
- attachFlowToken,
20
- getAnyFlowConditionalRules,
18
+ flowIdFromToken,
21
19
  getFlowRulesCacheSize
22
20
  } = require('../helpers/templateFlowControllerHelper');
23
21
 
24
- const flowTokenRuleCache = new Map();
25
-
26
22
  async function resolveFlowRules(flow_id, flow_token, initialScreen) {
27
23
  let flowRules = null;
28
24
  let ruleSource = 'none';
29
25
  if (flow_id) {
30
26
  flowRules = await getFlowConditionalRules(flow_id);
31
- if (flowRules && flow_token) flowTokenRuleCache.set(flow_token, flowRules);
32
27
  if (flowRules) ruleSource = 'flow_id';
33
28
  }
34
- if (!flowRules && flow_token) {
35
- if (flowTokenRuleCache.has(flow_token)) {
36
- flowRules = flowTokenRuleCache.get(flow_token);
37
- if (flowRules) ruleSource = 'flow_token_cache';
38
- } else {
39
- flowRules = await getFlowConditionalRulesByToken(flow_token);
40
- if (flowRules) {
41
- flowTokenRuleCache.set(flow_token, flowRules);
42
- ruleSource = 'flow_token_db';
43
- }
44
- }
45
- }
46
- if (!flowRules && flow_token) {
47
- const latest = await getAnyFlowConditionalRules();
48
- if (latest) {
49
- flowRules = latest;
50
- await attachFlowToken(latest.flowId, flow_token);
51
- flowTokenRuleCache.set(flow_token, flowRules);
52
- ruleSource = 'bound_latest_to_token';
53
- }
54
- }
55
29
  if (!flowRules) {
56
- flowRules = await getAnyFlowConditionalRules();
57
- if (flowRules && flow_token) flowTokenRuleCache.set(flow_token, flowRules);
58
- if (flowRules && ruleSource === 'none') ruleSource = 'latest_db';
30
+ const tokenFlowId = flowIdFromToken(flow_token);
31
+ if (tokenFlowId) {
32
+ flowRules = await getFlowConditionalRules(tokenFlowId);
33
+ if (flowRules) ruleSource = 'flow_token';
34
+ }
59
35
  }
60
36
  const defaultScreen = initialScreen || flowRules?.flowFirstPageId;
61
37
  return { flowRules, defaultScreen, ruleSource };
@@ -95,11 +71,6 @@ async function processDataExchange(req) {
95
71
  const { formResponses, responses } = extractResponses(sourceData);
96
72
  const conditionalVisibility = flowRules ? getConditionalVisibilityFromRules(screen, responses, safeRules) : {};
97
73
 
98
- if (flowRules && flow_token && flowRules.flowToken !== flow_token) {
99
- await attachFlowToken(flowRules.flowId || flow_id, flow_token);
100
- flowRules.flowToken = flow_token;
101
- flowTokenRuleCache.set(flow_token, flowRules);
102
- }
103
74
  if (flowRules) logger.debug('[DataExchange] Using stored conditional rules', { screen, rulesCount: safeRules.length, ruleSource });
104
75
  else logger.debug('[DataExchange] No conditional rules found for flow', { flow_id, flow_token, cacheSize: getFlowRulesCacheSize(), dbCount: await FlowRouting.countDocuments().catch(() => null) });
105
76
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "6.4.18",
3
+ "version": "6.4.20",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",