Skip to content

Commit

Permalink
Merge pull request #7 from fga-eps-mds/fixing-18-supplier
Browse files Browse the repository at this point in the history
fixing-18-supplier
  • Loading branch information
AlGouvea authored Aug 19, 2024
2 parents 9f23999 + 901e40c commit 29b4528
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 164 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ yarn-error.log
.idea
.vscode
.env
package-lock.json

/coverage
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@
"prettier": "^3.3.3"
},
"devDependencies": {
"nodemon": "^3.1.0"
"@eslint/js": "^9.8.0",
"eslint": "^9.8.0",
"husky": "^8.0.0",
"jest": "^29.7.0",
"jest-sonar": "^0.2.16",
"mongodb-memory-server": "^10.0.0",
"nodemon": "^3.1.0",
"prettier": "^3.3.3",
"supertest": "^7.0.0"
}
}
17 changes: 0 additions & 17 deletions src/Models/supplierFormSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ const supplierFormSchema = new mongoose.Schema({
},
cpfCnpj: {
type: String,
unique: true,
immutable: true,
sparse: true,
},
statusFornecedor: {
type: String,
Expand All @@ -26,26 +23,18 @@ const supplierFormSchema = new mongoose.Schema({
},
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,
Expand Down Expand Up @@ -97,18 +86,12 @@ const supplierFormSchema = new mongoose.Schema({
},
numeroBanco: {
type: String,
unique: true,
sparse: true,
},
dv: {
type: String,
unique: true,
sparse: true,
},
chavePix: {
type: String,
unique: true,
sparse: true,
},
createdAt: {
type: Date,
Expand Down
146 changes: 0 additions & 146 deletions src/Util/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,152 +3,6 @@ const validator = (dados) => {
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;
};

Expand Down
99 changes: 99 additions & 0 deletions src/__tests__/suppllierFormController.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const request = require("supertest");
const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");
const routes = require("../routes");
const { MongoMemoryServer } = require("mongodb-memory-server");

const app = express();
let mongoServer;

const corsOptions = {
origin: "*",
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
};

app.use(cors(corsOptions));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.use("/", routes);

beforeAll(async () => {
mongoServer = await MongoMemoryServer.create();
const uri = mongoServer.getUri();

try {
await mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
} catch (err) {
console.error("Error connecting to MongoDB:", err);

Check warning on line 32 in src/__tests__/suppllierFormController.test.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
process.exit(1);
}
}, 30000);

afterAll(async () => {
await mongoose.connection.close();
await mongoServer.stop();
}, 30000);

describe("Supplier Form Controller Tests", () => {
let supplierId;

it("should create a new supplier form", async () => {
const res = await request(app)
.post("/SupplierForm/create")
.send({
supplierData: {
nome: "Test Supplier",
email: "[email protected]",
celular: "123456789",
logradouro: "123 Supplier St",
},
});

expect(res.status).toBe(201);
expect(res.body).toHaveProperty("_id");
expect(res.body.nome).toBe("Test Supplier");

supplierId = res.body._id;
});

it("should get all supplier forms", async () => {
const res = await request(app).get("/SupplierForm");

expect(res.status).toBe(200);
expect(Array.isArray(res.body)).toBe(true);
});

it("should get a supplier form by ID", async () => {
const res = await request(app).get(`/SupplierForm/${supplierId}`);

expect(res.status).toBe(200);
expect(res.body).toHaveProperty("nome", "Test Supplier");
});

it("should update a supplier form by ID", async () => {
const res = await request(app)
.patch(`/SupplierForm/update/${supplierId}`)
.send({ supplierData: { nome: "Updated Supplier" } });

expect(res.status).toBe(200);
expect(res.body).toHaveProperty("nome", "Updated Supplier");
});

it("should delete a supplier form by ID", async () => {
const res = await request(app).delete(
`/SupplierForm/delete/${supplierId}`
);

expect(res.status).toBe(200);

const checkSupplier = await request(app).get(
`/SupplierForm/delete/${supplierId}`
);
expect(checkSupplier.status).toBe(404);
});
});

0 comments on commit 29b4528

Please sign in to comment.