Skip to content

Commit

Permalink
Merge pull request #303 from app-masters/282-lidar-com-nova-data-do-b…
Browse files Browse the repository at this point in the history
…eneficio

Lidar com nova data do beneficio
  • Loading branch information
jfbaraky authored Jun 9, 2020
2 parents 99d4e28 + c61ece6 commit de076ac
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 46 deletions.
108 changes: 93 additions & 15 deletions backend/database/migrations/20200605124402-alter-table-beneficio.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,101 @@

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

try {
// The column needs to allow null as the column does not have and defualt value
await queryInterface.addColumn(
'Benefits',
'date',
{
type: Sequelize.DATE
},
{ transaction }
);
// Generate a date based on the year and month column
await queryInterface.sequelize.query(
'UPDATE "Benefits" SET "date" = make_timestamptz("year", "month", 1, 3, 0, 0);',
{
transaction
}
);
// Now the date column is populated, it can be set to `Not Null`
await queryInterface.changeColumn(
'Benefits',
'date',
{
type: Sequelize.DATE,
allowNull: false
},
{ transaction }
);
// Delete old date values
await queryInterface.removeColumn('Benefits', 'year', { transaction });
await queryInterface.removeColumn('Benefits', 'month', { transaction });

await transaction.commit();
} catch (error) {
console.error(error);
await transaction.rollback();
throw error;
}
},

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
});
const transaction = await queryInterface.sequelize.transaction();
try {
await queryInterface.addColumn(
'Benefits',
'year',
{
type: Sequelize.INTEGER
},
{ transaction }
);
await queryInterface.addColumn(
'Benefits',
'month',
{
type: Sequelize.INTEGER
},
{ transaction }
);

// Extract month and year from the timestamp
await queryInterface.sequelize.query(
'UPDATE "Benefits" SET "month" = extract(month from "date"), "year" = extract(year from "date");',
{
transaction
}
);

// Now the year and month column are populated, they can be set to `Not Null`
await queryInterface.changeColumn(
'Benefits',
'year',
{
type: Sequelize.INTEGER,
allowNull: false
},
{ transaction }
);
await queryInterface.changeColumn(
'Benefits',
'month',
{
type: Sequelize.INTEGER,
allowNull: false
},
{ transaction }
);
await queryInterface.removeColumn('Benefits', 'date', { transaction });

await transaction.commit();
} catch (error) {
console.error(error);
await transaction.rollback();
throw error;
}
}
};
10 changes: 5 additions & 5 deletions backend/database/seeders/benefits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ const list = [
{
title: '[CAD25123] Auxilio municipal de alimentação',
groupName: 'extreme-poverty',
institutionId: 1,
date: moment().toDate(),
date: moment().startOf('month').month(4).year(2020).toDate(),
institutionId: 1
value: 600
},
{
title: '[CAD25123] Auxilio municipal de alimentação',
groupName: 'poverty-line',
institutionId: 1,
date: moment().toDate(),
date: moment().startOf('month').month(4).year(2020).toDate(),
institutionId: 1.
value: 400
},
{
title: '[CAD25123] Auxilio municipal de alimentação',
groupName: 'cad',
date: moment().startOf('month').month(4).year(2020).toDate(),
institutionId: 1,
date: moment().toDate(),
value: 300
}
] as Benefit[];
Expand Down
37 changes: 11 additions & 26 deletions backend/src/models/consumptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,29 +110,23 @@ export const getFamilyDependentBalanceTicket = async (family: Family, availableB
});
}

const todayMonth = moment().month() + 1;
const todayYear = moment().year();
const todayDate = moment();

let balance = 0;
for (const dependent of family.dependents as Dependent[]) {
const startMonth = moment(dependent.createdAt as Date).month() + 1;
const startYear = moment(dependent.createdAt as Date).year();
const endMonth = moment(dependent.deactivatedAt as Date).month() + 1;
const endYear = moment(dependent.deactivatedAt as Date).year();
const startDate = moment(dependent.createdAt as Date);
const endDate = moment(dependent.deactivatedAt as Date);

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

// Check all the dates
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 notInFuture = benefitDate.toDate() <= todayDate.endOf('month').toDate();
const afterCreation = benefitDate.toDate() >= startDate.startOf('month').toDate();
const beforeDeactivation = dependent.deactivatedAt
? benefitDate.year() < endYear || (benefitDate.year() === endYear && benefitDate.month() + 1 < endMonth)
? benefitDate.toDate() <= endDate.endOf('month').toDate()
: true;

if (benefit.value && notInFuture && afterCreation && beforeDeactivation) {
// Valid benefit
balance += Number(benefit.value);
Expand Down Expand Up @@ -193,26 +187,17 @@ export const getBalanceReport = async (cityId: NonNullable<City['id']>) => {
*/
export const getFamilyBalance = async (family: Family): Promise<number> => {
// Get all benefits from the family group

const familyStartMonth = moment(family.createdAt as Date).month() + 1;
const familyStartYear = moment(family.createdAt as Date).year();
const todayMonth = moment().month() + 1;
const todayYear = moment().year();
const familyStart = moment(family.createdAt as Date);
const todayDate = moment();

const [benefit] = await db.benefits.findAll({
where: {
[Sequelize.Op.and]: [
{
[Sequelize.Op.or]: [
{ year: { [Sequelize.Op.gt]: familyStartYear } },
{ year: familyStartYear, month: { [Sequelize.Op.gte]: familyStartMonth } }
]
date: { [Sequelize.Op.gte]: familyStart.startOf('month').toDate() }
},
{
[Sequelize.Op.or]: [
{ year: { [Sequelize.Op.lt]: todayYear } },
{ year: todayYear, month: { [Sequelize.Op.lte]: todayMonth } }
]
date: { [Sequelize.Op.lte]: todayDate.endOf('month').toDate() }
},
{ groupName: family.groupName }
]
Expand Down

0 comments on commit de076ac

Please sign in to comment.