@@ -10,6 +10,7 @@ import { Workflow } from './workflow.model';
1010import { Record } from './record.model' ;
1111import { Resource } from './resource.model' ;
1212import { ReferenceData } from './referenceData.model' ;
13+ import { has } from 'lodash' ;
1314
1415/** Interface for the page context */
1516export type PageContextT = (
@@ -49,6 +50,8 @@ export interface Page extends Document {
4950 canDelete ?: ( mongoose . Types . ObjectId | Role ) [ ] ;
5051 } ;
5152 visible : boolean ;
53+ archived : boolean ;
54+ archivedAt ?: Date ;
5255}
5356
5457/** Mongoose page schema declaration */
@@ -109,12 +112,101 @@ const pageSchema = new Schema<Page>(
109112 type : Boolean ,
110113 default : true ,
111114 } ,
115+ archived : {
116+ type : Boolean ,
117+ default : false ,
118+ } ,
119+ archivedAt : {
120+ type : Date ,
121+ expires : 2592000 ,
122+ } ,
112123 } ,
113124 {
114125 timestamps : { createdAt : 'createdAt' , updatedAt : 'modifiedAt' } ,
115126 }
116127) ;
117128
129+ // We need to declare it like that, otherwise we cannot access the 'this'.
130+ pageSchema . pre ( [ 'updateOne' , 'findOneAndUpdate' ] , async function ( ) {
131+ const update = this . getUpdate ( ) ;
132+ if ( has ( update , 'archived' ) ) {
133+ const page : Page = await this . clone ( ) . findOne ( ) ;
134+ switch ( page . type ) {
135+ case contentType . workflow : {
136+ const workflow = await Workflow . findById ( page . content ) ;
137+ if ( workflow ) {
138+ // eslint-disable-next-line @typescript-eslint/dot-notation
139+ if ( update [ 'archived' ] ) {
140+ await workflow . updateOne ( {
141+ archived : true ,
142+ // eslint-disable-next-line @typescript-eslint/dot-notation
143+ archivedAt : update [ 'archivedAt' ] ,
144+ } ) ;
145+ } else {
146+ await workflow . updateOne ( {
147+ archived : false ,
148+ archivedAt : null ,
149+ } ) ;
150+ }
151+ }
152+ break ;
153+ }
154+ case contentType . dashboard : {
155+ const dashboard = await Dashboard . findById ( page . content ) ;
156+ if ( dashboard ) {
157+ // eslint-disable-next-line @typescript-eslint/dot-notation
158+ if ( update [ 'archived' ] ) {
159+ dashboard . archived = true ;
160+ // eslint-disable-next-line @typescript-eslint/dot-notation
161+ dashboard . archivedAt = update [ 'archivedAt' ] ;
162+ await dashboard . save ( ) ;
163+ } else {
164+ dashboard . archived = false ;
165+ // eslint-disable-next-line @typescript-eslint/dot-notation
166+ dashboard . archivedAt = null ;
167+ await dashboard . save ( ) ;
168+ }
169+ }
170+ if ( page . contentWithContext ) {
171+ const dashboards : Dashboard [ ] = [ ] ;
172+ page . contentWithContext . forEach ( ( item : any ) => {
173+ if ( item . content ) {
174+ dashboards . push ( item . content ) ;
175+ }
176+ } ) ;
177+ // eslint-disable-next-line @typescript-eslint/dot-notation
178+ if ( update [ 'archived' ] ) {
179+ await Dashboard . updateMany (
180+ { _id : { $in : dashboards } } ,
181+ {
182+ $set : {
183+ archived : true ,
184+ // eslint-disable-next-line @typescript-eslint/dot-notation
185+ archivedAt : update [ 'archivedAt' ] ,
186+ } ,
187+ }
188+ ) ;
189+ } else {
190+ await Dashboard . updateMany (
191+ { _id : { $in : dashboards } } ,
192+ {
193+ $set : {
194+ archived : false ,
195+ archivedAt : null ,
196+ } ,
197+ }
198+ ) ;
199+ }
200+ }
201+ break ;
202+ }
203+ default : {
204+ break ;
205+ }
206+ }
207+ }
208+ } ) ;
209+
118210// handle cascading deletion and references deletion for pages
119211addOnBeforeDeleteMany ( pageSchema , async ( pages ) => {
120212 // CASCADE DELETION
0 commit comments