-
Notifications
You must be signed in to change notification settings - Fork 12
/
calcom.go
327 lines (309 loc) · 13.9 KB
/
calcom.go
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package routes
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/openline-ai/openline-customer-os/packages/server/comms-api/model"
s "github.com/openline-ai/openline-customer-os/packages/server/comms-api/service"
cosModel "github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/graph/model"
commonService "github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/service"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/utils"
postgresRepository "github.com/openline-ai/openline-customer-os/packages/server/customer-os-postgres-repository/repository"
"github.com/sirupsen/logrus"
"io"
"log"
"net/http"
)
func AddCalComRoutes(rg *gin.RouterGroup, cosService s.CustomerOSService, secretsRepo postgresRepository.PersonalIntegrationRepository) {
var appSource = "calcom"
rg.POST("/calcom", func(ctx *gin.Context) {
body, err := io.ReadAll(ctx.Request.Body)
if err != nil {
log.Printf("unable to read body: %v", err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"result": fmt.Sprintf("unable to parse json: %v", err.Error()),
})
return
}
var triggerEvent struct {
TriggerEvent string `json:"triggerEvent"`
}
if err = json.Unmarshal(body, &triggerEvent); err != nil {
log.Printf("unable to parse json: %v", err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"result": fmt.Sprintf("unable to parse json: %v", err.Error()),
})
return
}
calcom := string(commonService.CALCOM)
hSignature := ctx.GetHeader(commonService.CalComHeader)
switch triggerEvent.TriggerEvent {
case "BOOKING_CREATED":
log.Printf("BOOKING_CREATED Trigger Event: %s", triggerEvent.TriggerEvent)
request := model.BookingCreatedRequest{}
if err = json.Unmarshal(body, &request); err != nil {
log.Printf("unable to parse json: %v", err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"result": fmt.Sprintf("unable to parse json: %v", err.Error()),
})
return
}
logrus.Info("calcom request body: ", request)
meetingId, err := bookingCreatedHandler(cosService, request, body, secretsRepo, hSignature, calcom, appSource)
if err != nil {
log.Printf("unable to create meeting: %v", err.Error())
ctx.JSON(http.StatusUnprocessableEntity, gin.H{
"result": fmt.Sprintf("Invalid input %s", err.Error()),
})
return
} else {
log.Printf("calcom meeting created: externalId %s internalId: %s", request.Payload.Uid, *meetingId)
ctx.JSON(http.StatusOK, gin.H{
"result": fmt.Sprintf("calcom meeting created: externalId %s internalId: %s", request.Payload.Uid, *meetingId),
})
return
}
case "BOOKING_RESCHEDULED":
request := model.BookingRescheduleRequest{}
if err = json.Unmarshal(body, &request); err != nil {
log.Printf("unable to parse json: %v", err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"result": fmt.Sprintf("unable to parse json: %v", err.Error()),
})
return
}
logrus.Info("calcom request body: ", request)
meetingId, err := bookingRescheduledHandler(cosService, request, body, secretsRepo, hSignature, calcom, appSource)
if err != nil {
log.Printf("unable to update meeting: %v", err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"result": fmt.Sprintf("nable to update meeting: %v", err.Error()),
})
return
} else {
log.Printf("calcom meeting rescheduled: externalId %s internalId: %s", request.Payload.Uid, *meetingId)
ctx.JSON(http.StatusOK, gin.H{
"result": fmt.Sprintf("calcom meeting rescheduled: externalId %s internalId: %s", request.Payload.Uid, *meetingId),
})
return
}
case "BOOKING_CANCELLED":
request := model.BookingCancelRequest{}
if err = json.Unmarshal(body, &request); err != nil {
log.Printf("unable to parse json: %v", err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"result": fmt.Sprintf("unable to parse json: %v", err.Error()),
})
return
}
logrus.Info("calcom request body: ", request)
handler, err := bookingCanceledHandler(cosService, request, body, secretsRepo, hSignature, calcom)
if err != nil {
log.Printf("unable to cancel meeting: %v", err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"result": fmt.Sprintf("unable to cancel meeting: %v", err.Error()),
})
return
} else {
log.Printf("calcom meeting canceled: externalId %s internalId: %s", request.Payload.Uid, *handler)
ctx.JSON(http.StatusOK, gin.H{
"result": fmt.Sprintf("calcom meeting canceled: externalId %s internalId: %s", request.Payload.Uid, *handler),
})
return
}
default:
format := "Unhandled Trigger Event: %s"
log.Printf(format, triggerEvent.TriggerEvent)
ctx.JSON(http.StatusUnprocessableEntity, gin.H{
"result": fmt.Sprintf(format, triggerEvent.TriggerEvent),
})
return
}
})
}
func bookingCanceledHandler(cosService s.CustomerOSService, request model.BookingCancelRequest, body []byte, secretsRepo postgresRepository.PersonalIntegrationRepository, hSignature, calcom string) (*string, error) {
_, tenant, err := getParticipantAndTenant(cosService, &request.Payload.Organizer.Email)
if err != nil {
return nil, fmt.Errorf("can't identify participant or tenant by for email: %v", err.Error())
}
sigCheck := commonService.SignatureCheck(hSignature, body, secretsRepo, *tenant, request.Payload.Organizer.Email, calcom)
if sigCheck != nil {
return nil, fmt.Errorf("signature check failed: %v", sigCheck.Error())
}
log.Printf("BOOKING_CANCELLED Trigger Event: %s", request.TriggerEvent)
exMeeting, err := cosService.ExternalMeeting("calcom", request.Payload.Uid, &request.Payload.Organizer.Email)
if err != nil {
log.Printf("unable to find external meeting: %v", err.Error())
return nil, fmt.Errorf("invalid input %v", err.Error())
} else {
canceled := cosModel.MeetingStatusCanceled
input := cosModel.MeetingUpdateInput{
Status: &canceled,
}
meeting, err := cosService.UpdateMeeting(exMeeting.ID, input, &request.Payload.Organizer.Email)
if err != nil {
log.Printf("unable to cancel booking: %v", err.Error())
return nil, fmt.Errorf("unable to cancel booking %v", err.Error())
} else {
log.Printf("calcom booking updated: externalId %s internalId: %s", request.Payload.Uid, *meeting)
return meeting, nil
}
}
}
func bookingRescheduledHandler(cosService s.CustomerOSService, request model.BookingRescheduleRequest, body []byte, secretsRepo postgresRepository.PersonalIntegrationRepository, hSignature, calcom string, appSource string) (*string, error) {
_, tenant, err := getParticipantAndTenant(cosService, &request.Payload.Organizer.Email)
if err != nil {
return nil, fmt.Errorf("can't identify participant or tenant for email: %v", err.Error())
}
sigCheck := commonService.SignatureCheck(hSignature, body, secretsRepo, *tenant, request.Payload.Organizer.Email, calcom)
if sigCheck != nil {
return nil, fmt.Errorf("signature check failed: %v", sigCheck.Error())
}
log.Printf("BOOKING_RESCHEDULED Trigger Event: %s", request.TriggerEvent)
exMeeting, err := cosService.ExternalMeeting("calcom", request.Payload.RescheduleUid, &request.Payload.Organizer.Email)
if err != nil {
return nil, fmt.Errorf("unable to find external booking %v", err.Error())
} else {
externalSystem := cosModel.ExternalSystemReferenceInput{
ExternalID: request.Payload.Uid,
Type: "CALCOM",
ExternalURL: &request.Payload.Metadata.VideoCallUrl,
ExternalSource: &appSource,
}
input := cosModel.MeetingUpdateInput{
Name: &request.Payload.Title,
StartedAt: &request.Payload.RescheduleStartTime,
EndedAt: &request.Payload.RescheduleEndTime,
AppSource: &appSource,
ExternalSystem: &externalSystem,
}
meeting, err := cosService.UpdateMeeting(exMeeting.ID, input, &request.Payload.Organizer.Email)
if err != nil {
return nil, fmt.Errorf("unable to update cos meeting %v", err.Error())
} else {
log.Printf("calcom meeting rescheduled: externalId %s internalId: %s", externalSystem.ExternalID, *meeting)
var participantsEmailsList []string
participantsEmailsSet := make(map[string]struct{})
for _, participant := range exMeeting.AttendedBy {
for _, contactEmail := range participant.ContactParticipant.Emails {
participantsEmailsSet[contactEmail.Email] = struct{}{}
participantsEmailsList = append(participantsEmailsList, contactEmail.Email)
}
}
var attendeesEmailsList []string
attendeesEmailsSet := make(map[string]struct{})
for _, attendee := range request.Payload.Attendees {
attendeesEmailsSet[attendee.Email] = struct{}{}
attendeesEmailsList = append(attendeesEmailsList, attendee.Email)
}
// Add attendees that are not in participants
for _, email := range attendeesEmailsList {
if _, found := participantsEmailsSet[email]; !found {
contactId, err := cosService.GetContactByEmail(&request.Payload.Organizer.Email, &email)
if err != nil {
log.Printf("unable to find contact with email. Creating contact %s: %v", email, err.Error())
contactId, err = cosService.CreateContact(&request.Payload.Organizer.Email, &email)
if err != nil {
log.Printf("Unable to create contact with email %s: %v", email, err.Error())
} else {
log.Printf("attendedBy: %s %s", *contactId, email)
meetingId, err := cosService.MeetingLinkAttendedBy(exMeeting.ID, cosModel.MeetingParticipantInput{ContactID: contactId}, &request.Payload.Organizer.Email)
if err != nil {
log.Printf("unable to link new meeting participant: %v", err.Error())
} else {
log.Printf("contact participant %s added to meeting: %s", *contactId, *meetingId)
}
}
} else {
log.Printf("attendedBy: %s %s", *contactId, email)
meetingId, err := cosService.MeetingLinkAttendedBy(exMeeting.ID, cosModel.MeetingParticipantInput{ContactID: contactId}, &request.Payload.Organizer.Email)
if err != nil {
log.Printf("unable to link new meeting participant: %v", err.Error())
} else {
log.Printf("contact participant %s added to meeting: %s", *contactId, *meetingId)
}
}
}
}
// Remove participants that are not in attendees
for _, email := range participantsEmailsList {
if _, found := attendeesEmailsSet[email]; !found {
contactId, err := cosService.GetContactByEmail(&request.Payload.Organizer.Email, &email)
if err == nil {
log.Printf("unlinking attendedBy: %s %s", *contactId, email)
meetingId, err := cosService.MeetingUnLinkAttendedBy(exMeeting.ID, cosModel.MeetingParticipantInput{ContactID: contactId}, &request.Payload.Organizer.Email)
if err != nil {
log.Printf("unable to un link meeting participant: %v", err.Error())
} else {
log.Printf("contact participant %s removed to meeting: %s", *contactId, *meetingId)
}
}
}
}
return meeting, nil
}
}
}
func bookingCreatedHandler(cosService s.CustomerOSService, request model.BookingCreatedRequest, body []byte, secretsRepo postgresRepository.PersonalIntegrationRepository, hSignature, calcom, appSource string) (*string, error) {
participant, tenant, err := getParticipantAndTenant(cosService, &request.Payload.Organizer.Email)
if err != nil {
return nil, fmt.Errorf("meeting created handler error: %v", err.Error())
}
sigCheck := commonService.SignatureCheck(hSignature, body, secretsRepo, *tenant, request.Payload.Organizer.Email, calcom)
if sigCheck != nil {
return nil, fmt.Errorf("unable to check signature: %v", sigCheck.Error())
}
createdBy := []*cosModel.MeetingParticipantInput{participant}
var attendedBy []*cosModel.MeetingParticipantInput
for _, attendee := range request.Payload.Attendees {
contactId, err := cosService.GetContactByEmail(&request.Payload.Organizer.Email, &attendee.Email)
if err != nil {
log.Printf("unable to find contact with email. Creating contact %s: %v", attendee.Email, err.Error())
contactId, err = cosService.CreateContact(&request.Payload.Organizer.Email, &attendee.Email)
if err != nil {
log.Printf("Unable to create contact with email %s: %v", attendee.Email, err.Error())
} else {
log.Printf("attendedBy: %s %s", *contactId, attendee.Email)
attendedBy = append(attendedBy, &cosModel.MeetingParticipantInput{ContactID: contactId})
}
} else {
log.Printf("attendedBy: %s %s", *contactId, attendee.Email)
attendedBy = append(attendedBy, &cosModel.MeetingParticipantInput{ContactID: contactId})
}
}
noteInput := cosModel.NoteInput{Content: utils.StringPtr(request.Payload.AdditionalNotes), AppSource: &appSource}
externalSystem := cosModel.ExternalSystemReferenceInput{
ExternalID: request.Payload.Uid,
Type: "CALCOM",
ExternalURL: &request.Payload.Metadata.VideoCallUrl,
ExternalSource: &appSource,
}
input := cosModel.MeetingInput{
Name: &request.Payload.Title,
CreatedBy: createdBy,
AttendedBy: attendedBy,
StartedAt: &request.Payload.StartTime,
EndedAt: &request.Payload.EndTime,
Note: ¬eInput,
ExternalSystem: &externalSystem,
AppSource: &appSource,
}
meeting, err := cosService.CreateMeeting(input, &request.Payload.Organizer.Email)
if err != nil {
return nil, fmt.Errorf("unable to create meeting: %v", err.Error())
}
return meeting, nil
}
func getParticipantAndTenant(cosService s.CustomerOSService, userEmail *string) (*cosModel.MeetingParticipantInput, *string, error) {
user, err := cosService.GetUserByEmail(userEmail)
if err != nil {
return nil, nil, fmt.Errorf("no user for meeting creation to parse json: %v", err.Error())
} else {
log.Printf("createdBy: %s %s", user.UserByEmail.ID, *userEmail)
}
tenant, err := cosService.GetTenant(userEmail)
if err != nil {
return nil, nil, fmt.Errorf("unable to get tenant by for user: %v", err.Error())
}
return &cosModel.MeetingParticipantInput{UserID: &user.UserByEmail.ID}, &tenant.Tenant, nil
}