-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: MarcoTulioSoares <[email protected]> Co-authored-by: LucasAvelar2711 <[email protected]>
- Loading branch information
1 parent
07979bd
commit 7e0a82c
Showing
16 changed files
with
348 additions
and
159 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import unitService from "../services/unit.service.js"; | ||
|
||
const findAllUnit = async (req, res) => { | ||
try{ | ||
const unit = await unitService.findAllService(); | ||
|
||
if (unit.length === 0) { | ||
return res.status(200).send({message: 'Any user was created'}); | ||
} | ||
|
||
return res.status(201).send({ unit: unit }); | ||
}catch(err) { | ||
return res.status(500).send({ message: err.message }); | ||
} | ||
} | ||
|
||
const createUnit = async (req, res) => { | ||
try{ | ||
const { | ||
name, | ||
numberOfficials, | ||
cnpj, | ||
state, | ||
street, | ||
complement | ||
} = req.body; | ||
|
||
if (!name || !numberOfficials || !cnpj || !state || !street || !complement) { | ||
return res.status(400).send({ message: 'Submit all fields for unit', substatus: 1}); | ||
}; | ||
|
||
const unit = await unitService.createService(req.body); | ||
|
||
return res.status(200).send({message: 'Unity and company successfully created ', unit: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 }); | ||
} | ||
|
||
return res.status(500).send({message: err.message}); | ||
} | ||
} | ||
|
||
const updateUnit = async (req, res) => { | ||
try{ | ||
const {id} = req.params; | ||
|
||
if(!id) { | ||
return res.status(404).send({message: 'Unit ID is required'}); | ||
} | ||
|
||
const unit = await unitService.updateService({_id: id}, req.body); | ||
|
||
if (!unit) { | ||
return res.status(404).send({ message: 'Unit not found' }); | ||
} | ||
|
||
return res.status(200).send({message: 'Unit successfully updated ', unit:unit}); | ||
}catch (err){ | ||
return res.status(500).send({message: err.message}); | ||
} | ||
} | ||
|
||
const deleteUnit = async (req,res) => { | ||
try{ | ||
const {id} = req.params; | ||
|
||
if (!id) { | ||
return res.status(404).send({message: 'Unit ID is required'}); | ||
} | ||
|
||
const unit = await unitService.deleteService({ _id: id}); | ||
|
||
if (!unit) { | ||
return res.status(404).send({ message: 'This unit does not exist' }); | ||
} | ||
|
||
return res.status(200).send({ message: 'Unit was deleted successfully', unit }); | ||
}catch(err){ | ||
return res.status(500).send({ message: err.message }); | ||
} | ||
} | ||
|
||
export default { | ||
findAllUnit, | ||
createUnit, | ||
updateUnit, | ||
deleteUnit, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import unitUserService from "../services/unitUser.service.js"; | ||
|
||
const findAllUnitUser = async (req, res) => { | ||
try{ | ||
const unitUser = await unitUserService.findAllService(); | ||
|
||
if (unitUser.length === 0) { | ||
return res.status(200).send({message: 'Any UnitUser was created'}); | ||
} | ||
|
||
return res.status(201).send({ unitUser: unitUser }); | ||
}catch(err) { | ||
return res.status(500).send({ message: err.message }); | ||
} | ||
} | ||
|
||
const createUnitUser = async (req, res) => { | ||
try{ | ||
const { | ||
user, | ||
unit, | ||
} = req.body; | ||
|
||
if (!user || !unit) { | ||
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){ | ||
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 }); | ||
} | ||
|
||
return res.status(500).send({message: err.message}); | ||
} | ||
} | ||
|
||
const findByUserIdUnitUser = async (req, res) => { | ||
try { | ||
const {id} = req.body; | ||
|
||
if(!id){ | ||
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'}); | ||
} | ||
|
||
return res.status(200).send({message: 'UserUnit was found it', unitUser:unitUser}); | ||
}catch(err) { | ||
return res.status(500).send({ message: err.message }); | ||
} | ||
} | ||
|
||
const deleteUnitUser = async (req,res) => { | ||
try{ | ||
const {id} = req.params; | ||
|
||
if (!id) { | ||
return res.status(404).send({message: 'UnitUser ID is required'}); | ||
} | ||
|
||
const unitUser = await unitUserService.deleteService({ _id: id}); | ||
|
||
if (!unit) { | ||
return res.status(404).send({ message: 'This unitUser does not exist' }); | ||
} | ||
|
||
return res.status(200).send({ message: 'UnitUser was deleted successfully', unitUser }); | ||
}catch(err){ | ||
return res.status(500).send({ message: err.message }); | ||
} | ||
} | ||
|
||
export default { | ||
findAllUnitUser, | ||
createUnitUser, | ||
findByUserIdUnitUser, | ||
deleteUnitUser, | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import mongoose from 'mongoose'; | ||
|
||
const UnitSchema = new mongoose.Schema({ | ||
name: { | ||
type: String, | ||
required: true | ||
}, | ||
numberOfficials: { | ||
type: Number, | ||
required: true | ||
}, | ||
cnpj: { | ||
type: String, | ||
required: true, | ||
unique: true, | ||
}, | ||
state: { | ||
type: String, | ||
required: true | ||
}, | ||
street: { | ||
type: String, | ||
required: true | ||
}, | ||
complement: { | ||
type: String, | ||
required: true | ||
}, | ||
number: { | ||
type: Number, | ||
required: false | ||
} | ||
}, {timestamps:true}); | ||
|
||
const Unit = mongoose.model('Unit', UnitSchema); | ||
export default Unit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import mongoose from 'mongoose'; | ||
|
||
const UnitUserSchema = new mongoose.Schema({ | ||
user: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'User', | ||
required: true | ||
}, | ||
unit: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'Unit', | ||
required: true | ||
} | ||
}, {timestamps: true}); | ||
|
||
// Garantir que um usuário não possa estar na mesma unidade mais de uma vez | ||
UnitUserSchema.index({ user: 1, unit: 1 }, { unique: true }); | ||
|
||
const UnitUser = mongoose.model('UnitUser', UnitUserSchema); | ||
export default UnitUser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import { Router } from "express"; | ||
import cors from 'cors'; | ||
import userRouter from "./user.route.js"; | ||
import unidadeEmpresaRouter from "./unidadeEmpresa.route.js"; | ||
import unitRouter from "./unit.route.js"; | ||
import unitUserRouter from "./unitUser.js"; | ||
|
||
const router = Router(); | ||
|
||
router.use("/user", cors(), userRouter); | ||
|
||
router.use("/unidadeEmpresa", cors(), unidadeEmpresaRouter); | ||
router.use("/unit", cors(), unitRouter); | ||
router.use("/unitUser", cors(), unitUserRouter); | ||
|
||
export default router; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import express from 'express'; | ||
import unitController from '../controllers/unit.controller.js'; | ||
|
||
const unitRouter = express.Router() | ||
|
||
unitRouter.get('/findAllUnit', unitController.findAllUnit); | ||
unitRouter.post('/createUnit',unitController.createUnit); | ||
unitRouter.patch('/updateUnit/:id', unitController.updateUnit); | ||
unitRouter.delete('/deleteUnit/:id',unitController.deleteUnit); | ||
|
||
export default unitRouter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import express from 'express'; | ||
import unitUserController from '../controllers/unitUser.controller.js'; | ||
|
||
const unitUserRouter = express.Router() | ||
|
||
unitUserRouter.get('/findAllUnitUser', unitUserController.findAllUnitUser); | ||
unitUserRouter.post('/createUnitUser',unitUserController.createUnitUser); | ||
unitUserRouter.get('/findByUserId/:id', unitUserController.findByUserIdUnitUser); | ||
unitUserRouter.delete('/deleteUnitUser/:id',unitUserController.deleteUnitUser); | ||
|
||
export default unitUserRouter; |
Oops, something went wrong.