From 92f65c49d83d11acb3f656751046978cc83ed9cb Mon Sep 17 00:00:00 2001 From: DanielRogs Date: Sat, 24 Aug 2024 20:29:38 -0300 Subject: [PATCH 1/2] feat: adicionando campos e endpoins para editar colaboradores --- .../src/controllers/unit.controller.js | 8 +-- .../src/controllers/unitUser.controller.js | 50 +++++++++++++------ .../src/controllers/user.controller.js | 10 ++-- riso_backend/src/models/UnitUser.js | 6 ++- riso_backend/src/routes/unitUser.js | 3 +- riso_backend/src/services/unitUser.service.js | 4 +- 6 files changed, 55 insertions(+), 26 deletions(-) diff --git a/riso_backend/src/controllers/unit.controller.js b/riso_backend/src/controllers/unit.controller.js index 4c64b105..03bc1cd0 100644 --- a/riso_backend/src/controllers/unit.controller.js +++ b/riso_backend/src/controllers/unit.controller.js @@ -8,7 +8,7 @@ const findAllUnit = async (req, res) => { return res.status(200).send({message: 'Any user was created'}); } - return res.status(201).send({ unit: unit }); + return res.status(201).send({ message: 'Units were found', unit }); }catch(err) { return res.status(500).send({ message: err.message }); } @@ -21,7 +21,7 @@ const findByCodeUnit = async (req, res) => { if (!unit) { return res.status(404).send({ message: 'Unit not found' }); } - return res.status(200).send(unit); + return res.status(200).send({ message: 'Unit was found', unit }); } catch (err) { return res.status(500).send({ message: err.message }); } @@ -76,7 +76,7 @@ const createUnit = async (req, res) => { const unit = await unitService.createService(unitData); - return res.status(200).send({message: 'Unity and company successfully created ', unit:unit}); + return res.status(200).send({message: 'Unity and company successfully created ', unit}); }catch (err){ if (err.code === 11000) { // Código de erro para violação de chave única em MongoDB/Mongoose return res.status(400).send({ message: 'CNPJ already in use', substatus: 2 }); @@ -100,7 +100,7 @@ const updateUnit = async (req, res) => { return res.status(404).send({ message: 'Unit not found' }); } - return res.status(200).send({message: 'Unit successfully updated ', unit:unit}); + return res.status(200).send({message: 'Unit successfully updated ', unit}); }catch (err){ return res.status(500).send({message: err.message}); } diff --git a/riso_backend/src/controllers/unitUser.controller.js b/riso_backend/src/controllers/unitUser.controller.js index d9373d4b..77305e20 100644 --- a/riso_backend/src/controllers/unitUser.controller.js +++ b/riso_backend/src/controllers/unitUser.controller.js @@ -8,27 +8,28 @@ const findAllUnitUser = async (req, res) => { return res.status(200).send({message: 'Any UnitUser was created'}); } - return res.status(201).send({ unitUser: unitUser }); + return res.status(201).send({message: 'All units and users relationship was returned', unitUser }); }catch(err) { return res.status(500).send({ message: err.message }); } } const createUnitUser = async (req, res) => { - try{ + try { const { user, unit, + master_adm, } = req.body; - if (!user || !unit) { + if (!user || !unit || !master_adm) { return res.status(400).send({ message: 'Submit all fields for userUnit', substatus: 1}); }; const unitUser = await unitUserService.createService(req.body); - return res.status(200).send({message: 'Unity and company successfully created ', unitUser:unitUser}); - }catch (err){ + return res.status(200).send({message: 'Unity and company successfully created ', unitUser}); + } catch (err) { if (err.code === 11000) { // Código de erro para violação de chave única em MongoDB/Mongoose return res.status(400).send({ message: 'Match User and Unit already registrated', substatus: 2 }); } @@ -39,22 +40,42 @@ const createUnitUser = async (req, res) => { const findByUserIdUnitUser = async (req, res) => { try { - const {id} = req.body; + const { userId } = req.body; - if(!id){ - return res.status(400).send({ message: 'User ID is required'}) + if (!userId) { + return res.status(400).send({ message: 'User ID is required' }); } - const unitUser = await unitUserService.findById({user:id}); - if (!unitUser) { - return res.status(404).send({ message: 'This user dont have units associations'}); + const unitUser = await unitUserService.findByUserIdService(userId); + if (!unitUser || unitUser.length === 0) { + return res.status(404).send({ message: 'This user doesn\'t have any unit associations' }); } - return res.status(200).send({message: 'UserUnit was found it', unitUser:unitUser}); - }catch(err) { + return res.status(200).send({ message: 'UserUnit found', unitUser }); + } catch (err) { return res.status(500).send({ message: err.message }); } -} +}; + +const findAllUsersByUnitIdUserUnit = async (req, res) => { + try { + const { unitId } = req.params; + + if (!unitId) { + return res.status(400).send({ message: 'Unit ID is required' }); + } + + const users = await unitUserService.findAllUsersByUnitIdService(unitId); + + if (!users || users.length === 0) { + return res.status(404).send({ message: 'No users found for this unit' }); + } + + return res.status(200).send({ message: 'Users found', users }); + } catch (err) { + return res.status(500).send({ message: err.message }); + } +}; const deleteUnitUser = async (req,res) => { try{ @@ -80,5 +101,6 @@ export default { findAllUnitUser, createUnitUser, findByUserIdUnitUser, + findAllUsersByUnitIdUserUnit, deleteUnitUser, }; \ No newline at end of file diff --git a/riso_backend/src/controllers/user.controller.js b/riso_backend/src/controllers/user.controller.js index 53d3b291..ef8fc706 100644 --- a/riso_backend/src/controllers/user.controller.js +++ b/riso_backend/src/controllers/user.controller.js @@ -25,7 +25,7 @@ const findAllUser = async (req, res) => { return res.status(200).send({message: 'Any user was created'}); } - return res.status(201).send({ user: user }); + return res.status(201).send({ message: 'Users were found', user }); }catch(err) { return res.status(500).send({ message: err.message }); } @@ -55,7 +55,7 @@ const createUser = async (req, res) => { return res.status(403).send({ message: 'Error creating User' }); } - return res.status(201).send({message: 'User created sucefully', user: user }); + return res.status(201).send({message: 'User created sucefully', user }); }catch(err){ if (err.code === 11000) { // Código de erro para violação de chave única em MongoDB/Mongoose return res.status(400).send({ message: 'Email already in use', substatus: 2 }); @@ -81,7 +81,7 @@ const loginUser = async (req, res) => { return res.status(400).send({ message: 'Password or email is invalid', substatus: 2}); } - return res.status(200).send({message: 'User was found it', user:user}); + return res.status(200).send({ message: 'User was found it', user }); }catch(err) { return res.status(500).send({ message: err.message }); } @@ -92,7 +92,7 @@ const updateUser = async (req, res) => { const {id} = req.params; if(!id) { - return res.status(404).send({message: 'User ID is required'}); + return res.status(404).send({ message: 'User ID is required' }); } const user = await userService.updateService({_id: id}, req.body); @@ -101,7 +101,7 @@ const updateUser = async (req, res) => { return res.status(404).send({ message: 'This user does not exist' }); } - return res.status(200).send({message: 'This user was updated', user:user}); + return res.status(200).send({ message: 'This user was updated', user }); }catch(err) { return res.status(500).send({ message: err.message }); } diff --git a/riso_backend/src/models/UnitUser.js b/riso_backend/src/models/UnitUser.js index 762b7995..41a71aa9 100644 --- a/riso_backend/src/models/UnitUser.js +++ b/riso_backend/src/models/UnitUser.js @@ -10,7 +10,11 @@ const UnitUserSchema = new mongoose.Schema({ type: mongoose.Schema.Types.ObjectId, ref: 'Unit', required: true - } + }, + master_adm: { + type: Boolean, + required: true, + }, }, {timestamps: true}); // Garantir que um usuário não possa estar na mesma unidade mais de uma vez diff --git a/riso_backend/src/routes/unitUser.js b/riso_backend/src/routes/unitUser.js index f2b476f6..1b81784e 100644 --- a/riso_backend/src/routes/unitUser.js +++ b/riso_backend/src/routes/unitUser.js @@ -5,7 +5,8 @@ const unitUserRouter = express.Router() unitUserRouter.get('/findAllUnitUser', unitUserController.findAllUnitUser); unitUserRouter.post('/createUnitUser',unitUserController.createUnitUser); -unitUserRouter.get('/findByUserId/:id', unitUserController.findByUserIdUnitUser); +unitUserRouter.get('/findByUserId/:UserId', unitUserController.findByUserIdUnitUser); +unitUserRouter.get('/findAllUsersByUnitId/:UnitId', unitUserController.findAllUsersByUnitIdUserUnit); unitUserRouter.delete('/deleteUnitUser/:id',unitUserController.deleteUnitUser); export default unitUserRouter; \ No newline at end of file diff --git a/riso_backend/src/services/unitUser.service.js b/riso_backend/src/services/unitUser.service.js index 3333c700..7b342392 100644 --- a/riso_backend/src/services/unitUser.service.js +++ b/riso_backend/src/services/unitUser.service.js @@ -2,12 +2,14 @@ import UnitUser from "../models/UnitUser.js"; const findAllService = () => UnitUser.find(); const createService = (params, body) => UnitUser.create(params, body); -const findByUserIdService = (body) => UnitUser.find(body); +const findByUserIdService = (userID) => UnitUser.find({ user: userID }); +const findAllUsersByUnitIdService = (unitID) => UnitUser.find({ unit: unitID }); const deleteService = (params) => UnitUser.deleteOne(params); export default { findAllService, createService, findByUserIdService, + findAllUsersByUnitIdService, deleteService, } \ No newline at end of file From 9e5344a4f7abd1d8453e4613a9e18756fa96fec9 Mon Sep 17 00:00:00 2001 From: DanielRogs Date: Sat, 24 Aug 2024 20:35:39 -0300 Subject: [PATCH 2/2] feat: adicionando endpoits de edicao de dados --- .../src/controllers/unitUser.controller.js | 21 +++++++++++++++++++ riso_backend/src/routes/unitUser.js | 1 + riso_backend/src/services/unitUser.service.js | 2 ++ 3 files changed, 24 insertions(+) diff --git a/riso_backend/src/controllers/unitUser.controller.js b/riso_backend/src/controllers/unitUser.controller.js index 77305e20..8dbec1ae 100644 --- a/riso_backend/src/controllers/unitUser.controller.js +++ b/riso_backend/src/controllers/unitUser.controller.js @@ -77,6 +77,26 @@ const findAllUsersByUnitIdUserUnit = async (req, res) => { } }; +const updateUnitUser = async (req, res) => { + try{ + const { id } = req.params; + + if(!id) { + return res.status(404).send({ message: 'User ID is required' }); + } + + const unitUser = await unitUserService.updateService({_id: id}, req.body); + + if (!unitUser) { + return res.status(404).send({ message: 'This unit and user relationship does not exist' }); + } + + return res.status(200).send({ message: 'This unit and user relationship were updated', unitUser }); + }catch(err) { + return res.status(500).send({ message: err.message }); + } +} + const deleteUnitUser = async (req,res) => { try{ const {id} = req.params; @@ -102,5 +122,6 @@ export default { createUnitUser, findByUserIdUnitUser, findAllUsersByUnitIdUserUnit, + updateUnitUser, deleteUnitUser, }; \ No newline at end of file diff --git a/riso_backend/src/routes/unitUser.js b/riso_backend/src/routes/unitUser.js index 1b81784e..8f341b4c 100644 --- a/riso_backend/src/routes/unitUser.js +++ b/riso_backend/src/routes/unitUser.js @@ -7,6 +7,7 @@ unitUserRouter.get('/findAllUnitUser', unitUserController.findAllUnitUser); unitUserRouter.post('/createUnitUser',unitUserController.createUnitUser); unitUserRouter.get('/findByUserId/:UserId', unitUserController.findByUserIdUnitUser); unitUserRouter.get('/findAllUsersByUnitId/:UnitId', unitUserController.findAllUsersByUnitIdUserUnit); +unitUserRouter.patch('/updateUnitUser/:id', ) unitUserRouter.delete('/deleteUnitUser/:id',unitUserController.deleteUnitUser); export default unitUserRouter; \ No newline at end of file diff --git a/riso_backend/src/services/unitUser.service.js b/riso_backend/src/services/unitUser.service.js index 7b342392..5147b471 100644 --- a/riso_backend/src/services/unitUser.service.js +++ b/riso_backend/src/services/unitUser.service.js @@ -4,6 +4,7 @@ const findAllService = () => UnitUser.find(); const createService = (params, body) => UnitUser.create(params, body); const findByUserIdService = (userID) => UnitUser.find({ user: userID }); const findAllUsersByUnitIdService = (unitID) => UnitUser.find({ unit: unitID }); +const updateService = (params, body) => UnitUser.updateOne(params, body, {new: true}); const deleteService = (params) => UnitUser.deleteOne(params); export default { @@ -11,5 +12,6 @@ export default { createService, findByUserIdService, findAllUsersByUnitIdService, + updateService, deleteService, } \ No newline at end of file