powr-sdk-api 4.3.11 → 4.3.12
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/dist/managers/tasks.js +67 -6
- package/package.json +1 -1
package/dist/managers/tasks.js
CHANGED
|
@@ -475,11 +475,35 @@ class TasksManager {
|
|
|
475
475
|
|
|
476
476
|
// Log workflow execution
|
|
477
477
|
await this.logWorkflowExecution(workflow, results);
|
|
478
|
+
|
|
479
|
+
// For one-time workflows, deactivate after execution
|
|
480
|
+
if (workflow.schedule && !this.isCronExpression(workflow.schedule)) {
|
|
481
|
+
try {
|
|
482
|
+
const db = await getDb();
|
|
483
|
+
await db.collection("tasks").updateOne({
|
|
484
|
+
_id: workflow._id
|
|
485
|
+
}, {
|
|
486
|
+
$set: {
|
|
487
|
+
isActive: false
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
console.log(`🔄 Deactivated one-time workflow: ${workflow.name}`);
|
|
491
|
+
} catch (error) {
|
|
492
|
+
console.error("❌ Failed to deactivate workflow:", error);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// Determine overall success - consider it successful if at least one action executed successfully
|
|
497
|
+
const executedActions = results.filter(r => r.type === 'action' && !r.skipped);
|
|
498
|
+
const successfulActions = executedActions.filter(r => r.success);
|
|
499
|
+
const overallSuccess = executedActions.length === 0 || successfulActions.length > 0;
|
|
478
500
|
return {
|
|
479
|
-
success:
|
|
501
|
+
success: overallSuccess,
|
|
480
502
|
workflowName: workflow.name,
|
|
481
503
|
stepsExecuted: results.length,
|
|
482
|
-
results: results
|
|
504
|
+
results: results,
|
|
505
|
+
actionsExecuted: executedActions.length,
|
|
506
|
+
actionsSuccessful: successfulActions.length
|
|
483
507
|
};
|
|
484
508
|
} catch (error) {
|
|
485
509
|
console.error(`❌ Workflow execution failed: ${workflow.name}`, error);
|
|
@@ -554,15 +578,35 @@ class TasksManager {
|
|
|
554
578
|
return true; // No condition, always execute
|
|
555
579
|
}
|
|
556
580
|
|
|
557
|
-
//
|
|
558
|
-
// For now, support basic conditions like "step1 === true"
|
|
581
|
+
// Parse condition like "step1.result === 'rain'" or "step1.result !== 'rain'"
|
|
559
582
|
const condition = step.condition;
|
|
583
|
+
|
|
584
|
+
// Extract step ID and condition
|
|
585
|
+
const stepMatch = condition.match(/step(\d+)\.result\s*(===|!==)\s*['"]([^'"]+)['"]/);
|
|
586
|
+
if (stepMatch) {
|
|
587
|
+
const stepId = stepMatch[1];
|
|
588
|
+
const operator = stepMatch[2];
|
|
589
|
+
const expectedValue = stepMatch[3];
|
|
590
|
+
const stepResult = previousResults.find(r => r.stepId === stepId);
|
|
591
|
+
if (stepResult && stepResult.result) {
|
|
592
|
+
var _stepResult$result$da;
|
|
593
|
+
// Get actual value from the result structure
|
|
594
|
+
const actualValue = stepResult.result.actualValue || ((_stepResult$result$da = stepResult.result.data) === null || _stepResult$result$da === void 0 ? void 0 : _stepResult$result$da.currentWeather) || stepResult.result.condition || 'unknown';
|
|
595
|
+
console.log(`🔍 Condition check: ${actualValue} ${operator} ${expectedValue}`);
|
|
596
|
+
if (operator === '===') {
|
|
597
|
+
return actualValue.toLowerCase() === expectedValue.toLowerCase();
|
|
598
|
+
} else if (operator === '!==') {
|
|
599
|
+
return actualValue.toLowerCase() !== expectedValue.toLowerCase();
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
// Fallback: check for simple boolean conditions
|
|
560
605
|
if (condition.includes('step1') && condition.includes('=== true')) {
|
|
561
606
|
const step1Result = previousResults.find(r => r.stepId === 'step1');
|
|
562
607
|
return step1Result && step1Result.conditionMet === true;
|
|
563
608
|
}
|
|
564
|
-
|
|
565
|
-
// Add more condition parsing as needed
|
|
609
|
+
console.log(`⚠️ Could not parse condition: ${condition}, defaulting to execute`);
|
|
566
610
|
return true; // Default to execute if condition parsing fails
|
|
567
611
|
}
|
|
568
612
|
|
|
@@ -589,12 +633,20 @@ class TasksManager {
|
|
|
589
633
|
async logWorkflowExecution(workflow, results) {
|
|
590
634
|
try {
|
|
591
635
|
const db = await getDb();
|
|
636
|
+
|
|
637
|
+
// Calculate overall success
|
|
638
|
+
const executedActions = results.filter(r => r.type === 'action' && !r.skipped);
|
|
639
|
+
const successfulActions = executedActions.filter(r => r.success);
|
|
640
|
+
const overallSuccess = executedActions.length === 0 || successfulActions.length > 0;
|
|
592
641
|
await db.collection("workflow_executions").insertOne({
|
|
593
642
|
workflowId: workflow._id,
|
|
594
643
|
workflowName: workflow.name,
|
|
595
644
|
userId: workflow.userId,
|
|
596
645
|
projectId: workflow.projectId,
|
|
597
646
|
results: results,
|
|
647
|
+
success: overallSuccess,
|
|
648
|
+
actionsExecuted: executedActions.length,
|
|
649
|
+
actionsSuccessful: successfulActions.length,
|
|
598
650
|
executedAt: new Date()
|
|
599
651
|
});
|
|
600
652
|
} catch (error) {
|
|
@@ -667,6 +719,15 @@ class TasksManager {
|
|
|
667
719
|
}
|
|
668
720
|
}
|
|
669
721
|
|
|
722
|
+
// Check if a string is a cron expression
|
|
723
|
+
isCronExpression(value) {
|
|
724
|
+
if (!value || typeof value !== 'string') return false;
|
|
725
|
+
|
|
726
|
+
// Basic cron pattern: 5 or 6 fields separated by spaces
|
|
727
|
+
const cronPattern = /^(\*|\d+|\d+-\d+|\d+\/\d+)(\s+(\*|\d+|\d+-\d+|\d+\/\d+)){4,5}$/;
|
|
728
|
+
return cronPattern.test(value.trim());
|
|
729
|
+
}
|
|
730
|
+
|
|
670
731
|
// Check if a string is a cron expression
|
|
671
732
|
isCronExpression(value) {
|
|
672
733
|
try {
|