Skip to content

Commit

Permalink
feat: adicionando gerador de codigo aleatorio
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielRogs committed Aug 24, 2024
1 parent c106bb2 commit fd9b6e8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
50 changes: 48 additions & 2 deletions riso_backend/src/controllers/unit.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ const findAllUnit = async (req, res) => {
}
}

const findByCodeUnit = async (req, res) => {
try {
const { code } = req.params;
const unit = await unitService.findByCode(code);
if (!unit) {
return res.status(404).send({ message: 'Unit not found' });
}
return res.status(200).send(unit);
} catch (err) {
return res.status(500).send({ message: err.message });
}
};

const createUnit = async (req, res) => {
try{
const {
Expand All @@ -22,14 +35,46 @@ const createUnit = async (req, res) => {
cnpj,
state,
street,
complement
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);
// Função para gerar um código aleatório de 5 dígitos alfanuméricos
const generateCode = () => {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < 5; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
};

// Função para gerar um código único, garantindo que ele não se repita
const generateUniqueCode = async () => {
let unique = false;
let newCode;
while (!unique) {
newCode = generateCode();
const existingUnit = await unitService.findByCode(newCode);
if (!existingUnit) {
unique = true;
}
}
return newCode;
};

// Se o código não for passado no body, geramos um automaticamente
const generatedCode = await generateUniqueCode();

const unitData = {
...req.body,
code: generatedCode
};

const unit = await unitService.createService(unitData);

return res.status(200).send({message: 'Unity and company successfully created ', unit:unit});
}catch (err){
Expand Down Expand Up @@ -83,6 +128,7 @@ const deleteUnit = async (req,res) => {

export default {
findAllUnit,
findByCodeUnit,
createUnit,
updateUnit,
deleteUnit,
Expand Down
5 changes: 5 additions & 0 deletions riso_backend/src/models/Unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ const UnitSchema = new mongoose.Schema({
number: {
type: Number,
required: false
},
code: {
type: String,
required: true,
unique: true,
}
}, {timestamps:true});

Expand Down
1 change: 1 addition & 0 deletions riso_backend/src/routes/unit.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import unitController from '../controllers/unit.controller.js';
const unitRouter = express.Router()

unitRouter.get('/findAllUnit', unitController.findAllUnit);
unitRouter.get('/findByCode/:code', unitController.findByCodeUnit);
unitRouter.post('/createUnit',unitController.createUnit);
unitRouter.patch('/updateUnit/:id', unitController.updateUnit);
unitRouter.delete('/deleteUnit/:id',unitController.deleteUnit);
Expand Down
2 changes: 2 additions & 0 deletions riso_backend/src/services/unit.service.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import Unit from "../models/Unit.js";

const findAllService = () => Unit.find();
const findByCode = (code) => Unit.findOne({ code });
const createService = (body) => Unit.create(body);
const updateService = (params, body) => Unit.updateOne(params, body, {new: true});
const deleteService = (params) => Unit.deleteOne(params);

export default {
findAllService,
findByCode,
createService,
updateService,
deleteService,
Expand Down
2 changes: 1 addition & 1 deletion riso_backend/src/services/unitUser.service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import UnitUser from "../models/UnitUser.js";

const findAllService = () => UnitUser.find();
const createService = (body) => UnitUser.create(body);
const createService = (params, body) => UnitUser.create(params, body);
const findByUserIdService = (body) => UnitUser.find(body);
const deleteService = (params) => UnitUser.deleteOne(params);

Expand Down

0 comments on commit fd9b6e8

Please sign in to comment.