1
1
import type { App_RoutingForms_Form , User } from "@prisma/client" ;
2
2
import async from "async" ;
3
- import os from "os " ;
3
+ import { Config , Utils as QbUtils } from "react-awesome-query-builder " ;
4
4
5
5
import getWebhooks from "@calcom/features/webhooks/lib/getWebhooks" ;
6
6
import { sendGenericWebhookPayload } from "@calcom/features/webhooks/lib/sendPayload" ;
@@ -10,13 +10,14 @@ import { safeStringify } from "@calcom/lib/safeStringify";
10
10
import { WebhookTriggerEvents } from "@calcom/prisma/client" ;
11
11
import type { Ensure } from "@calcom/types/utils" ;
12
12
13
- import { evaluateRaqbLogic , RaqbLogicResult } from "../lib/evaluateRaqbLogic" ;
13
+ import { RaqbLogicResult } from "../lib/evaluateRaqbLogic" ;
14
14
import {
15
15
getTeamMembersWithAttributeOptionValuePerAttribute ,
16
16
getAttributesForTeam ,
17
17
} from "../lib/getAttributes" ;
18
18
import isRouter from "../lib/isRouter" ;
19
- import type { SerializableField , OrderedResponses } from "../types/types" ;
19
+ import jsonLogic from "../lib/jsonLogic" ;
20
+ import type { SerializableField , OrderedResponses , AttributesQueryValue } from "../types/types" ;
20
21
import type { FormResponse , SerializableForm } from "../types/types" ;
21
22
import { acrossQueryValueCompatiblity } from "./raqbUtils" ;
22
23
@@ -117,6 +118,37 @@ function perf<ReturnValue>(fn: () => ReturnValue): [ReturnValue, number | null]
117
118
return [ result , end - start ] ;
118
119
}
119
120
121
+ function getJsonLogic ( {
122
+ attributesQueryValue,
123
+ attributesQueryBuilderConfig,
124
+ } : {
125
+ attributesQueryValue : AttributesQueryValue ;
126
+ attributesQueryBuilderConfig : Config ;
127
+ } ) {
128
+ const state = {
129
+ tree : QbUtils . checkTree (
130
+ QbUtils . loadTree ( attributesQueryValue ) ,
131
+ // We know that attributesQueryBuilderConfig is a Config because getAttributesQueryBuilderConfig returns a Config. So, asserting it.
132
+ attributesQueryBuilderConfig as unknown as Config
133
+ ) ,
134
+ config : attributesQueryBuilderConfig as unknown as Config ,
135
+ } ;
136
+
137
+ const jsonLogicQuery = QbUtils . jsonLogicFormat ( state . tree , state . config ) ;
138
+ const logic = jsonLogicQuery . logic ;
139
+ if ( ! logic ) {
140
+ if ( attributesQueryValue . children1 && Object . keys ( attributesQueryValue . children1 ) . length > 0 ) {
141
+ throw new Error ( "Couldn't build the logic from the query value" ) ;
142
+ }
143
+ console . log (
144
+ "No logic found" ,
145
+ safeStringify ( { attributesQueryValue, queryBuilderConfigFields : attributesQueryBuilderConfig . fields } )
146
+ ) ;
147
+ }
148
+ console . log ( "Using LOGIC" , safeStringify ( logic ) ) ;
149
+ return logic ;
150
+ }
151
+
120
152
export async function findTeamMembersMatchingAttributeLogicOfRoute (
121
153
{
122
154
form,
@@ -206,6 +238,24 @@ export async function findTeamMembersMatchingAttributeLogicOfRoute(
206
238
getTeamMembersWithAttributeOptionValuePerAttributeTimeTaken ,
207
239
] = await aPf ( ( ) => getTeamMembersWithAttributeOptionValuePerAttribute ( { teamId : teamId } ) ) ;
208
240
241
+ const logic = getJsonLogic ( {
242
+ attributesQueryValue,
243
+ attributesQueryBuilderConfig : attributesQueryBuilderConfig as unknown as Config ,
244
+ } ) ;
245
+
246
+ if ( ! logic ) {
247
+ return {
248
+ teamMembersMatchingAttributeLogic : [ ] ,
249
+ timeTaken : {
250
+ gAtr : getAttributesForTeamTimeTaken ,
251
+ gQryCnfg : getAttributesQueryBuilderConfigTimeTaken ,
252
+ gMbrWtAtr : getTeamMembersWithAttributeOptionValuePerAttributeTimeTaken ,
253
+ lgcFrMbrs : null ,
254
+ gQryVal : getAttributesQueryValueTimeTaken ,
255
+ } ,
256
+ } ;
257
+ }
258
+
209
259
const [ _ , teamMembersMatchingAttributeLogicTimeTaken ] = await aPf ( async ( ) => {
210
260
return await async . mapLimit < TeamMemberWithAttributeOptionValuePerAttribute , Promise < void > > (
211
261
teamMembersWithAttributeOptionValuePerAttribute ,
@@ -215,30 +265,15 @@ export async function findTeamMembersMatchingAttributeLogicOfRoute(
215
265
attributesData : member . attributes ,
216
266
attributesQueryValue,
217
267
} ) ;
218
- moduleLogger . debug (
219
- `Checking team member ${ member . userId } with attributes logic` ,
220
- safeStringify ( { attributes : attributesData , attributesQueryValue } )
221
- ) ;
222
- const result = evaluateRaqbLogic (
223
- {
224
- queryValue : attributesQueryValue ,
225
- queryBuilderConfig : attributesQueryBuilderConfig ,
226
- data : attributesData ,
227
- beStrictWithEmptyLogic : true ,
228
- } ,
229
- {
230
- // This logic runs too many times as it is per team member and we don't want to spam the console with logs. It might also take a performance hit otherwise
231
- logLevel : 2 ,
232
- }
233
- ) ;
234
-
235
- if ( result === RaqbLogicResult . MATCH || result === RaqbLogicResult . LOGIC_NOT_FOUND_SO_MATCHED ) {
236
- moduleLogger . debug ( `Team member ${ member . userId } matches attributes logic` ) ;
237
- teamMembersMatchingAttributeLogicMap . set ( member . userId , result ) ;
238
- } else {
239
- moduleLogger . debug ( `Team member ${ member . userId } does not match attributes logic` ) ;
268
+
269
+ const result = ! ! jsonLogic . apply ( logic as any , attributesData )
270
+ ? RaqbLogicResult . MATCH
271
+ : RaqbLogicResult . NO_MATCH ;
272
+
273
+ if ( result !== RaqbLogicResult . MATCH ) {
240
274
return ;
241
275
}
276
+ teamMembersMatchingAttributeLogicMap . set ( member . userId , result ) ;
242
277
}
243
278
) ;
244
279
} ) ;
0 commit comments