diff --git a/src/libs/db2/model/plan.js b/src/libs/db2/model/plan.js index bc270d62..0b435c8b 100644 --- a/src/libs/db2/model/plan.js +++ b/src/libs/db2/model/plan.js @@ -158,23 +158,24 @@ export default class Plan extends Model { return result.agreement_id; } - // Fetch the Agreement ID associated with a given Plan - static async agreementForPlanId(db, planId) { - if (!db || !planId) { - return []; - } - - const results = await db - .select('*') - .from(Plan.table) - .where({ id: planId }); - - if (results.length === 0) return null; - - const [result] = results; - return result; + // Fetch the Agreement ID associated with a given Plan + static async agreementForPlanId(db, planId) { + if (!db || !planId) { + return []; } + const results = await db + .select('*') + .from(Plan.table) + .join(Agreement.table, { 'plan.agreement_id': 'agreement.forest_file_id' }) + .where({ id: planId }); + + if (results.length === 0) return null; + + const [result] = results; + return result; + } + static async createSnapshot(db, planId, userId) { const [plan] = await Plan.findWithStatusExtension(db, { 'plan.id': planId }, ['id', 'desc']); if (!plan) { @@ -853,4 +854,4 @@ export default class Plan extends Model { this.files = planFiles; } -} +} \ No newline at end of file diff --git a/src/router/controllers_v1/PlanStatusController.js b/src/router/controllers_v1/PlanStatusController.js index 304f8bb7..2f750a12 100644 --- a/src/router/controllers_v1/PlanStatusController.js +++ b/src/router/controllers_v1/PlanStatusController.js @@ -9,6 +9,7 @@ import { Mailer } from '../../libs/mailer'; import Client from '../../libs/db2/model/client'; import User from '../../libs/db2/model/user'; import EmailTemplate from '../../libs/db2/model/emailtemplate'; +import Zone from '../../libs/db2/model/zone'; const dm = new DataManager(config); const { @@ -154,7 +155,7 @@ export default class PlanStatusController { } try { - const { 'agreement_id': agreementId, 'creator_id': creatorId } = await Plan.agreementForPlanId(db, planId); + const { 'agreement_id': agreementId, 'creator_id': creatorId, 'zone_id': zoneId } = await Plan.agreementForPlanId(db, planId); await PlanRouteHelper.canUserAccessThisAgreement(db, Agreement, user, agreementId); const planStatuses = await PlanStatus.find(db, { active: true }); // make sure the status exists. @@ -162,7 +163,9 @@ export default class PlanStatusController { if (!status) { throw errorWithCode('You must supply a valid status ID', 403); } - const plan = await Plan.findOne(db, { id: planId }); + const plan = await Plan.findById(db, planId); + const zone = await Zone.findById(db, zoneId) + const rangeOfficer = await User.findById(db, zone.userId) const { statusId: prevStatusId } = plan; await PlanStatusController.updatePlanStatus(planId, status, user); await PlanStatusHistory.create(db, { @@ -185,7 +188,13 @@ export default class PlanStatusController { const fromStatus = await PlanStatus.findById(db, prevStatusId) const templates = await EmailTemplate.findWithExclusion(db, { 'name': 'Plan Status Change' }) const template = templates[0] - const emailFields = { '{agreementId}': agreementId, '{fromStatus}': fromStatus.name, '{toStatus}': toStatus.name } + const emailFields = { + '{agreementId}': agreementId, + '{fromStatus}': fromStatus.name, + '{toStatus}': toStatus.name, + '{rangeOfficerName}': rangeOfficer.givenName + ' ' + rangeOfficer.familyName, + '{rangeOfficerEmail}': rangeOfficer.email, + } const mailer = new Mailer() mailer.sendEmail(emails, template.fromEmail, substituteFields(template.subject, emailFields), substituteFields(template.body, emailFields), 'html') diff --git a/src/router/routes_v1/report.js b/src/router/routes_v1/report.js index 457e0039..7b6bbf02 100644 --- a/src/router/routes_v1/report.js +++ b/src/router/routes_v1/report.js @@ -141,4 +141,4 @@ router.get('/:planId/', asyncMiddleware(async (req, res) => { } })); -module.exports = router; +module.exports = router; \ No newline at end of file