Skip to content

Commit

Permalink
Merge pull request #284 from app-masters/data-beneficio
Browse files Browse the repository at this point in the history
change year/month to date
  • Loading branch information
jfbaraky authored Jun 9, 2020
2 parents aaa1fbf + c44190f commit e46a3d5
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

module.exports = {
up: async (queryInterface, Sequelize) => {
queryInterface.removeColumn('Benefits', 'year');
queryInterface.removeColumn('Benefits', 'month');
queryInterface.addColumn('Benefits', 'date', {
type: Sequelize.DATE,
allowNull: false
});
},

down: async (queryInterface, Sequelize) => {
queryInterface.removeColumn('Benefits', 'date');
queryInterface.addColumn('Benefits', 'year', {
type: Sequelize.INTEGER,
allowNull: false
});
queryInterface.addColumn('Benefits', 'month', {
type: Sequelize.INTEGER,
allowNull: false
});
}
};
9 changes: 6 additions & 3 deletions backend/src/models/consumptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@ export const getFamilyDependentBalance = async (family: Family, availableBenefit
const endYear = moment(dependent.deactivatedAt as Date).year();

for (const benefit of availableBenefits) {
const benefitDate = moment(benefit.date);
if (benefit.groupName !== family.groupName) continue; // Don't check if it's from another group

// Check all the dates
const notInFuture = benefit.year < todayYear || (benefit.year === todayYear && benefit.month <= todayMonth);
const afterCreation = benefit.year > startYear || (benefit.year === startYear && benefit.month >= startMonth);
const notInFuture =
benefitDate.year() < todayYear || (benefitDate.year() === todayYear && benefitDate.month() + 1 <= todayMonth);
const afterCreation =
benefitDate.year() > startYear || (benefitDate.year() === startYear && benefitDate.month() + 1 >= startMonth);
const beforeDeactivation = dependent.deactivatedAt
? benefit.year < endYear || (benefit.year === endYear && benefit.month < endMonth)
? benefitDate.year() < endYear || (benefitDate.year() === endYear && benefitDate.month() + 1 < endMonth)
: true;

if (notInFuture && afterCreation && beforeDeactivation) {
Expand Down
11 changes: 3 additions & 8 deletions backend/src/schemas/benefits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ export interface Benefit {
institutionId: number | string;
groupName: string;
title: string;
month: number;
year: number;
date: Date;
products?: BenefitProduct[];
value?: number;
createdAt?: number | Date | null;
Expand Down Expand Up @@ -48,12 +47,8 @@ export const attributes = {
type: DataTypes.STRING,
allowNull: false
},
month: {
type: DataTypes.INTEGER,
allowNull: false
},
year: {
type: DataTypes.INTEGER,
date: {
type: DataTypes.DATE,
allowNull: false
},
value: {
Expand Down
3 changes: 1 addition & 2 deletions backend/tests/balance-refactor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ let createdFamily: Family;
const benefit = {
title: '[CAD25123] Auxilio merenda',
groupName: getFamilyGroupByCode(0)?.key,
month: moment().month() + 1,
year: moment().year(),
date: moment().toDate(),
value: 500,
institutionId: 0
} as Benefit;
Expand Down
3 changes: 1 addition & 2 deletions backend/tests/consumption.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ let createdFamily: Family;
const benefit = {
title: '[CAD25123] Auxilio municipal de alimentação',
groupName: getFamilyGroupByCode(1)?.key,
month: 5,
year: 2020,
date: moment().toDate(),
value: 500,
institutionId: 0
} as Benefit;
Expand Down

0 comments on commit e46a3d5

Please sign in to comment.