@primocaredentgroup/elettromedicali 0.1.0 → 0.1.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.
Files changed (150) hide show
  1. package/dist/client/index.d.ts +0 -2
  2. package/dist/client/index.d.ts.map +1 -1
  3. package/dist/client/index.js +2 -28
  4. package/dist/client/index.js.map +1 -1
  5. package/dist/component/_generated/api.d.ts +4 -4
  6. package/dist/component/_generated/api.d.ts.map +1 -1
  7. package/dist/component/_generated/component.d.ts +165 -218
  8. package/dist/component/_generated/component.d.ts.map +1 -1
  9. package/dist/component/contracts.d.ts +9 -9
  10. package/dist/component/contracts.d.ts.map +1 -1
  11. package/dist/component/contracts.js +7 -13
  12. package/dist/component/contracts.js.map +1 -1
  13. package/dist/component/crons.d.ts.map +1 -1
  14. package/dist/component/crons.js +1 -2
  15. package/dist/component/crons.js.map +1 -1
  16. package/dist/component/dashboardStats.d.ts +8 -3
  17. package/dist/component/dashboardStats.d.ts.map +1 -1
  18. package/dist/component/dashboardStats.js +24 -39
  19. package/dist/component/dashboardStats.js.map +1 -1
  20. package/dist/component/dashboardStatsCache.d.ts +5 -11
  21. package/dist/component/dashboardStatsCache.d.ts.map +1 -1
  22. package/dist/component/dashboardStatsCache.js +12 -53
  23. package/dist/component/dashboardStatsCache.js.map +1 -1
  24. package/dist/component/deviceCategories.d.ts +22 -15
  25. package/dist/component/deviceCategories.d.ts.map +1 -1
  26. package/dist/component/deviceCategories.js +10 -4
  27. package/dist/component/deviceCategories.js.map +1 -1
  28. package/dist/component/deviceQuestions.d.ts +36 -27
  29. package/dist/component/deviceQuestions.d.ts.map +1 -1
  30. package/dist/component/deviceQuestions.js +22 -5
  31. package/dist/component/deviceQuestions.js.map +1 -1
  32. package/dist/component/deviceRepairHistory.d.ts +3 -3
  33. package/dist/component/deviceRepairHistory.js +1 -1
  34. package/dist/component/deviceRepairHistory.js.map +1 -1
  35. package/dist/component/deviceStatus.d.ts +8 -57
  36. package/dist/component/deviceStatus.d.ts.map +1 -1
  37. package/dist/component/deviceStatus.js +32 -30
  38. package/dist/component/deviceStatus.js.map +1 -1
  39. package/dist/component/devices.d.ts +39 -22
  40. package/dist/component/devices.d.ts.map +1 -1
  41. package/dist/component/devices.js +85 -96
  42. package/dist/component/devices.js.map +1 -1
  43. package/dist/component/emailHelpers.d.ts +10 -3
  44. package/dist/component/emailHelpers.d.ts.map +1 -1
  45. package/dist/component/emailHelpers.js +9 -20
  46. package/dist/component/emailHelpers.js.map +1 -1
  47. package/dist/component/emails.d.ts +5 -5
  48. package/dist/component/emails.js +2 -2
  49. package/dist/component/emails.js.map +1 -1
  50. package/dist/component/http.d.ts.map +1 -1
  51. package/dist/component/http.js +3 -108
  52. package/dist/component/http.js.map +1 -1
  53. package/dist/component/migrationHelpers.d.ts +29 -0
  54. package/dist/component/migrationHelpers.d.ts.map +1 -0
  55. package/dist/component/migrationHelpers.js +84 -0
  56. package/dist/component/migrationHelpers.js.map +1 -0
  57. package/dist/component/roles.d.ts +1 -0
  58. package/dist/component/roles.d.ts.map +1 -1
  59. package/dist/component/roles.js +5 -6
  60. package/dist/component/roles.js.map +1 -1
  61. package/dist/component/schema.d.ts +69 -150
  62. package/dist/component/schema.d.ts.map +1 -1
  63. package/dist/component/schema.js +35 -88
  64. package/dist/component/schema.js.map +1 -1
  65. package/dist/component/slaMonitoring.d.ts +16 -30
  66. package/dist/component/slaMonitoring.d.ts.map +1 -1
  67. package/dist/component/slaMonitoring.js +48 -99
  68. package/dist/component/slaMonitoring.js.map +1 -1
  69. package/dist/component/spareParts.d.ts +11 -48
  70. package/dist/component/spareParts.d.ts.map +1 -1
  71. package/dist/component/spareParts.js +41 -11
  72. package/dist/component/spareParts.js.map +1 -1
  73. package/dist/component/suppliers.d.ts +38 -19
  74. package/dist/component/suppliers.d.ts.map +1 -1
  75. package/dist/component/suppliers.js +63 -44
  76. package/dist/component/suppliers.js.map +1 -1
  77. package/dist/component/ticketComments.d.ts +18 -12
  78. package/dist/component/ticketComments.d.ts.map +1 -1
  79. package/dist/component/ticketComments.js +28 -59
  80. package/dist/component/ticketComments.js.map +1 -1
  81. package/dist/component/ticketDeviceData.d.ts +63 -0
  82. package/dist/component/ticketDeviceData.d.ts.map +1 -0
  83. package/dist/component/ticketDeviceData.js +103 -0
  84. package/dist/component/ticketDeviceData.js.map +1 -0
  85. package/dist/component/ticketExport.d.ts +22 -40
  86. package/dist/component/ticketExport.d.ts.map +1 -1
  87. package/dist/component/ticketExport.js +43 -109
  88. package/dist/component/ticketExport.js.map +1 -1
  89. package/dist/component/ticketHistory.d.ts +4 -4
  90. package/dist/component/ticketHistory.d.ts.map +1 -1
  91. package/dist/component/ticketHistory.js +6 -9
  92. package/dist/component/ticketHistory.js.map +1 -1
  93. package/dist/component/ticketMacros.d.ts +19 -18
  94. package/dist/component/ticketMacros.d.ts.map +1 -1
  95. package/dist/component/ticketMacros.js +24 -30
  96. package/dist/component/ticketMacros.js.map +1 -1
  97. package/dist/component/ticketStatuses.d.ts +1 -0
  98. package/dist/component/ticketStatuses.d.ts.map +1 -1
  99. package/dist/component/ticketStatuses.js +5 -6
  100. package/dist/component/ticketStatuses.js.map +1 -1
  101. package/dist/component/ticketTriggers.d.ts +36 -16
  102. package/dist/component/ticketTriggers.d.ts.map +1 -1
  103. package/dist/component/ticketTriggers.js +115 -153
  104. package/dist/component/ticketTriggers.js.map +1 -1
  105. package/dist/component/userProfiles.d.ts +25 -120
  106. package/dist/component/userProfiles.d.ts.map +1 -1
  107. package/dist/component/userProfiles.js +73 -384
  108. package/dist/component/userProfiles.js.map +1 -1
  109. package/dist/test.d.ts +69 -150
  110. package/dist/test.d.ts.map +1 -1
  111. package/package.json +12 -3
  112. package/src/client/index.ts +2 -30
  113. package/src/component/_generated/api.ts +4 -4
  114. package/src/component/_generated/component.ts +228 -350
  115. package/src/component/contracts.ts +7 -14
  116. package/src/component/crons.ts +2 -7
  117. package/src/component/dashboardStats.ts +24 -41
  118. package/src/component/dashboardStatsCache.ts +12 -61
  119. package/src/component/deviceCategories.ts +12 -4
  120. package/src/component/deviceQuestions.ts +28 -5
  121. package/src/component/deviceRepairHistory.ts +1 -1
  122. package/src/component/deviceStatus.ts +43 -45
  123. package/src/component/devices.ts +87 -106
  124. package/src/component/emailHelpers.ts +9 -19
  125. package/src/component/emails.ts +2 -2
  126. package/src/component/http.ts +3 -108
  127. package/src/component/migrationHelpers.ts +96 -0
  128. package/src/component/roles.ts +5 -6
  129. package/src/component/schema.ts +35 -93
  130. package/src/component/slaMonitoring.ts +52 -107
  131. package/src/component/spareParts.ts +46 -12
  132. package/src/component/suppliers.ts +71 -48
  133. package/src/component/ticketComments.ts +28 -71
  134. package/src/component/ticketDeviceData.ts +113 -0
  135. package/src/component/ticketExport.ts +52 -137
  136. package/src/component/ticketHistory.ts +6 -9
  137. package/src/component/ticketMacros.ts +25 -37
  138. package/src/component/ticketStatuses.ts +5 -6
  139. package/src/component/ticketTriggers.ts +121 -217
  140. package/src/component/userProfiles.ts +67 -451
  141. package/dist/component/clinics.d.ts +0 -103
  142. package/dist/component/clinics.d.ts.map +0 -1
  143. package/dist/component/clinics.js +0 -126
  144. package/dist/component/clinics.js.map +0 -1
  145. package/dist/component/maintenanceTasks.d.ts +0 -733
  146. package/dist/component/maintenanceTasks.d.ts.map +0 -1
  147. package/dist/component/maintenanceTasks.js +0 -937
  148. package/dist/component/maintenanceTasks.js.map +0 -1
  149. package/src/component/clinics.ts +0 -136
  150. package/src/component/maintenanceTasks.ts +0 -1003
@@ -123,7 +123,7 @@ export const createTrigger = mutation({
123
123
  v.literal('specific_user'),
124
124
  v.literal('specific_supplier')
125
125
  ),
126
- userId: v.optional(v.id('user_profiles')),
126
+ userId: v.optional(v.string()),
127
127
  supplierId: v.optional(v.id('suppliers')),
128
128
  })),
129
129
  message: v.string(),
@@ -264,7 +264,7 @@ export const updateTrigger = mutation({
264
264
  v.literal('specific_user'),
265
265
  v.literal('specific_supplier')
266
266
  ),
267
- userId: v.optional(v.id('user_profiles')),
267
+ userId: v.optional(v.string()),
268
268
  supplierId: v.optional(v.id('suppliers')),
269
269
  })),
270
270
  message: v.string(),
@@ -377,7 +377,20 @@ export const reorderTriggers = mutation({
377
377
 
378
378
  export const executeTriggers = mutation({
379
379
  args: {
380
- ticketId: v.id('maintenance_tasks'),
380
+ ticketId: v.string(),
381
+ ticketData: v.object({
382
+ status: v.string(),
383
+ priority: v.optional(v.string()),
384
+ supplierId: v.optional(v.string()),
385
+ clinicId: v.optional(v.string()),
386
+ customFields: v.optional(v.any()),
387
+ notes: v.optional(v.string()),
388
+ }),
389
+ deviceData: v.optional(v.object({
390
+ category: v.optional(v.string()),
391
+ brand: v.optional(v.string()),
392
+ model: v.optional(v.string()),
393
+ })),
381
394
  triggerOn: v.union(
382
395
  v.literal('create'),
383
396
  v.literal('status_change'),
@@ -386,16 +399,11 @@ export const executeTriggers = mutation({
386
399
  v.literal('sla_breach')
387
400
  ),
388
401
  oldStatus: v.optional(v.string()),
402
+ clinicRegion: v.optional(v.string()),
389
403
  },
390
404
  handler: async (ctx, args) => {
391
- const ticket = await ctx.db.get(args.ticketId);
392
- if (!ticket) {
393
- throw new Error('Ticket not found');
394
- }
395
-
396
- const device = ticket.deviceId ? await ctx.db.get(ticket.deviceId) : null;
397
-
398
- const clinic = await ctx.db.get(ticket.clinicId);
405
+ const ticket = args.ticketData;
406
+ const device = args.deviceData || null;
399
407
 
400
408
  const triggers = await ctx.db
401
409
  .query('ticket_triggers')
@@ -413,65 +421,39 @@ export const executeTriggers = mutation({
413
421
  result = value === args.triggerOn;
414
422
  break;
415
423
  case 'status':
416
- const statusValues = Array.isArray(value) ? value : [value];
417
- result = statusValues.includes(ticket.status);
424
+ result = (Array.isArray(value) ? value : [value]).includes(ticket.status);
418
425
  break;
419
426
  case 'category':
420
- const categoryValues = Array.isArray(value) ? value : [value];
421
- result = device ? categoryValues.includes(device.category) : false;
427
+ result = device ? (Array.isArray(value) ? value : [value]).includes(device.category) : false;
422
428
  break;
423
429
  case 'brand':
424
- const brandValues = Array.isArray(value) ? value : [value];
425
- result = device ? brandValues.includes(device.brand) : false;
430
+ result = device ? (Array.isArray(value) ? value : [value]).includes(device.brand) : false;
426
431
  break;
427
432
  case 'model':
428
- const modelValues = Array.isArray(value) ? value : [value];
429
- result = device ? modelValues.includes(device.model) : false;
433
+ result = device ? (Array.isArray(value) ? value : [value]).includes(device.model) : false;
430
434
  break;
431
435
  case 'region':
432
- const regionValues = Array.isArray(value) ? value : [value];
433
- const clinicRegion = (clinic as any)?.region;
434
- result = clinicRegion ? regionValues.includes(clinicRegion) : false;
436
+ result = args.clinicRegion ? (Array.isArray(value) ? value : [value]).includes(args.clinicRegion) : false;
435
437
  break;
436
438
  case 'priority':
437
- const priorityValues = Array.isArray(value) ? value : [value];
438
- result = priorityValues.includes((ticket as any).priority);
439
+ result = (Array.isArray(value) ? value : [value]).includes(ticket.priority);
439
440
  break;
440
- case 'custom_field':
441
+ case 'custom_field': {
441
442
  const { customFieldId, customFieldOperator } = condition;
442
- if (!customFieldId) {
443
- result = true;
444
- break;
445
- }
443
+ if (!customFieldId) { result = true; break; }
446
444
  const fieldValue = ticket.customFields?.[customFieldId];
447
- const operator = customFieldOperator || 'equals';
448
-
449
- switch (operator) {
450
- case 'equals':
451
- result = fieldValue === value;
452
- break;
453
- case 'not_equals':
454
- result = fieldValue !== value;
455
- break;
456
- case 'contains':
457
- result = fieldValue && String(fieldValue).includes(String(value));
458
- break;
459
- case 'greater_than':
460
- result = fieldValue && Number(fieldValue) > Number(value);
461
- break;
462
- case 'less_than':
463
- result = fieldValue && Number(fieldValue) < Number(value);
464
- break;
465
- case 'is_empty':
466
- result = fieldValue === undefined || fieldValue === null || fieldValue === '';
467
- break;
468
- case 'is_not_empty':
469
- result = fieldValue !== undefined && fieldValue !== null && fieldValue !== '';
470
- break;
471
- default:
472
- result = true;
445
+ switch (customFieldOperator || 'equals') {
446
+ case 'equals': result = fieldValue === value; break;
447
+ case 'not_equals': result = fieldValue !== value; break;
448
+ case 'contains': result = fieldValue && String(fieldValue).includes(String(value)); break;
449
+ case 'greater_than': result = fieldValue && Number(fieldValue) > Number(value); break;
450
+ case 'less_than': result = fieldValue && Number(fieldValue) < Number(value); break;
451
+ case 'is_empty': result = fieldValue === undefined || fieldValue === null || fieldValue === ''; break;
452
+ case 'is_not_empty': result = fieldValue !== undefined && fieldValue !== null && fieldValue !== ''; break;
453
+ default: result = true;
473
454
  }
474
455
  break;
456
+ }
475
457
  default:
476
458
  result = true;
477
459
  }
@@ -484,206 +466,141 @@ export const executeTriggers = mutation({
484
466
  if (conditions.length === 1) return evaluateCondition(conditions[0]);
485
467
 
486
468
  const results = conditions.map(c => evaluateCondition(c));
487
-
488
469
  const andGroups: boolean[][] = [[]];
489
470
  let currentGroup = 0;
490
471
 
491
472
  for (let i = 0; i < conditions.length; i++) {
492
473
  andGroups[currentGroup].push(results[i]);
493
-
494
- if (i < conditions.length - 1) {
495
- const nextOp = conditions[i].nextOperator || 'AND';
496
- if (nextOp === 'OR') {
497
- currentGroup++;
498
- andGroups[currentGroup] = [];
499
- }
474
+ if (i < conditions.length - 1 && (conditions[i].nextOperator || 'AND') === 'OR') {
475
+ currentGroup++;
476
+ andGroups[currentGroup] = [];
500
477
  }
501
478
  }
502
479
 
503
- const groupResults = andGroups.map(group => group.every(r => r));
504
- return groupResults.some(r => r);
480
+ return andGroups.map(group => group.every(r => r)).some(r => r);
505
481
  };
506
482
 
483
+ const allActions: Array<{
484
+ triggerId: string;
485
+ triggerName: string;
486
+ actions: any;
487
+ }> = [];
488
+
489
+ let currentStatus = ticket.status;
490
+ let currentNotes = ticket.notes || '';
491
+
507
492
  for (const trigger of sortedTriggers) {
508
493
  const triggerConditions = trigger.conditions as any;
509
494
 
510
- if (triggerConditions.triggerOn && triggerConditions.triggerOn !== args.triggerOn) {
511
- continue;
512
- }
495
+ if (triggerConditions.triggerOn && triggerConditions.triggerOn !== args.triggerOn) continue;
513
496
 
514
- if (triggerConditions.conditionsList && triggerConditions.conditionsList.length > 0) {
515
- const shouldExecute = evaluateConditionsWithOperators(triggerConditions.conditionsList);
497
+ let shouldExecute = false;
516
498
 
517
- if (!shouldExecute) {
518
- continue;
519
- }
499
+ if (triggerConditions.conditionsList && triggerConditions.conditionsList.length > 0) {
500
+ shouldExecute = evaluateConditionsWithOperators(triggerConditions.conditionsList);
520
501
  } else {
521
- if (trigger.conditions.triggerOn !== args.triggerOn) {
522
- continue;
523
- }
502
+ if (trigger.conditions.triggerOn !== args.triggerOn) continue;
524
503
 
525
504
  const conditionLogic = trigger.conditions.conditionLogic || 'AND';
526
505
  const conditionResults: boolean[] = [];
527
506
 
528
507
  let categoryMatch = true;
529
508
  if (!trigger.conditions.applyToAllCategories && device) {
530
- if (!trigger.conditions.categories || !trigger.conditions.categories.includes(device.category)) {
531
- categoryMatch = false;
532
- }
509
+ if (!trigger.conditions.categories || !device.category || !trigger.conditions.categories.includes(device.category)) categoryMatch = false;
533
510
  }
534
511
  conditionResults.push(categoryMatch);
535
512
 
536
- let brandMatch = true;
537
- if (triggerConditions.brands && triggerConditions.brands.length > 0 && device) {
538
- const brandOperator = triggerConditions.brandOperator || 'is';
513
+ if (triggerConditions.brands?.length > 0 && device) {
539
514
  const brandInList = triggerConditions.brands.includes(device.brand);
515
+ conditionResults.push((triggerConditions.brandOperator || 'is') === 'is' ? brandInList : !brandInList);
516
+ } else conditionResults.push(true);
540
517
 
541
- if (brandOperator === 'is') {
542
- brandMatch = brandInList;
543
- } else {
544
- brandMatch = !brandInList;
545
- }
546
- }
547
- conditionResults.push(brandMatch);
548
-
549
- let modelMatch = true;
550
- if (triggerConditions.models && triggerConditions.models.length > 0 && device) {
551
- const modelOperator = triggerConditions.modelOperator || 'is';
518
+ if (triggerConditions.models?.length > 0 && device) {
552
519
  const modelInList = triggerConditions.models.includes(device.model);
553
-
554
- if (modelOperator === 'is') {
555
- modelMatch = modelInList;
556
- } else {
557
- modelMatch = !modelInList;
558
- }
559
- }
560
- conditionResults.push(modelMatch);
520
+ conditionResults.push((triggerConditions.modelOperator || 'is') === 'is' ? modelInList : !modelInList);
521
+ } else conditionResults.push(true);
561
522
 
562
523
  let regionMatch = true;
563
524
  if (trigger.conditions.applyToAllRegions === false) {
564
- const clinicRegion = (clinic as any)?.region;
565
-
566
- if (!clinicRegion || !trigger.conditions.regions || !trigger.conditions.regions.includes(clinicRegion)) {
567
- regionMatch = false;
568
- }
525
+ if (!args.clinicRegion || !trigger.conditions.regions?.includes(args.clinicRegion)) regionMatch = false;
569
526
  }
570
527
  conditionResults.push(regionMatch);
571
528
 
572
- let statusMatch = true;
573
- if (trigger.conditions.statuses && trigger.conditions.statuses.length > 0) {
574
- const statusOperator = trigger.conditions.statusOperator || 'is';
575
- const statusInList = trigger.conditions.statuses.includes(ticket.status);
576
-
577
- if (statusOperator === 'is') {
578
- statusMatch = statusInList;
579
- } else {
580
- statusMatch = !statusInList;
581
- }
582
- }
583
- conditionResults.push(statusMatch);
584
-
585
- let customFieldsMatch = true;
586
- if (trigger.conditions.customFieldConditions && trigger.conditions.customFieldConditions.length > 0) {
587
- for (const condition of trigger.conditions.customFieldConditions) {
588
- const fieldValue = ticket.customFields?.[condition.fieldId];
589
- let conditionMet = false;
590
-
591
- switch (condition.operator) {
592
- case 'equals':
593
- conditionMet = fieldValue === condition.value;
594
- break;
595
- case 'not_equals':
596
- conditionMet = fieldValue !== condition.value;
597
- break;
598
- case 'contains':
599
- conditionMet = fieldValue && String(fieldValue).includes(String(condition.value));
600
- break;
601
- case 'greater_than':
602
- conditionMet = fieldValue && Number(fieldValue) > Number(condition.value);
603
- break;
604
- case 'less_than':
605
- conditionMet = fieldValue && Number(fieldValue) < Number(condition.value);
606
- break;
607
- case 'is_empty':
608
- conditionMet = fieldValue === undefined || fieldValue === null || fieldValue === '';
609
- break;
610
- case 'is_not_empty':
611
- conditionMet = fieldValue !== undefined && fieldValue !== null && fieldValue !== '';
612
- break;
613
- }
614
-
615
- if (!conditionMet) {
616
- customFieldsMatch = false;
617
- break;
529
+ const statuses = trigger.conditions.statuses;
530
+ if (statuses && statuses.length > 0) {
531
+ const statusInList = statuses.includes(currentStatus);
532
+ conditionResults.push((trigger.conditions.statusOperator || 'is') === 'is' ? statusInList : !statusInList);
533
+ } else conditionResults.push(true);
534
+
535
+ const cfConditions = trigger.conditions.customFieldConditions;
536
+ if (cfConditions && cfConditions.length > 0) {
537
+ let cfMatch = true;
538
+ for (const cond of cfConditions) {
539
+ const fv = ticket.customFields?.[cond.fieldId];
540
+ let met = false;
541
+ switch (cond.operator) {
542
+ case 'equals': met = fv === cond.value; break;
543
+ case 'not_equals': met = fv !== cond.value; break;
544
+ case 'contains': met = fv && String(fv).includes(String(cond.value)); break;
545
+ case 'greater_than': met = fv && Number(fv) > Number(cond.value); break;
546
+ case 'less_than': met = fv && Number(fv) < Number(cond.value); break;
547
+ case 'is_empty': met = fv === undefined || fv === null || fv === ''; break;
548
+ case 'is_not_empty': met = fv !== undefined && fv !== null && fv !== ''; break;
618
549
  }
550
+ if (!met) { cfMatch = false; break; }
619
551
  }
620
- }
621
- conditionResults.push(customFieldsMatch);
552
+ conditionResults.push(cfMatch);
553
+ } else conditionResults.push(true);
622
554
 
623
- let shouldExecute = false;
624
- if (conditionLogic === 'OR') {
625
- shouldExecute = conditionResults.some(result => result === true);
626
- } else {
627
- shouldExecute = conditionResults.every(result => result === true);
628
- }
629
-
630
- if (!shouldExecute) {
631
- continue;
632
- }
555
+ shouldExecute = conditionLogic === 'OR'
556
+ ? conditionResults.some(r => r)
557
+ : conditionResults.every(r => r);
633
558
  }
634
559
 
635
- const updates: any = {};
636
-
637
- const actions = trigger.actions as any;
638
- if (actions.setPriority) {
639
- updates.priority = actions.setPriority;
640
- }
560
+ if (!shouldExecute) continue;
641
561
 
642
- if (trigger.actions.changeStatus) {
643
- updates.status = trigger.actions.changeStatus;
644
- }
562
+ const triggerActions = trigger.actions as any;
563
+ const updates: any = {};
645
564
 
646
- if (trigger.actions.assignSupplier) {
647
- updates.supplierId = trigger.actions.assignSupplier;
648
- }
565
+ if (triggerActions.setPriority) updates.priority = triggerActions.setPriority;
566
+ if (triggerActions.changeStatus) { updates.status = triggerActions.changeStatus; currentStatus = updates.status; }
567
+ if (triggerActions.assignSupplier) updates.supplierId = triggerActions.assignSupplier;
649
568
 
650
- if (actions.applySlaRule && actions.assignSupplier) {
651
- const priority = actions.setPriority || (ticket as any).priority || 'medium';
569
+ if (triggerActions.applySlaRule && triggerActions.assignSupplier) {
570
+ const priority = triggerActions.setPriority || ticket.priority || 'medium';
652
571
  const slaRule = await ctx.db
653
572
  .query('sla_rules')
654
573
  .withIndex('by_supplierId_priority', (q: any) =>
655
- q.eq('supplierId', actions.assignSupplier).eq('priority', priority)
574
+ q.eq('supplierId', triggerActions.assignSupplier).eq('priority', priority)
656
575
  )
657
576
  .first();
658
-
659
- if (slaRule) {
660
- const slaDeadline = Date.now() + (slaRule.resolutionTimeHours * 60 * 60 * 1000);
661
- updates.slaDeadline = slaDeadline;
662
- }
663
- } else if (trigger.actions.setSlaHours) {
664
- const slaDeadline = Date.now() + (trigger.actions.setSlaHours * 60 * 60 * 1000);
665
- updates.slaDeadline = slaDeadline;
577
+ if (slaRule) updates.slaDeadline = Date.now() + (slaRule.resolutionTimeHours * 60 * 60 * 1000);
578
+ } else if (triggerActions.setSlaHours) {
579
+ updates.slaDeadline = Date.now() + (triggerActions.setSlaHours * 60 * 60 * 1000);
666
580
  }
667
581
 
668
- if (trigger.actions.addNote) {
669
- const currentNotes = ticket.notes || '';
670
- const newNote = `[Trigger: ${trigger.name}] ${trigger.actions.addNote}`;
671
- updates.notes = currentNotes ? `${currentNotes}\n\n${newNote}` : newNote;
582
+ if (triggerActions.addNote) {
583
+ const newNote = `[Trigger: ${trigger.name}] ${triggerActions.addNote}`;
584
+ currentNotes = currentNotes ? `${currentNotes}\n\n${newNote}` : newNote;
585
+ updates.notes = currentNotes;
672
586
  }
673
587
 
674
588
  if (Object.keys(updates).length > 0) {
675
- updates.updated_at = Date.now();
676
- await ctx.db.patch(args.ticketId, updates);
589
+ allActions.push({
590
+ triggerId: trigger._id,
591
+ triggerName: trigger.name,
592
+ actions: updates,
593
+ });
677
594
 
678
595
  try {
679
- const actionDescriptions: string[] = [];
680
- if (updates.status) actionDescriptions.push(`Stato → ${updates.status}`);
596
+ const desc: string[] = [];
597
+ if (updates.status) desc.push(`Stato → ${updates.status}`);
681
598
  if (updates.supplierId) {
682
599
  const supplier = await ctx.db.get(updates.supplierId);
683
- actionDescriptions.push(`Assegnato a ${(supplier as any)?.name || 'fornitore'}`);
600
+ desc.push(`Assegnato a ${(supplier as any)?.name || 'fornitore'}`);
684
601
  }
685
- if (updates.priority) actionDescriptions.push(`Priorità → ${updates.priority}`);
686
- if (updates.slaDeadline) actionDescriptions.push(`SLA impostato`);
602
+ if (updates.priority) desc.push(`Priorità → ${updates.priority}`);
603
+ if (updates.slaDeadline) desc.push(`SLA impostato`);
687
604
 
688
605
  await ctx.runMutation(api.ticketHistory.recordHistoryEvent, {
689
606
  ticketId: args.ticketId,
@@ -692,26 +609,24 @@ export const executeTriggers = mutation({
692
609
  triggerId: trigger._id,
693
610
  oldValue: updates.status ? ticket.status : undefined,
694
611
  newValue: updates.status,
695
- oldSupplierId: updates.supplierId ? ticket.supplierId : undefined,
696
- newSupplierId: updates.supplierId,
697
- notes: actionDescriptions.join(', '),
612
+ notes: desc.join(', '),
698
613
  isSystemAction: true,
699
614
  });
700
615
  } catch (error) {
701
616
  console.error('Error recording trigger history:', error);
702
617
  }
703
618
  }
704
-
705
619
  }
706
620
 
707
- return { success: true };
621
+ return { success: true, actions: allActions };
708
622
  },
709
623
  });
710
624
 
711
625
  export const checkAttachmentRequired = query({
712
626
  args: {
713
627
  deviceId: v.optional(v.id('devices')),
714
- clinicId: v.optional(v.id('clinics')),
628
+ clinicId: v.optional(v.string()),
629
+ clinicRegion: v.optional(v.string()),
715
630
  status: v.optional(v.string()),
716
631
  priority: v.optional(v.union(v.literal('low'), v.literal('medium'), v.literal('high'))),
717
632
  customFields: v.optional(v.any()),
@@ -732,7 +647,6 @@ export const checkAttachmentRequired = query({
732
647
  }
733
648
 
734
649
  let device = null;
735
- let clinic = null;
736
650
 
737
651
  try {
738
652
  if (args.deviceId) {
@@ -742,14 +656,6 @@ export const checkAttachmentRequired = query({
742
656
  console.error('Error fetching device:', e);
743
657
  }
744
658
 
745
- try {
746
- if (args.clinicId) {
747
- clinic = await ctx.db.get(args.clinicId);
748
- }
749
- } catch (e) {
750
- console.error('Error fetching clinic:', e);
751
- }
752
-
753
659
  const evaluateCondition = (condition: any): boolean => {
754
660
  const { type, negated, value } = condition;
755
661
  let result = false;
@@ -776,8 +682,7 @@ export const checkAttachmentRequired = query({
776
682
  break;
777
683
  case 'region':
778
684
  const regionValues = Array.isArray(value) ? value : [value];
779
- const clinicRegion = (clinic as any)?.region;
780
- result = clinicRegion ? regionValues.includes(clinicRegion) : false;
685
+ result = args.clinicRegion ? regionValues.includes(args.clinicRegion) : false;
781
686
  break;
782
687
  case 'priority':
783
688
  const priorityValues = Array.isArray(value) ? value : [value];
@@ -886,8 +791,7 @@ export const checkAttachmentRequired = query({
886
791
 
887
792
  let regionMatch = true;
888
793
  if (triggerConditions.applyToAllRegions === false) {
889
- const clinicRegion = (clinic as any)?.region;
890
- if (!clinicRegion || !triggerConditions.regions || !triggerConditions.regions.includes(clinicRegion)) {
794
+ if (!args.clinicRegion || !triggerConditions.regions || !triggerConditions.regions.includes(args.clinicRegion)) {
891
795
  regionMatch = false;
892
796
  }
893
797
  }