Skip to content

Commit

Permalink
Merge pull request #5 from fga-eps-mds/18-supplier-form
Browse files Browse the repository at this point in the history
18 supplier form
  • Loading branch information
yukioz authored Aug 2, 2024
2 parents ddad9c3 + ebf843d commit bd3022b
Show file tree
Hide file tree
Showing 5 changed files with 307 additions and 4 deletions.
69 changes: 69 additions & 0 deletions src/Controllers/supplierFormController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const SupplierForm = require("../Models/supplierFormSchema");
const util = require("../Util/utils");

const createSupplierForm = async (req, res) => {
try {

Check failure on line 5 in src/Controllers/supplierFormController.js

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
message = util.validator(req.body.supplierData);

Check failure on line 6 in src/Controllers/supplierFormController.js

View workflow job for this annotation

GitHub Actions / lint

Insert `····`

if (message) {

Check failure on line 8 in src/Controllers/supplierFormController.js

View workflow job for this annotation

GitHub Actions / lint

Insert `····`
return res.status(404).send({ erro: message });

Check failure on line 9 in src/Controllers/supplierFormController.js

View workflow job for this annotation

GitHub Actions / lint

Replace `······` with `············`
}

Check failure on line 10 in src/Controllers/supplierFormController.js

View workflow job for this annotation

GitHub Actions / lint

Insert `····`

const supplier = new SupplierForm(req.body.supplierData);

Check failure on line 12 in src/Controllers/supplierFormController.js

View workflow job for this annotation

GitHub Actions / lint

Replace `····` with `········`

await supplier.save();

Check failure on line 14 in src/Controllers/supplierFormController.js

View workflow job for this annotation

GitHub Actions / lint

Insert `····`
return res.status(201).send(supplier);

Check failure on line 15 in src/Controllers/supplierFormController.js

View workflow job for this annotation

GitHub Actions / lint

Insert `····`
} catch (error) {

Check failure on line 16 in src/Controllers/supplierFormController.js

View workflow job for this annotation

GitHub Actions / lint

Replace `··` with `····`
return res.status(400).send(error);

Check failure on line 17 in src/Controllers/supplierFormController.js

View workflow job for this annotation

GitHub Actions / lint

Insert `····`
}
};

const getSupplierForm = async (req, res) => {
try {
const supplier = await SupplierForm.find({});
return res.status(200).send(supplier);
} catch (error) {
return res.status(400).send(error);
}
};

const getSupplierFormById = async (req, res) => {
try {
const supplier = await SupplierForm.findById(req.params.id);
return res.status(200).send(supplier);
} catch (error) {
return res.status(400).send(error);
}
};

const deleteSupplierFormById = async (req, res) => {
try {
const deletedSupplier = await SupplierForm.findByIdAndDelete(req.params.id);
return res.status(200).send(deletedSupplier);
} catch (error) {
return res.status(400).send(error);
}
};

const updateSupplierFormById = async (req, res) => {
try {
const updatedSupplier = await SupplierForm.findById(req.params.id);
if (!updatedSupplier) {
return res.status(404).send();
}
Object.assign(updatedSupplier, req.body.supplierData);
updatedSupplier.updatedAt = new Date();
await updatedSupplier.save();
return res.status(200).send(updatedSupplier);
} catch (error) {
return res.status(400).send(error);
}
};

module.exports = {
createSupplierForm,
getSupplierForm,
getSupplierFormById,
deleteSupplierFormById,
updateSupplierFormById,
};
96 changes: 96 additions & 0 deletions src/Models/supplierFormSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const mongoose = require('mongoose');

const supplierFormSchema = new mongoose.Schema({

nome: {
type: String,
required: true,
unique: true
},
tipoPessoa: {
type: String,
enum: ['Jurídica', 'Física', '']
},
cpfCnpj: {
type: String,
unique: true,
immutable: true,
sparse: true
},
statusFornecedor: {
type: String,
enum: ['Ativo', 'Inativo', '']
},
naturezaTransacao: {
type: String,
enum: ['Receita', 'Despesa', '']
},
email: {
type: String,
unique: true,
sparse:true
},
nomeContato: {
type: String
},
celular: {
type: String,
unique: true,
sparse:true
},
telefone: {
type: String,
unique: true,
sparse:true
},
cep: {
type: String,
unique: true,
sparse:true
},
cidade: {
type: String
},
uf_endereco: {
type: String,
enum: ['AC', 'AL', 'AP', 'AM', 'BA', 'CE', 'DF', 'ES', 'GO', 'MA', 'MT', 'MS', 'MG', 'PA', 'PB', 'PR', 'PE', 'PI', 'RJ', 'RN', 'RS', 'RO', 'RR', 'SC', 'SP', 'SE', 'TO', '']
},
logradouro: {
type: String
},
complemento: {
type: String
},
nomeBanco: {
type: String
},
agencia: {
type: String
},
numeroBanco: {
type: String,
unique: true,
sparse: true
},
dv: {
type: String,
unique: true,
sparse: true
},
chavePix: {
type: String,
unique: true,
sparse: true
},
createdAt: {
type: Date,
default: Date.now
},
updatedAt: {
type: Date,
default: Date.now
},
})

const supplierForm = mongoose.model('Supplier', supplierFormSchema);
module.exports = supplierForm;
130 changes: 130 additions & 0 deletions src/Util/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
const validator = (dados) => {

if (typeof dados.nome !== "string" || dados.nome === "") {
return "Nome ou Razão social inválidos";
}

if (dados.tipoPessoa !== null) {
const tipoPessoaValidas = ["Jurídica", "Física"];
if (!tipoPessoaValidas.includes(dados.tipoPessoa)) {
return "Tipo de pessoa inválida";
}
}

if (dados.cpfCnpj !== null) {
const cpfValido = /^(\d{3}\.\d{3}\.\d{3}-\d{2})$/;
const cnpjValido = /^(\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2})$/;
if (
(!cpfValido.test(dados.cpfCnpj) && dados.tipoPessoa === "Física") ||
(!cnpjValido.test(dados.cpfCnpj) && dados.tipoPessoa === "Jurídica")
) {
return "CPF ou CNPJ inválido";
}
}

if (dados.statusFornecedor !== null) {
const statusFornecedorValido = ["Ativo", "Inativo"];
if (!statusFornecedorValido.includes(dados.statusFornecedor)) {
return "Status de fornecedor inválido";
}
}

if (dados.naturezaTransacao !== null) {
const tipoTransacaoValida = ["Receita", "Despesa"];
if (!tipoTransacaoValida.includes(dados.naturezaTransacao)) {
return "Tipo de transação inválida";
}
}

if (dados.email !== null) {
const emailValido = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailValido.test(dados.email)) {
return "E-mail inválido";
}
}

if (dados.nomeContato !== null) {
if (typeof dados.nomeContato !== "string") {
return "Nome de contato inválido";
}
}

if (dados.celular !== null) {
const celularValido = /^\(\d{2}\) \d{5}-\d{4}$/;
if (!celularValido.test(dados.celular)) {
return "Número de celular inválido";
}
}

if (dados.telefone !== null) {
const telefoneValido = /^\(\d{2}\) \d{4}-\d{4}$/;
if (!telefoneValido.test(dados.telefone)) {
return "Número de telefone inválido";
}
}

if (dados.cep !== null) {
const cepValido = /^\d{5}-\d{3}$/;
if (!cepValido.test(dados.cep)) {
return "Cep inválido";
}
}

if (dados.cidade !== null) {
if (typeof dados.cidade !== "string") {
return "Cidade inválida";
}
}

if (dados.uf_endereco !== null) {
const ufsValidos = ["AC", "AL", "AP", "AM", "BA", "CE", "DF", "ES", "GO", "MA", "MT", "MS", "MG", "PA", "PB", "PR", "PE", "PI", "RJ", "RN", "RS", "RO", "RR", "SC", "SP", "SE", "TO"];
if (!ufsValidos.includes(dados.uf_endereco)) {
return "UF inválida";
}
}

if (dados.logradouro !== null) {
const logradouro = /^[a-zA-Z0-9\s,.()-]{5,100}$/;
if (!logradouro.test(dados.logradouro)) {
return "Logradouro inválido. Deve conter entre 5 e 100 caracteres.";
}
}

if (dados.complemento !== null) {
if (typeof dados.complemento !== "string") {
return "Complemento inválido";
}
}

if (dados.agencia !== null) {
if (typeof dados.agencia !== "string") {
return "Agência inválida";
}
}

if (dados.numeroBanco !== null) {
const numeroValido = /^\d*$/;
if (!numeroValido.test(dados.numeroBanco)) {
return "Número inválido";
}
}

if (dados.dv !== null) {
const dvValido = /^\d*$/;
if (!dvValido.test(dados.dv)) {
return "DV inválido";
}
}

if (dados.chavePix !== null) {
if (typeof dados.chavePix !== "string") {
return "Chave Pix inválida";
}
}

return null;
};

module.exports = {
validator,
};
7 changes: 3 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ const {
} = process.env;

const corsOption = {
origin: ["http://localhost:5173"],
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
credentials: true,
};
origin: "*",
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE'
}

// Aplicar o middleware CORS antes das rotas
app.use(cors(corsOption));
Expand Down
9 changes: 9 additions & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
const express = require("express");
const routes = express.Router();
<<<<<<< HEAD
const supplierFormController = require('./Controllers/supplierFormController');
=======
const NewController = require("./Controllers/newController");
>>>>>>> devel

// Private
// routes.get('/finance', tokenValidation, ???.getUsers);
// routes.get('/finance/:id', tokenValidation, ???.getUserById);
routes.post('/SupplierForm/create', supplierFormController.createSupplierForm);
routes.get('/SupplierForm', supplierFormController.getSupplierForm);
routes.get('/SupplierForm/:id', supplierFormController.getSupplierFormById);
routes.delete('/SupplierForm/delete/:id', supplierFormController.deleteSupplierFormById);
routes.patch('/SupplierForm/update/:id', supplierFormController.updateSupplierFormById);

// Public
routes.post("/finance/create", NewController.createNew);
Expand Down

0 comments on commit bd3022b

Please sign in to comment.