@@ -15,7 +15,7 @@ const {
15
15
} = require ( '../esProcessors/UserMeetingSettingsProcessor' )
16
16
17
17
const UserMeetingSettings = models . UserMeetingSettings
18
- const { Interviews : InterviewConstants } = require ( '../../app-constants' )
18
+ const { Interviews : InterviewConstants , NylasVirtualCalendarProvider } = require ( '../../app-constants' )
19
19
const esClient = helper . getESClient ( )
20
20
const NylasService = require ( './NylasService' )
21
21
const jwt = require ( 'jsonwebtoken' )
@@ -171,27 +171,30 @@ async function syncUserMeetingsSettings (currentUser, data, transaction) {
171
171
if ( data . calendar ) {
172
172
const calendarIndexInUserMeetingSettings = _ . findIndex ( userMeetingSettings . nylasCalendars , { id : data . calendar . id } )
173
173
174
- // if calendar is not yet on the list, then add it
175
- if ( calendarIndexInUserMeetingSettings === - 1 ) {
176
- updatePayload . nylasCalendars = [ ... updatePayload . nylasCalendars , data . calendar ]
177
- } else {
178
- const updatedNylasCalendarsArray = _ . map ( Array . from ( userMeetingSettings . nylasCalendars ) , ( item , index ) => {
174
+ const updatedNylasCalendarsArray = _ . map ( userMeetingSettings . nylasCalendars || [ ] , ( item , index ) => {
175
+ // process all other calendar, except the one wa are adding/updating
176
+ if ( index !== calendarIndexInUserMeetingSettings ) {
177
+ const updatedItem = { ... item }
178
+
179
179
// if we are adding primary calendar, then make all other calendars non-primary
180
- if ( index !== calendarIndexInUserMeetingSettings ) {
181
- if ( data . calendar . isPrimary ) {
182
- return { ...item , isPrimary : false }
183
- }
180
+ if ( data . calendar . isPrimary ) {
181
+ updatedItem . isPrimary = false
182
+ }
184
183
185
- // otherwise don't update other calendars
186
- return item
184
+ // if we are adding not-Nylas calendar, mark all other not-Nylas calendars as removed, as we don't allow having multiple not-Nylas calendars
185
+ if ( data . calendar . accountProvider !== NylasVirtualCalendarProvider && updatedItem . provider !== NylasVirtualCalendarProvider ) {
186
+ updatedItem . isDeleted = true
187
187
}
188
188
189
- // update calendar record
190
- return { ...item , ...data . calendar }
191
- } )
189
+ return updatedItem
190
+ }
192
191
193
- updatePayload . nylasCalendars = updatedNylasCalendarsArray
194
- }
192
+ // if we are updating existent calendar, then update it
193
+ return { ...item , ...data . calendar }
194
+ } )
195
+
196
+ // add new calendar to the list updated list or just use updated list
197
+ updatePayload . nylasCalendars = calendarIndexInUserMeetingSettings === - 1 ? [ ...updatedNylasCalendarsArray , data . calendar ] : updatedNylasCalendarsArray
195
198
}
196
199
197
200
const updateUserMeetingSettingsResponse = await UserMeetingSettings . update ( updatePayload , { where : { id : userMeetingSettings . id } , returning : true , transaction : null } )
0 commit comments