Skip to content

Commit

Permalink
ambika manually merges PR 1061
Browse files Browse the repository at this point in the history
  • Loading branch information
kabraambika committed Aug 16, 2024
1 parent 1e837a2 commit 3345a1e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
15 changes: 12 additions & 3 deletions src/controllers/titleController.js
Original file line number Diff line number Diff line change
@@ -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))
Expand Down Expand Up @@ -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;
Expand All @@ -128,3 +136,4 @@ const titlecontroller = function (Title) {
};

module.exports = titlecontroller;

38 changes: 37 additions & 1 deletion src/controllers/userProfileController.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,6 @@ const userProfileController = function (UserProfile, Project) {
'totalTangibleHrs',
'totalIntangibleHrs',
'isFirstTimelog',
'teamCode',
'isVisible',
'bioPosted',
];
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -1587,6 +1621,8 @@ const userProfileController = function (UserProfile, Project) {
changeUserRehireableStatus,
authorizeUser,
getProjectsByPerson,
getAllTeamCode,
getAllTeamCodeHelper,
};
};

Expand Down
7 changes: 7 additions & 0 deletions src/models/team.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -14,6 +20,7 @@ const team = new Schema({
visible: { type : 'Boolean', default:true},
},
],
// Deprecated field
teamCode: {
type: 'String',
default: '',
Expand Down
5 changes: 4 additions & 1 deletion src/models/userProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const userProfileSchema = new Schema({
startDate: {
type: Date,
required: true,
default() {
default () {
return this.createdDate;
},
},
Expand Down Expand Up @@ -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');
2 changes: 2 additions & 0 deletions src/routes/userProfileRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down

0 comments on commit 3345a1e

Please sign in to comment.