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 new warnings for no summary and not enough hours backend #1183

Open
wants to merge 10 commits into
base: development
Choose a base branch
from
155 changes: 155 additions & 0 deletions src/controllers/currentWarningsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/* 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({ message: '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 },
},
},
);

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;
28 changes: 15 additions & 13 deletions src/controllers/userProfileController.js
Original file line number Diff line number Diff line change
Expand Up @@ -1693,7 +1693,6 @@ const userProfileController = function (UserProfile, Project) {
return;
}
const { userId, blueSquareId } = req.params;
// console.log(userId, blueSquareId);

UserProfile.findById(userId, async (err, record) => {
if (err || !record) {
Expand All @@ -1715,7 +1714,10 @@ const userProfileController = function (UserProfile, Project) {
_id: record._id,
});
})
.catch((error) => res.status(400).send(error));
.catch((error) => {
console.log('error', error);
res.status(400).send(error);
});
});
};

Expand Down Expand Up @@ -1797,20 +1799,20 @@ const userProfileController = function (UserProfile, Project) {
}
};

const updateUserInformation = async function (req,res){
const updateUserInformation = async function (req, res) {
try {
const data=req.body;
data.map(async (e)=> {
const data = req.body;
data.map(async (e) => {
let result = await UserProfile.findById(e.user_id);
result[e.item]=e.value
let newdata=await result.save()
})
res.status(200).send({ message: 'Update successful'});
result[e.item] = e.value;
let newdata = await result.save();
});
res.status(200).send({ message: 'Update successful' });
} catch (error) {
console.log(error)
return res.status(500)
console.log(error);
return res.status(500);
}
}
};

return {
postUserProfile,
Expand Down Expand Up @@ -1841,7 +1843,7 @@ const userProfileController = function (UserProfile, Project) {
getAllTeamCode,
getAllTeamCodeHelper,
updateUserInformation,
getUserProfileBasicInfo
getUserProfileBasicInfo,
};
};

Expand Down
Loading