-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle-api.js
More file actions
482 lines (429 loc) · 15 KB
/
Copy pathgoogle-api.js
File metadata and controls
482 lines (429 loc) · 15 KB
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/**
* google-api.js
* Handles Google OAuth authentication and REST calls to Calendar and Tasks APIs.
* Includes a robust Mock fallback when credentials are not configured or Offline.
*/
class GoogleAPIManager {
constructor() {
this.clientId = '';
this.accessToken = null;
this.tokenClient = null;
this.gisLoaded = false;
// Scopes needed for calendar and tasks
this.scopes = 'https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/tasks';
// Mock data storage key
this.mockCalendarKey = 'concierge_mock_calendar';
this.mockTasksKey = 'concierge_mock_tasks';
}
init(clientId) {
this.clientId = clientId || localStorage.getItem('google_client_id') || '';
this.accessToken = localStorage.getItem('google_access_token') || null;
// Load Google Identity Services SDK
this.loadGIS();
// Initialize mock data if empty
this.initMockData();
}
loadGIS() {
if (window.google && window.google.accounts) {
this.gisLoaded = true;
this.initTokenClient();
return;
}
const script = document.createElement('script');
script.src = 'https://accounts.google.com/gsi/client';
script.async = true;
script.defer = true;
script.onload = () => {
this.gisLoaded = true;
this.initTokenClient();
};
document.head.appendChild(script);
}
initTokenClient() {
if (!this.clientId || !window.google) return;
try {
this.tokenClient = google.accounts.oauth2.initTokenClient({
client_id: this.clientId,
scope: this.scopes,
callback: (response) => {
if (response.error !== undefined) {
console.error("Google Auth Error:", response);
return;
}
this.accessToken = response.access_token;
localStorage.setItem('google_access_token', this.accessToken);
// Trigger a global event to refresh dashboard
window.dispatchEvent(new Event('google-auth-success'));
},
});
} catch (e) {
console.error("Error initializing Google Identity Services Token Client:", e);
}
}
setClientId(clientId) {
this.clientId = clientId;
localStorage.setItem('google_client_id', clientId);
this.initTokenClient();
}
isAuthorized() {
return !!this.accessToken;
}
async login() {
if (!this.clientId) {
throw new Error("No Google Client ID configured. Please set it in Settings.");
}
if (!this.tokenClient) {
this.initTokenClient();
}
if (!this.tokenClient) {
throw new Error("Google GIS client not initialized. Check internet connection.");
}
// Request permission (forces popup)
this.tokenClient.requestAccessToken({ prompt: 'consent' });
}
logout() {
this.accessToken = null;
localStorage.removeItem('google_access_token');
window.dispatchEvent(new Event('google-auth-logout'));
}
// ----------------------------------------------------
// Mock Data Generators for Fallback / Demonstration
// ----------------------------------------------------
initMockData() {
const today = toLocalDateString();
const getRelativeDate = (offsetDays) => {
const d = new Date();
d.setDate(d.getDate() + offsetDays);
return toLocalDateString(d);
};
if (!localStorage.getItem(this.mockCalendarKey)) {
const defaultEvents = [
{
id: 'mock-evt-1',
summary: 'Morning Vital Checks',
description: 'Blood pressure & sugar checks for John.',
start: { dateTime: `${today}T09:00:00` },
end: { dateTime: `${today}T10:00:00` },
completed: true
},
{
id: 'mock-evt-2',
summary: 'Physical Therapy',
description: 'Dr. Marcus home visit for stretches.',
start: { dateTime: `${today}T11:30:00` },
end: { dateTime: `${today}T12:30:00` },
completed: false
},
{
id: 'mock-evt-3',
summary: 'Optometrist Appointment',
description: '10:00 AM • Main St Vision Center',
start: { dateTime: `${getRelativeDate(1)}T10:00:00` },
end: { dateTime: `${getRelativeDate(1)}T11:00:00` },
completed: false
},
{
id: 'mock-evt-4',
summary: 'Grocery Restock',
description: 'Focused on fresh organic greens.',
start: { dateTime: `${getRelativeDate(2)}T14:00:00` },
end: { dateTime: `${getRelativeDate(2)}T15:00:00` },
completed: false
},
{
id: 'mock-evt-5',
summary: 'Social Tea Hour',
description: 'Community Center Hall B',
start: { dateTime: `${getRelativeDate(4)}T15:30:00` },
end: { dateTime: `${getRelativeDate(4)}T17:00:00` },
completed: false
}
];
localStorage.setItem(this.mockCalendarKey, JSON.stringify(defaultEvents));
}
if (!localStorage.getItem(this.mockTasksKey)) {
const defaultTasks = [
{ id: 'mock-tsk-1', title: 'Submit weekly meal plan', status: 'needsAction' },
{ id: 'mock-tsk-2', title: 'Update hydration tracker', status: 'needsAction' },
{ id: 'mock-tsk-3', title: 'Coordinate family visit', status: 'needsAction' }
];
localStorage.setItem(this.mockTasksKey, JSON.stringify(defaultTasks));
}
}
// ----------------------------------------------------
// CALENDAR OPERATIONS (Google API or Mock Fallback)
// ----------------------------------------------------
async listCalendarEvents() {
if (!this.accessToken) {
// Mock Fallback
return JSON.parse(localStorage.getItem(this.mockCalendarKey)) || [];
}
try {
const today = new Date();
today.setHours(0,0,0,0);
const timeMin = today.toISOString();
const response = await fetch(
`https://www.googleapis.com/calendar/v3/calendars/primary/events?timeMin=${timeMin}&singleEvents=true&orderBy=startTime`,
{
headers: {
'Authorization': `Bearer ${this.accessToken}`,
'Content-Type': 'application/json'
}
}
);
if (response.status === 401) {
this.logout();
return this.listCalendarEvents(); // Recursively call using mock fallback after logout
}
const data = await response.json();
return data.items || [];
} catch (e) {
console.warn("Google Calendar fetch failed. Using mock fallback.", e);
return JSON.parse(localStorage.getItem(this.mockCalendarKey)) || [];
}
}
async createCalendarEvent(eventDetails) {
if (!this.accessToken) {
// Mock Creation
const mockEvents = JSON.parse(localStorage.getItem(this.mockCalendarKey)) || [];
const newEvent = {
id: 'mock-evt-' + Date.now(),
summary: eventDetails.summary,
description: eventDetails.description || '',
start: { dateTime: eventDetails.startDateTime },
end: { dateTime: eventDetails.endDateTime },
completed: false
};
mockEvents.push(newEvent);
// Sort mock events by start time
mockEvents.sort((a, b) => a.start.dateTime.localeCompare(b.start.dateTime));
localStorage.setItem(this.mockCalendarKey, JSON.stringify(mockEvents));
return newEvent;
}
try {
const body = {
summary: eventDetails.summary,
description: eventDetails.description || '',
start: { dateTime: eventDetails.startDateTime, timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone },
end: { dateTime: eventDetails.endDateTime, timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone }
};
const response = await fetch(
`https://www.googleapis.com/calendar/v3/calendars/primary/events`,
{
method: 'POST',
headers: {
'Authorization': `Bearer ${this.accessToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
}
);
if (response.status === 401) {
this.logout();
throw new Error("Google Session Expired. Re-authenticating.");
}
return await response.json();
} catch (e) {
console.error("Google Calendar Create failed.", e);
throw e;
}
}
async deleteCalendarEvent(eventId) {
if (!this.accessToken || eventId.startsWith('mock-')) {
// Mock Delete
let mockEvents = JSON.parse(localStorage.getItem(this.mockCalendarKey)) || [];
mockEvents = mockEvents.filter(evt => evt.id !== eventId);
localStorage.setItem(this.mockCalendarKey, JSON.stringify(mockEvents));
return true;
}
try {
const response = await fetch(
`https://www.googleapis.com/calendar/v3/calendars/primary/events/${eventId}`,
{
method: 'DELETE',
headers: {
'Authorization': `Bearer ${this.accessToken}`
}
}
);
if (response.status === 401) {
this.logout();
throw new Error("Google Session Expired.");
}
return response.ok;
} catch (e) {
console.error("Google Calendar delete failed.", e);
throw e;
}
}
async toggleCalendarEventCompleted(eventId, completed) {
// Note: Google Calendar events don't have a native 'completed' status, so we append a checkmark in the title or store the completed state locally
const mockEvents = JSON.parse(localStorage.getItem(this.mockCalendarKey)) || [];
const eventIndex = mockEvents.findIndex(evt => evt.id === eventId);
if (eventIndex !== -1) {
mockEvents[eventIndex].completed = completed;
localStorage.setItem(this.mockCalendarKey, JSON.stringify(mockEvents));
return mockEvents[eventIndex];
}
// For live Google Events, we will track completion state in a localStorage map indexed by event ID
const completedMapKey = 'concierge_completed_events_map';
const completedMap = JSON.parse(localStorage.getItem(completedMapKey)) || {};
completedMap[eventId] = completed;
localStorage.setItem(completedMapKey, JSON.stringify(completedMap));
return { id: eventId, completed };
}
async getEventCompletionState(eventId) {
if (eventId.startsWith('mock-')) {
const mockEvents = JSON.parse(localStorage.getItem(this.mockCalendarKey)) || [];
const evt = mockEvents.find(e => e.id === eventId);
return evt ? !!evt.completed : false;
}
const completedMapKey = 'concierge_completed_events_map';
const completedMap = JSON.parse(localStorage.getItem(completedMapKey)) || {};
return !!completedMap[eventId];
}
// ----------------------------------------------------
// TASKS OPERATIONS (Google API or Mock Fallback)
// ----------------------------------------------------
async listTasks() {
if (!this.accessToken) {
// Mock Fallback
return JSON.parse(localStorage.getItem(this.mockTasksKey)) || [];
}
try {
const response = await fetch(
`https://www.googleapis.com/tasks/v1/lists/@default/tasks?showCompleted=true`,
{
headers: {
'Authorization': `Bearer ${this.accessToken}`,
'Content-Type': 'application/json'
}
}
);
if (response.status === 401) {
this.logout();
return this.listTasks();
}
const data = await response.json();
return data.items || [];
} catch (e) {
console.warn("Google Tasks fetch failed. Using mock fallback.", e);
return JSON.parse(localStorage.getItem(this.mockTasksKey)) || [];
}
}
async createTask(title) {
if (!this.accessToken) {
// Mock Creation
const mockTasks = JSON.parse(localStorage.getItem(this.mockTasksKey)) || [];
const newTask = {
id: 'mock-tsk-' + Date.now(),
title: title,
status: 'needsAction'
};
mockTasks.push(newTask);
localStorage.setItem(this.mockTasksKey, JSON.stringify(mockTasks));
return newTask;
}
try {
const response = await fetch(
`https://www.googleapis.com/tasks/v1/lists/@default/tasks`,
{
method: 'POST',
headers: {
'Authorization': `Bearer ${this.accessToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ title: title, status: 'needsAction' })
}
);
if (response.status === 401) {
this.logout();
throw new Error("Google Session Expired.");
}
return await response.json();
} catch (e) {
console.error("Google Tasks Create failed.", e);
throw e;
}
}
async updateTaskStatus(taskId, completed) {
const status = completed ? 'completed' : 'needsAction';
if (!this.accessToken || taskId.startsWith('mock-')) {
// Mock Update
const mockTasks = JSON.parse(localStorage.getItem(this.mockTasksKey)) || [];
const taskIndex = mockTasks.findIndex(tsk => tsk.id === taskId);
if (taskIndex !== -1) {
mockTasks[taskIndex].status = status;
localStorage.setItem(this.mockTasksKey, JSON.stringify(mockTasks));
return mockTasks[taskIndex];
}
return null;
}
try {
// Get the full task representation first (required by patch/update API)
const getResponse = await fetch(
`https://www.googleapis.com/tasks/v1/lists/@default/tasks/${taskId}`,
{
headers: { 'Authorization': `Bearer ${this.accessToken}` }
}
);
const task = await getResponse.json();
task.status = status;
if (completed) {
task.completed = new Date().toISOString();
} else {
delete task.completed;
}
const response = await fetch(
`https://www.googleapis.com/tasks/v1/lists/@default/tasks/${taskId}`,
{
method: 'PUT',
headers: {
'Authorization': `Bearer ${this.accessToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(task)
}
);
if (response.status === 401) {
this.logout();
throw new Error("Google Session Expired.");
}
return await response.json();
} catch (e) {
console.error("Google Tasks status update failed.", e);
throw e;
}
}
async deleteTask(taskId) {
if (!this.accessToken || taskId.startsWith('mock-')) {
// Mock Delete
let mockTasks = JSON.parse(localStorage.getItem(this.mockTasksKey)) || [];
mockTasks = mockTasks.filter(tsk => tsk.id !== taskId);
localStorage.setItem(this.mockTasksKey, JSON.stringify(mockTasks));
return true;
}
try {
const response = await fetch(
`https://www.googleapis.com/tasks/v1/lists/@default/tasks/${taskId}`,
{
method: 'DELETE',
headers: {
'Authorization': `Bearer ${this.accessToken}`
}
}
);
if (response.status === 401) {
this.logout();
throw new Error("Google Session Expired.");
}
return response.ok;
} catch (e) {
console.error("Google Tasks delete failed.", e);
throw e;
}
}
}
// Export singleton instance
const googleAPI = new GoogleAPIManager();
window.googleAPI = googleAPI;