import ApiManager, { Registration } from './ApiManager'; import { Method, _success } 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); if (!credentials?.access_token) { const url = GoogleApiServerUtils.generateAuthenticationUrl(); return res.send(url); } 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); 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); 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 }) => { const code = req.query.code as string; console.log('/refreshGoogle hit with code:', code); try { const enriched = await GoogleApiServerUtils.processNewUser(user.id, code); if (enriched.refresh_token) { console.log('Enriched credentials:', enriched); if (enriched.refresh_token) { user.googleToken = enriched.refresh_token; await user.save(); console.log('Saved refresh token to user model'); } else { console.warn('No refresh token returned'); } } // await user.save(); // _success(res, 'Google account successfully linked!'); res.redirect('/home'); } catch (err) { console.error('Failed to process Google code:', err); res.status(500).send('Error linking Google account'); } // const response2 = await Networking.PostToServer('/writeGoogleAccessToken', { authenticationCode }); // runInAction(() => { // this.success = true; // this.credentials = response2 as { user_info: { name: string; picture: string }; access_token: string }; // }); }, }); } }