Skip to content

Commit

Permalink
Refactor initiative creation to remove user retrieval and validate su…
Browse files Browse the repository at this point in the history
…bdivision existence
  • Loading branch information
guillecro committed Jan 23, 2025
1 parent ad4444e commit f469e4d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
26 changes: 8 additions & 18 deletions controllers/initiativeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,6 @@ exports.create = async (req, res) => {
dimensionIds,
subdivisionId
} = req.body;
const userId = req.user.id;

// get the user
const user = await models.User.findOne({
where: { id: userId },
include: [
{
model: models.Subdivision,
as: 'subdivision',
}
]
});

if(!user) {
return res.status(404).json({ message: 'Usuario no encontrado' });
}

// get the dimensions
const dimensions = await models.Dimension.findAll({
Expand All @@ -160,9 +144,15 @@ exports.create = async (req, res) => {
}
});

// TODO: check if the subdivisionId exists and the city of the subdivision is the same as the user's city
// check if the subdivisionId exists
const subdivision = await models.Subdivision.findByPk(subdivisionId);

if(!subdivision) {
return res.status(404).json({ message: 'Subdivisión no encontrada' });
}

const t = await models.sequelize.transaction();

try {
// create the contact
const contact = {
Expand All @@ -181,7 +171,7 @@ exports.create = async (req, res) => {
needsAndOffers,
contactId: newContact.id,
subdivisionId: subdivisionId,
authorId: user.id,
authorId: null,
}

const newInitiative = await models.Initiative.create(initiative, { transaction: t });
Expand Down
1 change: 0 additions & 1 deletion routes/initiative.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ router.get('/',
)

router.post('/',
authorize(),
[
check('name').isString().withMessage(msg.validationError.string),
check('description').isString().withMessage(msg.validationError.string),
Expand Down

0 comments on commit f469e4d

Please sign in to comment.