@@ -48,14 +48,20 @@ function stripUnwantedData (userMeetingSettings) {
48
48
return userMeetingSettings
49
49
}
50
50
51
+ function handleUserMeetingSettingsData ( data , shouldNotStripUnwantedData ) {
52
+ return shouldNotStripUnwantedData ? data : stripUnwantedData ( data )
53
+ }
54
+
51
55
/**
52
56
* Get UserMeetingsettings by userid
53
57
* @param {Object } currentUser the user who perform this operation.
54
58
* @param {String } userId the user id
55
59
* @param {Boolean } fromDb flag if query db for data or not
60
+ * @param {object } options { shouldNotStripUnwantedData: false }
61
+ * shouldNotStripUnwantedData - flag indicating if unwanted data should be stripped or not
56
62
* @returns {Object } the userMeetingSetting object
57
63
*/
58
- async function getUserMeetingSettingsByUserId ( currentUser , userId , fromDb ) {
64
+ async function getUserMeetingSettingsByUserId ( currentUser , userId , fromDb , options = { shouldNotStripUnwantedData : false } ) {
59
65
// check permission
60
66
await ensureUserIsPermitted ( currentUser , userId )
61
67
if ( ! fromDb ) {
@@ -68,7 +74,7 @@ async function getUserMeetingSettingsByUserId (currentUser, userId, fromDb) {
68
74
// extract interviews from ES object
69
75
const userMeetingSettings = _ . get ( userMeetingSettingsES , 'body._source' , [ ] )
70
76
if ( userMeetingSettings ) {
71
- return stripUnwantedData ( userMeetingSettings )
77
+ return handleUserMeetingSettingsData ( userMeetingSettings , options . shouldNotStripUnwantedData )
72
78
}
73
79
throw new errors . NotFoundError ( `The userMeetingSettings for userId=${ userId } not found.` )
74
80
} catch ( err ) {
@@ -79,6 +85,7 @@ async function getUserMeetingSettingsByUserId (currentUser, userId, fromDb) {
79
85
throw err
80
86
}
81
87
}
88
+
82
89
// either ES query failed or `fromDb` is set - fallback to DB
83
90
logger . info ( { component : 'InterviewService' , context : 'getUserMeetingSettingsByUserId' , message : 'try to query db for data' } )
84
91
@@ -88,12 +95,15 @@ async function getUserMeetingSettingsByUserId (currentUser, userId, fromDb) {
88
95
throw new errors . NotFoundError ( `The userMeetingSettings for userId=${ userId } not found.` )
89
96
}
90
97
91
- return stripUnwantedData ( userMeetingSettings . dataValues )
98
+ return handleUserMeetingSettingsData ( userMeetingSettings , options . shouldNotStripUnwantedData )
92
99
}
93
100
getUserMeetingSettingsByUserId . schema = Joi . object ( ) . keys ( {
94
101
currentUser : Joi . object ( ) . required ( ) ,
95
102
userId : Joi . string ( ) . uuid ( ) . required ( ) ,
96
- fromDb : Joi . boolean ( )
103
+ fromDb : Joi . boolean ( ) ,
104
+ options : Joi . object ( ) . keys ( {
105
+ shouldNotStripUnwantedData : Joi . boolean ( )
106
+ } )
97
107
} ) . required ( )
98
108
99
109
// TODO document
@@ -102,6 +112,7 @@ async function createUserMeetingSettingsIfNotExisting (currentUser, userId, cale
102
112
await ensureUserIsPermitted ( currentUser , userId )
103
113
104
114
let userMeetingSettings = await UserMeetingSettings . findById ( userId , false )
115
+
105
116
const payload = {
106
117
id : userId ,
107
118
defaultAvailableTime : await NylasService . getAvailableTimeFromSchedulingPage ( schedulingPage ) ,
@@ -115,6 +126,7 @@ async function createUserMeetingSettingsIfNotExisting (currentUser, userId, cale
115
126
isPrimary : calendar . is_primary
116
127
} )
117
128
}
129
+
118
130
if ( _ . isNil ( userMeetingSettings ) ) {
119
131
userMeetingSettings = await UserMeetingSettings . create ( payload , { transaction : transaction } )
120
132
await processCreate ( userMeetingSettings . toJSON ( ) )
@@ -179,20 +191,18 @@ async function handleConnectCalendarCallback (reqQuery) {
179
191
try {
180
192
// getting user's accessToken from Nylas using 'code' found in request query
181
193
const { accessToken, accountId, provider } = await NylasService . getAccessToken ( reqQuery . code )
182
-
183
194
// view https://developer.nylas.com/docs/api/#post/oauth/token for error response schema
184
195
if ( ! accessToken || ! accountId ) {
185
196
throw new errors . BadRequestError ( 'Error during getting access token for the calendar.' )
186
197
}
187
198
188
199
// getting user's all existing calendars
189
200
const calendars = await NylasService . getExistingCalendars ( accessToken )
190
-
191
201
if ( ! Array . isArray ( calendars ) || calendars . length < 1 ) {
192
202
throw new errors . BadRequestError ( 'Error getting calendar data for the user.' )
193
203
}
194
204
195
- const primaryCalendar = NylasService . getPrimaryCalendar ( calendars )
205
+ const primaryCalendar = await NylasService . getPrimaryCalendar ( calendars )
196
206
if ( ! primaryCalendar ) {
197
207
throw new errors . NotFoundError ( 'Could not find any writable calendar.' )
198
208
}
@@ -215,6 +225,9 @@ async function handleConnectCalendarCallback (reqQuery) {
215
225
216
226
// reuse this method to create UserMeetingSettings object
217
227
if ( _ . isNil ( userMeetingSettings ) ) {
228
+ // method 'createUserMeetingSettingsIfNotExisting' expects keys in Nylas backend
229
+ // API format, so extend calendarDetails object with 'is_primary' key
230
+ _ . extend ( calendarDetails , { is_primary : calendarDetails . isPrimary } )
218
231
userMeetingSettings = await createUserMeetingSettingsIfNotExisting (
219
232
currentUser ,
220
233
userId ,
@@ -272,7 +285,7 @@ async function deleteUserCalendar (currentUser, reqParams) {
272
285
await ensureUserIsPermitted ( currentUser , reqParams . userId )
273
286
274
287
try {
275
- const userMeetingSettings = await getUserMeetingSettingsByUserId ( currentUser , reqParams . userId )
288
+ const userMeetingSettings = await getUserMeetingSettingsByUserId ( currentUser , reqParams . userId , false , { shouldNotStripUnwantedData : true } )
276
289
277
290
// error if no calendar found with the given id in request param
278
291
if (
0 commit comments