Skip to content

Commit

Permalink
Merge pull request #775 from OneCommunityGlobal/Sucheta-create-summar…
Browse files Browse the repository at this point in the history
…y-report-recipient-button

Sucheta - Create Summary Report Recipient Button
  • Loading branch information
one-community authored Mar 8, 2024
2 parents 313bd24 + c9dd61d commit 3f7b27f
Show file tree
Hide file tree
Showing 12 changed files with 730 additions and 565 deletions.
116 changes: 58 additions & 58 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/controllers/bmdashboard/bmIssueController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const bmIssueController = function (BuildingIssue) {
BuildingIssue
.find()
.populate()
.then(result => res.status(200).send(result))
.catch(error => res.status(500).send(error));
.then((result) => res.status(200).send(result))
.catch((error) => res.status(500).send(error));
} catch (err) {
res.json(err);
}
Expand All @@ -16,8 +16,8 @@ const bmIssueController = function (BuildingIssue) {
const bmPostIssue = async (req, res) => {
try {
const newIssue = BuildingIssue.create(req.body)
.then(result => res.status(201).send(result))
.catch(error => res.status(500).send(error));
.then((result) => res.status(201).send(result))
.catch((error) => res.status(500).send(error));
} catch (err) {
res.json(err);
}
Expand Down
105 changes: 104 additions & 1 deletion src/controllers/reportsController.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const mongoose = require('mongoose');
const reporthelper = require('../helpers/reporthelper')();
const { hasPermission } = require('../utilities/permissions');
const UserProfile = require('../models/userProfile');
const userhelper = require('../helpers/userHelper')();

const reportsController = function () {
const getWeeklySummaries = async function (req, res) {
if (!await hasPermission(req.body.requestor, 'getWeeklySummaries')) {
if (!(await hasPermission(req.body.requestor, 'getWeeklySummaries'))) {
res.status(403).send('You are not authorized to view all users');
return;
}
Expand All @@ -17,8 +20,108 @@ const reportsController = function () {
.catch((error) => res.status(404).send(error));
};

/**
* Gets the Recipients added by the owner to receive the Weekly Summary Reports
* @param {*} req
* @param {*} res
*/

const getReportRecipients = function (req, res) {
try {
UserProfile.find(
{ getWeeklyReport: true },
{
email: 1,
firstName: 1,
lastName: 1,
createdDate: 1,
getWeeklyReport: 1,
},
)
.then((results) => {
res.status(200).send(results);
})
.catch((error) => {
console.log('error:', error); // need to delete later *
res.status(404).send({ error });
});
} catch (err) {
console.log('error:', err); // need to delete later *
res.status(404).send(err);
}
};

/**
* Function deletes slected Recipients from the list
* @param {*} req
* @param {*} res
* @returns
*/
const deleteReportsRecepients = (req, res) => {
const { userid } = req.params;
const id = userid;
try {
if (!mongoose.Types.ObjectId.isValid(id)) {
return res.status(404).json({
msg: `No task with id :${id}`,
});
}

UserProfile.updateOne({ _id: id }, { $set: { getWeeklyReport: false } })
.then((record) => {
if (!record) {
console.log("'No valid records found'");
res.status(404).send('No valid records found');
return;
}
res.status(200).send({
message: 'updated user record with getWeeklyReport false',
});
})
.catch((err) => {
console.log('error in catch block last:', err);
res.status(404).send(err);
});
} catch (error) {
res.status(404).send(error);
}
};

const saveReportsRecepients = (req, res) => {
const { userid } = req.params;
const id = userid;
try {
if (!mongoose.Types.ObjectId.isValid(id)) {
return res.status(404).json({
msg: `No task with id :${id}`,
});
}

UserProfile.updateOne({ _id: id }, { $set: { getWeeklyReport: true } })
.then((record) => {
if (!record) {
console.log("'No valid records found'");
res.status(404).send('No valid records found');
return;
}
res
.status(200)
.send({ message: 'updated user record with getWeeklyReport true' });
})
.catch((err) => {
console.log('error in catch block last:', err);
res.status(404).send(err);
});
} catch (error) {
res.status(404).send(error);
}
};

return {
getWeeklySummaries,
getReportRecipients,
deleteReportsRecepients,
saveReportsRecepients,
};
};

Expand Down
43 changes: 38 additions & 5 deletions src/controllers/userProfileController.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,9 +668,9 @@ const userProfileController = function (UserProfile) {
cache.removeCache(`user-${userId}`);
if (!key || value === undefined) {
return res.status(400).send({ error: 'Missing property or value' });
}
}

return UserProfile.findById(userId)
return UserProfile.findById(userId)
.then((user) => {
user.set({
[key]: value,
Expand Down Expand Up @@ -954,7 +954,7 @@ const userProfileController = function (UserProfile) {

// Search for user by first name
const getUserBySingleName = (req, res) => {
const pattern = new RegExp(`^${ req.params.singleName}`, 'i');
const pattern = new RegExp(`^${req.params.singleName}`, 'i');

// Searches for first or last name
UserProfile.find({
Expand Down Expand Up @@ -984,8 +984,8 @@ const userProfileController = function (UserProfile) {
.split(' ')
.filter((name) => name !== '');
// Creates a partial match regex for both first and last name
const firstNameRegex = new RegExp(`^${ escapeRegExp(fullName[0])}`, 'i');
const lastNameRegex = new RegExp(`^${ escapeRegExp(fullName[1])}`, 'i');
const firstNameRegex = new RegExp(`^${escapeRegExp(fullName[0])}`, 'i');
const lastNameRegex = new RegExp(`^${escapeRegExp(fullName[1])}`, 'i');

// Verfies both the first and last name are present
if (fullName.length < 2) {
Expand All @@ -1010,6 +1010,38 @@ const userProfileController = function (UserProfile) {
.catch((error) => res.status(500).send(error));
};

/**
* Authorizes user to be able to add Weekly Report Recipients
*/
const authorizeUser = async (req, res) => {
try {
await UserProfile.findOne({
email: {
$regex: escapeRegex('[email protected]'), // PLEASE CHANGE THIS EMAIL TO MATCH THE USER PROFILE WHILE TESTING THE PR
$options: 'i',
},
}).then(async (user) => {
await bcrypt
.compare(req.body.currentPassword, user.password)
.then((passwordMatch) => {
if (!passwordMatch) {
return res.status(400).send({
error: 'Incorrect current password',
});
}
return res
.status(200)
.send({ message: 'Correct Password, Password matches!' });
})
.catch((error) => {
res.status(500).send(error);
});
});
} catch (err) {
res.status(500).send(err);
}
};

return {
postUserProfile,
getUserProfiles,
Expand All @@ -1029,6 +1061,7 @@ const userProfileController = function (UserProfile) {
refreshToken,
getUserBySingleName,
getUserByFullName,
authorizeUser,
};
};

Expand Down
1 change: 1 addition & 0 deletions src/cronjobs/userProfileJobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const userProfileJobs = () => {
const allUserProfileJobs = new CronJob(
// '* * * * *', // Comment out for testing. Run Every minute.
'1 0 * * 0', // Every Sunday, 1 minute past midnight.

async () => {
const SUNDAY = 0;
if (moment().tz('America/Los_Angeles').day() === SUNDAY) {
Expand Down
Loading

0 comments on commit 3f7b27f

Please sign in to comment.