Skip to content

Commit 58a975f

Browse files
committed
fix replacing connected calendar
ref issue #531
1 parent f6d560a commit 58a975f

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/services/UserMeetingSettingsService.js

+20-17
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const {
1515
} = require('../esProcessors/UserMeetingSettingsProcessor')
1616

1717
const UserMeetingSettings = models.UserMeetingSettings
18-
const { Interviews: InterviewConstants } = require('../../app-constants')
18+
const { Interviews: InterviewConstants, NylasVirtualCalendarProvider } = require('../../app-constants')
1919
const esClient = helper.getESClient()
2020
const NylasService = require('./NylasService')
2121
const jwt = require('jsonwebtoken')
@@ -171,27 +171,30 @@ async function syncUserMeetingsSettings (currentUser, data, transaction) {
171171
if (data.calendar) {
172172
const calendarIndexInUserMeetingSettings = _.findIndex(userMeetingSettings.nylasCalendars, { id: data.calendar.id })
173173

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+
179179
// 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+
}
184183

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
187187
}
188188

189-
// update calendar record
190-
return { ...item, ...data.calendar }
191-
})
189+
return updatedItem
190+
}
192191

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
195198
}
196199

197200
const updateUserMeetingSettingsResponse = await UserMeetingSettings.update(updatePayload, { where: { id: userMeetingSettings.id }, returning: true, transaction: null })

0 commit comments

Comments
 (0)