11import { startDatabaseForMigration } from '../src/utils/migrations/database.helper' ;
22import get from 'lodash/get' ;
33import set from 'lodash/set' ;
4- import { contentType } from '@const/enumTypes' ;
5- import { Application , Dashboard , Page , Aggregation , Form } from '../src/models' ;
4+ import {
5+ Application ,
6+ Dashboard ,
7+ Aggregation ,
8+ Form ,
9+ Step ,
10+ Page ,
11+ Workflow ,
12+ } from '../src/models' ;
613import { logger } from '../src/services/logger.service' ;
714
15+ /**
16+ * Get parent application from dashboard. Including dashboard in step.
17+ *
18+ * @param dashboardId Id of the dashboard.
19+ * @param applications List of populated applications.
20+ * @param populatedApplications List of fully populated applications.
21+ * @returns Parent application.
22+ */
23+ const getApplication = (
24+ dashboardId : any ,
25+ applications : Application [ ] ,
26+ populatedApplications : Application [ ]
27+ ) : Application => {
28+ const application = applications . find ( ( app ) =>
29+ app . pages . some ( ( page ) => ( page as Page ) ?. content ?. equals ( dashboardId ) )
30+ ) ;
31+ if ( ! ! application ) return application ;
32+ return populatedApplications . find ( ( app ) =>
33+ app . pages . some ( ( page ) =>
34+ ( ( page as Page ) ?. content as Workflow ) ?. steps ?. some ( ( step ) =>
35+ ( step as Step ) ?. content ?. equals ( dashboardId )
36+ )
37+ )
38+ ) ;
39+ } ;
40+
841/**
942 * Use to aggregations migrate up.
1043 *
@@ -13,89 +46,99 @@ import { logger } from '../src/services/logger.service';
1346export const up = async ( ) => {
1447 await startDatabaseForMigration ( ) ;
1548 try {
49+ const dashboards = await Dashboard . find ( { } ) ;
1650 const applications = await Application . find ( )
1751 . populate ( {
1852 path : 'pages' ,
1953 model : 'Page' ,
2054 } )
2155 . select ( 'name pages' ) ;
22-
23- for ( const application of applications ) {
24- if ( application . pages . length > 0 ) {
25- // Update dashboard pages
26- const dashboards = await Dashboard . find ( {
27- _id : {
28- $in : application . pages
29- . filter ( ( x : Page ) => x . type === contentType . dashboard )
30- . map ( ( x : any ) => x . content ) ,
56+ const populatedApplications = await Application . find ( )
57+ . populate ( {
58+ path : 'pages' ,
59+ model : 'Page' ,
60+ populate : {
61+ path : 'content' ,
62+ model : 'Workflow' ,
63+ populate : {
64+ path : 'steps' ,
65+ model : 'Step' ,
3166 } ,
32- } ) ;
33- for ( const dashboard of dashboards ) {
34- if ( ! ! dashboard . structure ) {
35- let index = 0 ;
36- for ( const widget of dashboard . structure ) {
37- if (
38- widget &&
39- widget . component == 'chart' &&
40- get ( widget , 'settings.chart.aggregation' , null ) &&
41- ! get ( widget , 'settings.chart.aggregationId' , null )
42- ) {
43- if ( widget . settings ?. chart . aggregation . dataSource ) {
44- const aggregation : Aggregation = get (
45- widget ,
46- 'settings.chart.aggregation' ,
47- null
48- ) ;
49- aggregation . name = `${ widget . settings . title } - ${ application . name } ` ;
50- const dataSourceId = get (
51- widget ,
52- 'settings.chart.aggregation.dataSource' ,
53- null
54- ) ;
67+ } ,
68+ } )
69+ . select ( 'name pages' ) ;
70+ for ( const dashboard of dashboards ) {
71+ if ( ! ! dashboard . structure ) {
72+ let index = 0 ;
73+ for ( const widget of dashboard . structure ) {
74+ if (
75+ widget &&
76+ widget . component == 'chart' &&
77+ get ( widget , 'settings.chart.aggregation' , null ) &&
78+ ! get ( widget , 'settings.chart.aggregationId' , null )
79+ ) {
80+ if ( widget . settings ?. chart . aggregation . dataSource ) {
81+ const aggregation : Aggregation = get (
82+ widget ,
83+ 'settings.chart.aggregation' ,
84+ null
85+ ) ;
86+ const application = await getApplication (
87+ dashboard . _id ,
88+ applications ,
89+ populatedApplications
90+ ) ;
91+ aggregation . name = `${ widget . settings . title } - ${ application ?. name } ` ;
92+ const dataSourceId = get (
93+ widget ,
94+ 'settings.chart.aggregation.dataSource' ,
95+ null
96+ ) ;
5597
56- if ( dataSourceId ) {
57- //get form and resource
58- const form = await Form . findById ( dataSourceId ) . populate (
59- 'resource'
60- ) ;
98+ if ( dataSourceId ) {
99+ //get form and resource
100+ const form = await Form . findById ( dataSourceId ) . populate (
101+ 'resource'
102+ ) ;
61103
62- if ( form ) {
63- form . resource . aggregations . push ( aggregation ) ;
104+ if ( form ) {
105+ form . resource . aggregations . push ( aggregation ) ;
64106
65- //save aggregation object in the resource
66- const resource = await form . resource . save ( ) ;
107+ //save aggregation object in the resource
108+ const resource = await form . resource . save ( ) ;
109+ logger . info (
110+ `Add Aggregation ${ aggregation . name } to Resource ${ resource . name } `
111+ ) ;
67112
68- set ( widget , 'settings.resource' , resource . id ) ;
69- set (
70- widget ,
71- 'settings.chart.aggregationId' ,
72- resource . aggregations . pop ( ) . id
73- ) ;
74- set (
75- widget ,
76- 'settings.chart.mapping' ,
77- get ( widget , 'settings.chart.aggregation.mapping' , null )
78- ) ;
113+ set ( widget , 'settings.resource' , resource . id ) ;
114+ set (
115+ widget ,
116+ 'settings.chart.aggregationId' ,
117+ resource . aggregations . pop ( ) . id
118+ ) ;
119+ set (
120+ widget ,
121+ 'settings.chart.mapping' ,
122+ get ( widget , 'settings.chart.aggregation.mapping' , null )
123+ ) ;
79124
80- dashboard . structure [ index ] = widget ;
125+ dashboard . structure [ index ] = widget ;
81126
82- //add aggregation id in the dashboard documents
83- await Dashboard . findByIdAndUpdate (
84- dashboard . _id ,
85- {
86- structure : dashboard . structure ,
87- } ,
88- { new : true }
89- ) ;
90- } else {
91- logger . info ( 'skip: related resource / form not found' ) ;
92- }
93- }
127+ //add aggregation id in the dashboard documents
128+ await Dashboard . findByIdAndUpdate (
129+ dashboard . _id ,
130+ {
131+ structure : dashboard . structure ,
132+ } ,
133+ { new : true }
134+ ) ;
135+ } else {
136+ logger . info ( 'skip: related resource / form not found' ) ;
94137 }
95138 }
96- index ++ ;
97139 }
98140 }
141+ index ++ ;
99142 }
100143 }
101144 }
0 commit comments