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
|
import ApiManager, { Registration } from './ApiManager';
import { Method } from '../RouteManager';
import { GoogleApiServerUtils } from '../apis/google/GoogleApiServerUtils';
import RouteSubscriber from '../RouteSubscriber';
import { Database } from '../database';
import { google } from 'googleapis';
const EndpointHandlerMap = new Map<GoogleApiServerUtils.Action, GoogleApiServerUtils.ApiRouter>([
['create', (api, params) => api.create(params)],
['retrieve', (api, params) => api.get(params)],
['update', (api, params) => api.batchUpdate(params)],
]);
export default class GeneralGoogleManager extends ApiManager {
protected initialize(register: Registration): void {
register({
method: Method.GET,
subscription: '/readGoogleAccessToken',
secureHandler: async ({ user, res }) => {
const { credentials } = await GoogleApiServerUtils.retrieveCredentials(user.id);
if (!credentials?.access_token) {
return res.send(GoogleApiServerUtils.generateAuthenticationUrl());
}
return res.send(credentials);
},
});
register({
method: Method.POST,
subscription: '/writeGoogleAccessToken',
secureHandler: async ({ user, req, res }) => {
res.send(await GoogleApiServerUtils.processNewUser(user.id, req.body.authenticationCode));
},
});
register({
method: Method.GET,
subscription: '/revokeGoogleAccessToken',
secureHandler: async ({ user, res }) => {
await Database.Auxiliary.GoogleAccessToken.Revoke(user.id);
res.send();
},
});
register({
method: Method.POST,
subscription: new RouteSubscriber('googleDocs').add('sector', 'action'),
secureHandler: async ({ req, res, user }) => {
const sector: GoogleApiServerUtils.Service = req.params.sector as GoogleApiServerUtils.Service;
const action: GoogleApiServerUtils.Action = req.params.action as GoogleApiServerUtils.Action;
const endpoint = await GoogleApiServerUtils.GetEndpoint(GoogleApiServerUtils.Service[sector], user.id);
const handler = EndpointHandlerMap.get(action);
if (endpoint && handler) {
try {
const response = await handler(endpoint, req.body);
res.send(response.data);
} catch (e) {
res.send(e);
}
return;
}
res.send(undefined);
},
});
// Task Creation
register({
method: Method.POST,
subscription: new RouteSubscriber('googleTasks').add('create'),
secureHandler: async ({ req, res, user }) => {
try {
const auth = await GoogleApiServerUtils.retrieveOAuthClient(user.id);
if (!auth) {
return res.status(401).send('Google credentials missing or invalid.');
}
const tasks = google.tasks({ version: 'v1', auth });
const { title, notes, due, status, completed, deleted } = req.body;
const result = await tasks.tasks.insert({
tasklist: '@default',
requestBody: { title, notes, due, status, completed, deleted },
});
res.status(200).send(result.data);
} catch (err) {
console.error('Google Tasks error:', err);
res.status(500).send('Failed to create task.');
}
},
});
// Task Update
register({
method: Method.PATCH,
subscription: new RouteSubscriber('googleTasks').add('taskId'),
// any way to add static params? like /update (this is not very descriptive)
secureHandler: async ({ req, res, user }) => {
try {
const auth = await GoogleApiServerUtils.retrieveOAuthClient(user.id);
if (!auth) {
return res.status(401).send('Google credentials missing or invalid.');
}
const tasks = google.tasks({ version: 'v1', auth });
const { taskId } = req.params;
const { title, notes, due, status, completed, deleted } = req.body;
const result = await tasks.tasks.patch({
tasklist: '@default',
task: taskId,
requestBody: { title, notes, due, status, completed, deleted},
});
res.status(200).send(result.data);
} catch (err) {
console.error('Google Tasks update error:', err);
res.status(500).send('Failed to update task.');
}
},
});
// Task Deletion
register({
method: Method.DELETE,
subscription: new RouteSubscriber('googleTasks').add('taskId'),
secureHandler: async ({ req, res, user }) => {
try {
const auth = await GoogleApiServerUtils.retrieveOAuthClient(user.id);
if (!auth) {
return res.status(401).send('Google credentials missing or invalid.');
}
const tasks = google.tasks({ version: 'v1', auth });
const { taskId } = req.params;
await tasks.tasks.delete({
tasklist: '@default',
task: taskId,
});
res.status(200).send({ success: true });
} catch (err) {
console.error('Google Tasks delete error:', err);
res.status(500).send('Failed to delete task.');
}
},
});
// Google Account Linking
register({
method: Method.GET,
subscription: '/refreshGoogle',
secureHandler: async ({ user, req, res }) =>
new Promise<void>(resolve =>
GoogleApiServerUtils.processNewUser(user.id, req.query.code as string)
.then(() => res.status(200).send('Google account linked successfully!'))
.catch(err => {
console.error('Failed to process Google code:', err);
res.status(500).send('Error linking Google account');
})
.finally(resolve)
),
});
// Task Retrieval
register({
method: Method.GET,
subscription: new RouteSubscriber('googleTasks').add('taskId'),
secureHandler: async ({ req, res, user }) => {
try {
const auth = await GoogleApiServerUtils.retrieveOAuthClient(user.id);
if (!auth) {
return res.status(401).send('Google credentials missing or invalid.');
}
const tasks = google.tasks({ version: 'v1', auth });
const { taskId } = req.params;
const result = await tasks.tasks.get({
tasklist: '@default',
task: taskId,
});
res.status(200).send(result.data);
} catch (err) {
console.error('Google Tasks retrieval error:', err);
res.status(500).send('Failed to retrieve task.');
}
},
});
}
}
|