From 2722e7dc164482749532790dd84c569f3160f2d5 Mon Sep 17 00:00:00 2001 From: caiodasilva2005 Date: Tue, 17 Dec 2024 18:17:03 -0500 Subject: [PATCH] #3060-added onboarding endpoints --- src/backend/index.ts | 2 ++ .../src/controllers/onboarding.controller.ts | 22 +++++++++++++++++++ src/backend/src/routes/onboarding.routes.ts | 8 +++++++ .../src/services/onboarding.services.ts | 12 ++++++++++ src/frontend/src/apis/onboarding.api.ts | 17 ++++++++++++++ src/frontend/src/hooks/organizations.hooks.ts | 2 +- src/frontend/src/utils/urls.ts | 5 +++++ 7 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/backend/src/controllers/onboarding.controller.ts create mode 100644 src/backend/src/routes/onboarding.routes.ts create mode 100644 src/backend/src/services/onboarding.services.ts create mode 100644 src/frontend/src/apis/onboarding.api.ts diff --git a/src/backend/index.ts b/src/backend/index.ts index babf50b843..680ead6886 100644 --- a/src/backend/index.ts +++ b/src/backend/index.ts @@ -17,6 +17,7 @@ import workPackageTemplatesRouter from './src/routes/work-package-templates.rout import carsRouter from './src/routes/cars.routes'; import organizationRouter from './src/routes/organizations.routes'; import recruitmentRouter from './src/routes/recruitment.routes'; +import onboardingRouter from './src/routes/onboarding.routes'; const app = express(); @@ -68,6 +69,7 @@ app.use('/templates', workPackageTemplatesRouter); app.use('/cars', carsRouter); app.use('/organizations', organizationRouter); app.use('/recruitment', recruitmentRouter); +app.use('/onboarding', onboardingRouter); app.use('/', (_req, res) => { res.status(200).json('Welcome to FinishLine'); }); diff --git a/src/backend/src/controllers/onboarding.controller.ts b/src/backend/src/controllers/onboarding.controller.ts new file mode 100644 index 0000000000..9c89420e78 --- /dev/null +++ b/src/backend/src/controllers/onboarding.controller.ts @@ -0,0 +1,22 @@ +import { NextFunction, Request, Response } from 'express'; +import OnboardingServices from '../services/onboarding.services'; + +export default class OnboardingController { + static async downloadImage(req: Request, res: Response, next: NextFunction) { + try { + const { fileId } = req.params; + console.log('FILE ID:', fileId); + + const imageData = await OnboardingServices.downloadImage(fileId); + + // Set the appropriate headers for the HTTP response + res.setHeader('content-type', String(imageData.type)); + res.setHeader('content-length', imageData.buffer.length); + + // Send the Buffer as the response body + res.send(imageData.buffer); + } catch (error: unknown) { + return next(error); + } + } +} diff --git a/src/backend/src/routes/onboarding.routes.ts b/src/backend/src/routes/onboarding.routes.ts new file mode 100644 index 0000000000..fabce68796 --- /dev/null +++ b/src/backend/src/routes/onboarding.routes.ts @@ -0,0 +1,8 @@ +import express from 'express'; +import OnboardingController from '../controllers/onboarding.controller'; + +const onboardingRouter = express.Router(); + +onboardingRouter.get('/image/:fileId', OnboardingController.downloadImage); + +export default onboardingRouter; diff --git a/src/backend/src/services/onboarding.services.ts b/src/backend/src/services/onboarding.services.ts new file mode 100644 index 0000000000..823451c580 --- /dev/null +++ b/src/backend/src/services/onboarding.services.ts @@ -0,0 +1,12 @@ +import { NotFoundException } from '../utils/errors.utils'; +import { downloadImageFile } from '../utils/google-integration.utils'; + +export default class OnboardingServices { + static async downloadImage(fileId: string) { + const fileData = await downloadImageFile(fileId); + console.log('FILE DATA RECEIVED'); + + if (!fileData) throw new NotFoundException('Image File', fileId); + return fileData; + } +} diff --git a/src/frontend/src/apis/onboarding.api.ts b/src/frontend/src/apis/onboarding.api.ts new file mode 100644 index 0000000000..e6cbf8c53a --- /dev/null +++ b/src/frontend/src/apis/onboarding.api.ts @@ -0,0 +1,17 @@ +import axios from 'axios'; +import { apiUrls } from '../utils/urls'; + +/** + * API Call to download a google image + * @param fileId file id to be downloaded + * @returns an image blob + */ +export const downloadGoogleImage = async (fileId: string): Promise => { + const response = await axios.get(apiUrls.imageById(fileId), { + responseType: 'arraybuffer' // Set the response type to 'arraybuffer' to receive the image as a Buffer + }); + console.log('ID IN API:', fileId); + const imageBuffer = new Uint8Array(response.data); + const imageBlob = new Blob([imageBuffer], { type: response.headers['content-type'] }); + return imageBlob; +}; diff --git a/src/frontend/src/hooks/organizations.hooks.ts b/src/frontend/src/hooks/organizations.hooks.ts index bd2f3ac964..ddcbaf1190 100644 --- a/src/frontend/src/hooks/organizations.hooks.ts +++ b/src/frontend/src/hooks/organizations.hooks.ts @@ -10,7 +10,7 @@ import { setOrganizationLogo, setOrganizationFeaturedProjects } from '../apis/organizations.api'; -import { downloadGoogleImage } from '../apis/finance.api'; +import { downloadGoogleImage } from '../apis/onboarding.api'; interface OrganizationProvider { organizationId: string; diff --git a/src/frontend/src/utils/urls.ts b/src/frontend/src/utils/urls.ts index 4cb11c03e9..ae65afe744 100644 --- a/src/frontend/src/utils/urls.ts +++ b/src/frontend/src/utils/urls.ts @@ -196,6 +196,10 @@ const faqCreate = () => `${recruitment()}/faq/create`; const faqEdit = (id: string) => `${recruitment()}/faq/${id}/edit`; const faqDelete = (id: string) => `${recruitment()}/faq/${id}/delete`; +/************** Onboarding Endpoints ***************/ +const onboarding = () => `${API_URL}/onboarding`; +const imageById = (imageId: string) => `${onboarding()}/image/${imageId}`; + /**************** Other Endpoints ****************/ const version = () => `https://api.github.com/repos/Northeastern-Electric-Racing/FinishLine/releases/latest`; @@ -354,6 +358,7 @@ export const apiUrls = { faqCreate, faqEdit, faqDelete, + imageById, version };