serverest 2.26.0 → 2.26.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +11 -1
- package/package.json +2 -2
- package/src/controllers/carrinhos-controller.js +22 -33
- package/src/middlewares/error-handler.js +6 -2
- package/src/services/carrinhos-service.js +29 -2
- package/src/services/produtos-service.js +22 -0
- package/src/utils/constants.js +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [2.26.1](https://github.com/ServeRest/ServeRest/compare/v2.26.0...v2.26.1) (2022-07-08)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* show user that request ended with timeout ([1f432c4](https://github.com/ServeRest/ServeRest/commit/1f432c429647c871b2075dc8a4259ab52e714e04))
|
|
9
|
+
|
|
3
10
|
# [2.26.0](https://github.com/ServeRest/ServeRest/compare/v2.25.4...v2.26.0) (2022-06-19)
|
|
4
11
|
|
|
5
12
|
|
package/README.md
CHANGED
|
@@ -178,7 +178,15 @@ Sua empresa usa o ServeRest? Pergunte ao seu gerente ou equipe de marketing se s
|
|
|
178
178
|
1. Subdomínio próprio (_nome-escolhido.serverest.dev_)
|
|
179
179
|
1. Acesso a todas as requests e respostas feitas nos últimos 7 dias no subdomínio
|
|
180
180
|
|
|
181
|
-
[](https://opencollective.com/serverest)
|
|
182
|
+
|
|
183
|
+
Empresas que apoiam o ServeRest:
|
|
184
|
+
|
|
185
|
+
<p align="center">
|
|
186
|
+
<img alt="Logo da EBAC" src="https://user-images.githubusercontent.com/29241659/177436481-2a6a3324-1b0e-4d28-8a40-d885f54291c0.png#gh-light-mode-only" height="120">
|
|
187
|
+
<img alt="Logo da EBAC" src="https://user-images.githubusercontent.com/29241659/177436489-5d2f50f8-2fb3-4091-b822-446d24c83722.png#gh-dark-mode-only" height="120">
|
|
188
|
+
<img alt="Logo da Agilizei" src="https://user-images.githubusercontent.com/29241659/177436678-8187f90f-bb4a-4978-87ab-a03f2f80820f.png" height="124">
|
|
189
|
+
</p>
|
|
182
190
|
|
|
183
191
|
### Individuais
|
|
184
192
|
|
|
@@ -210,6 +218,8 @@ Veja aqui [como você pode contribuir](https://github.com/ServeRest/ServeRest/bl
|
|
|
210
218
|
<td align="center"><a href="https://about.me/rustnnes"><img src="https://avatars1.githubusercontent.com/u/638445?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Diego Bandeira</b></sub></a><br /><a href="#infra-rustnnes" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a></td>
|
|
211
219
|
<td align="center"><a href="https://github.com/maximilianoalves"><img src="https://avatars3.githubusercontent.com/u/11561118?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Maximiliano Alves</b></sub></a><br /><a href="#talk-maximilianoalves" title="Talks">📢</a></td>
|
|
212
220
|
<td align="center"><a href="https://github.com/murilomaiaa"><img src="https://avatars.githubusercontent.com/u/56596799?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Murilo Maia</b></sub></a><br /><a href="https://github.com/ServeRest/ServeRest/commits?author=murilomaiaa" title="Code">💻</a></td>
|
|
221
|
+
<td align="center"><a href="https://github.com/crisnazario"><img src="https://avatars.githubusercontent.com/u/37200398?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Cristina Nazário</b></sub></a><br /><a href="#ideas-crisnazario" title="Ideas, Planning, & Feedback">🤔</a></td>
|
|
222
|
+
<td align="center"><a href="http://www.eduardosantos.dev"><img src="https://avatars.githubusercontent.com/u/10568807?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Eduardo Santos</b></sub></a><br /><a href="https://github.com/ServeRest/ServeRest/commits?author=edumaxsantos" title="Code">💻</a></td>
|
|
213
223
|
</tr>
|
|
214
224
|
</table>
|
|
215
225
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "serverest",
|
|
3
|
-
"version": "2.26.
|
|
3
|
+
"version": "2.26.1",
|
|
4
4
|
"description": "Servidor REST local de forma rápida e simples para estudo de testes de API",
|
|
5
5
|
"author": "Paulo Gonçalves <paulorochag@hotmail.com> (https://www.linkedin.com/in/paulo-goncalves/)",
|
|
6
6
|
"license": "GPL-3.0",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"dev": "nodemon --legacy-watch -e json,js ./src/server.js --nodoc",
|
|
35
35
|
"test:mutation": "stryker run ./test/stryker.conf.js",
|
|
36
36
|
"test:mutation:diff": "stryker-diff-runner --path ./test/stryker.conf.js --branch trunk",
|
|
37
|
-
"test:unit": "nyc --report-dir ./coverage-unit mocha --config test/unit/.mocharc.js",
|
|
37
|
+
"test:unit": "nyc --report-dir ./coverage-unit --check-coverage false mocha --config test/unit/.mocharc.js",
|
|
38
38
|
"test:integration": "nyc --report-dir ./coverage-integration mocha --config test/integration/.mocharc.js",
|
|
39
39
|
"test:e2e": "mocha --config test/integration/.mocharc.js --fgrep @skipE2E --invert",
|
|
40
40
|
"test:smoke": "mocha --config test/integration/.mocharc.js --grep @smokeE2E",
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const authService = require('../services/auth-service')
|
|
4
3
|
const constant = require('../utils/constants')
|
|
5
4
|
const produtosService = require('../services/produtos-service')
|
|
6
5
|
const service = require('../services/carrinhos-service')
|
|
7
|
-
const usuariosService = require('../services/usuarios-service')
|
|
8
6
|
|
|
9
7
|
exports.get = async (req, res) => {
|
|
10
8
|
const carrinhos = await service.getAll(req.query)
|
|
@@ -21,53 +19,40 @@ exports.getOne = async (req, res) => {
|
|
|
21
19
|
}
|
|
22
20
|
|
|
23
21
|
exports.post = async (req, res) => {
|
|
24
|
-
const {
|
|
25
|
-
|
|
26
|
-
const usuarioJaPossuiCarrinho = await service.existeCarrinho({ idUsuario: _id })
|
|
27
|
-
if (usuarioJaPossuiCarrinho) {
|
|
22
|
+
const { idUsuario, possuiCarrinho } = await service.usuarioJaPossuiCarrinho(req.headers.authorization)
|
|
23
|
+
if (possuiCarrinho) {
|
|
28
24
|
return res.status(400).send({ message: constant.LIMITE_1_CARRINHO })
|
|
29
25
|
}
|
|
30
26
|
|
|
31
27
|
const idProdutosDuplicados = service.extrairProdutosDuplicados(req.body.produtos)
|
|
32
|
-
const temProdutosDuplicados =
|
|
28
|
+
const temProdutosDuplicados = isNotUndefined(idProdutosDuplicados[0])
|
|
33
29
|
if (temProdutosDuplicados) {
|
|
34
30
|
return res.status(400).send({ message: constant.CARRINHO_COM_PRODUTO_DUPLICADO, idProdutosDuplicados })
|
|
35
31
|
}
|
|
36
32
|
|
|
37
|
-
const produtos = req.body
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const {
|
|
41
|
-
if (
|
|
42
|
-
|
|
33
|
+
const { produtos } = req.body
|
|
34
|
+
const produtosComPrecoUnitario = []
|
|
35
|
+
for (const produto of produtos) {
|
|
36
|
+
const { precoUnitario: preco, error } = await produtosService.getPrecoUnitarioOuErro(produto)
|
|
37
|
+
if (error) {
|
|
38
|
+
const index = produtos.indexOf(produto)
|
|
39
|
+
const item = { ...error.item, index }
|
|
40
|
+
return res.status(error.statusCode).send({ message: error.message, item })
|
|
43
41
|
}
|
|
44
|
-
|
|
45
|
-
const { quantidade: quantidadeEstoque, preco } = await produtosService.getDadosDoProduto({ _id: idProduto })
|
|
46
|
-
if (quantidade > quantidadeEstoque) {
|
|
47
|
-
return res.status(400).send({ message: constant.ESTOQUE_INSUFICIENTE, item: { index, idProduto, quantidade, quantidadeEstoque } })
|
|
48
|
-
}
|
|
49
|
-
Object.assign(produtos[index], { precoUnitario: preco })
|
|
50
|
-
}
|
|
51
|
-
let precoTotal = 0
|
|
52
|
-
let quantidadeTotal = 0
|
|
53
|
-
for (let index = 0; index < produtos.length; index++) {
|
|
54
|
-
const { idProduto, quantidade } = produtos[index]
|
|
55
|
-
const { quantidade: quantidadeEmEstoque, preco } = await produtosService.getDadosDoProduto({ _id: idProduto })
|
|
56
|
-
const novaQuantidade = quantidadeEmEstoque - quantidade
|
|
57
|
-
await produtosService.updateById(idProduto, { $set: { quantidade: novaQuantidade } })
|
|
58
|
-
precoTotal += preco * quantidade
|
|
59
|
-
quantidadeTotal += quantidade
|
|
42
|
+
produtosComPrecoUnitario.push({ ...produto, precoUnitario: preco })
|
|
60
43
|
}
|
|
44
|
+
const precoTotal = await service.precoTotal(produtosComPrecoUnitario)
|
|
45
|
+
const quantidadeTotal = await service.quantidadeTotal(produtosComPrecoUnitario)
|
|
61
46
|
|
|
62
|
-
|
|
47
|
+
const carrinho = { produtos: produtosComPrecoUnitario, precoTotal, quantidadeTotal, idUsuario }
|
|
63
48
|
|
|
64
|
-
const dadosCadastrados = await service.criarCarrinho(
|
|
49
|
+
const dadosCadastrados = await service.criarCarrinho(carrinho)
|
|
65
50
|
res.status(201).send({ message: constant.POST_SUCESS, _id: dadosCadastrados._id })
|
|
66
51
|
}
|
|
67
52
|
|
|
68
53
|
exports.cancelarCompra = async (req, res) => {
|
|
69
54
|
const carrinhoDoUsuario = await service.getCarrinhoDoUsuario(req.headers.authorization)
|
|
70
|
-
const usuarioTemCarrinho =
|
|
55
|
+
const usuarioTemCarrinho = isNotUndefined(carrinhoDoUsuario[0])
|
|
71
56
|
|
|
72
57
|
if (usuarioTemCarrinho) {
|
|
73
58
|
const produtos = carrinhoDoUsuario[0].produtos
|
|
@@ -87,10 +72,14 @@ exports.cancelarCompra = async (req, res) => {
|
|
|
87
72
|
|
|
88
73
|
exports.concluirCompra = async (req, res) => {
|
|
89
74
|
const carrinhoDoUsuario = await service.getCarrinhoDoUsuario(req.headers.authorization)
|
|
90
|
-
const usuarioTemCarrinho =
|
|
75
|
+
const usuarioTemCarrinho = isNotUndefined(carrinhoDoUsuario[0])
|
|
91
76
|
if (usuarioTemCarrinho) {
|
|
92
77
|
await service.deleteById(carrinhoDoUsuario[0]._id)
|
|
93
78
|
return res.status(200).send({ message: constant.DELETE_SUCESS })
|
|
94
79
|
}
|
|
95
80
|
res.status(200).send({ message: constant.SEM_CARRINHO })
|
|
96
81
|
}
|
|
82
|
+
|
|
83
|
+
const isUndefined = (object) => typeof object === 'undefined'
|
|
84
|
+
|
|
85
|
+
const isNotUndefined = (object) => !isUndefined(object)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { INTERNAL_ERROR } = require('../utils/constants')
|
|
1
|
+
const { INTERNAL_ERROR, TIMEOUT } = require('../utils/constants')
|
|
2
2
|
const montarMensagemDeErroDeSchema = require('../utils/montarMensagemDeErroDeSchema')
|
|
3
3
|
|
|
4
4
|
function errorHandler (error, _req, res, _next) {
|
|
@@ -10,9 +10,13 @@ function errorHandler (error, _req, res, _next) {
|
|
|
10
10
|
if (error.type === 'entity.parse.failed') {
|
|
11
11
|
console.log('Erro 500')
|
|
12
12
|
return res.status(500).json({
|
|
13
|
-
message: 'Adicione aspas em todos os valores.
|
|
13
|
+
message: 'Adicione aspas em todos os valores. Para mais informações acesse a issue https://github.com/ServeRest/ServeRest/issues/225'
|
|
14
14
|
})
|
|
15
15
|
}
|
|
16
|
+
/* istanbul ignore if */
|
|
17
|
+
if (error.code === 'ETIMEDOUT') {
|
|
18
|
+
return res.status(408).json({ message: TIMEOUT })
|
|
19
|
+
}
|
|
16
20
|
return res.status(500).json({ message: INTERNAL_ERROR, error })
|
|
17
21
|
}
|
|
18
22
|
|
|
@@ -6,6 +6,7 @@ const { join } = require('path')
|
|
|
6
6
|
const alterarValoresParaRegex = require('../utils/alterarValoresParaRegex')
|
|
7
7
|
const authService = require('../services/auth-service')
|
|
8
8
|
const usuariosService = require('../services/usuarios-service')
|
|
9
|
+
const produtosService = require('../services/produtos-service')
|
|
9
10
|
|
|
10
11
|
const datastore = Datastore.create({ filename: join(__dirname, '../data/carrinhos.db'), autoload: true })
|
|
11
12
|
|
|
@@ -42,7 +43,33 @@ exports.deleteById = async id => {
|
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
exports.getCarrinhoDoUsuario = async (authorization) => {
|
|
46
|
+
const _id = await idUsuario(authorization)
|
|
47
|
+
return this.getAll({ idUsuario: _id })
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
exports.usuarioJaPossuiCarrinho = async (authorization) => {
|
|
51
|
+
const _id = await idUsuario(authorization)
|
|
52
|
+
return {
|
|
53
|
+
idUsuario: _id,
|
|
54
|
+
possuiCarrinho: await this.existeCarrinho({ idUsuario: _id })
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
exports.precoTotal = async (produtos) => {
|
|
59
|
+
return produtos.reduce(async (precoAnterior, produto) => {
|
|
60
|
+
return (await precoAnterior) + produto.precoUnitario * produto.quantidade
|
|
61
|
+
}, Promise.resolve(0))
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
exports.quantidadeTotal = async (produtos) => {
|
|
65
|
+
return produtos.reduce(async (quantidadeAnterior, produto) => {
|
|
66
|
+
await produtosService.updateQuantidade(produto)
|
|
67
|
+
return (await quantidadeAnterior) + produto.quantidade
|
|
68
|
+
}, Promise.resolve(0))
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const idUsuario = async (authorization) => {
|
|
45
72
|
const { email, password } = authService.verifyToken(authorization)
|
|
46
|
-
const { _id
|
|
47
|
-
return
|
|
73
|
+
const { _id } = await usuariosService.getDadosDoUsuario({ email, password })
|
|
74
|
+
return _id
|
|
48
75
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
const constant = require('../utils/constants')
|
|
3
4
|
const Datastore = require('nedb-promises')
|
|
4
5
|
const { join } = require('path')
|
|
5
6
|
|
|
@@ -24,11 +25,32 @@ exports.existeProduto = pesquisa => {
|
|
|
24
25
|
return datastore.count(pesquisa)
|
|
25
26
|
}
|
|
26
27
|
|
|
28
|
+
exports.updateQuantidade = async ({ idProduto, quantidade }) => {
|
|
29
|
+
const { quantidade: quantidadeEmEstoque } = await this.getDadosDoProduto({ _id: idProduto })
|
|
30
|
+
const novaQuantidade = quantidadeEmEstoque - quantidade
|
|
31
|
+
await this.updateById(idProduto, { $set: { quantidade: novaQuantidade } })
|
|
32
|
+
}
|
|
33
|
+
|
|
27
34
|
exports.criarProduto = async body => {
|
|
28
35
|
body = formatarValores(body)
|
|
29
36
|
return datastore.insert(body)
|
|
30
37
|
}
|
|
31
38
|
|
|
39
|
+
exports.getPrecoUnitarioOuErro = async (produto) => {
|
|
40
|
+
const quantidade = parseInt(produto.quantidade)
|
|
41
|
+
const { idProduto } = produto
|
|
42
|
+
const existePedido = await this.existeProduto({ _id: idProduto })
|
|
43
|
+
if (!existePedido) {
|
|
44
|
+
return { error: { statusCode: 400, message: constant.IDPRODUTO_INVALIDO, item: { idProduto, quantidade } } }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const { quantidade: quantidadeEstoque, preco } = await this.getDadosDoProduto({ _id: idProduto })
|
|
48
|
+
if (quantidade > quantidadeEstoque) {
|
|
49
|
+
return { error: { statusCode: 400, message: constant.ESTOQUE_INSUFICIENTE, item: { idProduto, quantidade, quantidadeEstoque } } }
|
|
50
|
+
}
|
|
51
|
+
return { precoUnitario: preco }
|
|
52
|
+
}
|
|
53
|
+
|
|
32
54
|
exports.deleteById = async id => {
|
|
33
55
|
return datastore.remove({ _id: id }, {})
|
|
34
56
|
}
|
package/src/utils/constants.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
module.exports = {
|
|
4
4
|
INTERNAL_ERROR: 'Abra uma issue informando essa resposta. https://github.com/ServeRest/ServeRest/issues',
|
|
5
|
+
TIMEOUT: 'Timeout da requisição atingido',
|
|
5
6
|
POST_SUCESS: 'Cadastro realizado com sucesso',
|
|
6
7
|
DELETE_SUCESS: 'Registro excluído com sucesso',
|
|
7
8
|
DELETE_NONE: 'Nenhum registro excluído',
|