-
Notifications
You must be signed in to change notification settings - Fork 5
/
integrationTypeAll.gs
419 lines (358 loc) · 13.1 KB
/
integrationTypeAll.gs
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/**
* @fileoverview Code related to all integration types that the Odo Add-on
* supports, mostly related to setting and saving the configurations for
* those integration types.
*/
/**
* Integration types that Odo can be configured to simulate.
* @enum {string}
*/
const INTEGRATION_TYPE = {
GENERIC_SERVICE: 'GENERIC_SERVICE', // see integrationTypeService.gs
RECORDS_BASED: 'RECORDS_BASED', // see integrationTypeRecords.gs
FILE_REPOSITORY: 'FILE_REPOSITORY', // see integrationFileRepository.gs
IMAGE_LIBRARY: 'IMG_LIBRARY', // see integrationImageLibrary.gs
};
/**
* Hooks that each integration type must implement of the following format:
*
* [INTEGRATION_TYPE.XXX] {
* // Printable string for this integration type (shown in configuration
* // card)
* printableString: <string>,
*
* // Function the returns a struct with a default configuration
* // for this integration type (to be used if user doesn't configure
* // it themselves, as well to define default values on the integration
* // configuration card. Stored in 'integrationData' field of
* // the 'config' object.
* //
* // @return {Object}
* defaultIntegrationConfig: <function>,
*
* // Handler that builds a card with customization options. Shown
* // when user selects 'Customize Integration' from configuration card.
* //
* // @return {CardService.Card}
* buildConfigureIntegrationCard: <function>,
*
* // Handler that saves results of integration configuration card (when
* // user clicks "← Done"). Passed a formInputs objects with user's
* // selections. Should return the struct/Object to be saved in the
* // 'integrationData' field of the 'config' object.
* // @param {Objects} formInputs
* //
* // @return {Object}
* saveConfigureIntegrationSelections: <function>,
*
* // Define context specific handlers. All handlers get passed
* // the context (i.e. CALL_CONTEXT.DOCS) they were called from.
* //
* // Handler:
* // @param {string} context
* //
* // @return {CardService.Card}
* contextSpecificHandlers: {
* [CALL_CONTEXT.XXX] : <function>,
* [CALL_CONTEXT.DEFAULT] : <function>, // optional
* }
* }
*
*/
const INTEGRATION_HOOKS = {
[INTEGRATION_TYPE.RECORDS_BASED]: {
printableString: 'Records Based',
defaultIntegrationConfig: recordGetDefaultConfig,
buildConfigureIntegrationCard: buildRecordsConfigureCard,
saveConfigureIntegrationSelections: saveRecordsConfigureSelections,
contextSpecificHandlers: {
[CALL_CONTEXT.GMAIL_HOMEPAGE]: buildGmailHomepage,
[CALL_CONTEXT.GMAIL_COMPOSE]: buildRecordCard,
[CALL_CONTEXT.GMAIL_VIEW]: buildRecordCard,
[CALL_CONTEXT.DOCS]: buildRecordCard,
[CALL_CONTEXT.SHEETS]: buildRecordCard,
},
},
[INTEGRATION_TYPE.GENERIC_SERVICE]: {
printableString: 'Generic Service',
defaultIntegrationConfig: serviceBasicGetDefaultConfig,
buildConfigureIntegrationCard: buildServiceBasicConfigureCard,
saveConfigureIntegrationSelections: saveServiceBasicConfigureSelections,
// define context specific handlers. all handlers get passed
// the context (i.e. CALL_CONTEXT.DOCS) they were called from.
contextSpecificHandlers: {
[CALL_CONTEXT.DEFAULT]: buildServiceBasicCard,
},
},
[INTEGRATION_TYPE.FILE_REPOSITORY]: {
printableString: 'File Repository',
defaultIntegrationConfig: fileRepoGetDefaultConfig,
buildConfigureIntegrationCard: buildFileRepoConfigureCard,
saveConfigureIntegrationSelections: saveFileRepoConfigureSelections,
// define context specific handlers. all handlers get passed
// the context (i.e. CALL_CONTEXT.DOCS) they were called from.
contextSpecificHandlers: {
[CALL_CONTEXT.DRIVE] : buildFileRepoCard,
},
},
[INTEGRATION_TYPE.IMAGE_LIBRARY]: {
printableString: 'Image Library for Slides',
defaultIntegrationConfig: imgLibraryGetDefaultConfig,
buildConfigureIntegrationCard: buildImgLibraryConfigureCard,
saveConfigureIntegrationSelections: saveImgLibraryConfigureSelections,
// define context specific handlers. all handlers get passed
// the context (i.e. CALL_CONTEXT.DOCS) they were called from.
contextSpecificHandlers: {
[CALL_CONTEXT.SLIDES] : buildImgLibraryCard,
},
},
};
/**
* Function that converts an Integration Type enum into a printable
* string that can be shown to the user.
*
* @param {string} Record type (i.e. INTEGRATION_TYPE.RECORDS_BASED)
*
* @return {string}
*/
function integrationTypeToPrintableString(integrationType) {
let integrationConfig = INTEGRATION_HOOKS[integrationType];
return integrationConfig.printableString;
}
/**
* Function used to return the a formatted Card to be displayed.
* The specific card returned will depend on the simulated integration type,
* and the context it was called from. This function serves as a central
* dispatch hub to generate most cards shown in the Add-on.
*
* @param {string} context Calling context (i.e. CALL_CONTEXT.GMAIL_MESSAGE)
*
* @return {string{CardService.Card}
*/
function buildIntegrationCard(context) {
console.log('buildIntegrationCard: entering...');
let config = getConfig();
// if Odo yet to be configured, show splash screen
if (!config || !config.saved) {
return buildOdoWelcomeCard();
} else if (!config.welcomeSplashShown) {
return buildCustomerToolWelcomeSplash(context);
}
console.log(
'buildIntegrationCard: context = ' +
context +
', ' +
'integrationType = ' +
config.integrationType
);
let contextHandlers =
INTEGRATION_HOOKS[config.integrationType].contextSpecificHandlers;
if (contextHandlers.hasOwnProperty(context)) {
return contextHandlers[context](context);
} else if (contextHandlers.hasOwnProperty(CALL_CONTEXT.DEFAULT)) {
return contextHandlers[CALL_CONTEXT.DEFAULT](context);
} else {
return buildContextNotSupportedCard(context);
}
}
/**
* Builds and returns a placeholder card to indicate that a particular
* implementation has not yet been implemented.
*
* @return {CardService.Card}
*/
function buildIntegrationNotYetImplementedCard() {
let card = CardService.newCardBuilder();
let brandedHeader = buildCustomerBrandedHeader();
card.setHeader(brandedHeader);
let section = CardService.newCardSection();
let message = 'This integration has not yet been implemented.';
section.addWidget(CardService.newTextParagraph().setText(message));
card.addSection(section);
return card;
}
/**
* Builds and returns a placeholder card to indicate that a particular
* implementation does not support any actions in the given context
*
* @param {string} context - The calling context (CALL_CONTEXT.XXX)
*
* @return {CardService.Card}
*/
function buildContextNotSupportedCard(context) {
let card = CardService.newCardBuilder();
let brandedHeader = buildCustomerBrandedHeader();
card.setHeader(brandedHeader);
let message = 'This integration does not support '
+ `this context (${context}).`;
let section = CardService.newCardSection();
section.addWidget(CardService.newTextParagraph().setText(message));
card.addSection(section);
return card.build();
}
/**
* Builds and returns the card to customize the integration for the
* particular integration type specified by the paramater integrationType.
* As part of this, calls the 'buildConfigureIntegrationCard' function defined
* for the integration.
*
* This function is called from config.gs, but can also be called by
* specific integrations as a means to refresh their configuration card (see
* integrationTypeRecords.gs for an example).
*
* @param {string} integrationType - The selected integration type
*
* @return {CardService.Card}
*/
function buildCustomizeIntegrationCard(integrationType) {
// call the integration specific hook (function) to
// generate the card to configure the specific integration
let hooks = INTEGRATION_HOOKS[integrationType];
let card = hooks.buildConfigureIntegrationCard();
let subTitle = `${integrationTypeToPrintableString(integrationType)}`;
let header = CardService.newCardHeader()
.setTitle(`Customize Integration`)
.setSubtitle(subTitle)
.setImageStyle(CardService.ImageStyle.SQUARE)
.setImageUrl(ODO_ICON);
card.setHeader(header);
// Set up params to pass to save handler when user clicks "Done"
let params = {
selectedIntegrationType: integrationType,
};
let doneAction = CardService.newAction()
.setFunctionName('saveOdoIntegrationCustomizationSelections')
.setParameters(params);
let footer = CardService.newFixedFooter();
footer.setPrimaryButton(
CardService.newTextButton().setText('← Done').setOnClickAction(doneAction)
);
card.setFixedFooter(footer);
return card.build();
}
/**
* Saves the integration specific configurations when user clicks the
* '← Done' button in the configuration card. Does this by calling the
* 'saveConfigureIntegrationSelections' function defined for the selected
* integration type.
*
* Note: The data returned from 'saveConfigureIntegrationsSelections' is
* saved in the property PROP_STRINGIFIED_INTEGRATION_CUSTOMIZATIONS such that
* it can be written to the 'integrationData' section of the config if/when the
* user clicks 'Save' in the main configuration card.
*
* Returns ActionResponse that causes card stack to pop back to main
* configuration card.
*
* @param {Object} event
*
* @return {CardService.ActionResponse}
*/
function saveOdoIntegrationCustomizationSelections(event) {
let selectedIntegrationType = event.parameters.selectedIntegrationType;
let formInputs = event.commonEventObject.formInputs;
// pop this integration customization card to reveal the
// main Odo configuration card.
let action = CardService.newActionResponseBuilder()
.setNavigation(
CardService.newNavigation().popToNamedCard('mainConfigurationCard')
)
.setStateChanged(false)
.build();
// call the integration specific handler to save the selections
let hooks = INTEGRATION_HOOKS[selectedIntegrationType];
if (!hooks.saveConfigureIntegrationSelections) {
return action;
}
let integrationData = hooks.saveConfigureIntegrationSelections(formInputs);
// save the configuration settings the user has selected when they click
// 'Done'. These will be saved as the integrationData portion of the 'config'
// structure if/when the user clicks 'Save' on the main configuraion card.
let up = PropertiesService.getUserProperties();
let integrationDataStr = JSON.stringify(integrationData);
up.setProperty(
PROP_STRINGIFIED_INTEGRATION_CUSTOMIZATIONS,
integrationDataStr
);
return action;
}
/**
* Builds and returns a card that shows a welcome splash message
* on first use of the integration after it's been configured.
*
* @param {String} context
*/
function buildCustomerToolWelcomeSplash(context) {
let config = getConfig();
let card = CardService.newCardBuilder();
let brandedHeader = buildCustomerBrandedHeader();
card.setHeader(brandedHeader);
let section = CardService.newCardSection();
section.addWidget(CardService.newImage().setImageUrl(config.customerLogoUrl));
let welcomeSplashMessage = findAndReplaceMergeKeys(
config.welcomeSplashMessage
);
section.addWidget(
CardService.newTextParagraph().setText(welcomeSplashMessage)
);
let params = {
context: context,
};
let action = CardService.newAction()
.setFunctionName('_refreshIntegrationCard')
.setParameters(params);
section.addWidget(
CardService.newTextButton()
.setText('Get Started')
.setOnClickAction(action)
);
card.addSection(section);
return card.build();
}
/**
* Returns the integrationData stored in the configuration for the
* given integration type passed. If the specific integration hasn't been
* configured yet, then returns the defaults.
*
* @param {String} integrationType
*
* @return {Object}
*/
function getConfigIntegrationData(integrationType) {
let integrationData;
let config = getConfig();
let hooks = INTEGRATION_HOOKS[integrationType];
// if there is previously stored data for this integration type (i.e. user
// clicked "Save" on "Configure Odo" card), then display it.
// else, show the default values.
if (
config.saved &&
config.integrationType === integrationType
) {
integrationData = config.integrationData;
} else {
integrationData = hooks.defaultIntegrationConfig();
}
return integrationData;
}
/**
* Returns an ActionResponse that causes the current card to be updated
* with the integration card for the specified context. The context is passed
* in via an event parameter.
*
* @param {Object} event
*
* @return {CardService.ActionResponse}
*/
function _refreshIntegrationCard(event) {
let context = event.parameters.context;
let config = getConfig();
config.welcomeSplashShown = true;
saveConfig(config);
let card = buildIntegrationCard(context);
let action = CardService.newActionResponseBuilder()
.setNavigation(CardService.newNavigation().updateCard(card))
.setStateChanged(false)
.build();
return action;
}