Skip to content

Commit

Permalink
Merge pull request #1006 from OneCommunityGlobal/Nathalia-new-filter-…
Browse files Browse the repository at this point in the history
…project-by-user-name

Nathalia new filter project by user name
  • Loading branch information
one-community authored Jul 6, 2024
2 parents 14918f5 + 7de007a commit 26ee65f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
54 changes: 53 additions & 1 deletion src/controllers/userProfileController.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const cacheClosure = require('../utilities/nodeCache');
const followUp = require('../models/followUp');
const userService = require('../services/userService');
// const { authorizedUserSara, authorizedUserJae } = process.env;
const authorizedUserSara = `sucheta_mu@test.com`; // To test this code please include your email here
const authorizedUserSara = `nathaliaowner@gmail.com`; // To test this code please include your email here
const authorizedUserJae = `[email protected]`;

const { hasPermission, canRequestorUpdateUser } = require('../utilities/permissions');
Expand Down Expand Up @@ -1452,6 +1452,57 @@ const userProfileController = function (UserProfile) {
}
};

const getProjectsByPerson = async function (req, res) {
try {
const { name } = req.params;
const match = name.trim().split(' ');
const firstName = match[0];
const lastName = match[match.length - 1];

const query = match[1]
? {
$or: [
{
firstName: { $regex: new RegExp(`${escapeRegExp(name)}`, 'i') },
},
{
$and: [
{ firstName: { $regex: new RegExp(`${escapeRegExp(firstName)}`, 'i') } },
{ lastName: { $regex: new RegExp(`${escapeRegExp(lastName)}`, 'i') } },
],
},
],
}
: {
$or: [
{
firstName: { $regex: new RegExp(`${escapeRegExp(name)}`, 'i') },
},
{
lastName: { $regex: new RegExp(`${escapeRegExp(name)}`, 'i') },
},
],
};

const userProfile = await UserProfile.find(query);

if (userProfile) {
const allProjects = userProfile
.map((user) => user.projects)
.filter((projects) => projects.length > 0)
.flat();

if (allProjects.length === 0) {
return res.status(400).send({ message: 'Projects not found' });
}

return res.status(200).send({ message: 'Found profile and related projects', allProjects });
}
} catch (error) {
return res.status(500).send({ massage: 'Encountered an error, please try again!' });
}
};

return {
postUserProfile,
getUserProfiles,
Expand All @@ -1473,6 +1524,7 @@ const userProfileController = function (UserProfile) {
getUserByFullName,
changeUserRehireableStatus,
authorizeUser,
getProjectsByPerson,
};
};

Expand Down
6 changes: 4 additions & 2 deletions src/routes/userProfileRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const routes = function (userProfile) {
.route('/userProfile')
.get(controller.getUserProfiles)
.post(
body('firstName').customSanitizer((value) => value.trim()),
body('lastName').customSanitizer((value) => value.trim()),
body('firstName').customSanitizer((req) => req.trim()),
body('lastName').customSanitizer((req) => req.trim()),
controller.postUserProfile,
);

Expand Down Expand Up @@ -87,6 +87,8 @@ const routes = function (userProfile) {
.route('/userProfile/authorizeUser/weeeklySummaries')
.post(controller.authorizeUser);

userProfileRouter.route('/userProfile/projects/:name').get(controller.getProjectsByPerson);

return userProfileRouter;
};

Expand Down

0 comments on commit 26ee65f

Please sign in to comment.