|
| 1 | +import mongoose from 'mongoose'; |
| 2 | +import User from '../models/user'; |
| 3 | +import Project from '../models/project'; |
| 4 | +import Collection from '../models/collection'; |
| 5 | +import { moveObjectToUserInS3 } from '../controllers/aws.controller'; |
| 6 | + |
| 7 | + |
| 8 | +const mongoConnectionString = process.env.MONGO_URL; |
| 9 | +const { ObjectId } = mongoose.Types; |
| 10 | +// Connect to MongoDB |
| 11 | +mongoose.Promise = global.Promise; |
| 12 | +mongoose.connect(mongoConnectionString, { useNewUrlParser: true, useUnifiedTopology: true }); |
| 13 | +mongoose.set('useCreateIndex', true); |
| 14 | +mongoose.connection.on('error', () => { |
| 15 | + console.error('MongoDB Connection Error. Please make sure that MongoDB is running.'); |
| 16 | + process.exit(1); |
| 17 | +}); |
| 18 | + |
| 19 | +/* |
| 20 | + * Requires the MongoDB Node.js Driver |
| 21 | + * https://mongodb.github.io/node-mongodb-native |
| 22 | + */ |
| 23 | + |
| 24 | +const agg = [ |
| 25 | + { |
| 26 | + $project: { |
| 27 | + email: { |
| 28 | + $toLower: [ |
| 29 | + '$email' |
| 30 | + ] |
| 31 | + } |
| 32 | + } |
| 33 | + }, { |
| 34 | + $group: { |
| 35 | + _id: '$email', |
| 36 | + total: { |
| 37 | + $sum: 1 |
| 38 | + } |
| 39 | + } |
| 40 | + }, { |
| 41 | + $match: { |
| 42 | + total: { |
| 43 | + $gt: 1 |
| 44 | + } |
| 45 | + } |
| 46 | + }, { |
| 47 | + $sort: { |
| 48 | + total: -1 |
| 49 | + } |
| 50 | + } |
| 51 | +]; |
| 52 | + |
| 53 | +let currentUser = null; |
| 54 | +let duplicates = null; |
| 55 | +User.aggregate(agg).then((result) => { |
| 56 | + const email = result[0]._id; |
| 57 | + return User.find({ email }).collation({ locale: 'en', strength: 2 }) |
| 58 | + .sort({ createdAt: 1 }).exec(); |
| 59 | +}).then((result) => { |
| 60 | + [currentUser, ...duplicates] = result; |
| 61 | + duplicates = duplicates.map(dup => dup._id); |
| 62 | + console.log(duplicates); |
| 63 | + return Project.find({ |
| 64 | + user: { $in: duplicates } |
| 65 | + }).exec(); |
| 66 | +}).then((sketches) => { |
| 67 | + const saveSketchPromises = []; |
| 68 | + sketches.forEach((sketch) => { |
| 69 | + const moveSketchFilesPromises = []; |
| 70 | + sketch.files.forEach((file) => { |
| 71 | + if (file.url.includes('assets.editor.p5js.org')) { |
| 72 | + const fileSavePromise = moveObjectToUserInS3(file.url, currentUser._id) |
| 73 | + .then((newUrl) => { |
| 74 | + file.url = newUrl; |
| 75 | + }); |
| 76 | + moveSketchFilesPromises.push(fileSavePromise); |
| 77 | + } |
| 78 | + }); |
| 79 | + const sketchSavePromise = Promise.all(moveSketchFilesPromises).then(() => { |
| 80 | + sketch.user = ObjectId(currentUser._id); |
| 81 | + return sketch.save(); |
| 82 | + }); |
| 83 | + saveSketchPromises.push(sketchSavePromise); |
| 84 | + }); |
| 85 | + 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); |
| 100 | +}); |
| 101 | + |
0 commit comments