data-primals-engine 1.7.0 → 1.7.1

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.
package/src/packs.js CHANGED
@@ -144,8 +144,11 @@ The core of this pack is a smart workflow that avoids overloading your server. I
144
144
  "type": "ExecuteScript",
145
145
  "script": `
146
146
  const chunkSize = 10; // Process 10 recipients per run
147
- const campaign = context.triggerData;
148
- const audience = await db.findOne("audience",{"_id": campaign.audience});
147
+ const campaignId = context.triggerData._id;
148
+
149
+ // Fetch fresh campaign data to get updated processedRecipients in the loop
150
+ const campaign = await db.findOne("campaign", { "$eq": ["$_id", campaignId] });
151
+ const audience = await db.findOne("audience", { "$eq": ["$_id", campaign.audience] });
149
152
 
150
153
  if (!audience || !audience.filter) {
151
154
  logger.error('Campaign audience or audience filter is not defined.');
@@ -153,13 +156,12 @@ return { chunk: [], message: 'Campaign audience or audience filter is not define
153
156
  }
154
157
 
155
158
  const processedIds = campaign.processedRecipients || [];
159
+ const query = { "$and": [ audience.filter ] };
156
160
 
157
- const query = {
158
- '$and': [
159
- audience.filter,
160
- { '$not':{ '$in': ["$_id", processedIds] } }
161
- ]
162
- };
161
+ // Only add the exclusion filter if there are already processed recipients
162
+ if (processedIds.length > 0) {
163
+ query["$and"].push({ "$not": [{ "$in": ["$_id", processedIds] }] });
164
+ }
163
165
 
164
166
  logger.info('Finding next chunk with filter:', JSON.stringify(query));
165
167
 
@@ -192,6 +194,8 @@ if (!emailResult || !Array.isArray(emailResult.sent) || emailResult.sent.length
192
194
  return { processedChunk: context.result.chunk };
193
195
  }
194
196
 
197
+ const campaignId = context.triggerData._id;
198
+ const campaign = await db.findOne('campaign', { "$eq": ["$_id", campaignId] });
195
199
  const processedIds = emailResult.sent.map(recipient => recipient._id.toString());
196
200
 
197
201
  logger.info(\`Updating campaign \${campaignId} with \${processedIds.length} new processed IDs.\`);
@@ -199,7 +203,7 @@ logger.info(\`Updating campaign \${campaignId} with \${processedIds.length} new
199
203
  await db.update(
200
204
  'campaign',
201
205
  { _id: campaignId },
202
- { 'processedRecipients': [...campaign.processedRecipients, ...processedIds] }
206
+ { 'processedRecipients': [...(campaign.processedRecipients || []), ...processedIds] }
203
207
  );
204
208
 
205
209
  // Return the original chunk for the condition check step
@@ -237,7 +241,7 @@ return { processedChunk: context.result.chunk };
237
241
  {
238
242
  "name": "Check if Campaign is Complete",
239
243
  "workflow": { "$link": { "name": "Campaign Emailing Workflow", "_model": "workflow" } },
240
- "conditions": { "$gt": [{ "$size": {$ifNull:["{context.result.processedChunk}", []]} }, 0] },
244
+ "conditions": { "$gt": [{ "$size": [ { "$ifNull": ["{context.result.processedChunk}", []] } ] }, 0] },
241
245
  "onSuccessStep": { "$link": { "name": "Process Recipient Chunk", "_model": "workflowStep" } },
242
246
  "onFailureStep": { "$link": { "name": "Finish Campaign", "_model": "workflowStep" } }
243
247
  },