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

Leon and Oleksandr add interactive map #599

Merged
merged 39 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
97b7420
add new fields to send in UserProfileController
leonzh2k Jul 6, 2023
95015c4
add new fields to send in UserProfileController
leonzh2k Jul 6, 2023
9600e9d
feat: adding location object to user profile schema
xaanders Sep 14, 2023
805cb6e
Merge branch 'leon_add_interactive_map' of https://github.com/OneComm…
xaanders Sep 14, 2023
2d1e82c
fix: editing email message after new user creating
xaanders Sep 16, 2023
9e74615
feat: adding a initial functionality for adding
xaanders Sep 22, 2023
656509d
fix: jobTitle model key, feat: adding isActive status, getting all lo…
xaanders Oct 2, 2023
82d29ef
feat: userprofiles sending totalTangibleHrs, update user controller, …
xaanders Oct 15, 2023
cf9d773
fix: moving part of logic from the frontend, bug fixes feat: updating…
xaanders Nov 1, 2023
37942f9
add new fields to send in UserProfileController
leonzh2k Jul 6, 2023
f63e1e8
feat: adding location object to user profile schema
xaanders Sep 14, 2023
61a4357
fix: editing email message after new user creating
xaanders Sep 16, 2023
2e4a2af
feat: adding a initial functionality for adding
xaanders Sep 22, 2023
fb1841d
fix: jobTitle model key, feat: adding isActive status, getting all lo…
xaanders Oct 2, 2023
f2a6e41
feat: userprofiles sending totalTangibleHrs, update user controller, …
xaanders Oct 15, 2023
f556211
fix: moving part of logic from the frontend, bug fixes feat: updating…
xaanders Nov 1, 2023
883a00b
fix: modifying users before sent
xaanders Nov 2, 2023
e5380d2
add new fields to send in UserProfileController
leonzh2k Jul 6, 2023
632ef20
feat: userprofiles sending totalTangibleHrs, update user controller, …
xaanders Oct 15, 2023
f7b7d12
fix: moving part of logic from the frontend, bug fixes feat: updating…
xaanders Nov 1, 2023
d92d431
Merge branch 'leon_add_interactive_map' of https://github.com/OneComm…
xaanders Nov 2, 2023
e7f2340
rebasing and resolving conflicts
xaanders Nov 2, 2023
b9e2859
add new fields to send in UserProfileController
leonzh2k Jul 6, 2023
c2d332f
feat: adding location object to user profile schema
xaanders Sep 14, 2023
3d5e454
fix: editing email message after new user creating
xaanders Sep 16, 2023
35662d5
feat: adding a initial functionality for adding
xaanders Sep 22, 2023
3e76b43
fix: jobTitle model key, feat: adding isActive status, getting all lo…
xaanders Oct 2, 2023
f3832a3
feat: userprofiles sending totalTangibleHrs, update user controller, …
xaanders Oct 15, 2023
ac823a5
fix: moving part of logic from the frontend, bug fixes feat: updating…
xaanders Nov 1, 2023
ac4aa7b
fix: modifying users before sent
xaanders Nov 2, 2023
23bb45d
add new fields to send in UserProfileController
leonzh2k Jul 6, 2023
082fd3f
feat: userprofiles sending totalTangibleHrs, update user controller, …
xaanders Oct 15, 2023
d6122b3
fix: moving part of logic from the frontend, bug fixes feat: updating…
xaanders Nov 1, 2023
1effe93
add new fields to send in UserProfileController
leonzh2k Jul 6, 2023
b2b2cb0
rebasing and resolving conflicts
xaanders Nov 2, 2023
423e48e
Merge branch 'leon_add_interactive_map' of https://github.com/OneComm…
xaanders Nov 4, 2023
7733cf3
Merge branch 'development' into leon_add_interactive_map
xaanders Nov 7, 2023
54aeac5
merging development
xaanders Nov 13, 2023
07f505d
Merge branch 'development' into leon_add_interactive_map
xaanders Nov 13, 2023
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
140 changes: 140 additions & 0 deletions src/controllers/mapLocationsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
const UserProfile = require('../models/userProfile');
const cache = require('../utilities/nodeCache')();


const mapLocationsController = function (MapLocation) {
const getAllLocations = async function (req, res) {

try {
const users = [];
const results = await UserProfile.find({},
'_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)
) {
users.push(item);
}
});
const modifiedUsers = users.map(item => ({
location: item.location,
isActive: item.isActive,
jobTitle: item.jobTitle[0],
_id: item._id,
firstName: item.firstName,
lastName: item.lastName
}));

const mUsers = await MapLocation.find({});
res.status(200).send({ users: modifiedUsers, mUsers });
} catch (err) {
res.status(404).send(err);
}

};
const deleteLocation = async function (req, res) {

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.locationId

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" }));
};
const putUserLocation = async function (req, res) {

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,
jobTitle: req.body.jobTitle,
location: req.body.location,
}
const location = new MapLocation(locationData);

try {
const response = await location.save()
if (!response) {
throw new Error('Something went wrong during saving the location...')
}
res.status(200).send(response);
} catch (err) {
console.log(err.message)
res.status(500).json({ message: err.message || 'Something went wrong...' });
}
};
const updateUserLocation = async function (req, res) {
if (!req.body.requestor.role === 'Owner') {
res.status(403).send('You are not authorized to make changes in the teams.');
return;
}
const userType = req.body.type;
const userId = req.body._id;
const updateData = {
firstName: req.body.firstName,
lastName: req.body.lastName,
jobTitle: req.body.jobTitle,
location: req.body.location,
}

if (req.body.timeZone) {
updateData.timeZone = req.body.timeZone
}

try {
let response;
if (userType === 'user') {
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 })
}

if (!response) {
throw new Error('Something went wrong during saving the location...')
}
const newData = {
firstName: response.firstName,
lastName: response.lastName,
jobTitle: response.jobTitle,
location: response.location,
_id: response._id,
type: userType
}

res.status(200).send(newData);
} catch (err) {
console.log(err.message)
res.status(500).json({ message: err.message || 'Something went wrong...' });
}
};

function calculateTotalHours(hoursByCategory) {
let hours = 0;
Object.keys(hoursByCategory).forEach((x) => {
hours += hoursByCategory[x];
});
return hours;
}

return {
getAllLocations,
deleteLocation,
putUserLocation,
updateUserLocation
};
};

module.exports = mapLocationsController;
2 changes: 1 addition & 1 deletion src/controllers/profileInitialSetupController.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function informManagerMessage(user) {
</tr>
<tr>
<td><strong>Location:</strong></td>
<td>${user.location}</td>
<td>${user.location.userProvided}, ${user.location.country}</td>
</tr>
</table>
<br>
Expand Down
43 changes: 43 additions & 0 deletions src/models/mapLocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const mongoose = require('mongoose');

const { Schema } = mongoose;

const mapLocation = new Schema({
title: {
type: String,
default: 'Prior to HGN Data Collection'
},
firstName: String,
lastName: String,
jobTitle: String,
isActive: {
type: Boolean,
default: false,
},
location: {
userProvided: {
type: String,
required: true,
},
coords: {
lat: {
type: String,
required: true,
},
lng: {
type: String,
required: true,
}
},
country: {
type: String,
required: true,
},
city: {
type: String,
default: '',
},
},
});

module.exports = mongoose.model('MapLocation', mapLocation, 'maplocations');
15 changes: 12 additions & 3 deletions src/models/userProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,16 @@ const userProfileSchema = new Schema({
infringements: [
{ date: { type: String, required: true }, description: { type: String, required: true } },
],
location: { type: String, default: '' },
location: {
userProvided: { type: String, default: '' },
coords: {
lat: { type: Number, default: '' },
lng: { type: Number, default: '' },
},
country: { type: String, default: '' },
city: { type: String, default: '' }

},
oldInfringements: [
{ date: { type: String, required: true }, description: { type: String, required: true } },
],
Expand Down Expand Up @@ -168,9 +177,9 @@ const userProfileSchema = new Schema({
},
infoCollections: [
{
areaName: { type: String },
areaName: { type: String },
areaContent: { type: String },
}],
}],
});

userProfileSchema.pre('save', function (next) {
Expand Down
19 changes: 19 additions & 0 deletions src/routes/mapLocationsRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const express = require('express');

const router = function (mapLocations) {
const controller = require('../controllers/mapLocationsController')(mapLocations);

const mapRouter = express.Router();

mapRouter.route('/mapLocations')
.get(controller.getAllLocations)
.put(controller.putUserLocation)
.patch(controller.updateUserLocation);

mapRouter.route('/mapLocations/:locationId')
.delete(controller.deleteLocation)

return mapRouter;
};

module.exports = router;
4 changes: 4 additions & 0 deletions src/startup/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const profileInitialSetuptoken = require('../models/profileInitialSetupToken');
const reason = require('../models/reason');
const mouseoverText = require('../models/mouseoverText');
const inventoryItemMaterial = require('../models/inventoryItemMaterial');
const mapLocations = require('../models/mapLocation');
const buildingProject = require('../models/bmdashboard/buildingProject');


Expand Down Expand Up @@ -58,6 +59,8 @@ const ownerStandardMessageRouter = require('../routes/ownerStandardMessageRouter
const reasonRouter = require('../routes/reasonRouter')(reason, userProfile);
const mouseoverTextRouter = require('../routes/mouseoverTextRouter')(mouseoverText);

const mapLocationRouter = require('../routes/mapLocationsRouter')(mapLocations);

// bm dashboard
const bmLoginRouter = require('../routes/bmdashboard/bmLoginRouter')();
const bmMaterialsRouter = require('../routes/bmdashboard/bmMaterialsRouter')(inventoryItemMaterial);
Expand Down Expand Up @@ -93,6 +96,7 @@ module.exports = function (app) {
app.use('/api', informationRouter);
app.use('/api', mouseoverTextRouter);
app.use('/api', isEmailExistsRouter);
app.use('/api', mapLocationRouter);
// bm dashboard
app.use('/api/bm', bmLoginRouter);
app.use('/api/bm', bmMaterialsRouter);
Expand Down
Loading