const Prospection = require('../models/prospection.model.js');
// Create and Save a new admin
exports.create = (req, res) => {
// Validate request
// Create a admin
const prospection = new Prospection({
typeChantier: req.body.typeChantier || "Untitled Prospection",
RPlus: req.body.RPlus,
zone: req.body.zone,
date: req.body.date,
idPI: req.body.idPI,
idEGC: req.body.idEGC,
idArchitect: req.body.idArchitect,
idApplicateur: req.body.idApplicateur,
etapeAvancement: req.body.etapeAvancement,
observation: req.body.observation,
progressionOpportunite: req.body.progressionOpportunite,
idcommerciale: req.body.idcommerciale,
budget: req.body.budget,
reclamation: req.body.reclamation,
idDepartement: req.body.idDepartement,
});
// Save admin in the database
prospection.save()
.then(data => {
res.send(data);
}).catch(err => {
res.status(500).send({
message: err.message || "Some error occurred while creating the prospection."
});
});
};
// Retrieve and return all admin from the database.
exports.findAll = (req, res) => {
Prospection.find()
.then(prospections => {
res.send(prospections);
}).catch(err => {
res.status(500).send({
message: err.message || "Some error occurred while retrieving prospections."
});
});
};
// Find a single admin with a adminId
exports.findOne = (req, res) => {
Prospection.findById(req.params.prospectionId)
.then(prospection => {
if (!prospection) {
return res.status(404).send({
message: "prospection not found with id " + req.params.prospectionId
});
}
res.send(prospection);
}).catch(err => {
if (err.kind === 'ObjectId') {
return res.status(404).send({
message: "prospection not found with id " + req.params.prospectionId
});
}
return res.status(500).send({
message: "Error retrieving prospection with id " + req.params.prospectionId
});
});
};
// Update a admin identified by the adminId in the request
exports.update = (req, res) => {
// Validate Request
// Find admin and update it with the request body
Prospection.findByIdAndUpdate(req.params.prospectionId, {
typeChantier: req.body.typeChantier || "Untitled Prospection",
RPlus: req.body.RPlus,
zone: req.body.zone,
date: req.body.date,
idPI: req.body.idPI,
idEGC: req.body.idEGC,
idArchitect: req.body.idArchitect,
idApplicateur: req.body.idApplicateur,
etapeAvancement: req.body.etapeAvancement,
observation: req.body.observation,
progressionOpportunite: req.body.progressionOpportunite,
idcommerciale: req.body.idcommerciale,
budget: req.body.budget,
reclamation: req.body.reclamation,
idDepartement: req.body.idDepartement,
}, { new: true })
.then(prospection => {
if (!prospection) {
return res.status(404).send({
message: "prospection not found with id " + req.params.prospectionId
});
}
res.send(prospection);
}).catch(err => {
if (err.kind === 'ObjectId') {
return res.status(404).send({
message: "prospection not found with id " + req.params.prospectionId
});
}
return res.status(500).send({
message: "Error updating prospection with id " + req.params.prospectionId
});
});
};
// Delete a admin with the specified adminId in the request
exports.delete = (req, res) => {
Prospection.findByIdAndRemove(req.params.prospectionId)
.then(prospection => {
if (!prospection) {
return res.status(404).send({
message: "prospection not found with id " + req.params.prospectionId
});
}
res.send({ message: "prospection deleted successfully!" });
}).catch(err => {
if (err.kind === 'ObjectId' || err.name === 'NotFound') {
return res.status(404).send({
message: "prospection not found with id " + req.params.prospectionId
});
}
return res.status(500).send({
message: "Could not delete prospection with id " + req.params.prospectionId
});
});
};