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

Luis add email notifcation to warning tracker backend #1123

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4f41d5e
test: completed backend for add/edit/remove warnigns
luisarevalo21 Jun 11, 2024
74c7850
test: changed get method return code to 200
luisarevalo21 Jun 14, 2024
7eb0a65
test: added missing code for backend to update with new warnings
luisarevalo21 Jun 14, 2024
bd8c58d
test: completed editing and adding bugs
luisarevalo21 Jun 20, 2024
301fd28
feat: used checkIfspecial function to check against regex
luisarevalo21 Jun 20, 2024
c1d6c76
fix: fixed bug that prevented numbers in new warning and edited warning
luisarevalo21 Jun 21, 2024
161b2c1
fix: fixed the lowercase issue and the 422 error code, also, added co…
luisarevalo21 Aug 13, 2024
fa01786
fix: added 200 response code instead of 422 for posting, editing a wa…
luisarevalo21 Aug 14, 2024
ab1e953
fix: removed logs and added isPermanent Attribute to new warnings
luisarevalo21 Aug 22, 2024
081c5a5
fix: merged add/edit pr into here
luisarevalo21 Aug 22, 2024
da17641
fix: attempted to send email to my email address to test
luisarevalo21 Aug 22, 2024
78ee068
feat: added send an email after 3 and 4 warnings function
luisarevalo21 Sep 11, 2024
91e8398
feat: added sending email features and adjusted the user profile mode
luisarevalo21 Sep 18, 2024
05a7524
adjusted email text and body
luisarevalo21 Sep 19, 2024
423c866
merged: dev into code base
luisarevalo21 Sep 25, 2024
d998541
adjusted email text when issuing a warning and blue square
luisarevalo21 Sep 28, 2024
92b2ec1
fetch admin's of the team and send an email cc them when a warning an…
luisarevalo21 Oct 2, 2024
2ab3c4b
removed test code
luisarevalo21 Oct 2, 2024
73d14f9
updated the reciever of the email, instead of my email and adjust the…
luisarevalo21 Oct 4, 2024
f74c289
removed duplicate admin emails
luisarevalo21 Oct 5, 2024
af2df1c
removed log
luisarevalo21 Oct 5, 2024
455c379
added jae's email
luisarevalo21 Oct 11, 2024
773186d
removed jaes email and replaced it with a dummy one
luisarevalo21 Oct 11, 2024
4de52b7
incorporated changes requested
luisarevalo21 Dec 17, 2024
339a1fe
adjusted the email sender
luisarevalo21 Dec 18, 2024
1c4b4e3
resolved merge conflicts
luisarevalo21 Dec 18, 2024
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
153 changes: 153 additions & 0 deletions src/controllers/currentWarningsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/* eslint-disable */
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 !/^[a-zA-Z][a-zA-Z0-9]*(?: [a-zA-Z0-9]+)*$/.test(warning);
luisarevalo21 marked this conversation as resolved.
Show resolved Hide resolved
};
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(200).send({ currentWarningDescriptions: response });
} catch (error) {
res.status(401).send({ message: error.message || error });
}
};

const postNewWarningDescription = async (req, res) => {
try {
const { newWarning, activeWarning, isPermanent } = req.body;

const warnings = await currentWarnings.find({});

if (warnings.length === 0) {
return res.status(400).send({ error: 'no valid records' });
}

const testWarning = checkIfSpecialCharacter(newWarning);
if (testWarning) {
return res.status(200).send({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errors shouldn't be returning a 200 code, a generic 400 should be enough. And why is errors returning two different bodies? ({ message: string } and { error: string })

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello
Thanks for the feedback!
I send a 200 because if I send a 400 error, there is a red warning that appears in the console and wanted to avoid that. After reaching out team members this was the solution

error: 'Warning cannot have special characters as the first letter',
});
}

if (checkForDuplicates(newWarning, warnings)) {
return res.status(200).send({ error: 'warning already exists' });
}

const newWarningDescription = new currentWarnings();
newWarningDescription.warningTitle = newWarning;
newWarningDescription.activeWarning = activeWarning;
newWarningDescription.isPermanent = isPermanent;

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;

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 = checkIfSpecialCharacter(lowerCaseWarning);

if (testWarning) {
return res.status(200).send({
error: 'Warning cannot have special characters as the first letter',
});
}

if (checkForDuplicates(lowerCaseWarning, warnings)) {
return res.status(200).send({ error: 'warning already exists try a different name' });
}

await currentWarnings.findOneAndUpdate(
{ _id: id },
[{ $set: { warningTitle: lowerCaseWarning.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 },
},
},
);

return res.status(200);
} catch (error) {
res.status(401).send({ message: error.message || error });
}
};

return {
getCurrentWarnings,
postNewWarningDescription,
updateWarningDescription,
deleteWarningDescription,
editWarningDescription,
};
};
module.exports = currentWarningsController;
Loading
Loading