From 3345a1ebfc7129f89beb487b8e7c09ff1f64dd66 Mon Sep 17 00:00:00 2001 From: Ambika Kabra Date: Fri, 16 Aug 2024 00:21:43 -0700 Subject: [PATCH] ambika manually merges PR 1061 --- src/controllers/titleController.js | 15 ++++++++-- src/controllers/userProfileController.js | 38 +++++++++++++++++++++++- src/models/team.js | 7 +++++ src/models/userProfile.js | 5 +++- src/routes/userProfileRouter.js | 2 ++ 5 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/controllers/titleController.js b/src/controllers/titleController.js index f351c6e77..08751bdef 100644 --- a/src/controllers/titleController.js +++ b/src/controllers/titleController.js @@ -1,7 +1,11 @@ const Team = require('../models/team'); const Project = require('../models/project'); +const cacheClosure = require('../utilities/nodeCache'); +const { getAllTeamCodeHelper } = require("./userProfileController"); const titlecontroller = function (Title) { + const cache = cacheClosure(); + const getAllTitles = function (req, res) { Title.find({}) .then((results) => res.status(200).send(results)) @@ -97,11 +101,15 @@ const titlecontroller = function (Title) { res.status(500).send(error); }); }; - + // Update: Confirmed with Jae. Team code is not related to the Team data model. But the team code field within the UserProfile data model. async function checkTeamCodeExists(teamCode) { try { - const team = await Team.findOne({ teamCode }).exec(); - return !!team; + if (cache.getCache('teamCodes')) { + const teamCodes = JSON.parse(cache.getCache('teamCodes')); + return teamCodes.includes(teamCode); + } + const teamCodes = await getAllTeamCodeHelper(); + return teamCodes.includes(teamCode); } catch (error) { console.error('Error checking if team code exists:', error); throw error; @@ -128,3 +136,4 @@ const titlecontroller = function (Title) { }; module.exports = titlecontroller; + \ No newline at end of file diff --git a/src/controllers/userProfileController.js b/src/controllers/userProfileController.js index a61cf3c43..47ab44478 100644 --- a/src/controllers/userProfileController.js +++ b/src/controllers/userProfileController.js @@ -495,7 +495,6 @@ const userProfileController = function (UserProfile, Project) { 'totalTangibleHrs', 'totalIntangibleHrs', 'isFirstTimelog', - 'teamCode', 'isVisible', 'bioPosted', ]; @@ -506,6 +505,16 @@ const userProfileController = function (UserProfile, Project) { } }); + // Since we leverage cache for all team code retrival (refer func getAllTeamCode()), + // we need to remove the cache when team code is updated in case of new team code generation + if (req.body.teamCode) { + // remove teamCode cache when new team assigned + if (req.body.teamCode !== record.teamCode) { + cache.removeCache('teamCodes'); + } + record.teamCode = req.body.teamCode; + } + record.lastModifiedDate = Date.now(); // find userData in cache @@ -1565,6 +1574,31 @@ const userProfileController = function (UserProfile, Project) { } }; + const getAllTeamCodeHelper = async function () { + try { + if (cache.hasCache('teamCodes')) { + const teamCodes = JSON.parse(cache.getCache('teamCodes')); + return teamCodes; + } + const distinctTeamCodes = await UserProfile.distinct('teamCode', { + teamCode: { $ne: null } + }); + cache.setCache('teamCodes', JSON.stringify(distinctTeamCodes)); + return distinctTeamCodes; + } catch (error) { + throw new Error('Encountered an error to get all team codes, please try again!'); + } + } + + const getAllTeamCode = async function (req, res) { + try { + const distinctTeamCodes = await getAllTeamCodeHelper(); + return res.status(200).send({ message: 'Found', distinctTeamCodes }); + } catch (error) { + return res.status(500).send({ message: 'Encountered an error to get all team codes, please try again!' }); + } + } + return { postUserProfile, getUserProfiles, @@ -1587,6 +1621,8 @@ const userProfileController = function (UserProfile, Project) { changeUserRehireableStatus, authorizeUser, getProjectsByPerson, + getAllTeamCode, + getAllTeamCodeHelper, }; }; diff --git a/src/models/team.js b/src/models/team.js index a92e740b1..4d73615f5 100644 --- a/src/models/team.js +++ b/src/models/team.js @@ -2,6 +2,12 @@ const mongoose = require('mongoose'); const { Schema } = mongoose; +/** + * This schema represents a team in the system. + * + * Deprecated field: teamCode. Team code is no longer associated with a team. + * Team code is used as a text string identifier in the user profile data model. + */ const team = new Schema({ teamName: { type: 'String', required: true }, isActive: { type: 'Boolean', required: true, default: true }, @@ -14,6 +20,7 @@ const team = new Schema({ visible: { type : 'Boolean', default:true}, }, ], + // Deprecated field teamCode: { type: 'String', default: '', diff --git a/src/models/userProfile.js b/src/models/userProfile.js index c3e42b778..cc7136f54 100644 --- a/src/models/userProfile.js +++ b/src/models/userProfile.js @@ -75,7 +75,7 @@ const userProfileSchema = new Schema({ startDate: { type: Date, required: true, - default() { + default () { return this.createdDate; }, }, @@ -271,4 +271,7 @@ userProfileSchema.pre('save', function (next) { .catch((error) => next(error)); }); +userProfileSchema.index({ teamCode: 1 }); +userProfileSchema.index({ email: 1 }); + module.exports = mongoose.model('userProfile', userProfileSchema, 'userProfiles'); diff --git a/src/routes/userProfileRouter.js b/src/routes/userProfileRouter.js index e9c458b1b..795f6cfa3 100644 --- a/src/routes/userProfileRouter.js +++ b/src/routes/userProfileRouter.js @@ -102,6 +102,8 @@ const routes = function (userProfile, project) { userProfileRouter.route('/userProfile/projects/:name').get(controller.getProjectsByPerson); + userProfileRouter.route('/userProfile/teamCode/list').get(controller.getAllTeamCode); + return userProfileRouter; };