diff --git a/server/controllers/profileController.ts b/server/controllers/profileController.ts deleted file mode 100644 index f51ac00..0000000 --- a/server/controllers/profileController.ts +++ /dev/null @@ -1,231 +0,0 @@ -import Profile from '../models/profileModel'; -import { Request, Response, NextFunction } from 'express'; -import { IProfile } from '../types/profile'; -import AWS from 'aws-sdk'; - -AWS.config.update({ - accessKeyId: process.env.AWS_ACCESS_KEY_ID, - secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, - region: process.env.AWS_REGION, -}); - -const s3 = new AWS.S3(); - -// ENDPOINT POST api/profiles/create -// PURPOSE Create a new profile -// ACCESS Private -const createProfile = async (req: Request, res: Response, next: NextFunction) => { - const { - user, - fullName, - nickname, - profilePhoto, - cohort, - graduationYear, - email, - linkedInProfile, - gitHubProfile, - professionalSummary, - skills, - specializations, - careerInformation, - education, - projects, - personalBio, - testimonials, - socialMediaLinks, - availabilityForNetworking, - bootcampExperience, - achievementsAndCertifications, - volunteerWork, - eventParticipation, - gallery, - blogOrWriting, - } = req.body; - - try { - const profile = await Profile.create({ - user, - fullName, - nickname, - profilePhoto, - cohort, - graduationYear, - email, - linkedInProfile, - gitHubProfile, - professionalSummary, - skills, - specializations, - careerInformation, - education, - projects, - personalBio, - testimonials, - socialMediaLinks, - availabilityForNetworking, - bootcampExperience, - achievementsAndCertifications, - volunteerWork, - eventParticipation, - gallery, - blogOrWriting, - }); - - if (profile) { - return res.status(201).json(profile); - } - } catch (error) { - console.error(error); - return next({ - log: 'Express error in createProfile Middleware', - status: 500, - message: { - err: 'An error occurred during profile creation. Please try again.', - }, - }); - } -}; - -// ENDPOINT PUT api/profiles/:UserID -// PURPOSE Update an existing profile -// ACCESS Private -const updateProfile = async (req: Request, res: Response, next: NextFunction) => { - const { userID } = req.params; - const { - nickName, - email, - personalBio, - linkedInProfile, - gitHubProfile, - cohort, - skills, - specializations, - careerInformation, - socialMediaLinks, - availabilityForNetworking, - } = req.body; - - const newProfile = { - nickName, - email, - personalBio, - linkedInProfile, - gitHubProfile, - cohort, - skills, - specializations, - careerInformation, - socialMediaLinks, - availabilityForNetworking, - }; - - try { - const profile: IProfile | null = await Profile.findOneAndUpdate({ user: userID }, newProfile, { - new: true, - }); - - if (!profile) { - return next({ - log: 'Express error in updateProfile Middleware - NO PROFILE FOUND', - status: 404, - message: { err: 'An error occurred during profile update' }, - }); - } else { - return res.status(200).json(profile); - } - } catch (error) { - return next({ - log: 'Express error in updateProfile Middleware', - status: 500, - message: { err: 'An error occurred during profile update' }, - }); - } -}; - -// ENDPOINT GET api/profiles -// PURPOSE Get all profiles -// ACCESS Private -const getAllProfiles = async (req: Request, res: Response, next: NextFunction) => { - try { - const profiles: IProfile[] = await Profile.find({}); - - if (profiles.length === 0) { - return next({ - log: 'There are no profiles to retrieve', - status: 404, - message: { err: 'There were no profiles to retrieve' }, - }); - } else { - // Route bypass for development - need AWS credentials to work on this route - // This allows Mock Profile Photos to work in development - if (process.env.NODE_ENV === 'development' && !process.env.IS_SK) { - return res.status(200).send(profiles); - } - - const processedProfiles = await Promise.all( - profiles.map(async (profile) => { - if (profile.profilePhoto) { - const presignedUrl = s3.getSignedUrl('getObject', { - Bucket: process.env.BUCKET_NAME, - Key: profile.profilePhoto, - Expires: 60 * 5, - }); - profile.profilePhoto = presignedUrl; - } - return profile.toObject(); - }), - ); - - return res.status(201).json(processedProfiles); - } - } catch (error) { - return next({ - log: 'Express error in getAllProfiles Middleware', - status: 500, - message: { err: 'An error occurred during profile creation' }, - }); - } -}; - -// ENDPOINT GET api/profiles/:userID -// PURPOSE Get profile by ID -// ACCESS Private -const getProfileById = async (req: Request, res: Response, next: NextFunction) => { - const { userID } = req.params; - try { - const profile: IProfile | null = await Profile.findOne({ user: userID }); - - if (!profile) { - return next({ - log: 'Profile does not exist', - status: 404, - message: { err: 'An error occurred during profile retrieval' }, - }); - } - // Route bypass for development - need AWS credentials to work on this route - // This allows Mock Profile Photos to work in development - if (process.env.NODE_ENV === 'development' && !process.env.IS_SK) { - return res.status(200).json(profile); - } - - if (profile.profilePhoto) { - const presignedUrl = s3.getSignedUrl('getObject', { - Bucket: process.env.BUCKET_NAME, - Key: profile.profilePhoto, - Expires: 60 * 5, - }); - profile.profilePhoto = presignedUrl; - } - - return res.status(200).json(profile); - } catch (error) { - return next({ - log: 'Express error in getProfileById Middleware', - status: 500, - message: { err: 'An error occurred during profile retrieval' }, - }); - } -}; - -export { createProfile, getAllProfiles, getProfileById, updateProfile }; diff --git a/server/controllers/profileController/createProfile/createProfile.ts b/server/controllers/profileController/createProfile/createProfile.ts index a38f22c..5f324cf 100644 --- a/server/controllers/profileController/createProfile/createProfile.ts +++ b/server/controllers/profileController/createProfile/createProfile.ts @@ -8,11 +8,13 @@ const createProfile = async (req: Request, res: Response, next: NextFunction) => const { user, fullName, + nickname, profilePhoto, cohort, graduationYear, email, linkedInProfile, + gitHubProfile, professionalSummary, skills, specializations, @@ -35,11 +37,13 @@ const createProfile = async (req: Request, res: Response, next: NextFunction) => const profile = await Profile.create({ user, fullName, + nickname, profilePhoto, cohort, graduationYear, email, linkedInProfile, + gitHubProfile, professionalSummary, skills, specializations, diff --git a/server/controllers/profileController/updateProfile/updateProfile.ts b/server/controllers/profileController/updateProfile/updateProfile.ts index a597bde..7db294f 100644 --- a/server/controllers/profileController/updateProfile/updateProfile.ts +++ b/server/controllers/profileController/updateProfile/updateProfile.ts @@ -14,13 +14,32 @@ AWS.config.update({ // ACCESS Private const updateProfile = async (req: Request, res: Response, next: NextFunction) => { const { userID } = req.params; - const { firstName, lastName, email, personalBio } = req.body; + const { + nickName, + email, + personalBio, + linkedInProfile, + gitHubProfile, + cohort, + skills, + specializations, + careerInformation, + socialMediaLinks, + availabilityForNetworking, + } = req.body; const newProfile = { - firstName, - lastName, + nickName, email, personalBio, + linkedInProfile, + gitHubProfile, + cohort, + skills, + specializations, + careerInformation, + socialMediaLinks, + availabilityForNetworking, }; try {