Skip to content

Commit

Permalink
Merge pull request #49 from mdsreq-fga-unb/feature/us04
Browse files Browse the repository at this point in the history
Feature 04
  • Loading branch information
DanielRogs authored Aug 24, 2024
2 parents 4a3ef25 + 9e5344a commit 06b1374
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 24 deletions.
8 changes: 4 additions & 4 deletions riso_backend/src/controllers/unit.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand All @@ -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 });
}
Expand Down Expand Up @@ -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 });
Expand All @@ -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});
}
Expand Down
67 changes: 55 additions & 12 deletions riso_backend/src/controllers/unitUser.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand All @@ -39,18 +40,58 @@ 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});
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 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 });
}
Expand Down Expand Up @@ -80,5 +121,7 @@ export default {
findAllUnitUser,
createUnitUser,
findByUserIdUnitUser,
findAllUsersByUnitIdUserUnit,
updateUnitUser,
deleteUnitUser,
};
10 changes: 5 additions & 5 deletions riso_backend/src/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down Expand Up @@ -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 });
Expand All @@ -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 });
}
Expand All @@ -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);
Expand All @@ -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 });
}
Expand Down
6 changes: 5 additions & 1 deletion riso_backend/src/models/UnitUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion riso_backend/src/routes/unitUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ 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.patch('/updateUnitUser/:id', )
unitUserRouter.delete('/deleteUnitUser/:id',unitUserController.deleteUnitUser);

export default unitUserRouter;
6 changes: 5 additions & 1 deletion riso_backend/src/services/unitUser.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ 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 updateService = (params, body) => UnitUser.updateOne(params, body, {new: true});
const deleteService = (params) => UnitUser.deleteOne(params);

export default {
findAllService,
createService,
findByUserIdService,
findAllUsersByUnitIdService,
updateService,
deleteService,
}

0 comments on commit 06b1374

Please sign in to comment.