-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (69 loc) · 2.06 KB
/
index.js
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
const autoBind = require('auto-bind');
const debug = require('debug');
module.exports = class PMRObserver {
constructor(observable, calendarService, opts = {}) {
if (!observable) {
throw new Error('An observable is expected, in order to subscribe to calendars');
}
if (!calendarService) {
throw new Error('A calendarService is expected');
}
this.debug = debug('observer::webex-pmr');
this.calendars = observable;
this.calendarService = calendarService;
this.cmrDomain = opts.cmrDomain || 'cisco';
this.field = opts.field || 'location';
autoBind(this);
}
handler(event) {
if (this.shouldProcess(event)) {
this.debug('Contains @webex, processing');
this.processEvent(event);
}
}
shouldProcess(event) {
if (event.status !== 'confirmed') {
debug('Not a confirmed event, will not process');
return false;
}
const conditionField = event[this.field];
if (!conditionField) {
return false;
}
const containsWebex = conditionField.match(/@webex/i);
if (containsWebex) {
return true;
}
}
processEvent(event) {
const descriptionExist = event.description;
if (descriptionExist) {
if (event.description.indexOf('WebEx Details') > 0) {
// do nothing if it already has webex details
debug('Already contains WebEx details, disregard');
return;
}
}
const existingDescription = (event.description) ? event.description : '';
const updateInfo = {
description: this.buildDescription(existingDescription, this.cmrDomain, event.userId),
colorId: 9
};
const updatedEvent = Object.assign({}, event, updateInfo);
this.calendarService.updateEvent({
calendarId: event.calendarId,
eventId: event.id
}, updatedEvent)
.then(() => this.debug(`Sucessfully update ${event.calendarId}'s event: ${event.id}`))
.catch(this.debug);
}
buildDescription(existingDescription, cmrSite, userId) {
return `${existingDescription}
==== WebEx Details: Do Not Touch ====
http://${cmrSite}.webex.com/meet/${userId}`;
}
init() {
this.calendars.subscribe(this.handler);
this.debug('Subscribed to Calendars');
}
};