aboutsummaryrefslogtreecommitdiff
path: root/scripts/migrateImages.ts
diff options
context:
space:
mode:
authorEugene Sokolov <eug-vs@keemail.me>2020-08-12 21:17:39 +0300
committerGitHub <noreply@github.com>2020-08-12 21:17:39 +0300
commit3e966272b855b4c167582abf21439429d11c2c8b (patch)
tree0425766e8fa6bad616f687e660e2469e914c6a98 /scripts/migrateImages.ts
parent5462174ca076aef53d06b92372141c665c923ea3 (diff)
parent4194a35550bb962563f447b91811759f0d291f75 (diff)
downloadwhich-api-3e966272b855b4c167582abf21439429d11c2c8b.tar.gz
Merge pull request #20 from which-ecosystem/s3-reuploads
Improve reuploads and add script
Diffstat (limited to 'scripts/migrateImages.ts')
-rw-r--r--scripts/migrateImages.ts46
1 files changed, 46 insertions, 0 deletions
diff --git a/scripts/migrateImages.ts b/scripts/migrateImages.ts
new file mode 100644
index 0000000..771880e
--- /dev/null
+++ b/scripts/migrateImages.ts
@@ -0,0 +1,46 @@
+import mongoose from 'mongoose';
+import bluebird from 'bluebird';
+import _ from 'lodash';
+import {
+ User,
+ Poll,
+ Vote,
+ Feedback
+} from 'which-types';
+
+import app from '../app';
+app.service('files').setup(app);
+
+const MONGODB_URL = process.env.MONGODB_URI || 'mongodb://localhost:27017/which';
+
+mongoose.connect(MONGODB_URL, {
+ useNewUrlParser: true,
+ useUnifiedTopology: true,
+ useCreateIndex: true,
+ useFindAndModify: false,
+ family: 4 // Use IPv4, skip trying IPv6
+});
+
+const patchPoll = (poll: Poll): Promise<Poll> => {
+ console.log(`Patching poll of user ${poll.author.username}`)
+ return app.service('polls').patch(poll._id.toString(), {}, { user: poll.author, authenticated: true });
+};
+
+const patchUser = (user: User): Promise<User> => {
+ console.log(`Patching user ${user.username}`)
+ return app.service('users').patch(user._id.toString(), {}, { user, authenticated: true });
+};
+
+const update = async () => {
+ const users = app.service('users').find();
+
+ await bluebird.mapSeries(users, async (user: User) => {
+ await patchUser(user);
+ const polls = await app.service('polls').find({ query: { authorId: user._id }});
+ await bluebird.mapSeries(polls, (poll: Poll) => patchPoll(poll));
+ return;
+ });
+};
+
+update();
+