Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrige cálculos de período mensal e anual #42

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "api-calculadoradeinvestimentos",
"version": "0.1.0",
"version": "0.1.2",
"description": "",
"main": "src/index.js",
"directories": {
Expand Down
5 changes: 5 additions & 0 deletions src/enums/period-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
enum PeriodType{
Monthly = 1,
Anual = 2
}
export default PeriodType;
5 changes: 0 additions & 5 deletions src/enums/profitability-type.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/requests/juros-compostos-request.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import ProfitabilityType from '@src/enums/profitability-type';
import PeriodType from '@src/enums/period-type';
import { Request } from 'express';

export interface JurosCompostosRequestBody {
period: number,
periodType: ProfitabilityType,
periodType: PeriodType,
profitability: number,
profitabilityType: ProfitabilityType,
profitabilityType: PeriodType,
initialValue: number
}

Expand Down
6 changes: 3 additions & 3 deletions src/responses/juros-compostos-response.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Response } from 'express';
import ProfitabilityType from '../enums/profitability-type';
import PeriodType from '../enums/period-type';

export interface JurosCompostosResponseBody {
period: number,
periodType: ProfitabilityType,
periodType: PeriodType,
profitability: number,
profitabilityType: ProfitabilityType,
profitabilityType: PeriodType,
realProfitability: number,
initialValue: number,
finalValue: number
Expand Down
10 changes: 5 additions & 5 deletions src/services/juros-compostos-service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import ProfitabilityType from '@src/enums/profitability-type';
import PeriodType from '@src/enums/period-type';
import { calcularCrescimento, calcularJurosCompostosMensais } from '@src/utils/normalize-utils';

type JurosCompostosServiceProps = {
period: number,
periodType: ProfitabilityType,
periodType: PeriodType,
profitability: number,
profitabilityType: ProfitabilityType,
profitabilityType: PeriodType,
initialValue: number
};

Expand All @@ -21,8 +21,8 @@ export default {
) {
initialValue = parseFloat(initialValue.toFixed(2));

const monthlyPeriod = periodType == ProfitabilityType.Monthly? period : period * 12;
const montlhyProfitability = profitabilityType == ProfitabilityType.Monthly? profitability : profitability / 12.68;
const monthlyPeriod = periodType == PeriodType.Monthly? period : period * 12;
const montlhyProfitability = profitabilityType == PeriodType.Monthly? profitability : profitability / 12.68;

const finalValue = calcularJurosCompostosMensais(initialValue, montlhyProfitability, monthlyPeriod);
const realProfitability = calcularCrescimento(initialValue, finalValue);
Expand Down
6 changes: 3 additions & 3 deletions src/validators/juros-compostos-request-validator.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import Joi from 'joi';
import ProfitabilityType from '@src/enums/profitability-type';
import PeriodType from '@src/enums/period-type';

export default {
body: Joi.object().keys(
{
period: Joi.number().integer().min(1).required(),
periodType: Joi.number().valid(ProfitabilityType.Monthly, ProfitabilityType.Anual).required(),
periodType: Joi.number().valid(PeriodType.Monthly, PeriodType.Anual).required(),
profitability: Joi.number().min(0.0).max(1.0).required(),
profitabilityType: Joi.number().valid(ProfitabilityType.Monthly, ProfitabilityType.Anual).required(),
profitabilityType: Joi.number().valid(PeriodType.Monthly, PeriodType.Anual).required(),
initialValue: Joi.number().min(0).required()
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { JurosCompostosRequestBody } from '@src/requests/juros-compostos-request';
import { faker } from '@faker-js/faker';
import ProfitabilityType from '@src/enums/profitability-type';
import PeriodType from '@src/enums/period-type';

type JurosCompostosRequestBodyFactoryProps = {
period?: number,
Expand All @@ -13,9 +13,9 @@ type JurosCompostosRequestBodyFactoryProps = {
export default function(body? :JurosCompostosRequestBodyFactoryProps): JurosCompostosRequestBody {
return {
period: body?.period ?? faker.number.int({min: 1, max: 100}),
periodType: body?.periodType ?? faker.helpers.enumValue(ProfitabilityType),
periodType: body?.periodType ?? faker.helpers.enumValue(PeriodType),
profitability: body?.profitability ?? faker.number.float({min: 0.1, max: 1.0}),
profitabilityType: body?.profitabilityType ?? faker.helpers.enumValue(ProfitabilityType),
profitabilityType: body?.profitabilityType ?? faker.helpers.enumValue(PeriodType),
initialValue: body?.initialValue ?? faker.number.float({min: 1000, max: 10000})
} as JurosCompostosRequestBody;
}
163 changes: 87 additions & 76 deletions tests/unit/services/juros-compostos-service.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import jurosCompostosService from '@src/services/juros-compostos-service';
import jurosCompostosRequestBodyFactory from '../factories/requests/juros-compostos-request-body-factory';
import ProfitabilityType from '@src/enums/profitability-type';
import PeriodType from '@src/enums/period-type';

describe('juros compostos service', () => {
describe('calcular', () => {
describe('no initialValue', () => {
describe.each(
[
['montlhy',ProfitabilityType.Monthly],
['anual',ProfitabilityType.Anual],
]
)('%s', (_,profitabilityType) => {
describe.each([
['montlhy', PeriodType.Monthly],
['anual', PeriodType.Anual],
])('%s', (_, profitabilityType) => {
it('should be zero', () => {
const body = jurosCompostosRequestBodyFactory({ initialValue: 0, profitabilityType });

Expand All @@ -22,12 +20,10 @@ describe('juros compostos service', () => {
});
});
describe('no profitability', () => {
describe.each(
[
['montlhy',ProfitabilityType.Monthly],
['anual',ProfitabilityType.Anual],
]
)('%s', (_,profitabilityType) => {
describe.each([
['montlhy', PeriodType.Monthly],
['anual', PeriodType.Anual],
])('%s', (_, profitabilityType) => {
it('should be equal to initialValue', () => {
const body = jurosCompostosRequestBodyFactory({ profitability: 0, profitabilityType });

Expand All @@ -38,12 +34,10 @@ describe('juros compostos service', () => {
});
});
describe('no period', () => {
describe.each(
[
['montlhy',ProfitabilityType.Monthly],
['anual',ProfitabilityType.Anual],
]
)('%s', (_,profitabilityType) => {
describe.each([
['montlhy', PeriodType.Monthly],
['anual', PeriodType.Anual],
])('%s', (_, profitabilityType) => {
it('should be equal to initialValue', () => {
const body = jurosCompostosRequestBodyFactory({ period: 0, profitabilityType });

Expand All @@ -53,71 +47,88 @@ describe('juros compostos service', () => {
});
});
});
describe('valid profitability', () => {
describe('monthly', () => {
it('should sum profitability\'s percentage to finalValue', () => {
const body = jurosCompostosRequestBodyFactory(
{
profitabilityType: ProfitabilityType.Monthly,
periodType: ProfitabilityType.Monthly,
initialValue: 1000,
profitability: 0.01,
period: 12
}
);
describe('anual profitability', () => {
it.each([
[0.1, 1000, 10, 1082.66],
[0.1, 1000, 12, 1100.00]
])('%s anual profitablity %s in %s months should become %s', (profitability, initialValue, period, expectedResult) => {
const body = jurosCompostosRequestBodyFactory(
{
period: period,
periodType: PeriodType.Monthly,

const result = jurosCompostosService.calcular(body);
profitability: profitability,
profitabilityType: PeriodType.Anual,

expect(result.finalValue).toBe(1126.83);
});
it('finalValue should be equivalent to Anual multiplied by 12.68', () => {
const monthlyBody = jurosCompostosRequestBodyFactory(
{
profitabilityType: ProfitabilityType.Monthly,
periodType: ProfitabilityType.Monthly,
period: 12
}
);
const monthlyResult = jurosCompostosService.calcular(monthlyBody);

const anualBody = jurosCompostosRequestBodyFactory(
{
initialValue: monthlyBody.initialValue,
profitabilityType: ProfitabilityType.Anual,
profitability: monthlyBody.profitability * 12.68,
periodType: ProfitabilityType.Anual,
period: monthlyBody.period / 12
}
);
const anualResult = jurosCompostosService.calcular(anualBody);

expect(monthlyResult.finalValue).toBe(anualResult.finalValue);
});
initialValue: initialValue,
}
);

const result = jurosCompostosService.calcular(body);

expect(result.finalValue).toBe(expectedResult);
});
describe('anual', () => {
it('should sum profitability\'s percentage divided by 12 to finalValue', () => {
const body = jurosCompostosRequestBodyFactory(
{
profitabilityType: ProfitabilityType.Anual,
initialValue: 1000,
profitability: 0.1268,
periodType: ProfitabilityType.Anual,
period: 1
}
);
it.each([
[0.1, 1000, 1, 1100.00],
[0.1, 1000, 2, 1210.00]
])('%s anual profitablity %s in a %s year period should become %s', (profitability, initialValue, period, expectedResult) => {
const body = jurosCompostosRequestBodyFactory(
{
period: period,
periodType: PeriodType.Anual,

const result = jurosCompostosService.calcular(body);
profitability: profitability,
profitabilityType: PeriodType.Anual,

expect(result.finalValue).toBe(1126.83);
});
initialValue: initialValue,
}
);

const result = jurosCompostosService.calcular(body);

expect(result.finalValue).toBe(expectedResult);
});
});
describe.skip('valid period', ()=>{
describe.skip('monthly', () => {

describe('monthly profitability', () => {
it.each([
[0.1, 1000, 1, 1100.00],
[0.1, 1000, 12, 3138.43]
])('%s monthly profitability %s in %s months should become %s', (profitability, initialValue, period, expectedResult) => {
const body = jurosCompostosRequestBodyFactory(
{
period: period,
periodType: PeriodType.Monthly,

profitability: profitability,
profitabilityType: PeriodType.Monthly,

initialValue: initialValue,
}
);

const result = jurosCompostosService.calcular(body);

expect(result.finalValue).toBe(expectedResult);
});
describe.skip('anual', () => {

it.each([
[0.1, 1000, 1, 3138.43],
[0.1, 1000, 2, 9849.73]
])('%s monthly profitability %s in a %s year period should become %s', (profitability, initialValue, period, expectedResult) => {
const body = jurosCompostosRequestBodyFactory(
{
period: period,
periodType: PeriodType.Anual,

profitability: profitability,
profitabilityType: PeriodType.Monthly,

initialValue: initialValue,
}
);

const result = jurosCompostosService.calcular(body);

expect(result.finalValue).toBe(expectedResult);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe(
'periodType',
()=>{
it.each([0,4,-1])(
'should not be a number outside of ProfitabilityType enum',
'should not be a number outside of PeriodType enum',
(type)=>{
const body = jurosCompostosRequestBodyFactory({periodType: type});
const valid = bodyValidator.validate(body);
Expand Down
Loading