diff options
-rw-r--r-- | src/client/apis/GoogleAuthenticationManager.tsx | 2 | ||||
-rw-r--r-- | src/client/views/nodes/TaskBox.tsx | 40 | ||||
-rw-r--r-- | src/server/ApiManagers/GeneralGoogleManager.ts | 35 | ||||
-rw-r--r-- | src/server/apis/google/GoogleTasksHandler.ts | 54 |
4 files changed, 77 insertions, 54 deletions
diff --git a/src/client/apis/GoogleAuthenticationManager.tsx b/src/client/apis/GoogleAuthenticationManager.tsx index 94ce42d8d..46581397d 100644 --- a/src/client/apis/GoogleAuthenticationManager.tsx +++ b/src/client/apis/GoogleAuthenticationManager.tsx @@ -41,6 +41,8 @@ export class GoogleAuthenticationManager extends React.Component<object> { if (new RegExp(AuthenticationUrl).test(response)) { this.isOpen = true; this.authenticationLink = response; + + // GETS STUCK AT THIS PROMISE!! return new Promise<string>(resolve => { this.disposer?.(); this.disposer = reaction( diff --git a/src/client/views/nodes/TaskBox.tsx b/src/client/views/nodes/TaskBox.tsx index 9d59746f8..1c7aeeb82 100644 --- a/src/client/views/nodes/TaskBox.tsx +++ b/src/client/views/nodes/TaskBox.tsx @@ -8,6 +8,7 @@ import { DateField } from '../../../fields/DateField'; import { Doc } from '../../../fields/Doc'; import './TaskBox.scss'; +import { GoogleAuthenticationManager } from '../../apis/GoogleAuthenticationManager'; /** * Props (reference to document) for Task Box @@ -267,6 +268,45 @@ export class TaskBox extends React.Component<TaskBoxProps> { </label> </div> )} + + {/** test button */} + <button + className="task-manager-google" + onClick={async () => { + console.log('GT button clicked'); + const token = await GoogleAuthenticationManager.Instance.fetchOrGenerateAccessToken(); + console.log('Got token', token); + + try { + const response = await fetch('/googleTasks/create', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}`, + }, + body: JSON.stringify({ + title: taskTitle || 'Untitled Task', + notes: taskDesc, + due: allDay + ? String(doc.$task_dateRange)?.split('|')[0] + 'T23:59:00Z' + : (doc.$task_endTime instanceof DateField && doc.$task_endTime.date?.toISOString()) || undefined, + }), + }); + + const result = await response.json(); + console.log("📬 Google Task result:", result); + alert(result?.id ? 'Task sent to Google Tasks!' : `Failed: ${result?.error?.message || 'Unknown error'}`); + } catch (err) { + console.error("Fetch error:", err); + alert("Fetch failed: "); + } + }} + > + GT + </button> + + + </div> ); } diff --git a/src/server/ApiManagers/GeneralGoogleManager.ts b/src/server/ApiManagers/GeneralGoogleManager.ts index 12913b1ef..aa06ca1b3 100644 --- a/src/server/ApiManagers/GeneralGoogleManager.ts +++ b/src/server/ApiManagers/GeneralGoogleManager.ts @@ -3,6 +3,8 @@ 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)], @@ -61,5 +63,38 @@ export default class GeneralGoogleManager extends ApiManager { res.send(undefined); }, }); + + // AARAV ADD (creating a task) + + register({ + method: Method.POST, + subscription: new RouteSubscriber('googleTasks').add('create'), + secureHandler: async ({ req, res, user }) => { + try { + const { credentials } = await GoogleApiServerUtils.retrieveCredentials(user.id); + if (!credentials?.access_token) { + return res.status(401).send('Google access token not found.'); + } + + const auth = new google.auth.OAuth2(); + auth.setCredentials({ access_token: credentials.access_token }); + + const tasks = google.tasks({ version: 'v1', auth }); + + const { title, notes, due } = req.body; + const result = await tasks.tasks.insert({ + tasklist: '@default', + requestBody: { title, notes, due }, + }); + + res.status(200).send(result.data); + } catch (err) { + console.error('Google Tasks error:', err); + res.status(500).send('Failed to create task.'); + } + }, + }); + + } } diff --git a/src/server/apis/google/GoogleTasksHandler.ts b/src/server/apis/google/GoogleTasksHandler.ts deleted file mode 100644 index a8af86fe2..000000000 --- a/src/server/apis/google/GoogleTasksHandler.ts +++ /dev/null @@ -1,54 +0,0 @@ -import express from 'express'; -import { google } from 'googleapis'; -import { GoogleCredentialsLoader } from './CredentialsLoader'; -import User from '../../authentication/DashUserModel'; -import { DashUserModel } from '../../authentication/DashUserModel'; - - -const router = express.Router(); - -router.post('/tasks/create', async (req, res) => { - try { - const { title, notes, due } = req.body; - - // Make sure user is authenticated - if (!req.user) { - return res.status(401).json({ error: 'User not authenticated' }); - } - - // Assuming access token is stored in user model - const user = req.user as typeof User; // replace with your actual User type if needed - const accessToken = user.googleAccessToken; // <-- change this based on where you store it - - if (!accessToken) { - return res.status(400).json({ error: 'Google access token not found for user' }); - } - - const credentials = GoogleCredentialsLoader.ProjectCredentials; - const auth = new google.auth.OAuth2( - credentials.client_id, - credentials.client_secret, - credentials.redirect_uris[0] - ); - - auth.setCredentials({ access_token: accessToken }); - - const tasks = google.tasks({ version: 'v1', auth }); - - const result = await tasks.tasks.insert({ - tasklist: '@default', - requestBody: { - title, - notes, - due, - }, - }); - - res.status(200).json(result.data); - } catch (err) { - console.error('Error creating Google Task:', err); - res.status(500).json({ error: 'Failed to create task' }); - } -}); - -export default router; |