@@ -15,17 +15,14 @@ function transformSection(section) {
1515 }
1616
1717 if ( activity . resourceType ) transformedResource . resourceType = activity . resourceType
18- if ( activity . resource . availability ) {
18+ if ( activity . resource ? .availability ) {
1919 transformedResource . availability = {
2020 status : activity . resource . availability . status ,
2121 date : activity . resource . availability . date
2222 }
2323 }
2424
25- if ( activity . completionStatus ) transformedResource . completionStatus = activity . completionStatus
26- else {
27- transformedResource . completionStatus = 'active'
28- }
25+ transformedResource . completionStatus = activity . completionStatus || 'active'
2926
3027 return transformedResource
3128 } )
@@ -56,6 +53,11 @@ async function processSections(db, cooperation) {
5653
5754module . exports = {
5855 async up ( db ) {
56+ await db
57+ . collection ( 'cooperation' )
58+ . aggregate ( [ { $match : { } } , { $out : 'cooperation_backup' } ] )
59+ . toArray ( )
60+
5961 const allUserIds = new Set (
6062 (
6163 await db
@@ -71,14 +73,12 @@ module.exports = {
7173 await db . collection ( 'cooperation' ) . deleteOne ( { _id : cooperation . _id } )
7274 }
7375
74- if ( Array . isArray ( cooperation . proficiencyLevel ) ) {
75- if ( cooperation . proficiencyLevel . length > 0 ) {
76- const averageProficiencyLevelIndex = cooperation . proficiencyLevel . length / 2
77- const averageProficiencyLevel = cooperation . proficiencyLevel [ Math . ceil ( averageProficiencyLevelIndex ) ]
78- await db
79- . collection ( 'cooperation' )
80- . updateOne ( { _id : cooperation . _id } , { $set : { proficiencyLevel : averageProficiencyLevel } } )
81- }
76+ if ( Array . isArray ( cooperation . proficiencyLevel ) && cooperation . proficiencyLevel . length > 0 ) {
77+ const averageProficiencyLevelIndex = cooperation . proficiencyLevel . length / 2
78+ const averageProficiencyLevel = cooperation . proficiencyLevel [ Math . ceil ( averageProficiencyLevelIndex ) ]
79+ await db
80+ . collection ( 'cooperation' )
81+ . updateOne ( { _id : cooperation . _id } , { $set : { proficiencyLevel : averageProficiencyLevel } } )
8282 }
8383
8484 if ( ! cooperation . title ) {
@@ -93,6 +93,7 @@ module.exports = {
9393 }
9494 await processSections ( db , cooperation )
9595 }
96+
9697 await db . collection ( 'cooperation' ) . updateMany (
9798 {
9899 $or : [ { category : { $exists : true } } , { subject : { $exists : true } } , { languages : { $exists : true } } ]
@@ -117,6 +118,21 @@ module.exports = {
117118 }
118119 )
119120 } ,
120- down ( ) { } ,
121+
122+ async down ( db ) {
123+ const backupExists = await db . listCollections ( { name : 'cooperation_backup' } ) . hasNext ( )
124+ if ( ! backupExists ) {
125+ throw new Error ( "Backup 'cooperation_backup' not found. Rollback is not possible." )
126+ }
127+
128+ await db . collection ( 'cooperation' ) . drop ( )
129+
130+ await db
131+ . collection ( 'cooperation_backup' )
132+ . aggregate ( [ { $match : { } } , { $out : 'cooperation' } ] )
133+ . toArray ( )
134+
135+ await db . collection ( 'cooperation_backup' ) . drop ( )
136+ } ,
121137 transformSection
122138}
0 commit comments