@@ -15,6 +15,7 @@ import { checkRequiredFields, objPathToCamelCase, removeCommonFields } from '../
1515import { PlanRouteHelper } from '../helpers' ;
1616import { generatePDFResponse } from './PDFGeneration' ;
1717import PlanExtensionRequests from '../../libs/db2/model/planextensionrequests' ;
18+ import PlanStatusController from './PlanStatusController' ;
1819
1920const dm = new DataManager ( config ) ;
2021const { db, Plan, Agreement, PlanConfirmation, PlanStatus, AdditionalRequirement, PlanFile } = dm ;
@@ -58,60 +59,30 @@ export default class PlanController {
5859 throw errorWithCode ( "Plan doesn't exist" , 404 ) ;
5960 }
6061 const { agreementId } = plan ;
61- const statusId = plan ?. status ?. id ;
62-
63- const isStaff = user . isAdministrator ( ) || user . isRangeOfficer ( ) || user . isDecisionMaker ( ) || user . canReadAll ( ) ;
64-
65- const [ privacyVersionRaw ] = await PlanSnapshot . findSummary ( db , {
66- plan_id : planId ,
67- privacyview : isStaff ? 'StaffView' : 'AHView' ,
68- } ) ;
69- const privacyVersion = privacyVersionRaw ?. snapshot ;
70-
71- const shouldBeLiveVersion = privacyVersion == null ;
72-
7362 await PlanRouteHelper . canUserAccessThisAgreement ( db , Agreement , user , agreementId ) ;
74-
7563 const [ agreement ] = await Agreement . findWithTypeZoneDistrictExemption ( db , { forest_file_id : agreementId } ) ;
7664 await agreement . eagerloadAllOneToManyExceptPlan ( ) ;
7765 agreement . transformToV1 ( ) ;
78-
79- if ( shouldBeLiveVersion ) {
80- await plan . eagerloadAllOneToMany ( ) ;
81- plan . agreement = agreement ;
82-
83- const filteredFiles = filterFiles ( plan . files , user ) ;
84-
85- const mappedGrazingSchedules = await Promise . all (
86- plan . grazingSchedules . map ( async ( schedule ) => {
87- let sanitizedSortBy = schedule . sortBy && objPathToCamelCase ( schedule . sortBy ) ;
88- sanitizedSortBy = sanitizedSortBy && sanitizedSortBy . replace ( 'pastureName' , 'pasture.name' ) ;
89- sanitizedSortBy = sanitizedSortBy && sanitizedSortBy . replace ( 'refLivestockName' , 'livestockType.name' ) ;
90- sanitizedSortBy = sanitizedSortBy && sanitizedSortBy . replace ( 'pldAuMs' , 'pldAUMs' ) ;
91- sanitizedSortBy = sanitizedSortBy && sanitizedSortBy . replace ( 'crownAuMs' , 'crownAUMs' ) ;
92- return {
93- ...schedule ,
94- sortBy : sanitizedSortBy ,
95- } ;
96- } ) ,
97- ) ;
98- return {
99- ...plan ,
100- grazingSchedules : mappedGrazingSchedules ,
101- files : filteredFiles ,
102- } ;
103- }
104-
105- logger . info ( 'loading last version' ) ;
106-
107- privacyVersion . status_id = statusId ;
108-
109- const filteredFiles = filterFiles ( privacyVersion . files , user ) ;
66+ await plan . eagerloadAllOneToMany ( ) ;
67+ plan . agreement = agreement ;
68+ const filteredFiles = filterFiles ( plan . files , user ) ;
69+ const mappedGrazingSchedules = await Promise . all (
70+ plan . grazingSchedules . map ( async ( schedule ) => {
71+ let sanitizedSortBy = schedule . sortBy && objPathToCamelCase ( schedule . sortBy ) ;
72+ sanitizedSortBy = sanitizedSortBy && sanitizedSortBy . replace ( 'pastureName' , 'pasture.name' ) ;
73+ sanitizedSortBy = sanitizedSortBy && sanitizedSortBy . replace ( 'refLivestockName' , 'livestockType.name' ) ;
74+ sanitizedSortBy = sanitizedSortBy && sanitizedSortBy . replace ( 'pldAuMs' , 'pldAUMs' ) ;
75+ sanitizedSortBy = sanitizedSortBy && sanitizedSortBy . replace ( 'crownAuMs' , 'crownAUMs' ) ;
76+ return {
77+ ...schedule ,
78+ sortBy : sanitizedSortBy ,
79+ } ;
80+ } ) ,
81+ ) ;
11082 return {
111- ...privacyVersion ,
83+ ...plan ,
84+ grazingSchedules : mappedGrazingSchedules ,
11285 files : filteredFiles ,
113- status : plan . status ,
114- statusId : plan . statusId ,
11586 } ;
11687 } catch ( error ) {
11788 console . log ( error . stack ) ;
@@ -178,7 +149,6 @@ export default class PlanController {
178149
179150 // Don't allow the agreement relation to be updated.
180151 delete body . agreementId ;
181-
182152 await Plan . update ( db , { id : planId } , body ) ;
183153 const [ plan ] = await Plan . findWithStatusExtension ( db , { 'plan.id' : planId } , [ 'id' , 'desc' ] ) ;
184154 await plan . eagerloadAllOneToMany ( ) ;
@@ -293,15 +263,7 @@ export default class PlanController {
293263 const agreementId = await Plan . agreementIdForPlanId ( db , planId ) ;
294264 await PlanRouteHelper . canUserAccessThisAgreement ( db , Agreement , user , agreementId ) ;
295265
296- const prevLegalVersion = await db
297- . table ( 'plan_snapshot_summary' )
298- . whereIn ( 'status_id' , Plan . legalStatuses )
299- . andWhere ( {
300- plan_id : planId ,
301- } )
302- . orderBy ( 'created_at' , 'desc' )
303- . first ( ) ;
304-
266+ const prevLegalVersion = await PlanStatusController . getLatestLegalVersion ( planId ) ;
305267 if ( ! prevLegalVersion ) {
306268 throw errorWithCode ( 'Could not find previous legal version.' , 500 ) ;
307269 }
@@ -311,7 +273,7 @@ export default class PlanController {
311273 await Plan . restoreVersion ( db , planId , prevLegalVersion . version ) ;
312274
313275 const versionsToDiscard = await db
314- . table ( 'plan_snapshot_summary ' )
276+ . table ( 'plan_snapshot ' )
315277 . select ( 'id' )
316278 . where ( { plan_id : planId } )
317279 . andWhereRaw ( 'created_at > ?::timestamp' , [ prevLegalVersion . created_at . toISOString ( ) ] ) ;
@@ -329,7 +291,7 @@ export default class PlanController {
329291 const { params, user, body } = req ;
330292 const { planId } = params ;
331293
332- if ( ! user || ! user . isRangeOfficer ( ) ) {
294+ if ( ! user || ( ! user . isRangeOfficer ( ) && ! user . isAdministrator ( ) ) ) {
333295 throw errorWithCode ( 'Unauthorized' , 403 ) ;
334296 }
335297
@@ -352,8 +314,7 @@ export default class PlanController {
352314 static async updateAttachment ( req , res ) {
353315 const { params, user, body } = req ;
354316 const { planId, attachmentId } = params ;
355-
356- if ( ! user || ! user . isRangeOfficer ( ) ) {
317+ if ( ! user || ( ! user . isRangeOfficer ( ) && ! user . isAdministrator ( ) ) ) {
357318 throw errorWithCode ( 'Unauthorized' , 403 ) ;
358319 }
359320
@@ -380,7 +341,7 @@ export default class PlanController {
380341 const { params, user } = req ;
381342 const { planId, attachmentId } = params ;
382343
383- if ( ! user || ! user . isRangeOfficer ( ) ) {
344+ if ( ! user || ( ! user . isRangeOfficer ( ) && ! user . isAdministrator ( ) ) ) {
384345 throw errorWithCode ( 'Unauthorized' , 403 ) ;
385346 }
386347
@@ -586,8 +547,10 @@ export default class PlanController {
586547 const { planId } = params ;
587548 const plan = await PlanController . fetchPlan ( planId , user ) ;
588549 plan . originalApproval = await PlanStatusHistory . fetchOriginalApproval ( db , planId ) ;
589- const amendmentSubmissions = await PlanStatusHistory . fetchAmendmentSubmissions ( db , planId ) ;
590- plan . amendmentSubmissions = amendmentSubmissions ;
550+ const amendmentSubmissions = await PlanSnapshot . fetchAmendmentSubmissions ( db , planId ) ;
551+ plan . amendmentSubmissions = amendmentSubmissions . filter ( ( item ) => {
552+ return item . amendmentType !== null ;
553+ } ) ;
591554 const response = await generatePDFResponse ( plan ) ;
592555 res . json ( response . data ) . end ( ) ;
593556 }
0 commit comments