@@ -3,6 +3,8 @@ import User from '../models/user';
33import Project from '../models/project' ;
44import Collection from '../models/collection' ;
55import { moveObjectToUserInS3 } from '../controllers/aws.controller' ;
6+ import mail from '../utils/mail' ;
7+ import { renderAccountConsolidation } from '../views/mail' ;
68
79
810const mongoConnectionString = process . env . MONGO_URL ;
@@ -50,16 +52,31 @@ const agg = [
5052 }
5153] ;
5254
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+
5368let currentUser = null ;
5469let duplicates = null ;
5570User . aggregate ( agg ) . then ( ( result ) => {
71+ console . log ( result ) ;
5672 const email = result [ 0 ] . _id ;
5773 return User . find ( { email } ) . collation ( { locale : 'en' , strength : 2 } )
5874 . sort ( { createdAt : 1 } ) . exec ( ) ;
5975} ) . then ( ( result ) => {
6076 [ currentUser , ...duplicates ] = result ;
77+ console . log ( 'Current User: ' , currentUser . _id , ' ' , currentUser . email ) ;
6178 duplicates = duplicates . map ( dup => dup . _id ) ;
62- console . log ( duplicates ) ;
79+ console . log ( 'Duplicates: ' , duplicates ) ;
6380 return Project . find ( {
6481 user : { $in : duplicates }
6582 } ) . exec ( ) ;
@@ -68,7 +85,7 @@ User.aggregate(agg).then((result) => {
6885 sketches . forEach ( ( sketch ) => {
6986 const moveSketchFilesPromises = [ ] ;
7087 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 ) ) {
7289 const fileSavePromise = moveObjectToUserInS3 ( file . url , currentUser . _id )
7390 . then ( ( newUrl ) => {
7491 file . url = newUrl ;
@@ -83,19 +100,38 @@ User.aggregate(agg).then((result) => {
83100 saveSketchPromises . push ( sketchSavePromise ) ;
84101 } ) ;
85102 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+ } ) ;
100132} ) ;
101133
134+ // ).then((result) => {
135+ // console.log(result);
136+ // });
137+
0 commit comments