Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend Release to Main [1.94] #1073

Merged
merged 5 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const Sentry = require('@sentry/node');

const app = express();
const logger = require('./startup/logger');
const globalErrorHandler = require('./utilities/errorHandling/globalErrorHandler').default;
const globalErrorHandler = require('./utilities/errorHandling/globalErrorHandler');

logger.init();

Expand Down
45 changes: 29 additions & 16 deletions src/controllers/mapLocationsController.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
const UserProfile = require('../models/userProfile');
const cache = require('../utilities/nodeCache')();
const cacheClosure = require('../utilities/nodeCache');

const mapLocationsController = function (MapLocation) {
const cache = cacheClosure();
const getAllLocations = async function (req, res) {
try {
const users = [];
const results = await UserProfile.find(
{},
'_id firstName lastName isActive location jobTitle totalTangibleHrs hoursByCategory homeCountry',
);
{},
'_id firstName lastName isActive location jobTitle totalTangibleHrs hoursByCategory',
);

results.forEach((item) => {
if (
(item.location?.coords.lat && item.location?.coords.lng && item.totalTangibleHrs >= 10)
|| (item.location?.coords.lat && item.location?.coords.lng && calculateTotalHours(item.hoursByCategory) >= 10)
(item.location?.coords.lat && item.location?.coords.lng && item.totalTangibleHrs >= 10) ||
(item.location?.coords.lat &&
item.location?.coords.lng &&
// eslint-disable-next-line no-use-before-define
calculateTotalHours(item.hoursByCategory) >= 10)
) {
users.push(item);
}
});
const modifiedUsers = users.map(item => ({
location: item.homeCountry || item.location,
const modifiedUsers = users.map((item) => ({
location: item.location,
isActive: item.isActive,
jobTitle: item.jobTitle[0],
_id: item._id,
Expand All @@ -34,21 +38,22 @@ const mapLocationsController = function (MapLocation) {
}
};
const deleteLocation = async function (req, res) {
if (!req.body.requestor.role === 'Administrator' || !req.body.requestor.role === 'Owner') {
if (req.body.requestor.role !== 'Administrator' && req.body.requestor.role !== 'Owner') {
res.status(403).send('You are not authorized to make changes in the teams.');
return;
}
const { locationId } = req.params;

MapLocation.findOneAndDelete({ _id: locationId })
.then(() => res.status(200).send({ message: 'The location was successfully removed!' }))
.catch(error => res.status(500).send({ message: error || "Couldn't remove the location" }));
.catch((error) => res.status(500).send({ message: error || "Couldn't remove the location" }));
};
const putUserLocation = async function (req, res) {
if (!req.body.requestor.role === 'Owner') {
if (req.body.requestor.role !== 'Owner') {
res.status(403).send('You are not authorized to make changes in the teams.');
return;
}

const locationData = {
firstName: req.body.firstName,
lastName: req.body.lastName,
Expand All @@ -65,11 +70,11 @@ const mapLocationsController = function (MapLocation) {
res.status(200).send(response);
} catch (err) {
console.log(err.message);
res.status(500).json({ message: err.message || 'Something went wrong...' });
res.status(500).send({ message: err.message || 'Something went wrong...' });
}
};
const updateUserLocation = async function (req, res) {
if (!req.body.requestor.role === 'Owner') {
if (req.body.requestor.role !== 'Owner') {
res.status(403).send('You are not authorized to make changes in the teams.');
return;
}
Expand All @@ -89,13 +94,21 @@ const mapLocationsController = function (MapLocation) {
try {
let response;
if (userType === 'user') {
response = await UserProfile.findOneAndUpdate({ _id: userId }, { $set: { ...updateData, jobTitle: [updateData.jobTitle] } }, { new: true });
response = await UserProfile.findOneAndUpdate(
{ _id: userId },
{ $set: { ...updateData, jobTitle: [updateData.jobTitle] } },
{ new: true },
);
cache.removeCache('allusers');
cache.removeCache(`user-${userId}`);

cache.setCache(`user-${userId}`, JSON.stringify(response));
} else {
response = await MapLocation.findOneAndUpdate({ _id: userId }, { $set: updateData }, { new: true });
response = await MapLocation.findOneAndUpdate(
{ _id: userId },
{ $set: updateData },
{ new: true },
);
}

if (!response) {
Expand All @@ -113,7 +126,7 @@ const mapLocationsController = function (MapLocation) {
res.status(200).send(newData);
} catch (err) {
console.log(err.message);
res.status(500).json({ message: err.message || 'Something went wrong...' });
res.status(500).send({ message: err.message || 'Something went wrong...' });
}
};

Expand Down
Loading
Loading