@primocaredentgroup/convex-campaigns-component 0.3.9 → 0.3.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.
|
@@ -3,6 +3,18 @@ import { action } from "../../../_generated/server";
|
|
|
3
3
|
import { internal } from "../../../_generated/api";
|
|
4
4
|
import { assertAuthorized } from "../permissions";
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Riferimento alla mutation che esegue il tick. L'API generata dall'host espone
|
|
8
|
+
* la mutation sotto functions.internal, non campaignsInternal.
|
|
9
|
+
*/
|
|
10
|
+
function getRunTickMutationRef(): any {
|
|
11
|
+
const i = internal as any;
|
|
12
|
+
return (
|
|
13
|
+
i?.components?.campaigns?.functions?.internal?.runTickMutation ??
|
|
14
|
+
i?.functions?.internal?.runTickMutation
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
6
18
|
async function isDuplicateByIdempotencyKey(ctx: any, idempotencyKey?: string) {
|
|
7
19
|
if (!idempotencyKey) return false;
|
|
8
20
|
const existing = await ctx.runQuery(
|
|
@@ -87,9 +99,8 @@ export const runTick: any = action({
|
|
|
87
99
|
args: {},
|
|
88
100
|
handler: async (ctx) => {
|
|
89
101
|
await assertAuthorized(ctx, ["System", "Admin", "Marketing"]);
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
);
|
|
102
|
+
const ref = getRunTickMutationRef();
|
|
103
|
+
if (!ref) throw new Error("runTick: riferimento a runTickMutation/_runTickInternal non trovato");
|
|
104
|
+
return ctx.runMutation(ref, {});
|
|
94
105
|
},
|
|
95
106
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { v } from "convex/values";
|
|
2
|
-
import { internalMutation } from "../../../_generated/server";
|
|
2
|
+
import { internalMutation, mutation } from "../../../_generated/server";
|
|
3
3
|
import type { Id, Doc } from "../../../_generated/dataModel";
|
|
4
4
|
import { assertAuthorized } from "../permissions";
|
|
5
5
|
import { canTransitionRecipientState } from "../domain/stateMachine";
|
|
@@ -521,33 +521,49 @@ export const _enqueueStepJobs = internalMutation({
|
|
|
521
521
|
},
|
|
522
522
|
});
|
|
523
523
|
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
if (buildingSnapshot) {
|
|
531
|
-
await buildSnapshotChunkCore(ctx as any, buildingSnapshot._id);
|
|
532
|
-
}
|
|
524
|
+
/** Logica condivisa del tick, usata da _runTickInternal e runTickMutation. */
|
|
525
|
+
async function runTickCore(ctx: any) {
|
|
526
|
+
const buildingSnapshot = await safeFirstSnapshotByStatus(ctx, "BUILDING");
|
|
527
|
+
if (buildingSnapshot) {
|
|
528
|
+
await buildSnapshotChunkCore(ctx, buildingSnapshot._id);
|
|
529
|
+
}
|
|
533
530
|
|
|
534
|
-
|
|
531
|
+
const runningCampaigns = await safeCollectCampaignsByStatus(ctx, "RUNNING");
|
|
535
532
|
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
533
|
+
for (const campaign of runningCampaigns.slice(0, 10)) {
|
|
534
|
+
const snapshots = await safeCollectSnapshotsByCampaign(ctx, campaign._id);
|
|
535
|
+
const latestReady = snapshots
|
|
536
|
+
.filter((s: any) => s.status === "READY")
|
|
537
|
+
.sort((a: any, b: any) => b.createdAt - a.createdAt)[0];
|
|
538
|
+
if (!latestReady) continue;
|
|
542
539
|
|
|
543
|
-
|
|
544
|
-
|
|
540
|
+
const steps = await safeCollectCampaignSteps(ctx, campaign._id);
|
|
541
|
+
const ordered = [...steps].sort((a, b) => a.order - b.order);
|
|
545
542
|
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
}
|
|
543
|
+
for (const step of ordered.slice(0, 5)) {
|
|
544
|
+
await enqueueStepJobsCore(ctx, latestReady._id, step._id);
|
|
549
545
|
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
return { ok: true, at: Date.now() };
|
|
549
|
+
}
|
|
550
550
|
|
|
551
|
-
|
|
551
|
+
export const _runTickInternal = internalMutation({
|
|
552
|
+
args: {},
|
|
553
|
+
handler: async (ctx) => {
|
|
554
|
+
await assertAuthorized(ctx, ["Admin", "Marketing", "System"]);
|
|
555
|
+
return runTickCore(ctx);
|
|
556
|
+
},
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* Mutation pubblica per eseguire il tick. Usata dall'action runTick perché le internal
|
|
561
|
+
* non si risolvono correttamente quando il componente è montato in un host.
|
|
562
|
+
*/
|
|
563
|
+
export const runTickMutation = mutation({
|
|
564
|
+
args: {},
|
|
565
|
+
handler: async (ctx) => {
|
|
566
|
+
await assertAuthorized(ctx, ["Admin", "Marketing", "System"]);
|
|
567
|
+
return runTickCore(ctx);
|
|
552
568
|
},
|
|
553
569
|
});
|