serverest 2.26.3 → 2.26.5

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 CHANGED
@@ -1,5 +1,31 @@
1
1
  # Changelog
2
2
 
3
+ ## [2.26.5](https://github.com/ServeRest/ServeRest/compare/v2.26.4...v2.26.5) (2022-10-18)
4
+
5
+
6
+ ### Code Refactoring
7
+
8
+ * add concluiCompra method ([0fc3d4c](https://github.com/ServeRest/ServeRest/commit/0fc3d4c217933f1aa73669fc5274c9feaae8b28b))
9
+ * add removeCarrinho method ([8fbbb1b](https://github.com/ServeRest/ServeRest/commit/8fbbb1b28e0ca783e11bce1aff8605ff4a28dc2c))
10
+ * check for carrinhoDoUsuario length and use removeCarrinho method from carrinhosService ([e560be5](https://github.com/ServeRest/ServeRest/commit/e560be539a1a0dc08732f5f863ee912a6afb417d))
11
+ * move all produtosService calls to carrinhosService ([a756731](https://github.com/ServeRest/ServeRest/commit/a756731eb019a17eafeb88880740f345464304b1))
12
+ * move produtosService usage inside carrinhosService ([2837023](https://github.com/ServeRest/ServeRest/commit/28370239995097c4b8cd53fa55e36e17f39def93))
13
+ * remove validation of undefined for returned array ([74a71e2](https://github.com/ServeRest/ServeRest/commit/74a71e20be717e65d5d5b1d1ef6a50466836afa7))
14
+ * use concluiCompra method from carrinhosService ([f5d3bb5](https://github.com/ServeRest/ServeRest/commit/f5d3bb55730a1ab17f813edbff726472e0c33834))
15
+
16
+
17
+ ### Continuous Integration
18
+
19
+ * fix sonar and contract jobs to clone PR from Fork ([ed48fe9](https://github.com/ServeRest/ServeRest/commit/ed48fe9d6a2ede2f1c97bf4cc6833e8ec8052480))
20
+
21
+
22
+ ### Miscellaneous
23
+
24
+ * Merge pull request #378 from edumaxsantos/refactoring/carrinhos-controller ([325cdf7](https://github.com/ServeRest/ServeRest/commit/325cdf79d16647b6989313439648a269fc389234)), closes [#378](https://github.com/ServeRest/ServeRest/issues/378)
25
+ * Revert "chore(release): use default changelog content" ([d7ec542](https://github.com/ServeRest/ServeRest/commit/d7ec542c3e1b87ce03b80b1f973f8748189685d6))
26
+
27
+ ## [2.26.4](https://github.com/ServeRest/ServeRest/compare/v2.26.3...v2.26.4) (2022-10-14)
28
+
3
29
  ## [2.26.3](https://github.com/ServeRest/ServeRest/compare/v2.26.2...v2.26.3) (2022-10-12)
4
30
 
5
31
  ## [2.26.2](https://github.com/ServeRest/ServeRest/compare/v2.26.1...v2.26.2) (2022-08-18)
package/docs/swagger.json CHANGED
@@ -998,7 +998,7 @@
998
998
  "type": "number",
999
999
  "example": 2
1000
1000
  },
1001
- "usuarios": {
1001
+ "produtos": {
1002
1002
  "type": "array",
1003
1003
  "items": {
1004
1004
  "$ref": "#/definitions/arrayProduto"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serverest",
3
- "version": "2.26.3",
3
+ "version": "2.26.5",
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",
@@ -1,7 +1,6 @@
1
1
  'use strict'
2
2
 
3
3
  const constant = require('../utils/constants')
4
- const produtosService = require('../services/produtos-service')
5
4
  const service = require('../services/carrinhos-service')
6
5
 
7
6
  exports.get = async (req, res) => {
@@ -24,22 +23,16 @@ exports.post = async (req, res) => {
24
23
  return res.status(400).send({ message: constant.LIMIT_JUST_ONE_CART })
25
24
  }
26
25
 
27
- const idProdutosDuplicados = service.extrairProdutosDuplicados(req.body.produtos)
28
- const temProdutosDuplicados = isNotUndefined(idProdutosDuplicados[0])
29
- if (temProdutosDuplicados) {
26
+ const { produtos } = req.body
27
+
28
+ const idProdutosDuplicados = service.extrairProdutosDuplicados(produtos)
29
+ if (idProdutosDuplicados.length) {
30
30
  return res.status(400).send({ message: constant.CART_WITH_DUPLICATE_PRODUCT, idProdutosDuplicados })
31
31
  }
32
32
 
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 })
41
- }
42
- produtosComPrecoUnitario.push({ ...produto, precoUnitario: preco })
33
+ const { produtosComPrecoUnitario, error } = await service.getProdutosComPrecoUnitarioOuErro(produtos)
34
+ if (error) {
35
+ return res.status(error.statusCode).send({ message: error.message, item: error.item })
43
36
  }
44
37
  const precoTotal = await service.precoTotal(produtosComPrecoUnitario)
45
38
  const quantidadeTotal = await service.quantidadeTotal(produtosComPrecoUnitario)
@@ -52,18 +45,9 @@ exports.post = async (req, res) => {
52
45
 
53
46
  exports.cancelarCompra = async (req, res) => {
54
47
  const carrinhoDoUsuario = await service.getCarrinhoDoUsuario(req.headers.authorization)
55
- const usuarioTemCarrinho = isNotUndefined(carrinhoDoUsuario[0])
56
-
57
- if (usuarioTemCarrinho) {
58
- const produtos = carrinhoDoUsuario[0].produtos
59
48
 
60
- produtos.forEach(async (produto) => {
61
- const { idProduto, quantidade } = produto
62
- const { quantidade: quantidadeEmEstoque } = await produtosService.getDadosDoProduto({ _id: idProduto })
63
- await produtosService.updateById(idProduto, { $set: { quantidade: quantidadeEmEstoque + quantidade } })
64
- })
65
-
66
- await service.deleteById(carrinhoDoUsuario[0]._id)
49
+ if (carrinhoDoUsuario.length) {
50
+ await service.removeCarrinho(carrinhoDoUsuario[0])
67
51
  return res.status(200).send({ message: `${constant.DELETE_SUCCESS}. ${constant.REPLENISHED_STOCK}` })
68
52
  }
69
53
 
@@ -72,14 +56,10 @@ exports.cancelarCompra = async (req, res) => {
72
56
 
73
57
  exports.concluirCompra = async (req, res) => {
74
58
  const carrinhoDoUsuario = await service.getCarrinhoDoUsuario(req.headers.authorization)
75
- const usuarioTemCarrinho = isNotUndefined(carrinhoDoUsuario[0])
76
- if (usuarioTemCarrinho) {
77
- await service.deleteById(carrinhoDoUsuario[0]._id)
59
+
60
+ if (carrinhoDoUsuario.length) {
61
+ await service.concluiCompra(carrinhoDoUsuario)
78
62
  return res.status(200).send({ message: constant.DELETE_SUCCESS })
79
63
  }
80
64
  res.status(200).send({ message: constant.NO_CART })
81
65
  }
82
-
83
- const isUndefined = (object) => typeof object === 'undefined'
84
-
85
- const isNotUndefined = (object) => !isUndefined(object)
@@ -38,10 +38,34 @@ exports.extrairProdutosDuplicados = arrayProdutos => {
38
38
  return produtosDuplicados
39
39
  }
40
40
 
41
+ exports.getProdutosComPrecoUnitarioOuErro = async arrayProdutos => {
42
+ const produtosComPrecoUnitario = []
43
+ for (const produto of arrayProdutos) {
44
+ const { precoUnitario: preco, error } = await produtosService.getPrecoUnitarioOuErro(produto)
45
+ if (error) {
46
+ const index = arrayProdutos.indexOf(produto)
47
+ const item = { ...error.item, index }
48
+ return { error: { ...error, item } }
49
+ }
50
+ produtosComPrecoUnitario.push({ ...produto, precoUnitario: preco })
51
+ }
52
+ return { produtosComPrecoUnitario }
53
+ }
54
+
41
55
  exports.deleteById = async id => {
42
56
  return datastore.remove({ _id: id }, {})
43
57
  }
44
58
 
59
+ exports.concluiCompra = async carrinho => {
60
+ await this.deleteById(carrinho._id)
61
+ }
62
+
63
+ exports.removeCarrinho = async carrinho => {
64
+ const { produtos, _id } = carrinho
65
+ this.reabasteceEstoque(produtos)
66
+ await this.deleteById(_id)
67
+ }
68
+
45
69
  exports.getCarrinhoDoUsuario = async (authorization) => {
46
70
  const _id = await idUsuario(authorization)
47
71
  return this.getAll({ idUsuario: _id })
@@ -68,6 +92,14 @@ exports.quantidadeTotal = async (produtos) => {
68
92
  }, Promise.resolve(0))
69
93
  }
70
94
 
95
+ exports.reabasteceEstoque = produtos => {
96
+ produtos.forEach(async (produto) => {
97
+ const { idProduto, quantidade } = produto
98
+ const { quantidade: quantidadeEmEstoque } = await produtosService.getDadosDoProduto({ _id: idProduto })
99
+ await produtosService.updateById(idProduto, { $set: { quantidade: quantidadeEmEstoque + quantidade } })
100
+ })
101
+ }
102
+
71
103
  const idUsuario = async (authorization) => {
72
104
  const { email, password } = authService.verifyToken(authorization)
73
105
  const { _id } = await usuariosService.getDadosDoUsuario({ email, password })