From 19e48378f81f1f4919096ed44e536e1e72923f6a Mon Sep 17 00:00:00 2001 From: Brijesh Date: Fri, 17 Jan 2025 07:13:23 -0800 Subject: [PATCH] Plan extension improvements --- scripts/plan_extension.js | 81 +++++++------------ src/libs/db2/model/agreement.js | 16 ++-- .../controllers_v1/PlanExtensionController.js | 2 +- 3 files changed, 35 insertions(+), 64 deletions(-) diff --git a/scripts/plan_extension.js b/scripts/plan_extension.js index afe8e890..af2d41f0 100644 --- a/scripts/plan_extension.js +++ b/scripts/plan_extension.js @@ -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'; @@ -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 && @@ -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); - // } - // } } }; @@ -137,6 +109,7 @@ const main = async () => { ); } await processExpiredPlans(trx); + await activateReplacementPlans(trx); trx.commit(); } catch (err) { trx.rollback(); diff --git a/src/libs/db2/model/agreement.js b/src/libs/db2/model/agreement.js index d7aada95..dc7e9d4c 100644 --- a/src/libs/db2/model/agreement.js +++ b/src/libs/db2/model/agreement.js @@ -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) => { @@ -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); diff --git a/src/router/controllers_v1/PlanExtensionController.js b/src/router/controllers_v1/PlanExtensionController.js index 1ede247b..b381f519 100644 --- a/src/router/controllers_v1/PlanExtensionController.js +++ b/src/router/controllers_v1/PlanExtensionController.js @@ -331,8 +331,8 @@ export default class PlanExtensionController { trx, { id: planId }, { + planEndDate: endDate, extensionStatus: PLAN_EXTENSION_STATUS.EXTENDED, - statusId: 6, }, ); trx.commit();