Skip to content

Commit

Permalink
Implementando novos endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Oct 17, 2019
1 parent 0ce6b8b commit 1e918bf
Show file tree
Hide file tree
Showing 12 changed files with 668 additions and 5 deletions.
33 changes: 33 additions & 0 deletions api/controller/beneficiario-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { Response, Logger } = require('../util');
const paramUtil = require('../helper/param-util');
const service = require('../service/beneficiario-service');
const BeneficiarioValidator = require('../http-validators/beneficiario-validator');

const consultarBeneficiarios = async (req, res, next) => {
try {
const params = paramUtil.getParams(req);
const options = BeneficiarioValidator.requisicaoConsultarBeneficiarios(params);
const resposta = await service.consultarBeneficiarios(options);
return Response.success(res, resposta, { contentType: Response.CONTENT_TYPE.APPLICATION_QR_PAGUE });
} catch (err) {
Logger.warn(err);
return Response.fromError(res, err);
}
}

const consultarBeneficiario = async (req, res, next) => {
try {
const params = paramUtil.getParams(req);
const options = BeneficiarioValidator.requisicaoConsultarBeneficiario(params);
const resposta = await service.consultarBeneficiario(options);
return Response.success(res, resposta, { contentType: Response.CONTENT_TYPE.APPLICATION_QR_PAGUE });
} catch (err) {
Logger.warn(err);
return Response.fromError(res, err);
}
}

module.exports = {
consultarBeneficiarios,
consultarBeneficiario
};
16 changes: 15 additions & 1 deletion api/controller/operacoes-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ const criarOperacao = async (req, res, next) => {
}
}

const alterarOperacao = async (req, res, next) => {
try {
const params = paramUtil.getParams(req);
const body = req.body;
const options = OperacaoValidator.requisicaoAlterarOperacao(params, body);
const resposta = await service.alterarOperacao(options);
return Response.success(res, resposta, { contentType: Response.CONTENT_TYPE.APPLICATION_QR_PAGUE });
} catch (err) {
Logger.warn(err);
return Response.fromError(res, err);
}
}

const consultarOperacoes = async (req, res, next) => {
try {
const params = paramUtil.getParams(req);
Expand Down Expand Up @@ -77,5 +90,6 @@ module.exports = {
consultarOperacoes,
consultarOperacao,
efetivarOperacao,
confirmarOperacao
confirmarOperacao,
alterarOperacao
};
59 changes: 59 additions & 0 deletions api/database/model/operacao.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ module.exports = (db, mongoose, promise) => {
return await operacao.save();
},

OperacaoModel.alterarOperacao = async (uuid, valor) => {
Logger.debug('Alteração de Operação Financeira');

const query = { uuid, situacao: SITUACAO.EMITIDO, dataHoraVencimento: { $gte: Date.now() }}
const update = { valor }
const options = { new: true }
return await OperacaoModel.findOneAndUpdate(query, update, options).populate('pagamentos');
}

OperacaoModel.recuperarOperacoes = async ({ idRequisicao, cpfCnpjBeneficiario, paginaInicial, tamanhoPagina, periodoInicio, periodoFim }) => {
Logger.debug('Consulta de Operações Financeiras');

Expand Down Expand Up @@ -229,5 +238,55 @@ module.exports = (db, mongoose, promise) => {
}
}

OperacaoModel.consultarBeneficiarios = async ({ cpfCnpj, paginaInicial, tamanhoPagina }) => {
Logger.debug('Consulta de Beneficiário');

const limit = tamanhoPagina ? parseInt(tamanhoPagina) : DEFAULT_MONGOOSE_LIMIT;
const skip = paginaInicial ? parseInt(paginaInicial) * limit : DEFAULT_MONGOOSE_SKIP;
const query = [
{
$group:{
_id: {
beneficiario: '$beneficiario'
}
}
}, {
"$project":{
_id: false,
"beneficiario":"$_id.beneficiario"
}
},{
"$skip":0
},{
"$limit":30
},{
$group: {
_id: 'lista',
resultado: {
$push: '$beneficiario'
}
}
}, {
$project: {
_id: false,
resultado: 1
}
}
];

if(cpfCnpj && (typeof cpfCnpj === 'string' || ( Array.isArray(cpfCnpj) && cpfCnpj.length > 0 ))) {
query.unshift({
$match: {
'beneficiario.cpfCnpj': {
$in: cpfCnpj
}
}
});
}

const resposta = await OperacaoModel.aggregate(query);
return (resposta && resposta[0]) ? resposta[0].resultado : resposta;
}

return OperacaoModel;
}
56 changes: 56 additions & 0 deletions api/http-validators/beneficiario-validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const { CONSTANTS } = require('../jwt');
const { Response, ResponseError, Err, Logger } = require('../util');
const { campoObrigatorio } = require('./commum');

const requisicaoConsultarBeneficiarios = (params) => {

try {

campoObrigatorio('token-instituicao', params[CONSTANTS.TOKEN_NAME]);

let requisicao = {
tokenInstituicao: params[CONSTANTS.TOKEN_NAME],
}

if(params.cpfCnpj) {
requisicao = { ...requisicao, cpfCnpj: params.cpfCnpj }
}

return requisicao;

} catch(err) {
Logger.warn(err);
if(!(err instanceof ResponseError)){
Err.throwError(Response.HTTP_STATUS.BAD_REQUEST, 997000);
}
throw err;
}
}

const requisicaoConsultarBeneficiario = (params) => {

try {

campoObrigatorio('token-instituicao', params[CONSTANTS.TOKEN_NAME]);
campoObrigatorio('cpfCnpj', params.cpfCnpj);

let requisicao = {
tokenInstituicao: params[CONSTANTS.TOKEN_NAME],
cpfCnpj: params.cpfCnpj
}

return requisicao;

} catch(err) {
Logger.warn(err);
if(!(err instanceof ResponseError)){
Err.throwError(Response.HTTP_STATUS.BAD_REQUEST, 997000);
}
throw err;
}
}

module.exports = {
requisicaoConsultarBeneficiarios,
requisicaoConsultarBeneficiario
}
27 changes: 25 additions & 2 deletions api/http-validators/operacoes-validator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { CONSTANTS } = require('../jwt');
const { ResponseError, Err, Logger } = require('../util');
const { Response, ResponseError, Err, Logger } = require('../util');
const { campoObrigatorio } = require('./commum');

const requisicaoCriarOperacao = (params, body) => {
Expand Down Expand Up @@ -350,10 +350,33 @@ const requisicaoConfirmarOperacao = (params, body) => {
}
}

const requisicaoAlterarOperacao = (params, body) => {
try {

campoObrigatorio('uuid', params.uuid);
campoObrigatorio('valor', body.valor);

let requisicao = {
uuid: params.uuid,
valor: body.valor
}

return requisicao;

} catch(err) {
Logger.warn(err);
if(!(err instanceof ResponseError)){
Err.throwError(Response.HTTP_STATUS.BAD_REQUEST, 997000);
}
throw err;
}
}

module.exports = {
requisicaoCriarOperacao,
requisicaoConsultarOperacoes,
requisicaoConsultarOperacao,
requisicaoEfetivarOperacao,
requisicaoConfirmarOperacao
requisicaoConfirmarOperacao,
requisicaoAlterarOperacao
}
2 changes: 1 addition & 1 deletion api/http-validators/pagamento-validator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { CONSTANTS } = require('../jwt');
const { ResponseError, Err, Logger } = require('../util');
const { Response, ResponseError, Err, Logger } = require('../util');
const { campoObrigatorio } = require('./commum');

const requisicaoCriarPagamento = (params, body) => {
Expand Down
5 changes: 5 additions & 0 deletions api/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ let express = require('express')

let Operacao = require('../controller/operacoes-controller');
let Pagamento = require('../controller/pagamento-controller');
let Beneficiario = require('../controller/beneficiario-controller');

const router = express.Router();

router.route('/operacoes').post(Operacao.criarOperacao);
router.route('/operacoes').get(Operacao.consultarOperacoes);
router.route('/operacoes/:uuid').get(Operacao.consultarOperacao);
router.route('/operacoes/:uuid').patch(Operacao.alterarOperacao);
router.route('/operacoes/:uuid/efetivacao').put(Operacao.efetivarOperacao);
router.route('/operacoes/:uuid/confirmacao').put(Operacao.confirmarOperacao);

Expand All @@ -16,4 +18,7 @@ router.route('/pagamentos').get(Pagamento.consultarPagamentos);
router.route('/pagamentos/:uuid').get(Pagamento.consultarPagamento);
router.route('/pagamentos/:uuid/confirmacao').put(Pagamento.confirmarPagamento);

router.route('/beneficiarios').get(Beneficiario.consultarBeneficiarios);
router.route('/beneficiarios/:cpfCnpj').get(Beneficiario.consultarBeneficiario);

module.exports = router;
Loading

0 comments on commit 1e918bf

Please sign in to comment.