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([ ['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); }, }); // AARAV ADD (creating a task) 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 } = req.body; const result = await tasks.tasks.insert({ tasklist: '@default', requestBody: { title, notes, due, status, completed }, }); res.status(200).send(result.data); } catch (err) { console.error('Google Tasks error:', err); res.status(500).send('Failed to create task.'); } }, }); register({ method: Method.GET, subscription: '/refreshGoogle', secureHandler: async ({ user, req, res }) => new Promise(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) ), }); } }