1
1
import { API_VERSIONS_VALUES } from "@/lib/api-versions" ;
2
+ import { TeamsEventTypesRepository } from "@/modules/teams/event-types/teams-event-types.repository" ;
2
3
import { Controller , Req , NotFoundException , Param , Post , Body } from "@nestjs/common" ;
3
4
import { ApiTags as DocsTags , ApiExcludeController as DocsExcludeController } from "@nestjs/swagger" ;
4
5
import { Request } from "express" ;
5
6
6
- import { getRoutedUrl } from "@calcom/platform-libraries" ;
7
+ import {
8
+ getRoutedUrl ,
9
+ getTeamMemberEmailForResponseOrContactUsingUrlQuery ,
10
+ } from "@calcom/platform-libraries" ;
7
11
import { ApiResponse } from "@calcom/platform-types" ;
8
12
9
13
@Controller ( {
@@ -13,6 +17,8 @@ import { ApiResponse } from "@calcom/platform-types";
13
17
@DocsTags ( "Router controller" )
14
18
@DocsExcludeController ( true )
15
19
export class RouterController {
20
+ constructor ( private readonly teamsEventTypesRepository : TeamsEventTypesRepository ) { }
21
+
16
22
@Post ( "/forms/:formId/submit" )
17
23
async getRoutingFormResponse (
18
24
@Req ( ) request : Request ,
@@ -27,13 +33,54 @@ export class RouterController {
27
33
}
28
34
29
35
if ( routedUrlData ?. redirect ?. destination ) {
30
- return { status : "success" , data : routedUrlData ? .redirect ? .destination , redirect : true } ;
36
+ return this . handleRedirect ( routedUrlData . redirect . destination ) ;
31
37
}
32
38
33
39
if ( routedUrlData ?. props ) {
34
- return { status : "success" , data : { message : routedUrlData ? .props ? .message ?? "" } , redirect : false } ;
40
+ return { status : "success" , data : { message : routedUrlData . props . message ?? "" } , redirect : false } ;
35
41
}
36
42
37
43
return { status : "success" , data : { message : "No Route nor custom message found." } , redirect : false } ;
38
44
}
45
+
46
+ private async handleRedirect ( destination : string ) : Promise < ApiResponse < unknown > & { redirect : boolean } > {
47
+ const routingUrl = new URL ( destination ) ;
48
+ const routingSearchParams = routingUrl . searchParams ;
49
+ if (
50
+ routingSearchParams . get ( "cal.action" ) === "eventTypeRedirectUrl" &&
51
+ routingSearchParams . has ( "email" ) &&
52
+ routingSearchParams . has ( "cal.teamId" ) &&
53
+ ! routingSearchParams . has ( "cal.skipContactOwner" )
54
+ ) {
55
+ return this . handleRedirectWithContactOwner ( routingUrl , routingSearchParams ) ;
56
+ }
57
+
58
+ return { status : "success" , data : destination , redirect : true } ;
59
+ }
60
+
61
+ private async handleRedirectWithContactOwner (
62
+ routingUrl : URL ,
63
+ routingSearchParams : URLSearchParams
64
+ ) : Promise < ApiResponse < unknown > & { redirect : boolean } > {
65
+ const pathNameParams = routingUrl . pathname . split ( "/" ) ;
66
+ const eventTypeSlug = pathNameParams [ pathNameParams . length - 1 ] ;
67
+ const teamId = Number ( routingSearchParams . get ( "cal.teamId" ) ) ;
68
+ const eventTypeData = this . teamsEventTypesRepository . getTeamEventTypeBySlug ( teamId , eventTypeSlug , 3 ) ;
69
+
70
+ // get the salesforce record owner email for the email given as a form response.
71
+ const {
72
+ email : teamMemberEmail ,
73
+ recordType : crmOwnerRecordType ,
74
+ crmAppSlug,
75
+ } = await getTeamMemberEmailForResponseOrContactUsingUrlQuery ( {
76
+ query : Object . fromEntries ( routingSearchParams ) ,
77
+ eventData : eventTypeData ,
78
+ } ) ;
79
+
80
+ Boolean ( teamMemberEmail ) && routingUrl . searchParams . set ( "cal.teamMemberEmail" , teamMemberEmail ) ;
81
+ Boolean ( crmOwnerRecordType ) && routingUrl . searchParams . set ( "cal.crmOwnerRecordType" , crmOwnerRecordType ) ;
82
+ Boolean ( crmAppSlug ) && routingUrl . searchParams . set ( "cal.crmAppSlug" , crmAppSlug ) ;
83
+
84
+ return { status : "success" , data : routingUrl . toString ( ) , redirect : true } ;
85
+ }
39
86
}
0 commit comments