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 { credentials } = await GoogleApiServerUtils.retrieveCredentials(user.id); const access_token = user.googleToken || credentials?.access_token; // if googleToken expires, we need to renew it. if (!access_token) { return res.status(401).send('Google access token not found.'); } const auth = new google.auth.OAuth2(); auth.setCredentials({ access_token: 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.'); } }, }); register({ method: Method.GET, subscription: '/refreshGoogle', secureHandler: async ({ user, req, res }) => { const code = req.query.code as string; _success(res, code); user.googleToken = code; user.save(); // const response2 = await Networking.PostToServer('/writeGoogleAccessToken', { authenticationCode }); // runInAction(() => { // this.success = true; // this.credentials = response2 as { user_info: { name: string; picture: string }; access_token: string }; // }); }, }); } }