@@ -3,6 +3,8 @@ import User from '../models/user';
3
3
import Project from '../models/project' ;
4
4
import Collection from '../models/collection' ;
5
5
import { moveObjectToUserInS3 } from '../controllers/aws.controller' ;
6
+ import mail from '../utils/mail' ;
7
+ import { renderAccountConsolidation } from '../views/mail' ;
6
8
7
9
8
10
const mongoConnectionString = process . env . MONGO_URL ;
@@ -50,16 +52,31 @@ const agg = [
50
52
}
51
53
] ;
52
54
55
+
56
+ // steps to make this work
57
+ // iterate through the results
58
+ // check if any files are on AWS
59
+ // if so, move them to the right user bucket
60
+ // then, update the user to currentUser
61
+ // then, after updating all of the projects
62
+ // also update the collections
63
+ // delete other users
64
+ // update user email so it is all lowercase
65
+ // then, send the email
66
+ // then, figure out how to iterate through all of the users.
67
+
53
68
let currentUser = null ;
54
69
let duplicates = null ;
55
70
User . aggregate ( agg ) . then ( ( result ) => {
71
+ console . log ( result ) ;
56
72
const email = result [ 0 ] . _id ;
57
73
return User . find ( { email } ) . collation ( { locale : 'en' , strength : 2 } )
58
74
. sort ( { createdAt : 1 } ) . exec ( ) ;
59
75
} ) . then ( ( result ) => {
60
76
[ currentUser , ...duplicates ] = result ;
77
+ console . log ( 'Current User: ' , currentUser . _id , ' ' , currentUser . email ) ;
61
78
duplicates = duplicates . map ( dup => dup . _id ) ;
62
- console . log ( duplicates ) ;
79
+ console . log ( 'Duplicates: ' , duplicates ) ;
63
80
return Project . find ( {
64
81
user : { $in : duplicates }
65
82
} ) . exec ( ) ;
@@ -68,7 +85,7 @@ User.aggregate(agg).then((result) => {
68
85
sketches . forEach ( ( sketch ) => {
69
86
const moveSketchFilesPromises = [ ] ;
70
87
sketch . files . forEach ( ( file ) => {
71
- if ( file . url . includes ( 'assets.editor.p5js.org' ) ) {
88
+ if ( file . url && file . url . includes ( process . env . S3_BUCKET_URL_BASE ) ) {
72
89
const fileSavePromise = moveObjectToUserInS3 ( file . url , currentUser . _id )
73
90
. then ( ( newUrl ) => {
74
91
file . url = newUrl ;
@@ -83,19 +100,38 @@ User.aggregate(agg).then((result) => {
83
100
saveSketchPromises . push ( sketchSavePromise ) ;
84
101
} ) ;
85
102
return Promise . all ( saveSketchPromises ) ;
86
- // iterate through the results
87
- // check if any files are on AWS
88
- // if so, move them to the right user bucket
89
- // then, update the user to currentUser
90
- // then, after updating all of the projects
91
- // also update the collections
92
- // delete other users
93
- // update user email so it is all lowercase
94
- // then, send the email
95
- } ) . then ( ( ) => Collection . updateMany (
96
- { owner : { $in : duplicates } } ,
97
- { $set : { owner : ObjectId ( currentUser . id ) } }
98
- ) ) . then ( ( ) => User . deleteMany ( { _id : { $in : duplicates } } ) ) . catch ( ( err ) => {
99
- console . log ( err ) ;
103
+ } ) . then ( ( ) => {
104
+ console . log ( 'Moved and updated all sketches.' ) ;
105
+ return Collection . updateMany (
106
+ { owner : { $in : duplicates } } ,
107
+ { $set : { owner : ObjectId ( currentUser . id ) } }
108
+ ) ;
109
+ } ) . then ( ( ) => {
110
+ console . log ( 'Moved and updated all collections.' ) ;
111
+ return User . deleteMany ( { _id : { $in : duplicates } } ) ;
112
+ } ) . then ( ( ) => {
113
+ console . log ( 'Deleted other user accounts.' ) ;
114
+ currentUser . email = currentUser . email . toLowerCase ( ) ;
115
+ return currentUser . save ( ) ;
116
+ } ) . then ( ( ) => {
117
+ console . log ( 'Migrated email to lowercase.' ) ;
118
+ // const protocol = process.env.NODE_ENV === 'production' ? 'https' : 'http';
119
+ const mailOptions = renderAccountConsolidation ( {
120
+ body : {
121
+ domain : 'https://editor.p5js.org' ,
122
+ username : currentUser . username ,
123
+ email : currentUser . email
124
+ } ,
125
+ to : currentUser . email ,
126
+ } ) ;
127
+
128
+ mail . send ( mailOptions , ( mailErr , result ) => {
129
+ console . log ( 'Sent email.' ) ;
130
+ process . exit ( 0 ) ;
131
+ } ) ;
100
132
} ) ;
101
133
134
+ // ).then((result) => {
135
+ // console.log(result);
136
+ // });
137
+
0 commit comments