-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinalizeStepResults.ts
More file actions
91 lines (82 loc) · 3.38 KB
/
Copy pathfinalizeStepResults.ts
File metadata and controls
91 lines (82 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { executeAction } from "@/core/action/executeActions/executeAction";
import { sendNewStepNotifications } from "@/core/notification/sendNewStepNotifications";
import { sendResultNotifications } from "@/core/notification/sendResultNotifications";
import { newResultsForStep } from "@/core/result/newResults/newResultsForStep";
import { prisma } from "@/prisma/client";
import { endTelegramPolls } from "@/telegram/endTelegramPolls";
import { finalizeActionAndRequest } from "./finalizeActionAndRequest";
import { finalizeStepResponses } from "./finalizeStepResponses";
import { resultGroupInclude } from "../../result/resultPrismaTypes";
// creates the results and then runs actions for a given request Step
// should only be run if resultsComplete is false
// note: all of the calls here are awaited, so that when this function is called by cron job, it doesn't exist the process prematurely
export const finalizeStepResults = async ({ requestStepId }: { requestStepId: string }) => {
try {
let hasResultConfigsWithoutResults = false;
const resultGroups = await prisma.resultGroup.findMany({
where: {
requestStepId,
},
include: resultGroupInclude,
orderBy: { index: "asc" },
});
const rs = await prisma.requestStep.findFirst({
where: { id: requestStepId },
include: {
Step: {
include: {
ResultConfigSet: {
include: {
ResultConfigs: true,
},
},
},
},
},
});
// see if there are any result configs that don't have corresponding results (even incomplete results)
(rs?.Step.ResultConfigSet?.ResultConfigs ?? []).forEach((rc) => {
const rg = resultGroups.find((rg) => rg.resultConfigId === rc.id);
if (!rg) {
hasResultConfigsWithoutResults = true;
}
});
// no results means that results were never created (e.g. not enough responses)
// in this case we should finalize the request and not run subsequent steps / actions
if (hasResultConfigsWithoutResults) {
await finalizeActionAndRequest({ requestStepId, finalizeRequest: true });
return;
}
const resultsFinal = resultGroups.every((result) => result.complete);
await prisma.requestStep.update({
where: {
id: requestStepId,
},
data: {
resultsFinal,
},
});
// this code block should only be run once per request step
if (resultsFinal) {
await endTelegramPolls({ requestStepId });
const { nextRequestStepId, responseComplete: runResultsForNextStep } = await executeAction({
requestStepId,
});
await sendResultNotifications({ requestStepId });
if (nextRequestStepId) {
// simplify this function later so it can just look up the flow id itself
await sendNewStepNotifications({
requestStepId: nextRequestStepId,
});
// run results for next step in cases where there is no response required (e.g. ai decision)
// and then proceed to finalizing the step responses (which in turn may lead to finalizeStepResults being called again)
await newResultsForStep({ requestStepId: nextRequestStepId });
if (runResultsForNextStep) {
await finalizeStepResponses({ requestStepId: nextRequestStepId });
}
}
}
} catch (e) {
console.log("Error runResultsAndActions: ", e);
}
};