-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask-create.js
123 lines (100 loc) · 2.84 KB
/
task-create.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
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
'use strict';
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const api = require('./common/api');
module.exports = async (activity) => {
try {
api.initialize(activity);
let data = {};
// extract _action from Request
let _action = getObjPath(activity.Request, 'Data.model._action');
if (_action) {
activity.Request.Data.model._action = {};
} else {
_action = {};
}
switch (activity.Request.Path) {
case 'create':
case 'submit': {
const form = _action.form;
const body = {
subject: form.subject,
body: {
contentType: 'Text',
content: form.description
}
};
if (form.starttime) body.startDateTime = {
dateTime: new Date(form.starttime)
};
if (form.duetime) body.dueDateTime = {
dateTime: new Date(form.duetime)
};
const response = await api.post('/beta/me/outlook/tasks', {
json: true,
body: body
});
if ($.isErrorResponse(activity, response, [200, 201])) return;
const comment = 'Task created';
data = getObjPath(activity.Request, 'Data.model');
data._action = {
response: {
success: true,
message: comment
}
};
break;
}
default: {
// initialize form subject with query parameter (if provided)
if (activity.Request.Query && activity.Request.Query.query) {
data = {
form: {
subject: activity.Request.Query.query
}
};
}
const fname = __dirname + path.sep + 'common' + path.sep + 'task-create.form';
const schema = yaml.safeLoad(fs.readFileSync(fname, 'utf8'));
data.title = T(activity, 'Create Outlook Task');
data.formSchema = schema;
// initialize form subject with query parameter (if provided)
if (activity.Request.Query && activity.Request.Query.query) {
data = {
form: {
subject: activity.Request.Query.query
}
};
}
data._actionList = [{
id: 'create',
label: T(activity, 'Create Task'),
settings: {
actionType: 'a'
}
}];
break;
}
}
// copy response data
activity.Response.Data = data;
} catch (error) {
// handle generic exception
api.handleError(activity, error);
}
function getObjPath(obj, path) {
if (!path) return obj;
if (!obj) return null;
const paths = path.split('.');
let current = obj;
for (let i = 0; i < paths.length; ++i) {
if (!current[paths[i]]) {
return undefined;
} else {
current = current[paths[i]];
}
}
return current;
}
};