Skip to content

Commit

Permalink
Merge pr #103 from Resilient-Labs/feature/company-controller-updates-…
Browse files Browse the repository at this point in the history
…best-worst; issue #99

#99 Feature/company controller updates best worst
  • Loading branch information
aidan-diaz authored Jan 9, 2025
2 parents 6504364 + b302e14 commit 8a61435
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 36 deletions.
99 changes: 65 additions & 34 deletions server/controllers/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,63 +30,94 @@ const companyController = {
try {
const companies = await Company.aggregate([
{
//Joins the reviews collection with the Company collection + stores the matching documents in the 'reviewsData' array.

// Joins the reviews collection with the Company collection
$lookup: {
from: 'reviews', // Collection that we want data to join
localField: 'reviews', //first field in the current collection
foreignField: '_id', // Other field in the other collection
as: 'reviewsData', // New array field that is storing the joined data
from: 'reviews', // Collection to join
localField: '_id', // Match company ID
foreignField: 'companyId', // Match review's companyId
as: 'reviewsData', // Store joined data
},
},
{
// Calculate the average of all questions for each company
$addFields: {
averageRating: { $avg: '$reviewsData.companyCulture' }, // Calculate the average rating
averageRating: {
$avg: {
$map: {
input: '$reviewsData', // Loop through reviewsData
as: 'review',
in: {
$avg: [
'$$review.questions.accountability',
'$$review.questions.representation',
'$$review.questions.workLifeBalance',
'$$review.questions.careerGrowth',
'$$review.questions.diversityScale',
'$$review.questions.companyCulture',
'$$review.questions.salary',
],
},
},
},
},
},
},
{
$sort: { averageRating: -1 }, // Sort by averageRating in descending order
},
{
$limit: 5, // Limits to top 5 companies
},
])

res.status(200).json(companies)
{ $sort: { averageRating: -1 } }, // Sort by highest averageRating
{ $limit: 5 }, // Limit to top 5 companies
]);

console.log('Best Companies:', companies);
res.status(200).json(companies);
} catch (error) {
next(error)
next(error);
}

},
//Fetches 5 worst companies, sorted in ascending order using mongoDB
getWorstCompanies: async (req, res, next) => {
try {
const companies = await Company.aggregate([
{
//Joins the reviews collection with the Company collection + stores the matching documents in the 'reviewsData' array.

// Joins the reviews collection with the Company collection
$lookup: {
from: 'reviews', // Collection that we want data to join
localField: 'reviews', //first field in the current collection
foreignField: '_id', // Other field in the other collection
as: 'reviewsData', // New array field that is storing the joined data
from: 'reviews', // Collection to join
localField: '_id', // Match company ID
foreignField: 'companyId', // Match review's companyId
as: 'reviewsData', // Store joined data
},
},
{
// Calculate the average of all questions for each company
$addFields: {
averageRating: { $avg: '$reviewsData.companyCulture' }, // Calculate the average rating
averageRating: {
$avg: {
$map: {
input: '$reviewsData', // Loop through reviewsData
as: 'review',
in: {
$avg: [
'$$review.questions.accountability',
'$$review.questions.representation',
'$$review.questions.workLifeBalance',
'$$review.questions.careerGrowth',
'$$review.questions.diversityScale',
'$$review.questions.companyCulture',
'$$review.questions.salary',
],
},
},
},
},
},
},
{
$sort: { averageRating: 1 }, // Sort by averageRating in ascending order
},
{
$limit: 5, // Limit to bottom 5 companies
},
])

res.status(200).json(companies)
{ $sort: { averageRating: 1 } }, // Sort by lowest averageRating
{ $limit: 5 }, // Limit to bottom 5 companies
]);

console.log('Worst Companies:', companies);
res.status(200).json(companies);
} catch (error) {
next(error)
next(error);
}
},
searchCompany: async (req, res, next) => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/RightSidebar/RightSidebar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function RightSidebar() {
if (!response.ok)
throw new Error('Failed to fetch best companies.')
const data = await response.json()
console.log(data)
console.log('Best Companies:', data)
setBestCompanies(data)
} catch (error) {
console.error('Error fetching best companies:', error)
Expand All @@ -25,7 +25,7 @@ export default function RightSidebar() {
if (!response.ok)
throw new Error('Failed to fetch worst companies.')
const data = await response.json()
console.log(data)
console.log('Worst Companies:', data)
setWorstCompanies(data)
} catch (error) {
console.error('Error fetching worst companies:', error)
Expand Down

0 comments on commit 8a61435

Please sign in to comment.