From 4f41d5e6e2ddf92744308d2a44304d9ca5a7d1c5 Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Mon, 10 Jun 2024 21:06:01 -0400 Subject: [PATCH 01/23] test: completed backend for add/edit/remove warnigns --- src/controllers/currentWarningsController.js | 140 +++++++++++++++++++ src/models/currentWarnings.js | 10 ++ src/routes/curentWarningsRouter.js | 23 +++ src/startup/routes.js | 6 +- 4 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 src/controllers/currentWarningsController.js create mode 100644 src/models/currentWarnings.js create mode 100644 src/routes/curentWarningsRouter.js diff --git a/src/controllers/currentWarningsController.js b/src/controllers/currentWarningsController.js new file mode 100644 index 000000000..1c56ceaca --- /dev/null +++ b/src/controllers/currentWarningsController.js @@ -0,0 +1,140 @@ +/* eslint-disable */ +const mongoose = require('mongoose'); +const userProfile = require('../models/userProfile'); + +const currentWarningsController = function (currentWarnings) { + const checkIfSpecialCharacter = (warning) => { + return !/^\b[a-zA-Z]+\b.*$/.test(warning); + }; + const getCurrentWarnings = async (req, res) => { + try { + const response = await currentWarnings.find({}); + + if (response.length === 0) { + return res.status(400).send({ message: 'no valid records' }); + } + return res.status(201).send({ currentWarningDescriptions: response }); + } catch (error) { + res.status(401).send({ message: error.message || error }); + } + }; + + const postNewWarningDescription = async (req, res) => { + try { + const { newWarning, activeWarning } = req.body; + + const newWarningLowerCase = newWarning + .split(' ') + .map((warning) => { + if (!/^\b[a-zA-Z]+\b.*$/.test(warning)) { + throw new Error('warning cannot have special characters as the first letter'); + } + return warning.toLowerCase(); + }) + .join(' '); + + const warnings = await currentWarnings.find({}); + + if (warnings.length === 0) { + return res.status(400).send({ message: 'no valid records' }); + } + + //check to see if it is deactivated or not + //deactaivted warnings should count and duplicates cannot be created + const duplicateFound = warnings.some( + (warning) => warning.warningTitle.toLowerCase() === newWarningLowerCase, + ); + if (duplicateFound) { + return res.status(422).send({ error: 'warning already exists' }); + } + const newWarningDescription = new currentWarnings(); + newWarningDescription.warningTitle = newWarning; + newWarningDescription.activeWarning = activeWarning; + + warnings.push(newWarningDescription); + await newWarningDescription.save(); + + return res.status(201).send({ newWarnings: warnings }); + } catch (error) { + return res.status(401).send({ message: error.message }); + } + }; + + const editWarningDescription = async (req, res) => { + try { + const { editedWarning } = req.body; + + const id = editedWarning._id; + + if (checkIfSpecialCharacter(editedWarning.warningTitle)) { + return res.status(422).send({ + error: 'warning cannot have special characters as the first letter', + }); + } + + await currentWarnings.findOneAndUpdate( + { _id: id }, + [{ $set: { warningTitle: editedWarning.warningTitle.trim() } }], + { new: true }, + ); + + res.status(201).send({ message: 'warning description was updated' }); + } catch (error) { + res.status(401).send({ message: error.message || error }); + } + }; + const updateWarningDescription = async (req, res) => { + try { + const { warningDescriptionId } = req.params; + + await currentWarnings.findOneAndUpdate( + { _id: warningDescriptionId }, + [{ $set: { activeWarning: { $not: '$activeWarning' } } }], + { new: true }, + ); + + res.status(201).send({ message: 'warning description was updated' }); + } catch (error) { + res.status(401).send({ message: error.message || error }); + } + }; + + const deleteWarningDescription = async (req, res) => { + try { + const { warningDescriptionId } = req.params; + const documentToDelete = await currentWarnings.findById(warningDescriptionId); + + await currentWarnings.deleteOne({ + _id: mongoose.Types.ObjectId(warningDescriptionId), + }); + + const deletedDescription = documentToDelete.warningTitle; + + await userProfile.updateMany( + { + 'warnings.description': deletedDescription, + }, + { + $pull: { + warnings: { description: deletedDescription }, + }, + }, + ); + + res.status(200).send({ + message: 'warning description was successfully deleted and user profiles updated', + }); + } catch (error) { + res.status(401).send({ message: error.message || error }); + } + }; + + return { + getCurrentWarnings, + postNewWarningDescription, + updateWarningDescription, + deleteWarningDescription, + editWarningDescription, + }; +}; +module.exports = currentWarningsController; diff --git a/src/models/currentWarnings.js b/src/models/currentWarnings.js new file mode 100644 index 000000000..427baf769 --- /dev/null +++ b/src/models/currentWarnings.js @@ -0,0 +1,10 @@ +const mongoose = require('mongoose'); + +const { Schema } = mongoose; + +const currentWarnings = new Schema({ + warningTitle: { type: String, required: true }, + activeWarning: { type: Boolean, required: true }, +}); + +module.exports = mongoose.model('currentWarning', currentWarnings, 'currentWarnings'); diff --git a/src/routes/curentWarningsRouter.js b/src/routes/curentWarningsRouter.js new file mode 100644 index 000000000..f1a004493 --- /dev/null +++ b/src/routes/curentWarningsRouter.js @@ -0,0 +1,23 @@ +const express = require('express'); + +const route = function (currentWarnings) { + const controller = require('../controllers/currentWarningsController')(currentWarnings); + + const currentWarningsRouter = express.Router(); + + currentWarningsRouter + .route('/currentWarnings') + .get(controller.getCurrentWarnings) + .post(controller.postNewWarningDescription); + + currentWarningsRouter.route('/currentWarnings/edit').put(controller.editWarningDescription); + + currentWarningsRouter + .route('/currentWarnings/:warningDescriptionId') + .delete(controller.deleteWarningDescription) + .put(controller.updateWarningDescription); + + return currentWarningsRouter; +}; + +module.exports = route; diff --git a/src/startup/routes.js b/src/startup/routes.js index 51273acbf..8687065d9 100644 --- a/src/startup/routes.js +++ b/src/startup/routes.js @@ -4,6 +4,7 @@ const project = require('../models/project'); const information = require('../models/information'); const team = require('../models/team'); // const actionItem = require('../models/actionItem'); +// eslint-disable-next-line const notification = require('../models/notification'); const wbs = require('../models/wbs'); const task = require('../models/task'); @@ -16,6 +17,8 @@ const inventoryItemType = require('../models/inventoryItemType'); const role = require('../models/role'); const rolePreset = require('../models/rolePreset'); const ownerMessage = require('../models/ownerMessage'); +const currentWarnings = require('../models/currentWarnings'); + // Title const title = require('../models/title'); @@ -48,6 +51,7 @@ const followUp = require('../models/followUp'); const userProfileRouter = require('../routes/userProfileRouter')(userProfile); const warningRouter = require('../routes/warningRouter')(userProfile); +const currentWarningsRouter = require('../routes/curentWarningsRouter')(currentWarnings); const badgeRouter = require('../routes/badgeRouter')(badge); const dashboardRouter = require('../routes/dashboardRouter')(weeklySummaryAIPrompt); const timeEntryRouter = require('../routes/timeentryRouter')(timeEntry); @@ -119,7 +123,6 @@ const bmToolRouter = require('../routes/bmdashboard/bmToolRouter')(buildingTool, const bmEquipmentRouter = require('../routes/bmdashboard/bmEquipmentRouter')(buildingEquipment); const bmIssueRouter = require('../routes/bmdashboard/bmIssueRouter')(buildingIssue); - module.exports = function (app) { app.use('/api', forgotPwdRouter); app.use('/api', loginRouter); @@ -153,6 +156,7 @@ module.exports = function (app) { app.use('/api', isEmailExistsRouter); app.use('/api', mapLocationRouter); app.use('/api', warningRouter); + app.use('/api', currentWarningsRouter); app.use('/api', titleRouter); app.use('/api', timeOffRequestRouter); app.use('/api', followUpRouter); From 74c7850bc2e8793f25875abc5e4c387e08de67b3 Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Thu, 13 Jun 2024 20:08:15 -0700 Subject: [PATCH 02/23] test: changed get method return code to 200 --- src/controllers/currentWarningsController.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/currentWarningsController.js b/src/controllers/currentWarningsController.js index 1c56ceaca..ab0969889 100644 --- a/src/controllers/currentWarningsController.js +++ b/src/controllers/currentWarningsController.js @@ -13,7 +13,7 @@ const currentWarningsController = function (currentWarnings) { if (response.length === 0) { return res.status(400).send({ message: 'no valid records' }); } - return res.status(201).send({ currentWarningDescriptions: response }); + return res.status(200).send({ currentWarningDescriptions: response }); } catch (error) { res.status(401).send({ message: error.message || error }); } From 7eb0a65e992b978d84f38268a243e1c2a2f79c99 Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Thu, 13 Jun 2024 21:26:46 -0700 Subject: [PATCH 03/23] test: added missing code for backend to update with new warnings --- src/controllers/warningsController.js | 60 ++++++++++++++------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/src/controllers/warningsController.js b/src/controllers/warningsController.js index c71e22f0d..a5461fba7 100644 --- a/src/controllers/warningsController.js +++ b/src/controllers/warningsController.js @@ -1,21 +1,34 @@ +/* eslint-disable */ const mongoose = require('mongoose'); const userProfile = require('../models/userProfile'); +const currentWarnings = require('../models/currentWarnings'); +let currentWarningDescriptions = null; + +async function getWarningDescriptions() { + currentWarningDescriptions = await currentWarnings.find({}, { warningTitle: 1, _id: 0 }); +} + +const convertObjectToArray = (obj) => { + const arr = []; + for (const key of obj) { + arr.push(key.warningTitle); + } + return arr; +}; -const descriptions = [ - 'Better Descriptions', - 'Log Time to Tasks', - 'Log Time as You Go', - 'Log Time to Action Items', - 'Intangible Time Log w/o Reason', -]; const warningsController = function (UserProfile) { const getWarningsByUserId = async function (req, res) { + currentWarningDescriptions = await currentWarnings.find({ + activeWarning: true, + }); + + currentWarningDescriptions = convertObjectToArray(currentWarningDescriptions); const { userId } = req.params; try { const { warnings } = await UserProfile.findById(userId); - const completedData = filterWarnings(warnings); + const completedData = filterWarnings(currentWarningDescriptions, warnings); if (!warnings) { return res.status(400).send({ message: 'no valiud records' }); @@ -30,9 +43,7 @@ const warningsController = function (UserProfile) { try { const { userId } = req.params; - const { - iconId, color, date, description, -} = req.body; + const { iconId, color, date, description } = req.body; const record = await UserProfile.findById(userId); if (!record) { @@ -46,9 +57,10 @@ const warningsController = function (UserProfile) { date, description, }); + await record.save(); - const completedData = filterWarnings(record.warnings); + const completedData = filterWarnings(currentWarningDescriptions, record.warnings); res.status(201).send({ message: 'success', warnings: completedData }); } catch (error) { @@ -71,10 +83,8 @@ const warningsController = function (UserProfile) { return res.status(400).send({ message: 'no valid records' }); } - const sortedWarnings = filterWarnings(warnings.warnings); - res - .status(201) - .send({ message: 'succesfully deleted', warnings: sortedWarnings }); + const sortedWarnings = filterWarnings(currentWarningDescriptions, warnings.warnings); + res.status(201).send({ message: 'succesfully deleted', warnings: sortedWarnings }); } catch (error) { res.status(401).send({ message: error.message || error }); } @@ -88,18 +98,10 @@ const warningsController = function (UserProfile) { }; // gests the dsecriptions key from the array -const getDescriptionKey = (val) => { - const descriptions = [ - 'Better Descriptions', - 'Log Time to Tasks', - 'Log Time as You Go', - 'Log Time to Action Items', - 'Intangible Time Log w/o Reason', - ]; - - return descriptions.indexOf(val); -}; +const getDescriptionKey = (val) => + // currentWarningDescriptions = convertObjectToArray(currentWarningDescriptions); + currentWarningDescriptions.indexOf(val); const sortKeysAlphabetically = (a, b) => getDescriptionKey(a) - getDescriptionKey(b); // method to see which color is first @@ -122,7 +124,7 @@ const sortByColorAndDate = (a, b) => { return colorComparison; }; -const filterWarnings = (warnings) => { +const filterWarnings = (currentWarningDescriptions, warnings) => { const warningsObject = {}; warnings.forEach((warning) => { @@ -145,7 +147,7 @@ const filterWarnings = (warnings) => { const completedData = []; - for (const descrip of descriptions) { + for (const descrip of currentWarningDescriptions) { completedData.push({ title: descrip, warnings: warns[descrip] ? warns[descrip] : [], From bd8c58dd159b75600444a4508cae414833e01961 Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Wed, 19 Jun 2024 20:28:34 -0700 Subject: [PATCH 04/23] test: completed editing and adding bugs --- src/controllers/currentWarningsController.js | 55 +++++++++++++------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/src/controllers/currentWarningsController.js b/src/controllers/currentWarningsController.js index ab0969889..bffb46ae9 100644 --- a/src/controllers/currentWarningsController.js +++ b/src/controllers/currentWarningsController.js @@ -3,8 +3,16 @@ const mongoose = require('mongoose'); const userProfile = require('../models/userProfile'); const currentWarningsController = function (currentWarnings) { + const checkForDuplicates = (currentWarning, warnings) => { + const duplicateFound = warnings.some( + (warning) => warning.warningTitle.toLowerCase() === currentWarning, + ); + + return duplicateFound; + }; + const checkIfSpecialCharacter = (warning) => { - return !/^\b[a-zA-Z]+\b.*$/.test(warning); + return !/^[a-zA-Z][a-zA-Z0-9]*(?: [a-zA-Z0-9]+)*$/.test(warning); }; const getCurrentWarnings = async (req, res) => { try { @@ -23,30 +31,24 @@ const currentWarningsController = function (currentWarnings) { try { const { newWarning, activeWarning } = req.body; - const newWarningLowerCase = newWarning - .split(' ') - .map((warning) => { - if (!/^\b[a-zA-Z]+\b.*$/.test(warning)) { - throw new Error('warning cannot have special characters as the first letter'); - } - return warning.toLowerCase(); - }) - .join(' '); - const warnings = await currentWarnings.find({}); if (warnings.length === 0) { return res.status(400).send({ message: 'no valid records' }); } - //check to see if it is deactivated or not - //deactaivted warnings should count and duplicates cannot be created - const duplicateFound = warnings.some( - (warning) => warning.warningTitle.toLowerCase() === newWarningLowerCase, - ); - if (duplicateFound) { + const lowerCaseWarning = newWarning.toLowerCase(); + const testWarning = !/^[a-zA-Z][a-zA-Z0-9]*(?: [a-zA-Z0-9]+)*$/.test(lowerCaseWarning); + if (testWarning) { + return res.status(422).send({ + error: 'Warning cannot have special characters as the first letter', + }); + } + + if (checkForDuplicates(lowerCaseWarning, warnings)) { return res.status(422).send({ error: 'warning already exists' }); } + const newWarningDescription = new currentWarnings(); newWarningDescription.warningTitle = newWarning; newWarningDescription.activeWarning = activeWarning; @@ -66,15 +68,28 @@ const currentWarningsController = function (currentWarnings) { const id = editedWarning._id; - if (checkIfSpecialCharacter(editedWarning.warningTitle)) { + const warnings = await currentWarnings.find({}); + + if (warnings.length === 0) { + return res.status(400).send({ message: 'no valid records' }); + } + + const lowerCaseWarning = editedWarning.warningTitle.toLowerCase(); + const testWarning = !/^[a-zA-Z][a-zA-Z0-9]*(?: [a-zA-Z0-9]+)*$/.test(lowerCaseWarning); + + if (testWarning) { return res.status(422).send({ - error: 'warning cannot have special characters as the first letter', + error: 'Warning cannot have special characters as the first letter', }); } + if (checkForDuplicates(lowerCaseWarning, warnings)) { + return res.status(422).send({ error: 'warning already exists try a different name' }); + } + await currentWarnings.findOneAndUpdate( { _id: id }, - [{ $set: { warningTitle: editedWarning.warningTitle.trim() } }], + [{ $set: { warningTitle: lowerCaseWarning.trim() } }], { new: true }, ); From 301fd28994838ed43993df41e41bf5b1c016d7a0 Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Wed, 19 Jun 2024 20:34:24 -0700 Subject: [PATCH 05/23] feat: used checkIfspecial function to check against regex --- src/controllers/currentWarningsController.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/controllers/currentWarningsController.js b/src/controllers/currentWarningsController.js index bffb46ae9..1dfd67fa0 100644 --- a/src/controllers/currentWarningsController.js +++ b/src/controllers/currentWarningsController.js @@ -38,7 +38,7 @@ const currentWarningsController = function (currentWarnings) { } const lowerCaseWarning = newWarning.toLowerCase(); - const testWarning = !/^[a-zA-Z][a-zA-Z0-9]*(?: [a-zA-Z0-9]+)*$/.test(lowerCaseWarning); + const testWarning = !checkIfSpecialCharacter(lowerCaseWarning); if (testWarning) { return res.status(422).send({ error: 'Warning cannot have special characters as the first letter', @@ -75,7 +75,7 @@ const currentWarningsController = function (currentWarnings) { } const lowerCaseWarning = editedWarning.warningTitle.toLowerCase(); - const testWarning = !/^[a-zA-Z][a-zA-Z0-9]*(?: [a-zA-Z0-9]+)*$/.test(lowerCaseWarning); + const testWarning = !checkIfSpecialCharacter(lowerCaseWarning); if (testWarning) { return res.status(422).send({ From c1d6c76c7c1be99b77315955af1bcb5999a2c6c3 Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Thu, 20 Jun 2024 20:48:45 -0700 Subject: [PATCH 06/23] fix: fixed bug that prevented numbers in new warning and edited warning --- src/controllers/currentWarningsController.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/controllers/currentWarningsController.js b/src/controllers/currentWarningsController.js index 1dfd67fa0..84c0e4ba6 100644 --- a/src/controllers/currentWarningsController.js +++ b/src/controllers/currentWarningsController.js @@ -38,7 +38,7 @@ const currentWarningsController = function (currentWarnings) { } const lowerCaseWarning = newWarning.toLowerCase(); - const testWarning = !checkIfSpecialCharacter(lowerCaseWarning); + const testWarning = checkIfSpecialCharacter(lowerCaseWarning); if (testWarning) { return res.status(422).send({ error: 'Warning cannot have special characters as the first letter', @@ -75,7 +75,7 @@ const currentWarningsController = function (currentWarnings) { } const lowerCaseWarning = editedWarning.warningTitle.toLowerCase(); - const testWarning = !checkIfSpecialCharacter(lowerCaseWarning); + const testWarning = checkIfSpecialCharacter(lowerCaseWarning); if (testWarning) { return res.status(422).send({ From 161b2c1ee1179ec79cd6a6912a46295544670908 Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Mon, 12 Aug 2024 22:32:31 -0700 Subject: [PATCH 07/23] fix: fixed the lowercase issue and the 422 error code, also, added code to prevent post method from crashing --- src/controllers/currentWarningsController.js | 9 ++++----- src/controllers/warningsController.js | 18 ++++++++---------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/controllers/currentWarningsController.js b/src/controllers/currentWarningsController.js index 84c0e4ba6..ecf35c3a2 100644 --- a/src/controllers/currentWarningsController.js +++ b/src/controllers/currentWarningsController.js @@ -37,16 +37,15 @@ const currentWarningsController = function (currentWarnings) { return res.status(400).send({ message: 'no valid records' }); } - const lowerCaseWarning = newWarning.toLowerCase(); - const testWarning = checkIfSpecialCharacter(lowerCaseWarning); + const testWarning = checkIfSpecialCharacter(newWarning); if (testWarning) { - return res.status(422).send({ + return res.status(200).send({ error: 'Warning cannot have special characters as the first letter', }); } - if (checkForDuplicates(lowerCaseWarning, warnings)) { - return res.status(422).send({ error: 'warning already exists' }); + if (checkForDuplicates(newWarning, warnings)) { + return res.status(200).send({ error: 'warning already exists' }); } const newWarningDescription = new currentWarnings(); diff --git a/src/controllers/warningsController.js b/src/controllers/warningsController.js index a5461fba7..0f80d2a8d 100644 --- a/src/controllers/warningsController.js +++ b/src/controllers/warningsController.js @@ -50,17 +50,15 @@ const warningsController = function (UserProfile) { return res.status(400).send({ message: 'No valid records found' }); } - record.warnings = record.warnings.concat({ - userId, - iconId, - color, - date, - description, - }); - - await record.save(); + const updatedWarnings = await userProfile.findByIdAndUpdate( + { + _id: userId, + }, + { $push: { warnings: { userId, iconId, color, date, description } } }, + { new: true, upsert: true }, + ); - const completedData = filterWarnings(currentWarningDescriptions, record.warnings); + const completedData = filterWarnings(currentWarningDescriptions, updatedWarnings.warnings); res.status(201).send({ message: 'success', warnings: completedData }); } catch (error) { From fa01786cacdaaa6351728d440c7a7852977fa17e Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Tue, 13 Aug 2024 20:33:33 -0700 Subject: [PATCH 08/23] fix: added 200 response code instead of 422 for posting, editing a warning --- src/controllers/currentWarningsController.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/controllers/currentWarningsController.js b/src/controllers/currentWarningsController.js index ecf35c3a2..a1f62cfc6 100644 --- a/src/controllers/currentWarningsController.js +++ b/src/controllers/currentWarningsController.js @@ -77,13 +77,13 @@ const currentWarningsController = function (currentWarnings) { const testWarning = checkIfSpecialCharacter(lowerCaseWarning); if (testWarning) { - return res.status(422).send({ + return res.status(200).send({ error: 'Warning cannot have special characters as the first letter', }); } if (checkForDuplicates(lowerCaseWarning, warnings)) { - return res.status(422).send({ error: 'warning already exists try a different name' }); + return res.status(200).send({ error: 'warning already exists try a different name' }); } await currentWarnings.findOneAndUpdate( From ab1e953d43890c0c4d5baca24dd3e13d44bfb02b Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Wed, 21 Aug 2024 20:04:59 -0700 Subject: [PATCH 09/23] fix: removed logs and added isPermanent Attribute to new warnings --- src/controllers/currentWarningsController.js | 3 ++- src/models/currentWarnings.js | 1 + src/models/userProfile.js | 12 +++--------- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/controllers/currentWarningsController.js b/src/controllers/currentWarningsController.js index a1f62cfc6..f064cb4f7 100644 --- a/src/controllers/currentWarningsController.js +++ b/src/controllers/currentWarningsController.js @@ -29,7 +29,7 @@ const currentWarningsController = function (currentWarnings) { const postNewWarningDescription = async (req, res) => { try { - const { newWarning, activeWarning } = req.body; + const { newWarning, activeWarning, isPermanent } = req.body; const warnings = await currentWarnings.find({}); @@ -51,6 +51,7 @@ const currentWarningsController = function (currentWarnings) { const newWarningDescription = new currentWarnings(); newWarningDescription.warningTitle = newWarning; newWarningDescription.activeWarning = activeWarning; + newWarningDescription.isPermanent = isPermanent; warnings.push(newWarningDescription); await newWarningDescription.save(); diff --git a/src/models/currentWarnings.js b/src/models/currentWarnings.js index 427baf769..18a446199 100644 --- a/src/models/currentWarnings.js +++ b/src/models/currentWarnings.js @@ -5,6 +5,7 @@ const { Schema } = mongoose; const currentWarnings = new Schema({ warningTitle: { type: String, required: true }, activeWarning: { type: Boolean, required: true }, + isPermanent: { type: Boolean, required: true }, }); module.exports = mongoose.model('currentWarning', currentWarnings, 'currentWarnings'); diff --git a/src/models/userProfile.js b/src/models/userProfile.js index 369708e4c..e6e9e51aa 100644 --- a/src/models/userProfile.js +++ b/src/models/userProfile.js @@ -55,9 +55,7 @@ const userProfileSchema = new Schema({ type: String, required: true, unique: true, - validate: [ - validate({ validator: 'isEmail', message: 'Email address is invalid' }), - ], + validate: [validate({ validator: 'isEmail', message: 'Email address is invalid' })], }, copiedAiPrompt: { type: Date, default: Date.now() }, emailSubscriptions: { @@ -77,7 +75,7 @@ const userProfileSchema = new Schema({ startDate: { type: Date, required: true, - default: function () { + default() { return this.createdDate; }, }, @@ -262,8 +260,4 @@ userProfileSchema.pre('save', function (next) { .catch((error) => next(error)); }); -module.exports = mongoose.model( - 'userProfile', - userProfileSchema, - 'userProfiles', -); +module.exports = mongoose.model('userProfile', userProfileSchema, 'userProfiles'); From da17641c20b8999777707315a23b4eac5619979d Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Wed, 21 Aug 2024 21:53:11 -0700 Subject: [PATCH 10/23] fix: attempted to send email to my email address to test --- src/cronjobs/userProfileJobs.js | 5 +++-- src/helpers/userHelper.js | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/cronjobs/userProfileJobs.js b/src/cronjobs/userProfileJobs.js index f0f69e146..8acd192f3 100644 --- a/src/cronjobs/userProfileJobs.js +++ b/src/cronjobs/userProfileJobs.js @@ -5,12 +5,13 @@ const userhelper = require('../helpers/userHelper')(); const userProfileJobs = () => { const allUserProfileJobs = new CronJob( - // '* * * * *', // Comment out for testing. Run Every minute. - '1 0 * * 0', // Every Sunday, 1 minute past midnight. + '* * * * *', // Comment out for testing. Run Every minute. + // '1 0 * * 0', // Every Sunday, 1 minute past midnight. async () => { const SUNDAY = 0; // will change back to 0 after fix if (moment().tz('America/Los_Angeles').day() === SUNDAY) { + console.log('Running Cron Jobs'); await userhelper.assignBlueSquareForTimeNotMet(); await userhelper.applyMissedHourForCoreTeam(); await userhelper.emailWeeklySummariesForAllUsers(); diff --git a/src/helpers/userHelper.js b/src/helpers/userHelper.js index a2bd94117..1f1ad9030 100644 --- a/src/helpers/userHelper.js +++ b/src/helpers/userHelper.js @@ -401,7 +401,7 @@ const userHelper = function () { const pdtEndOfLastWeek = moment().tz('America/Los_Angeles').endOf('week').subtract(1, 'week'); const users = await userProfile.find( - { isActive: true }, + { isActive: true, email: 'arevaloluis114@gmail.com' }, '_id weeklycommittedHours weeklySummaries missedHours', ); const usersRequiringBlueSqNotification = []; @@ -731,7 +731,7 @@ const userHelper = function () { 'New Infringement Assigned', emailBody, null, - 'onecommunityglobal@gmail.com', + 'arevaloluis114@gmail.com', status.email, null, ); From 78ee068e13a8d5586e1967df12806504ed846c8f Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Tue, 10 Sep 2024 20:55:17 -0700 Subject: [PATCH 11/23] feat: added send an email after 3 and 4 warnings function --- src/controllers/warningsController.js | 120 ++++++++++++++++++++++++-- 1 file changed, 113 insertions(+), 7 deletions(-) diff --git a/src/controllers/warningsController.js b/src/controllers/warningsController.js index 0f80d2a8d..9a2541791 100644 --- a/src/controllers/warningsController.js +++ b/src/controllers/warningsController.js @@ -2,8 +2,27 @@ const mongoose = require('mongoose'); const userProfile = require('../models/userProfile'); const currentWarnings = require('../models/currentWarnings'); -let currentWarningDescriptions = null; +const emailSender = require('../utilities/emailSender'); +let currentWarningDescriptions = null; +let currentUserName = null; +const emailTemplate = { + thirdWarning: { + subject: 'Third Warning', + body: `

This is the 3rd time the Admin team has requested the same thing from you. Specifically <“tracked area”>. Please carefully review the communications you’ve gotten about this so you understand what is being requested. Ask questions if anything isn’t clear, the Admin team is here to help.

+

Please also be sure to fix this from here on forward, asking for the same thing over and over requires administration that really shouldn’t be needed and will result in a blue square if it happens again.

+

With Gratitude,

+

One Community

`, + }, + fourthWarning: { + subject: 'Fourth Warning', + body: `

username !

+

This is the 3rd time the Admin team has requested the same thing from you. Specifically <“tracked area”>. Please carefully review the communications you’ve gotten about this so you understand what is being requested. Ask questions if anything isn’t clear, the Admin team is here to help.

+

Please also be sure to fix this from here on forward, asking for the same thing over and over requires administration that really shouldn’t be needed and will result in a blue square if it happens again.

+

With Gratitude,

+

One Community

`, + }, +}; async function getWarningDescriptions() { currentWarningDescriptions = await currentWarnings.find({}, { warningTitle: 1, _id: 0 }); } @@ -28,7 +47,7 @@ const warningsController = function (UserProfile) { try { const { warnings } = await UserProfile.findById(userId); - const completedData = filterWarnings(currentWarningDescriptions, warnings); + const { completedData } = filterWarnings(currentWarningDescriptions, warnings); if (!warnings) { return res.status(400).send({ message: 'no valiud records' }); @@ -44,12 +63,20 @@ const warningsController = function (UserProfile) { const { userId } = req.params; const { iconId, color, date, description } = req.body; + const { monitorData } = req.body; + const myData = { + firstName: 'tim', + lastName: 'smith', + email: 'tim@gmail.com', + }; const record = await UserProfile.findById(userId); if (!record) { return res.status(400).send({ message: 'No valid records found' }); } + currentUserName = `${record.firstName} ${record.lastName}`; + //check warning id const updatedWarnings = await userProfile.findByIdAndUpdate( { _id: userId, @@ -58,7 +85,15 @@ const warningsController = function (UserProfile) { { new: true, upsert: true }, ); - const completedData = filterWarnings(currentWarningDescriptions, updatedWarnings.warnings); + const { completedData, sendEmail } = filterWarnings( + currentWarningDescriptions, + updatedWarnings.warnings, + description, + iconId, + ); + if (sendEmail !== null) { + sendEmailToUser(sendEmail, description, currentUserName, monitorData); + } res.status(201).send({ message: 'success', warnings: completedData }); } catch (error) { @@ -81,8 +116,8 @@ const warningsController = function (UserProfile) { return res.status(400).send({ message: 'no valid records' }); } - const sortedWarnings = filterWarnings(currentWarningDescriptions, warnings.warnings); - res.status(201).send({ message: 'succesfully deleted', warnings: sortedWarnings }); + const { completedData } = filterWarnings(currentWarningDescriptions, warnings.warnings); + res.status(201).send({ message: 'succesfully deleted', warnings: completedData }); } catch (error) { res.status(401).send({ message: error.message || error }); } @@ -95,6 +130,41 @@ const warningsController = function (UserProfile) { }; }; +const sendEmailToUser = (sendEmail, warningDescription, currentUserName, monitorData) => { + let time = sendEmail === '3' ? '3rd' : '4th'; + console.log('monitorData', monitorData.firstName); + const emailTemplate = + time === '3rd' + ? ` +

Hello ${currentUserName},

+

This is the ${time} time the Admin team has requested the same thing from you. Specifically ${warningDescription}. Please carefully review the communications you’ve gotten about this so you understand what is being requested. Ask questions if anything isn’t clear, the Admin team is here to help.

+

Please also be sure to fix this from here on forward, asking for the same thing over and over requires administration that really shouldn’t be needed and will result in a blue square if it happens again.

+

The Admin memember who issued the warning is ${monitorData.firstName} ${monitorData.lastName} and their email is ${monitorData.email}

+

With Gratitude,

+

One Community

` + : `

Hello ${currentUserName},

+

This is the ${time} time the Admin team has requested the same thing from you. Specifically ${warningDescription}. Please carefully review the communications you’ve gotten about this so you understand what is being requested. Ask questions if anything isn’t clear, the Admin team is here to help.

+

Please also be sure to fix this from here on forward, asking for the same thing over and over requires administration that really shouldn’t be needed and will result in a blue square if it happens again.

+

The Admin memember who issued the warning is ${monitorData.firstName} ${monitorData.lastName} and their email is ${monitorData.email}

+

With Gratitude,

+

One Community

`; + + if (sendEmail === '3') { + emailSender('arevaloluis114@gmail.com', 'Third Warning', emailTemplate, null, null); + } else { + emailSender('arevaloluis114@gmail.com', 'Fourth Warning', emailTemplate, null, null); + } + + // const emailBody = `

Hello ${currentUserName},

\n ${emailTemplate.thirdWarning.body}`; + // emailSender( + // 'arevaloluis114@gmail.com', + // emailTemplate.thirdWarning.subject, + // emailBody, + // null, + // null, + // ); +}; + // gests the dsecriptions key from the array const getDescriptionKey = (val) => // currentWarningDescriptions = convertObjectToArray(currentWarningDescriptions); @@ -122,14 +192,50 @@ const sortByColorAndDate = (a, b) => { return colorComparison; }; -const filterWarnings = (currentWarningDescriptions, warnings) => { +const filterWarnings = ( + currentWarningDescriptions, + warnings, + description = null, + iconId = null, +) => { const warningsObject = {}; + let sendEmail = null; + warnings.forEach((warning) => { if (!warningsObject[warning.description]) { warningsObject[warning.description] = []; } warningsObject[warning.description].push(warning); + + if ( + warningsObject[warning.description].length === 3 && + description === warning.description && + sendEmail === null + ) { + sendEmail = '3'; + //send email + // const emailBody = `

Hello ${currentUserName},

\n ${emailTemplate.thirdWarning.body}`; + // emailSender( + // 'arevaloluis114@gmail.com', + // emailTemplate.thirdWarning.subject, + // emailBody, + // null, + // null, + // ); + } else if ( + warningsObject[warning.description].length === 4 && + description === warning.description && + sendEmail === null + ) { + sendEmail = '4'; + //send email + // const emailBody = `

Hello ${currentUserName},

\n ${emailTemplate.fourthWarning.body}`; + // emailSender( + // ' + } else { + sendEmail = null; + } }); const warns = Object.keys(warningsObject) @@ -151,7 +257,7 @@ const filterWarnings = (currentWarningDescriptions, warnings) => { warnings: warns[descrip] ? warns[descrip] : [], }); } - return completedData; + return { completedData, sendEmail }; }; module.exports = warningsController; From 91e839861ca13f08fe94c3d2dc599929c15bca10 Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Wed, 18 Sep 2024 15:14:17 -0400 Subject: [PATCH 12/23] feat: added sending email features and adjusted the user profile mode --- src/controllers/warningsController.js | 120 +++++++++++--------------- src/models/userProfile.js | 1 + 2 files changed, 49 insertions(+), 72 deletions(-) diff --git a/src/controllers/warningsController.js b/src/controllers/warningsController.js index 9a2541791..8ad00ab8f 100644 --- a/src/controllers/warningsController.js +++ b/src/controllers/warningsController.js @@ -65,11 +65,6 @@ const warningsController = function (UserProfile) { const { iconId, color, date, description } = req.body; const { monitorData } = req.body; - const myData = { - firstName: 'tim', - lastName: 'smith', - email: 'tim@gmail.com', - }; const record = await UserProfile.findById(userId); if (!record) { return res.status(400).send({ message: 'No valid records found' }); @@ -77,7 +72,7 @@ const warningsController = function (UserProfile) { currentUserName = `${record.firstName} ${record.lastName}`; //check warning id - const updatedWarnings = await userProfile.findByIdAndUpdate( + const updatedWarnings = await UserProfile.findByIdAndUpdate( { _id: userId, }, @@ -85,18 +80,20 @@ const warningsController = function (UserProfile) { { new: true, upsert: true }, ); - const { completedData, sendEmail } = filterWarnings( + const { completedData, sendEmail, size } = filterWarnings( currentWarningDescriptions, updatedWarnings.warnings, - description, iconId, + color, ); + if (sendEmail !== null) { - sendEmailToUser(sendEmail, description, currentUserName, monitorData); + sendEmailToUser(sendEmail, description, currentUserName, monitorData, size); } res.status(201).send({ message: 'success', warnings: completedData }); } catch (error) { + console.log('error', error); res.status(400).send({ message: error.message || error }); } }; @@ -130,42 +127,42 @@ const warningsController = function (UserProfile) { }; }; -const sendEmailToUser = (sendEmail, warningDescription, currentUserName, monitorData) => { - let time = sendEmail === '3' ? '3rd' : '4th'; - console.log('monitorData', monitorData.firstName); +//helper function to get the ordinal +function getOrdinal(n) { + const suffixes = ['th', 'st', 'nd', 'rd']; + const value = n % 100; + return n + (suffixes[(value - 20) % 10] || suffixes[value] || suffixes[0]); +} +const sendEmailToUser = (sendEmail, warningDescription, currentUserName, monitorData, size) => { + //issued blue square? if so, send second tempalte + // + const ordinal = getOrdinal(size); + const subjectTitle = ordinal + ' Warning'; + const emailTemplate = - time === '3rd' - ? ` -

Hello ${currentUserName},

-

This is the ${time} time the Admin team has requested the same thing from you. Specifically ${warningDescription}. Please carefully review the communications you’ve gotten about this so you understand what is being requested. Ask questions if anything isn’t clear, the Admin team is here to help.

-

Please also be sure to fix this from here on forward, asking for the same thing over and over requires administration that really shouldn’t be needed and will result in a blue square if it happens again.

-

The Admin memember who issued the warning is ${monitorData.firstName} ${monitorData.lastName} and their email is ${monitorData.email}

-

With Gratitude,

-

One Community

` + sendEmail === 'issue warning' + ? `

Hello ${currentUserName},

+

This is the ${ordinal} time the Admin team has requested the same thing from you. Specifically, ${warningDescription}. Please carefully review the previous communications you’ve received to fully understand what is being requested. If anything is unclear, don’t hesitate to ask questions—the Admin team is here to assist.

+

Please ensure this issue is resolved moving forward. Repeated requests for the same thing require unnecessary administrative attention and may result in a blue square if it happens again.

+

The Admin member who issued the warning is ${monitorData.firstName} ${monitorData.lastName} and their email is ${monitorData.email}. Please comment on your Google Doc and tag them via email if you have any questions.

+

With Gratitude,

+

One Community

` : `

Hello ${currentUserName},

-

This is the ${time} time the Admin team has requested the same thing from you. Specifically ${warningDescription}. Please carefully review the communications you’ve gotten about this so you understand what is being requested. Ask questions if anything isn’t clear, the Admin team is here to help.

-

Please also be sure to fix this from here on forward, asking for the same thing over and over requires administration that really shouldn’t be needed and will result in a blue square if it happens again.

-

The Admin memember who issued the warning is ${monitorData.firstName} ${monitorData.lastName} and their email is ${monitorData.email}

-

With Gratitude,

-

One Community

`; - - if (sendEmail === '3') { - emailSender('arevaloluis114@gmail.com', 'Third Warning', emailTemplate, null, null); - } else { - emailSender('arevaloluis114@gmail.com', 'Fourth Warning', emailTemplate, null, null); +

This is the ${ordinal} time the Admin team has requested the same thing from you. Specifically, ${warningDescription}.

+

Please ensure this issue is resolved moving forward. Repeated requests for the same thing require unnecessary administrative attention and have resulted in a blue square being issued.

+

Please carefully review the previous communications you’ve received to fully understand what is being requested. If anything is unclear, feel free to ask questions—the Admin team is here to help.

+

The Admin member who issued the warning is ${monitorData.firstName} ${monitorData.lastName} and their email is ${monitorData.email}. Please comment on your Google Doc and tag them via email if you have any questions.

+

With Gratitude,

+

One Community

`; + + if (sendEmail === 'issue warning') { + emailSender('arevaloluis114@gmail.com', subjectTitle, emailTemplate, null, null); + } else if (sendEmail === 'issue blue square') { + emailSender('arevaloluis114@gmail.com', subjectTitle, emailTemplate, null, null); } - - // const emailBody = `

Hello ${currentUserName},

\n ${emailTemplate.thirdWarning.body}`; - // emailSender( - // 'arevaloluis114@gmail.com', - // emailTemplate.thirdWarning.subject, - // emailBody, - // null, - // null, - // ); }; -// gests the dsecriptions key from the array +// gets the dsecriptions key from the array const getDescriptionKey = (val) => // currentWarningDescriptions = convertObjectToArray(currentWarningDescriptions); @@ -192,15 +189,11 @@ const sortByColorAndDate = (a, b) => { return colorComparison; }; -const filterWarnings = ( - currentWarningDescriptions, - warnings, - description = null, - iconId = null, -) => { +const filterWarnings = (currentWarningDescriptions, warnings, iconId = null, color = null) => { const warningsObject = {}; let sendEmail = null; + let size = null; warnings.forEach((warning) => { if (!warningsObject[warning.description]) { @@ -209,32 +202,15 @@ const filterWarnings = ( warningsObject[warning.description].push(warning); if ( - warningsObject[warning.description].length === 3 && - description === warning.description && - sendEmail === null - ) { - sendEmail = '3'; - //send email - // const emailBody = `

Hello ${currentUserName},

\n ${emailTemplate.thirdWarning.body}`; - // emailSender( - // 'arevaloluis114@gmail.com', - // emailTemplate.thirdWarning.subject, - // emailBody, - // null, - // null, - // ); - } else if ( - warningsObject[warning.description].length === 4 && - description === warning.description && - sendEmail === null + warningsObject[warning.description].length >= 3 && + warning.iconId === iconId && + color === 'yellow' ) { - sendEmail = '4'; - //send email - // const emailBody = `

Hello ${currentUserName},

\n ${emailTemplate.fourthWarning.body}`; - // emailSender( - // ' - } else { - sendEmail = null; + sendEmail = 'issue warning'; + size = warningsObject[warning.description].length; + } else if (warning.iconId === iconId && color === 'red') { + sendEmail = 'issue blue square'; + size = warningsObject[warning.description].length; } }); @@ -257,7 +233,7 @@ const filterWarnings = ( warnings: warns[descrip] ? warns[descrip] : [], }); } - return { completedData, sendEmail }; + return { completedData, sendEmail, size }; }; module.exports = warningsController; diff --git a/src/models/userProfile.js b/src/models/userProfile.js index e6e9e51aa..4f452cd52 100644 --- a/src/models/userProfile.js +++ b/src/models/userProfile.js @@ -127,6 +127,7 @@ const userProfileSchema = new Schema({ required: true, default: 'white', }, + iconId: { type: String, required: true }, }, ], location: { From 05a7524b442e6d477dcb52a3e4f1b94a46c4199c Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Thu, 19 Sep 2024 16:49:36 -0400 Subject: [PATCH 13/23] adjusted email text and body --- src/controllers/warningsController.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/controllers/warningsController.js b/src/controllers/warningsController.js index 8ad00ab8f..e45a25330 100644 --- a/src/controllers/warningsController.js +++ b/src/controllers/warningsController.js @@ -148,17 +148,23 @@ const sendEmailToUser = (sendEmail, warningDescription, currentUserName, monitor

With Gratitude,

One Community

` : `

Hello ${currentUserName},

-

This is the ${ordinal} time the Admin team has requested the same thing from you. Specifically, ${warningDescription}.

-

Please ensure this issue is resolved moving forward. Repeated requests for the same thing require unnecessary administrative attention and have resulted in a blue square being issued.

+

A blue square has been issued because this is the ${ordinal} time the Admin team has requested the same thing from you. Specifically, ${warningDescription}.

+

Please ensure this issue is resolved moving forward. Repeated requests for the same thing require unnecessary administrative attention, will result in additional blue square being issued, and could lead to termination.

Please carefully review the previous communications you’ve received to fully understand what is being requested. If anything is unclear, feel free to ask questions—the Admin team is here to help.

-

The Admin member who issued the warning is ${monitorData.firstName} ${monitorData.lastName} and their email is ${monitorData.email}. Please comment on your Google Doc and tag them via email if you have any questions.

+

The Admin member who issued this blue square is ${monitorData.firstName} ${monitorData.lastName} and can be reached at ${monitorData.email}. If you have any questions, please comment on your Google Doc and tag them via email.

With Gratitude,

One Community

`; if (sendEmail === 'issue warning') { emailSender('arevaloluis114@gmail.com', subjectTitle, emailTemplate, null, null); } else if (sendEmail === 'issue blue square') { - emailSender('arevaloluis114@gmail.com', subjectTitle, emailTemplate, null, null); + emailSender( + 'arevaloluis114@gmail.com', + `Blue Square issued for ${warningDescription}`, + emailTemplate, + null, + null, + ); } }; From d998541d5804ae173704f42b7230be86e06d7297 Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Fri, 27 Sep 2024 19:52:31 -0700 Subject: [PATCH 14/23] adjusted email text when issuing a warning and blue square --- src/controllers/warningsController.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/controllers/warningsController.js b/src/controllers/warningsController.js index e45a25330..ac64b0274 100644 --- a/src/controllers/warningsController.js +++ b/src/controllers/warningsController.js @@ -143,15 +143,15 @@ const sendEmailToUser = (sendEmail, warningDescription, currentUserName, monitor sendEmail === 'issue warning' ? `

Hello ${currentUserName},

This is the ${ordinal} time the Admin team has requested the same thing from you. Specifically, ${warningDescription}. Please carefully review the previous communications you’ve received to fully understand what is being requested. If anything is unclear, don’t hesitate to ask questions—the Admin team is here to assist.

-

Please ensure this issue is resolved moving forward. Repeated requests for the same thing require unnecessary administrative attention and may result in a blue square if it happens again.

-

The Admin member who issued the warning is ${monitorData.firstName} ${monitorData.lastName} and their email is ${monitorData.email}. Please comment on your Google Doc and tag them via email if you have any questions.

+

Moving forward, please ensure this issue is resolved. Repeated requests for the same thing require unnecessary administrative attention and may result in a blue square being issued if it happens again.

+

The Admin member who issued the warning is ${monitorData.firstName} ${monitorData.lastName} and their email is ${monitorData.email}. Please comment on your Google Doc and tag them using this email if you have any questions.

With Gratitude,

One Community

` : `

Hello ${currentUserName},

A blue square has been issued because this is the ${ordinal} time the Admin team has requested the same thing from you. Specifically, ${warningDescription}.

-

Please ensure this issue is resolved moving forward. Repeated requests for the same thing require unnecessary administrative attention, will result in additional blue square being issued, and could lead to termination.

+

Moving forward, please ensure this is resolved. Repeated requests for the same thing require unnecessary administrative attention, will result in an additional blue square being issued, and could lead to termination.

Please carefully review the previous communications you’ve received to fully understand what is being requested. If anything is unclear, feel free to ask questions—the Admin team is here to help.

-

The Admin member who issued this blue square is ${monitorData.firstName} ${monitorData.lastName} and can be reached at ${monitorData.email}. If you have any questions, please comment on your Google Doc and tag them via email.

+

The Admin member who issued this blue square is ${monitorData.firstName} ${monitorData.lastName} and can be reached at ${monitorData.email}. If you have any questions, please comment on your Google Doc and tag them using this email.

With Gratitude,

One Community

`; From 92b2ec13175b6302ed8786d77632c0f514ef159c Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Tue, 1 Oct 2024 20:49:20 -0700 Subject: [PATCH 15/23] fetch admin's of the team and send an email cc them when a warning and blue square is issued --- src/controllers/warningsController.js | 46 ++++++++++++++++++++------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/src/controllers/warningsController.js b/src/controllers/warningsController.js index ac64b0274..16130196a 100644 --- a/src/controllers/warningsController.js +++ b/src/controllers/warningsController.js @@ -3,7 +3,7 @@ const mongoose = require('mongoose'); const userProfile = require('../models/userProfile'); const currentWarnings = require('../models/currentWarnings'); const emailSender = require('../utilities/emailSender'); - +const userHelper = require('../helpers/userHelper')(); let currentWarningDescriptions = null; let currentUserName = null; const emailTemplate = { @@ -71,7 +71,7 @@ const warningsController = function (UserProfile) { } currentUserName = `${record.firstName} ${record.lastName}`; - //check warning id + const updatedWarnings = await UserProfile.findByIdAndUpdate( { _id: userId, @@ -87,8 +87,9 @@ const warningsController = function (UserProfile) { color, ); + const adminEmails = await getUserRoleByEmail(record); if (sendEmail !== null) { - sendEmailToUser(sendEmail, description, currentUserName, monitorData, size); + sendEmailToUser(sendEmail, description, currentUserName, monitorData, size, adminEmails); } res.status(201).send({ message: 'success', warnings: completedData }); @@ -127,15 +128,34 @@ const warningsController = function (UserProfile) { }; }; +//helper to get the team members admin emails +async function getUserRoleByEmail(user) { + const recipients = []; + for (const teamId of user.teams) { + const managementEmails = await userHelper.getTeamManagementEmail(teamId); + if (Array.isArray(managementEmails) && managementEmails.length > 0) { + managementEmails.forEach((management) => { + recipients.push(management.email); + }); + } + } + return recipients; +} + //helper function to get the ordinal function getOrdinal(n) { const suffixes = ['th', 'st', 'nd', 'rd']; const value = n % 100; return n + (suffixes[(value - 20) % 10] || suffixes[value] || suffixes[0]); } -const sendEmailToUser = (sendEmail, warningDescription, currentUserName, monitorData, size) => { - //issued blue square? if so, send second tempalte - // +const sendEmailToUser = ( + sendEmail, + warningDescription, + currentUserName, + monitorData, + size, + adminEmails, +) => { const ordinal = getOrdinal(size); const subjectTitle = ordinal + ' Warning'; @@ -156,23 +176,27 @@ const sendEmailToUser = (sendEmail, warningDescription, currentUserName, monitor

One Community

`; if (sendEmail === 'issue warning') { - emailSender('arevaloluis114@gmail.com', subjectTitle, emailTemplate, null, null); + emailSender( + 'arevaloluis114@gmail.com', + subjectTitle, + emailTemplate, + adminEmails.toString(), + null, + ); } else if (sendEmail === 'issue blue square') { emailSender( 'arevaloluis114@gmail.com', `Blue Square issued for ${warningDescription}`, emailTemplate, - null, + adminEmails.toString(), null, ); } }; // gets the dsecriptions key from the array -const getDescriptionKey = (val) => - // currentWarningDescriptions = convertObjectToArray(currentWarningDescriptions); +const getDescriptionKey = (val) => currentWarningDescriptions.indexOf(val); - currentWarningDescriptions.indexOf(val); const sortKeysAlphabetically = (a, b) => getDescriptionKey(a) - getDescriptionKey(b); // method to see which color is first From 2ab3c4bb5959e141f3fce5e724d4a3e6aa49381a Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Tue, 1 Oct 2024 20:55:12 -0700 Subject: [PATCH 16/23] removed test code --- src/cronjobs/userProfileJobs.js | 4 ++-- src/helpers/userHelper.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cronjobs/userProfileJobs.js b/src/cronjobs/userProfileJobs.js index 8acd192f3..68af6a12e 100644 --- a/src/cronjobs/userProfileJobs.js +++ b/src/cronjobs/userProfileJobs.js @@ -5,8 +5,8 @@ const userhelper = require('../helpers/userHelper')(); const userProfileJobs = () => { const allUserProfileJobs = new CronJob( - '* * * * *', // Comment out for testing. Run Every minute. - // '1 0 * * 0', // Every Sunday, 1 minute past midnight. + // '* * * * *', // Comment out for testing. Run Every minute. + '1 0 * * 0', // Every Sunday, 1 minute past midnight. async () => { const SUNDAY = 0; // will change back to 0 after fix diff --git a/src/helpers/userHelper.js b/src/helpers/userHelper.js index 59dd94fd8..7691be296 100644 --- a/src/helpers/userHelper.js +++ b/src/helpers/userHelper.js @@ -452,7 +452,7 @@ const userHelper = function () { const pdtEndOfLastWeek = moment().tz('America/Los_Angeles').endOf('week').subtract(1, 'week'); const users = await userProfile.find( - { isActive: true, email: 'arevaloluis114@gmail.com' }, + { isActive: true }, '_id weeklycommittedHours weeklySummaries missedHours', ); const usersRequiringBlueSqNotification = []; From 73d14f95a8ac1c6720698c801f12d9d452ac50f5 Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Thu, 3 Oct 2024 21:54:23 -0700 Subject: [PATCH 17/23] updated the reciever of the email, instead of my email and adjust the user data by creating an object --- src/controllers/warningsController.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/controllers/warningsController.js b/src/controllers/warningsController.js index 16130196a..b68fc2968 100644 --- a/src/controllers/warningsController.js +++ b/src/controllers/warningsController.js @@ -70,7 +70,11 @@ const warningsController = function (UserProfile) { return res.status(400).send({ message: 'No valid records found' }); } - currentUserName = `${record.firstName} ${record.lastName}`; + const userAssignedWarning = { + firstName: record.firstName, + lastName: record.lastName, + email: record.email, + }; const updatedWarnings = await UserProfile.findByIdAndUpdate( { @@ -89,7 +93,14 @@ const warningsController = function (UserProfile) { const adminEmails = await getUserRoleByEmail(record); if (sendEmail !== null) { - sendEmailToUser(sendEmail, description, currentUserName, monitorData, size, adminEmails); + sendEmailToUser( + sendEmail, + description, + userAssignedWarning, + monitorData, + size, + adminEmails, + ); } res.status(201).send({ message: 'success', warnings: completedData }); @@ -151,7 +162,7 @@ function getOrdinal(n) { const sendEmailToUser = ( sendEmail, warningDescription, - currentUserName, + userAssignedWarning, monitorData, size, adminEmails, @@ -159,6 +170,7 @@ const sendEmailToUser = ( const ordinal = getOrdinal(size); const subjectTitle = ordinal + ' Warning'; + const currentUserName = `${userAssignedWarning.firstName} ${userAssignedWarning.lastName}`; const emailTemplate = sendEmail === 'issue warning' ? `

Hello ${currentUserName},

@@ -177,7 +189,7 @@ const sendEmailToUser = ( if (sendEmail === 'issue warning') { emailSender( - 'arevaloluis114@gmail.com', + `${userAssignedWarning.email}`, subjectTitle, emailTemplate, adminEmails.toString(), @@ -185,7 +197,7 @@ const sendEmailToUser = ( ); } else if (sendEmail === 'issue blue square') { emailSender( - 'arevaloluis114@gmail.com', + `${userAssignedWarning.email}`, `Blue Square issued for ${warningDescription}`, emailTemplate, adminEmails.toString(), From f74c289fb11ca0ef53ce78bc91e83c3a1eafa944 Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Fri, 4 Oct 2024 19:48:59 -0700 Subject: [PATCH 18/23] removed duplicate admin emails --- src/controllers/warningsController.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/controllers/warningsController.js b/src/controllers/warningsController.js index b68fc2968..1ea979b7e 100644 --- a/src/controllers/warningsController.js +++ b/src/controllers/warningsController.js @@ -105,7 +105,6 @@ const warningsController = function (UserProfile) { res.status(201).send({ message: 'success', warnings: completedData }); } catch (error) { - console.log('error', error); res.status(400).send({ message: error.message || error }); } }; @@ -150,7 +149,10 @@ async function getUserRoleByEmail(user) { }); } } - return recipients; + + const sortedList = [...new Set(recipients)]; + console.log(sortedList); + return [...new Set(recipients)]; } //helper function to get the ordinal From af2df1cfd03fce7730c5793c99fb7e2760cde72f Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Fri, 4 Oct 2024 20:40:36 -0700 Subject: [PATCH 19/23] removed log --- src/controllers/warningsController.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/controllers/warningsController.js b/src/controllers/warningsController.js index 1ea979b7e..8b4759355 100644 --- a/src/controllers/warningsController.js +++ b/src/controllers/warningsController.js @@ -150,8 +150,6 @@ async function getUserRoleByEmail(user) { } } - const sortedList = [...new Set(recipients)]; - console.log(sortedList); return [...new Set(recipients)]; } From 455c37937620edad5b4e78756b93d4c78d1c4afa Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Thu, 10 Oct 2024 19:55:30 -0700 Subject: [PATCH 20/23] added jae's email --- src/controllers/warningsController.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/warningsController.js b/src/controllers/warningsController.js index 8b4759355..6334fdb52 100644 --- a/src/controllers/warningsController.js +++ b/src/controllers/warningsController.js @@ -140,7 +140,7 @@ const warningsController = function (UserProfile) { //helper to get the team members admin emails async function getUserRoleByEmail(user) { - const recipients = []; + const recipients = ['onecommunityglobal@gmail.com']; for (const teamId of user.teams) { const managementEmails = await userHelper.getTeamManagementEmail(teamId); if (Array.isArray(managementEmails) && managementEmails.length > 0) { From 773186df765a7a82a1655cd3b7a271578e307f3e Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Thu, 10 Oct 2024 20:43:26 -0700 Subject: [PATCH 21/23] removed jaes email and replaced it with a dummy one --- src/controllers/warningsController.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/controllers/warningsController.js b/src/controllers/warningsController.js index 6334fdb52..fb1aa3eef 100644 --- a/src/controllers/warningsController.js +++ b/src/controllers/warningsController.js @@ -140,7 +140,8 @@ const warningsController = function (UserProfile) { //helper to get the team members admin emails async function getUserRoleByEmail(user) { - const recipients = ['onecommunityglobal@gmail.com']; + //replacement for jae's email + const recipients = ['test@test.com']; for (const teamId of user.teams) { const managementEmails = await userHelper.getTeamManagementEmail(teamId); if (Array.isArray(managementEmails) && managementEmails.length > 0) { From 4de52b7088dcf077cf8dea749f5a5dff84131419 Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Mon, 16 Dec 2024 20:06:17 -0800 Subject: [PATCH 22/23] incorporated changes requested --- src/controllers/currentWarningsController.js | 6 ++---- src/controllers/warningsController.js | 10 +--------- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/src/controllers/currentWarningsController.js b/src/controllers/currentWarningsController.js index f064cb4f7..ec76d3326 100644 --- a/src/controllers/currentWarningsController.js +++ b/src/controllers/currentWarningsController.js @@ -34,7 +34,7 @@ const currentWarningsController = function (currentWarnings) { const warnings = await currentWarnings.find({}); if (warnings.length === 0) { - return res.status(400).send({ message: 'no valid records' }); + return res.status(400).send({ error: 'no valid records' }); } const testWarning = checkIfSpecialCharacter(newWarning); @@ -136,9 +136,7 @@ const currentWarningsController = function (currentWarnings) { }, ); - res.status(200).send({ - message: 'warning description was successfully deleted and user profiles updated', - }); + return res.status(200); } catch (error) { res.status(401).send({ message: error.message || error }); } diff --git a/src/controllers/warningsController.js b/src/controllers/warningsController.js index fb1aa3eef..e75c9f84c 100644 --- a/src/controllers/warningsController.js +++ b/src/controllers/warningsController.js @@ -27,21 +27,13 @@ async function getWarningDescriptions() { currentWarningDescriptions = await currentWarnings.find({}, { warningTitle: 1, _id: 0 }); } -const convertObjectToArray = (obj) => { - const arr = []; - for (const key of obj) { - arr.push(key.warningTitle); - } - return arr; -}; - const warningsController = function (UserProfile) { const getWarningsByUserId = async function (req, res) { currentWarningDescriptions = await currentWarnings.find({ activeWarning: true, }); - currentWarningDescriptions = convertObjectToArray(currentWarningDescriptions); + currentWarningDescriptions = currentWarningDescriptions.map((a) => a.warningTitle); const { userId } = req.params; try { From 339a1fe7a4ffc0e56b1008d66950e57649fa85e5 Mon Sep 17 00:00:00 2001 From: luisarevalo21 Date: Tue, 17 Dec 2024 20:05:15 -0800 Subject: [PATCH 23/23] adjusted the email sender --- src/controllers/warningsController.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/controllers/warningsController.js b/src/controllers/warningsController.js index e75c9f84c..08515c207 100644 --- a/src/controllers/warningsController.js +++ b/src/controllers/warningsController.js @@ -185,6 +185,7 @@ const sendEmailToUser = ( `${userAssignedWarning.email}`, subjectTitle, emailTemplate, + null, adminEmails.toString(), null, ); @@ -192,6 +193,7 @@ const sendEmailToUser = ( emailSender( `${userAssignedWarning.email}`, `Blue Square issued for ${warningDescription}`, + null, emailTemplate, adminEmails.toString(), null,