Skip to content

Commit

Permalink
Merge pull request #1123 from OneCommunityGlobal/luis-add-email-notif…
Browse files Browse the repository at this point in the history
…cation-to-warning-tracker-backend

Luis add email notifcation to warning tracker backend
  • Loading branch information
one-community authored Dec 18, 2024
2 parents 424ca84 + 1c4b4e3 commit eafa526
Show file tree
Hide file tree
Showing 8 changed files with 357 additions and 41 deletions.
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);
};
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({
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

0 comments on commit eafa526

Please sign in to comment.