Skip to content

Commit

Permalink
Merge pull request #293 from bcgov/add-rangeOfficerName-and-rangeOffi…
Browse files Browse the repository at this point in the history
…cerEmail-parameters-to-email

Add rangeOfficerName and rangeOfficeEmail parameters
brijesh-amin authored Aug 30, 2023

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
2 parents 3edb367 + 5ba2e77 commit 3733e7a
Showing 3 changed files with 30 additions and 20 deletions.
33 changes: 17 additions & 16 deletions src/libs/db2/model/plan.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
15 changes: 12 additions & 3 deletions src/router/controllers_v1/PlanStatusController.js
Original file line number Diff line number Diff line change
@@ -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,15 +155,17 @@ 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.
const status = planStatuses.find(s => s.id === statusId);
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')
2 changes: 1 addition & 1 deletion src/router/routes_v1/report.js
Original file line number Diff line number Diff line change
@@ -141,4 +141,4 @@ router.get('/:planId/', asyncMiddleware(async (req, res) => {
}
}));

module.exports = router;
module.exports = router;

0 comments on commit 3733e7a

Please sign in to comment.