serverest 2.26.1 → 2.26.2

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,7 @@
1
1
  # Changelog
2
2
 
3
+ ## [2.26.2](https://github.com/ServeRest/ServeRest/compare/v2.26.1...v2.26.2) (2022-08-18)
4
+
3
5
  ## [2.26.1](https://github.com/ServeRest/ServeRest/compare/v2.26.0...v2.26.1) (2022-07-08)
4
6
 
5
7
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serverest",
3
- "version": "2.26.1",
3
+ "version": "2.26.2",
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",
@@ -13,7 +13,7 @@ exports.getOne = async (req, res) => {
13
13
  const { id } = req.params
14
14
  const carrinho = await service.getOne(id)
15
15
  if (!carrinho) {
16
- return res.status(400).json({ message: constant.CARRINHO_NAO_ENCONTRADO })
16
+ return res.status(400).json({ message: constant.CART_NOT_FOUND })
17
17
  }
18
18
  return res.status(200).send(carrinho)
19
19
  }
@@ -21,13 +21,13 @@ exports.getOne = async (req, res) => {
21
21
  exports.post = async (req, res) => {
22
22
  const { idUsuario, possuiCarrinho } = await service.usuarioJaPossuiCarrinho(req.headers.authorization)
23
23
  if (possuiCarrinho) {
24
- return res.status(400).send({ message: constant.LIMITE_1_CARRINHO })
24
+ return res.status(400).send({ message: constant.LIMIT_JUST_ONE_CART })
25
25
  }
26
26
 
27
27
  const idProdutosDuplicados = service.extrairProdutosDuplicados(req.body.produtos)
28
28
  const temProdutosDuplicados = isNotUndefined(idProdutosDuplicados[0])
29
29
  if (temProdutosDuplicados) {
30
- return res.status(400).send({ message: constant.CARRINHO_COM_PRODUTO_DUPLICADO, idProdutosDuplicados })
30
+ return res.status(400).send({ message: constant.CART_WITH_DUPLICATE_PRODUCT, idProdutosDuplicados })
31
31
  }
32
32
 
33
33
  const { produtos } = req.body
@@ -47,7 +47,7 @@ exports.post = async (req, res) => {
47
47
  const carrinho = { produtos: produtosComPrecoUnitario, precoTotal, quantidadeTotal, idUsuario }
48
48
 
49
49
  const dadosCadastrados = await service.criarCarrinho(carrinho)
50
- res.status(201).send({ message: constant.POST_SUCESS, _id: dadosCadastrados._id })
50
+ res.status(201).send({ message: constant.POST_SUCCESS, _id: dadosCadastrados._id })
51
51
  }
52
52
 
53
53
  exports.cancelarCompra = async (req, res) => {
@@ -64,10 +64,10 @@ exports.cancelarCompra = async (req, res) => {
64
64
  })
65
65
 
66
66
  await service.deleteById(carrinhoDoUsuario[0]._id)
67
- return res.status(200).send({ message: `${constant.DELETE_SUCESS}. ${constant.ESTOQUE_REABASTECIDO}` })
67
+ return res.status(200).send({ message: `${constant.DELETE_SUCCESS}. ${constant.REPLENISHED_STOCK}` })
68
68
  }
69
69
 
70
- res.status(200).send({ message: constant.SEM_CARRINHO })
70
+ res.status(200).send({ message: constant.NO_CART })
71
71
  }
72
72
 
73
73
  exports.concluirCompra = async (req, res) => {
@@ -75,9 +75,9 @@ exports.concluirCompra = async (req, res) => {
75
75
  const usuarioTemCarrinho = isNotUndefined(carrinhoDoUsuario[0])
76
76
  if (usuarioTemCarrinho) {
77
77
  await service.deleteById(carrinhoDoUsuario[0]._id)
78
- return res.status(200).send({ message: constant.DELETE_SUCESS })
78
+ return res.status(200).send({ message: constant.DELETE_SUCCESS })
79
79
  }
80
- res.status(200).send({ message: constant.SEM_CARRINHO })
80
+ res.status(200).send({ message: constant.NO_CART })
81
81
  }
82
82
 
83
83
  const isUndefined = (object) => typeof object === 'undefined'
@@ -10,7 +10,7 @@ exports.post = async (req, res) => {
10
10
  const token = authService.createToken(req.body)
11
11
  /* istanbul ignore next */
12
12
  const authorization = conf.semBearer ? token : `Bearer ${token}`
13
- return res.status(200).send({ message: constant.LOGIN_SUCESS, authorization })
13
+ return res.status(200).send({ message: constant.LOGIN_SUCCESS, authorization })
14
14
  }
15
15
  res.status(401).send({ message: constant.LOGIN_FAIL })
16
16
  }
@@ -13,17 +13,17 @@ exports.getOne = async (req, res) => {
13
13
  const { id } = req.params
14
14
  const produto = await service.getOne(id)
15
15
  if (!produto) {
16
- return res.status(400).json({ message: constant.PRODUTO_NAO_ENCONTRADO })
16
+ return res.status(400).json({ message: constant.PRODUCT_NOT_FOUND })
17
17
  }
18
18
  return res.status(200).send(produto)
19
19
  }
20
20
 
21
21
  exports.post = async (req, res) => {
22
22
  if (await service.existeProduto({ nome: req.body.nome.trim() })) {
23
- return res.status(400).send({ message: constant.NOME_JA_USADO })
23
+ return res.status(400).send({ message: constant.NAME_ALREADY_USED })
24
24
  }
25
25
  const dadosCadastrados = await service.criarProduto(req.body)
26
- res.status(201).send({ message: constant.POST_SUCESS, _id: dadosCadastrados._id })
26
+ res.status(201).send({ message: constant.POST_SUCCESS, _id: dadosCadastrados._id })
27
27
  }
28
28
 
29
29
  exports.delete = async (req, res) => {
@@ -31,19 +31,19 @@ exports.delete = async (req, res) => {
31
31
  const usuarioTemCarrinho = typeof carrinhoDoUsuario[0] !== 'undefined'
32
32
  if (usuarioTemCarrinho) {
33
33
  const idCarrinhos = carrinhoDoUsuario.map((carrinhos) => { return carrinhos._id })
34
- return res.status(400).send({ message: constant.EXCLUIR_PRODUTO_COM_CARRINHO, idCarrinhos })
34
+ return res.status(400).send({ message: constant.DELETE_PRODUCT_WITH_CART, idCarrinhos })
35
35
  }
36
36
  const quantidadeRegistrosExcluidos = await service.deleteById(req.params.id)
37
- const message = quantidadeRegistrosExcluidos === 0 ? constant.DELETE_NONE : constant.DELETE_SUCESS
37
+ const message = quantidadeRegistrosExcluidos === 0 ? constant.DELETE_NONE : constant.DELETE_SUCCESS
38
38
  res.status(200).send({ message })
39
39
  }
40
40
 
41
41
  exports.put = async (req, res) => {
42
42
  const idInexistenteENomeRepetido = await service.existeProduto({ nome: req.body.nome.trim(), $not: { _id: req.params.id } })
43
43
  if (idInexistenteENomeRepetido) {
44
- return res.status(400).send({ message: constant.NOME_JA_USADO })
44
+ return res.status(400).send({ message: constant.NAME_ALREADY_USED })
45
45
  }
46
46
  const registroCriado = await service.createOrUpdateById(req.params.id, req.body)
47
- if (registroCriado._id !== req.params.id) { return res.status(201).send({ message: constant.POST_SUCESS, _id: registroCriado._id }) }
48
- res.status(200).send({ message: constant.PUT_SUCESS })
47
+ if (registroCriado._id !== req.params.id) { return res.status(201).send({ message: constant.POST_SUCCESS, _id: registroCriado._id }) }
48
+ res.status(200).send({ message: constant.PUT_SUCCESS })
49
49
  }
@@ -13,36 +13,36 @@ exports.getOne = async (req, res) => {
13
13
  const { id } = req.params
14
14
  const usuario = await service.getOne(id)
15
15
  if (!usuario) {
16
- return res.status(400).json({ message: constant.USUARIO_NAO_ENCONTRADO })
16
+ return res.status(400).json({ message: constant.USER_NOT_FOUND })
17
17
  }
18
18
  return res.status(200).send(usuario)
19
19
  }
20
20
 
21
21
  exports.post = async (req, res) => {
22
22
  if (await service.existeUsuario({ email: req.body.email })) {
23
- return res.status(400).send({ message: constant.EMAIL_JA_USADO })
23
+ return res.status(400).send({ message: constant.EMAIL_ALREADY_USED })
24
24
  }
25
25
  const dadosCadastrados = await service.createUser(req.body)
26
- res.status(201).send({ message: constant.POST_SUCESS, _id: dadosCadastrados._id })
26
+ res.status(201).send({ message: constant.POST_SUCCESS, _id: dadosCadastrados._id })
27
27
  }
28
28
 
29
29
  exports.delete = async (req, res) => {
30
30
  const carrinhoDoUsuario = await carrinhosService.getAll({ idUsuario: req.params.id })
31
31
  const usuarioTemCarrinho = typeof carrinhoDoUsuario[0] !== 'undefined'
32
32
  if (usuarioTemCarrinho) {
33
- return res.status(400).send({ message: constant.EXCLUIR_USUARIO_COM_CARRINHO, idCarrinho: carrinhoDoUsuario[0]._id })
33
+ return res.status(400).send({ message: constant.DELETE_USER_WITH_CART, idCarrinho: carrinhoDoUsuario[0]._id })
34
34
  }
35
35
  const quantidadeRegistrosExcluidos = await service.deleteById(req.params.id)
36
- const message = quantidadeRegistrosExcluidos === 0 ? constant.DELETE_NONE : constant.DELETE_SUCESS
36
+ const message = quantidadeRegistrosExcluidos === 0 ? constant.DELETE_NONE : constant.DELETE_SUCCESS
37
37
  res.status(200).send({ message })
38
38
  }
39
39
 
40
40
  exports.put = async (req, res) => {
41
41
  const idInexistenteEEmailRepetido = await service.existeUsuario({ email: req.body.email, $not: { _id: req.params.id } })
42
42
  if (idInexistenteEEmailRepetido) {
43
- return res.status(400).send({ message: constant.EMAIL_JA_USADO })
43
+ return res.status(400).send({ message: constant.EMAIL_ALREADY_USED })
44
44
  }
45
45
  const registroCriado = await service.createOrUpdateById(req.params.id, req.body)
46
- if (registroCriado._id !== req.params.id) { return res.status(201).send({ message: constant.POST_SUCESS, _id: registroCriado._id }) }
47
- res.status(200).send({ message: constant.PUT_SUCESS })
46
+ if (registroCriado._id !== req.params.id) { return res.status(201).send({ message: constant.POST_SUCCESS, _id: registroCriado._id }) }
47
+ res.status(200).send({ message: constant.PUT_SUCCESS })
48
48
  }
@@ -6,18 +6,18 @@ const usuariosService = require('../services/usuarios-service')
6
6
 
7
7
  exports.checkAdm = async (req, res, next) => {
8
8
  if (!await tokenValido(req.headers)) {
9
- return res.status(401).send({ message: constant.TOKEN_INVALID })
9
+ return res.status(401).send({ message: constant.INVALID_TOKEN })
10
10
  }
11
11
  const tokenDecodificado = authService.verifyToken(req.headers.authorization)
12
12
  if (!await usuariosService.usuarioEhAdministrador(tokenDecodificado)) {
13
- return res.status(403).send({ message: constant.NECESSARIO_ADM })
13
+ return res.status(403).send({ message: constant.REQUIRED_ADMIN })
14
14
  }
15
15
  next()
16
16
  }
17
17
 
18
18
  exports.checkToken = async (req, res, next) => {
19
19
  if (!await tokenValido(req.headers)) {
20
- return res.status(401).send({ message: constant.TOKEN_INVALID })
20
+ return res.status(401).send({ message: constant.INVALID_TOKEN })
21
21
  }
22
22
  next()
23
23
  }
@@ -41,12 +41,12 @@ exports.getPrecoUnitarioOuErro = async (produto) => {
41
41
  const { idProduto } = produto
42
42
  const existePedido = await this.existeProduto({ _id: idProduto })
43
43
  if (!existePedido) {
44
- return { error: { statusCode: 400, message: constant.IDPRODUTO_INVALIDO, item: { idProduto, quantidade } } }
44
+ return { error: { statusCode: 400, message: constant.PRODUCT_NOT_FOUND, item: { idProduto, quantidade } } }
45
45
  }
46
46
 
47
47
  const { quantidade: quantidadeEstoque, preco } = await this.getDadosDoProduto({ _id: idProduto })
48
48
  if (quantidade > quantidadeEstoque) {
49
- return { error: { statusCode: 400, message: constant.ESTOQUE_INSUFICIENTE, item: { idProduto, quantidade, quantidadeEstoque } } }
49
+ return { error: { statusCode: 400, message: constant.INSUFFICIENT_STOCK, item: { idProduto, quantidade, quantidadeEstoque } } }
50
50
  }
51
51
  return { precoUnitario: preco }
52
52
  }
@@ -3,25 +3,24 @@
3
3
  module.exports = {
4
4
  INTERNAL_ERROR: 'Abra uma issue informando essa resposta. https://github.com/ServeRest/ServeRest/issues',
5
5
  TIMEOUT: 'Timeout da requisição atingido',
6
- POST_SUCESS: 'Cadastro realizado com sucesso',
7
- DELETE_SUCESS: 'Registro excluído com sucesso',
6
+ POST_SUCCESS: 'Cadastro realizado com sucesso',
7
+ DELETE_SUCCESS: 'Registro excluído com sucesso',
8
8
  DELETE_NONE: 'Nenhum registro excluído',
9
- PUT_SUCESS: 'Registro alterado com sucesso',
10
- LOGIN_SUCESS: 'Login realizado com sucesso',
9
+ PUT_SUCCESS: 'Registro alterado com sucesso',
10
+ LOGIN_SUCCESS: 'Login realizado com sucesso',
11
11
  LOGIN_FAIL: 'Email e/ou senha inválidos',
12
- EMAIL_JA_USADO: 'Este email já está sendo usado',
13
- NOME_JA_USADO: 'Já existe produto com esse nome',
14
- NECESSARIO_ADM: 'Rota exclusiva para administradores',
15
- TOKEN_INVALID: 'Token de acesso ausente, inválido, expirado ou usuário do token não existe mais',
16
- LIMITE_1_CARRINHO: 'Não é permitido ter mais de 1 carrinho',
17
- IDPRODUTO_INVALIDO: 'Produto não encontrado',
18
- ESTOQUE_INSUFICIENTE: 'Produto não possui quantidade suficiente',
19
- EXCLUIR_USUARIO_COM_CARRINHO: 'Não é permitido excluir usuário com carrinho cadastrado',
20
- EXCLUIR_PRODUTO_COM_CARRINHO: 'Não é permitido excluir produto que faz parte de carrinho',
21
- CARRINHO_COM_PRODUTO_DUPLICADO: 'Não é permitido possuir produto duplicado',
22
- SEM_CARRINHO: 'Não foi encontrado carrinho para esse usuário',
23
- ESTOQUE_REABASTECIDO: 'Estoque dos produtos reabastecido',
24
- USUARIO_NAO_ENCONTRADO: 'Usuário não encontrado',
25
- CARRINHO_NAO_ENCONTRADO: 'Carrinho não encontrado',
26
- PRODUTO_NAO_ENCONTRADO: 'Produto não encontrado'
12
+ EMAIL_ALREADY_USED: 'Este email já está sendo usado',
13
+ NAME_ALREADY_USED: 'Já existe produto com esse nome',
14
+ REQUIRED_ADMIN: 'Rota exclusiva para administradores',
15
+ INVALID_TOKEN: 'Token de acesso ausente, inválido, expirado ou usuário do token não existe mais',
16
+ LIMIT_JUST_ONE_CART: 'Não é permitido ter mais de 1 carrinho',
17
+ INSUFFICIENT_STOCK: 'Produto não possui quantidade suficiente',
18
+ DELETE_USER_WITH_CART: 'Não é permitido excluir usuário com carrinho cadastrado',
19
+ DELETE_PRODUCT_WITH_CART: 'Não é permitido excluir produto que faz parte de carrinho',
20
+ CART_WITH_DUPLICATE_PRODUCT: 'Não é permitido possuir produto duplicado',
21
+ NO_CART: 'Não foi encontrado carrinho para esse usuário',
22
+ REPLENISHED_STOCK: 'Estoque dos produtos reabastecido',
23
+ USER_NOT_FOUND: 'Usuário não encontrado',
24
+ CART_NOT_FOUND: 'Carrinho não encontrado',
25
+ PRODUCT_NOT_FOUND: 'Produto não encontrado'
27
26
  }