Skip to content

Commit

Permalink
Plan extension improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Brijesh committed Jan 18, 2025
1 parent 0e06b19 commit 19e4837
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 64 deletions.
81 changes: 27 additions & 54 deletions scripts/plan_extension.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { replace } from 'lodash';
import config from '../src/config';
import { PLAN_EXTENSION_STATUS } from '../src/constants';
import DataManager from '../src/libs/db2';
Expand Down Expand Up @@ -27,34 +28,33 @@ const sendEmailToAgreementHolders = async (db, expiringPlan) => {
);
};

const activateReplacementPlans = async (trx) => {
const results = await trx
.select()
.from(Plan.table)
.where('plan_start_date', '<=', new Date())
.andWhere('extension_status', PLAN_EXTENSION_STATUS.INACTIVE_REPLACEMENT_PLAN);
for (const result of results) {
await Plan.update(
trx,
{ id: result.id },
{
extension_status: PLAN_EXTENSION_STATUS.ACTIVE_REPLACEMENT_PLAN,
},
);
await Plan.update(
trx,
{ id: result.replacement_of },
{
extension_status: PLAN_EXTENSION_STATUS.REPLACED_WITH_REPLACEMENT_PLAN,
},
);
}
};

const processExpiredPlans = async (trx) => {
const results = await trx.select().from(Plan.table).where('plan_end_date', '<', new Date()).whereNot('status_id', 26);
// .whereNot({
// extension_status: PLAN_EXTENSION_STATUS.ACTIVE_REPLACEMENT_PLAN,
// });
const results = await trx.select().from(Plan.table).where('plan_end_date', '<', new Date());
for (const result of results) {
console.log(result);
if (result.extension_status === PLAN_EXTENSION_STATUS.REPLACEMENT_PLAN_CREATED) {
try {
console.log(`Replacing planId: ${result.id} with the replacement plan ${result.replacement_plan_id}`);
await Plan.update(
trx,
{ id: result.id },
{
extension_status: PLAN_EXTENSION_STATUS.REPLACED_WITH_REPLACEMENT_PLAN,
},
);
await Plan.update(
trx,
{ id: result.replacement_plan_id },
{
extension_status: PLAN_EXTENSION_STATUS.ACTIVE_REPLACEMENT_PLAN,
},
);
} catch (error) {
console.log(error.stack);
}
}
if (
result.status_id !== 26 &&
result.extension_received_votes !== result.extensionr_received_votes &&
Expand All @@ -67,35 +67,7 @@ const processExpiredPlans = async (trx) => {
status_id: 26,
},
);
} else {
console.log('Not expiring this plan extension in-progress \r\n' + JSON.stringify(result, null, 2));
}
// if (
// [
// PLAN_EXTENSION_STATUS.AWAITING_VOTES,
// PLAN_EXTENSION_STATUS.AGREEMENT_HOLDER_REJECTED,
// PLAN_EXTENSION_STATUS.STAFF_REJECTED,
// PLAN_EXTENSION_STATUS.DISTRICT_MANAGER_REJECTED,
// PLAN_EXTENSION_STATUS.AWAITING_EXTENSION,
// ].includes(result.extension_status)
// ) {
// try {
// console.log(`Removing extension for expired planId: ${result.id}`);
// await Plan.update(
// trx,
// { id: result.id },
// {
// extension_status: null,
// extension_required_votes: 0,
// extension_received_votes: 0,
// extension_date: null,
// extension_rejected_by: null,
// },
// );
// } catch (error) {
// console.log(error.stack);
// }
// }
}
};

Expand Down Expand Up @@ -137,6 +109,7 @@ const main = async () => {
);
}
await processExpiredPlans(trx);
await activateReplacementPlans(trx);
trx.commit();
} catch (err) {
trx.rollback();
Expand Down
16 changes: 7 additions & 9 deletions src/libs/db2/model/agreement.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,13 @@ export default class Agreement extends Model {
q.where(where);
}
q.where(function () {
this.whereNull('plan.extension_status').orWhereNot({
'plan.extension_status': PLAN_EXTENSION_STATUS.INACTIVE_REPLACEMENT_PLAN,
});
this.whereNull('plan.extension_status')
.orWhereNot({
'plan.extension_status': PLAN_EXTENSION_STATUS.INACTIVE_REPLACEMENT_PLAN,
})
.whereNot({
'plan.extension_status': PLAN_EXTENSION_STATUS.REPLACED_WITH_REPLACEMENT_PLAN,
});
});
const columnFilters = filterSettings.columnFilters;
Object.keys(columnFilters).map((key) => {
Expand Down Expand Up @@ -178,12 +182,6 @@ export default class Agreement extends Model {
)
)`,
);
} else if (filterSettings.showReplacedPlans === true) {
q.where(function () {
this.whereNull('plan.extension_status').orWhereNot({
'plan.extension_status': PLAN_EXTENSION_STATUS.REPLACED_WITH_REPLACEMENT_PLAN,
});
});
}
if (filterSettings.page && filterSettings.limit) {
const offset = filterSettings.limit * (filterSettings.page - 1);
Expand Down
2 changes: 1 addition & 1 deletion src/router/controllers_v1/PlanExtensionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ export default class PlanExtensionController {
trx,
{ id: planId },
{
planEndDate: endDate,
extensionStatus: PLAN_EXTENSION_STATUS.EXTENDED,
statusId: 6,
},
);
trx.commit();
Expand Down

0 comments on commit 19e4837

Please sign in to comment.