serverest 2.26.4 → 2.26.6

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,42 @@
1
1
  # Changelog
2
2
 
3
+ ## [2.26.6](https://github.com/ServeRest/ServeRest/compare/v2.26.5...v2.26.6) (2022-10-28)
4
+
5
+
6
+ ### Chores
7
+
8
+ * **patch:** add new sponsor Cesar School ([fc0df2f](https://github.com/ServeRest/ServeRest/commit/fc0df2feacb529d618609a43b2cf941c85a97fdb))
9
+
10
+
11
+ ### Continuous Integration
12
+
13
+ * avoid concurrency while creating release and deploying ([4da150c](https://github.com/ServeRest/ServeRest/commit/4da150ca545a2cbe0032504e391f704141df38ac))
14
+ * bump actions version to avoid deprecated node version ([9dbd693](https://github.com/ServeRest/ServeRest/commit/9dbd693bd353f163bd96e548183cfa20ee26434b))
15
+
16
+ ## [2.26.5](https://github.com/ServeRest/ServeRest/compare/v2.26.4...v2.26.5) (2022-10-18)
17
+
18
+
19
+ ### Code Refactoring
20
+
21
+ * add concluiCompra method ([0fc3d4c](https://github.com/ServeRest/ServeRest/commit/0fc3d4c217933f1aa73669fc5274c9feaae8b28b))
22
+ * add removeCarrinho method ([8fbbb1b](https://github.com/ServeRest/ServeRest/commit/8fbbb1b28e0ca783e11bce1aff8605ff4a28dc2c))
23
+ * check for carrinhoDoUsuario length and use removeCarrinho method from carrinhosService ([e560be5](https://github.com/ServeRest/ServeRest/commit/e560be539a1a0dc08732f5f863ee912a6afb417d))
24
+ * move all produtosService calls to carrinhosService ([a756731](https://github.com/ServeRest/ServeRest/commit/a756731eb019a17eafeb88880740f345464304b1))
25
+ * move produtosService usage inside carrinhosService ([2837023](https://github.com/ServeRest/ServeRest/commit/28370239995097c4b8cd53fa55e36e17f39def93))
26
+ * remove validation of undefined for returned array ([74a71e2](https://github.com/ServeRest/ServeRest/commit/74a71e20be717e65d5d5b1d1ef6a50466836afa7))
27
+ * use concluiCompra method from carrinhosService ([f5d3bb5](https://github.com/ServeRest/ServeRest/commit/f5d3bb55730a1ab17f813edbff726472e0c33834))
28
+
29
+
30
+ ### Continuous Integration
31
+
32
+ * fix sonar and contract jobs to clone PR from Fork ([ed48fe9](https://github.com/ServeRest/ServeRest/commit/ed48fe9d6a2ede2f1c97bf4cc6833e8ec8052480))
33
+
34
+
35
+ ### Miscellaneous
36
+
37
+ * 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)
38
+ * Revert "chore(release): use default changelog content" ([d7ec542](https://github.com/ServeRest/ServeRest/commit/d7ec542c3e1b87ce03b80b1f973f8748189685d6))
39
+
3
40
  ## [2.26.4](https://github.com/ServeRest/ServeRest/compare/v2.26.3...v2.26.4) (2022-10-14)
4
41
 
5
42
  ## [2.26.3](https://github.com/ServeRest/ServeRest/compare/v2.26.2...v2.26.3) (2022-10-12)
package/README.md CHANGED
@@ -188,6 +188,7 @@ Empresas que apoiam o ServeRest:
188
188
  <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">
189
189
  <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">
190
190
  <img alt="Logo da Agilizei" src="https://user-images.githubusercontent.com/29241659/177436678-8187f90f-bb4a-4978-87ab-a03f2f80820f.png" height="124">
191
+ <img alt="Logo da Cesar School" src="https://user-images.githubusercontent.com/29241659/198725645-3e42d795-4393-4d9e-abf6-1655ecb83cfa.png" height="150">
191
192
  </p>
192
193
 
193
194
  ### Individuais
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serverest",
3
- "version": "2.26.4",
3
+ "version": "2.26.6",
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",
package/src/app.js CHANGED
@@ -68,6 +68,9 @@ switch (formaDeExecucao()) {
68
68
  case 'compassuol':
69
69
  swaggerDocument.host = 'compassuol.serverest.dev'
70
70
  break
71
+ case 'cesarschool':
72
+ swaggerDocument.host = 'cesarschool.serverest.dev'
73
+ break
71
74
  }
72
75
 
73
76
  swaggerDocument.info.version = packageJson.version
@@ -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 })
@@ -7,6 +7,7 @@ function formaDeExecucao () {
7
7
  process.env.USERNAME === 'serverest.dev' ||
8
8
  process.env.USERNAME === 'staging.serverest.dev' ||
9
9
  process.env.USERNAME === 'agilizei' ||
10
+ process.env.USERNAME === 'cesarschool' ||
10
11
  process.env.USERNAME === 'compassuol') {
11
12
  return process.env.USERNAME
12
13
  }
@@ -27,6 +28,8 @@ function urlDocumentacao () {
27
28
  return 'https://agilizei.serverest.dev'
28
29
  case 'compassuol':
29
30
  return 'https://compassuol.serverest.dev'
31
+ case 'cesarschool':
32
+ return 'https://cesarschool.serverest.dev'
30
33
  default:
31
34
  return `http://localhost:${conf.porta}`
32
35
  }