Skip to content

Commit 6c9ea63

Browse files
Adding present value and future value calculation
1 parent 47ab75e commit 6c9ea63

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* [Validador de CPF](#validaCPF)
88
* [Gerador de CNPJ](#geraCNPJ)
99
* [Validador de CNPJ](#validaCNPJ)
10+
* [Cálculo de Valor Futuro](#calculaVF)
11+
* [Cálculo de Valor Presente](#calculaVP)
1012
* [Setup](#setup)
1113

1214
## Informações Gerais
@@ -93,6 +95,46 @@
9395
```
9496
* Os stat codes e messages podem ser vistos em include/std_messages_api.js
9597

98+
## calculaVF
99+
* Arquivo: calculaVF.js
100+
* Cálculo financeiro para Valor Futuro
101+
* URL: http://.../calculaVF
102+
* Body => JSON
103+
```
104+
{ "authKey":"", "param": [vp, i, n]} Ex.: { "authKey":"", "param": [vp, i, n]}
105+
```
106+
* onde vp = valor presente / i = taxa de juros / n = numero de periodos
107+
* Method: GET ou POST
108+
* Retorno:
109+
```
110+
{
111+
"statCode": "<status code>",
112+
"statMsg": "<status message>",
113+
"result": ####.## correspondente ao valor futuro
114+
}
115+
```
116+
* Os stat codes e messages podem ser vistos em include/std_messages_api.js
117+
118+
## calculaVP
119+
* Arquivo: calculaVP.js
120+
* Cálculo financeiro para Valor Presente
121+
* URL: http://.../calculaVP
122+
* Body => JSON
123+
```
124+
{ "authKey":"", "param": [vf, i, n]} Ex.: { "authKey":"", "param": [vf, i, n]}
125+
```
126+
* onde vf = valor futuro / i = taxa de juros / n = numero de periodos
127+
* Method: GET ou POST
128+
* Retorno:
129+
```
130+
{
131+
"statCode": "<status code>",
132+
"statMsg": "<status message>",
133+
"result": ####.## correspondente ao valor presente
134+
}
135+
```
136+
* Os stat codes e messages podem ser vistos em include/std_messages_api.js
137+
96138
## Setup
97139
* Basta copiar os arquivos para uma pasta e executar o node
98140
* Por definição, o serviço é executado na porta 8080 do TCP/IP mas pode ser trocado pela edição do arquivo app.js na linha process.env.PORT

app.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const objGeraCPF = require('./scripts/geraCPF');
55
const objValidaCPF = require('./scripts/validaCPF');
66
const objGeraCNPJ = require('./scripts/geraCNPJ');
77
const objValidaCNPJ = require('./scripts/validaCNPJ');
8+
const objCalculaVF = require('./scripts/calculaVF');
9+
const objCalculaVP = require('./scripts/calculaVP');
810

911
var app = express();
1012
var port = process.env.PORT || 8080;
@@ -36,6 +38,14 @@ app.get('/validaCNPJ', function (req, res) {
3638
res.json(objValidaCNPJ.doValidaCNPJ(req.body));
3739
});
3840

41+
app.get('/calculaVF', function (req, res) {
42+
res.json(objCalculaVF.doCalculaVF(req.body));
43+
});
44+
45+
app.get('/calculaVP', function (req, res) {
46+
res.json(objCalculaVP.doCalculaVP(req.body));
47+
});
48+
3949
app.listen(port);
4050

4151
console.log("Servico de APIs iniciado na porta " + port);

scripts/calculaVF.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const objStdMessages = require('../include/std_messages_api');
2+
const objValidacoes = require('../include/validacoes');
3+
4+
var funMain = function (json_obj_entrada) {
5+
var result;
6+
var json_output;
7+
8+
console.log("Parametros de entrada: " + JSON.stringify(json_obj_entrada));
9+
10+
if (json_obj_entrada.length < 2) {
11+
json_output = objStdMessages.stdMessages[3];
12+
} else {
13+
14+
if (json_obj_entrada["authKey"] == null || json_obj_entrada["param"] == null) {
15+
json_output = objStdMessages.stdMessages[3];
16+
} else {
17+
18+
if (json_obj_entrada["authKey"] != null) {
19+
//Verifica a permissao de uso
20+
//Como eh publica, todos podem usar
21+
if (true) {
22+
23+
if (json_obj_entrada["param"].length != 3) {
24+
//Bad request
25+
json_output = objStdMessages.stdMessages[4].replace('__RESULT__', '"Parametros invalidos. Utilizar: Valor Presente, Tx. Juros, Periodos"');
26+
} else {
27+
//Executa o codigo da API
28+
//Parametros esperados
29+
//VP = valor presente
30+
//n = numero de periodos
31+
//i = taxa de juros
32+
33+
var vp = json_obj_entrada["param"][0];
34+
var i = json_obj_entrada["param"][1];
35+
var n = json_obj_entrada["param"][2];
36+
37+
var strResult = (vp) * Math.pow((1 + (i / 100)), n);
38+
39+
json_output = objStdMessages.stdMessages[0].replace('__RESULT__', strResult);
40+
41+
vp = null;
42+
i = null;
43+
n = null;
44+
45+
}
46+
} else {
47+
//Forbidden
48+
json_output = objStdMessages.stdMessages[2];
49+
}
50+
51+
} else {
52+
//Forbidden
53+
json_output = objStdMessages.stdMessages[2];
54+
}
55+
56+
}
57+
}
58+
return JSON.parse(json_output);
59+
}
60+
61+
module.exports = {
62+
doCalculaVF: funMain
63+
}

scripts/calculaVP.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const objStdMessages = require('../include/std_messages_api');
2+
const objValidacoes = require('../include/validacoes');
3+
4+
var funMain = function (json_obj_entrada) {
5+
var result;
6+
var json_output;
7+
8+
console.log("Parametros de entrada: " + JSON.stringify(json_obj_entrada));
9+
10+
if (json_obj_entrada.length < 2) {
11+
json_output = objStdMessages.stdMessages[3];
12+
} else {
13+
14+
if (json_obj_entrada["authKey"] == null || json_obj_entrada["param"] == null) {
15+
json_output = objStdMessages.stdMessages[3];
16+
} else {
17+
18+
if (json_obj_entrada["authKey"] != null) {
19+
//Verifica a permissao de uso
20+
//Como eh publica, todos podem usar
21+
if (true) {
22+
23+
if (json_obj_entrada["param"].length != 3) {
24+
//Bad request
25+
json_output = objStdMessages.stdMessages[4].replace('__RESULT__', '"Parametros invalidos. Utilizar: Valor Presente, Tx. Juros, Periodos"');
26+
} else {
27+
//Executa o codigo da API
28+
//Parametros esperados
29+
//VF = valor futuro
30+
//n = numero de periodos
31+
//i = taxa de juros
32+
33+
var vf = json_obj_entrada["param"][0];
34+
var i = json_obj_entrada["param"][1];
35+
var n = json_obj_entrada["param"][2];
36+
37+
var strResult = (vf) / (Math.pow((1 + (i / 100)), n));
38+
39+
json_output = objStdMessages.stdMessages[0].replace('__RESULT__', strResult);
40+
41+
vf = null;
42+
i = null;
43+
n = null;
44+
}
45+
} else {
46+
//Forbidden
47+
json_output = objStdMessages.stdMessages[2];
48+
}
49+
50+
} else {
51+
//Forbidden
52+
json_output = objStdMessages.stdMessages[2];
53+
}
54+
55+
}
56+
}
57+
return JSON.parse(json_output);
58+
}
59+
60+
module.exports = {
61+
doCalculaVP: funMain
62+
}

0 commit comments

Comments
 (0)