diff options
author | kimdahey <claire_kim1@brown.edu> | 2019-12-03 16:32:13 -0500 |
---|---|---|
committer | kimdahey <claire_kim1@brown.edu> | 2019-12-03 16:32:13 -0500 |
commit | 70583fa47bd9920d1823d381708c81283534d6ce (patch) | |
tree | 5c81976813436852403ea352797efe1d518dbb1a /src/server/ApiManagers/GeneralGoogleManager.ts | |
parent | 56b83d89f37a5523ab319977e3385f539ecaf996 (diff) | |
parent | 213962406327cc2f7267064f3016fabf0fd16872 (diff) |
merged w master
Diffstat (limited to 'src/server/ApiManagers/GeneralGoogleManager.ts')
-rw-r--r-- | src/server/ApiManagers/GeneralGoogleManager.ts | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/server/ApiManagers/GeneralGoogleManager.ts b/src/server/ApiManagers/GeneralGoogleManager.ts new file mode 100644 index 000000000..3617779d5 --- /dev/null +++ b/src/server/ApiManagers/GeneralGoogleManager.ts @@ -0,0 +1,61 @@ +import ApiManager, { Registration } from "./ApiManager"; +import { Method, _permission_denied } from "../RouteManager"; +import { GoogleApiServerUtils } from "../apis/google/GoogleApiServerUtils"; +import { Database } from "../database"; +import RouteSubscriber from "../RouteSubscriber"; + +const deletionPermissionError = "Cannot perform specialized delete outside of the development environment!"; + +const EndpointHandlerMap = new Map<GoogleApiServerUtils.Action, GoogleApiServerUtils.ApiRouter>([ + ["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", + onValidation: async ({ user, res }) => { + const token = await GoogleApiServerUtils.retrieveAccessToken(user.id); + if (!token) { + return res.send(GoogleApiServerUtils.generateAuthenticationUrl()); + } + return res.send(token); + } + }); + + register({ + method: Method.POST, + subscription: "/writeGoogleAccessToken", + onValidation: async ({ user, req, res }) => { + res.send(await GoogleApiServerUtils.processNewUser(user.id, req.body.authenticationCode)); + } + }); + + register({ + method: Method.POST, + subscription: new RouteSubscriber("googleDocs").add("sector", "action"), + onValidation: 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); + } + }); + + } +}
\ No newline at end of file |