|
| 1 | +import mongoose from 'mongoose'; |
| 2 | +import bluebird from 'bluebird'; |
| 3 | +import _ from 'lodash'; |
| 4 | +import { |
| 5 | + User, |
| 6 | + Poll, |
| 7 | + Vote, |
| 8 | + Feedback |
| 9 | +} from 'which-types'; |
| 10 | + |
| 11 | +import app from '../app'; |
| 12 | +app.service('files').setup(app); |
| 13 | + |
| 14 | +const MONGODB_URL = process.env.MONGODB_URI || 'mongodb://localhost:27017/which'; |
| 15 | + |
| 16 | +mongoose.connect(MONGODB_URL, { |
| 17 | + useNewUrlParser: true, |
| 18 | + useUnifiedTopology: true, |
| 19 | + useCreateIndex: true, |
| 20 | + useFindAndModify: false, |
| 21 | + family: 4 // Use IPv4, skip trying IPv6 |
| 22 | +}); |
| 23 | + |
| 24 | +const patchPoll = (poll: Poll): Promise<Poll> => { |
| 25 | + console.log(`Patching poll of user ${poll.author.username}`) |
| 26 | + return app.service('polls').patch(poll._id.toString(), {}, { user: poll.author, authenticated: true }); |
| 27 | +}; |
| 28 | + |
| 29 | +const patchUser = (user: User): Promise<User> => { |
| 30 | + console.log(`Patching user ${user.username}`) |
| 31 | + return app.service('users').patch(user._id.toString(), {}, { user, authenticated: true }); |
| 32 | +}; |
| 33 | + |
| 34 | +const update = async () => { |
| 35 | + const users = app.service('users').find(); |
| 36 | + |
| 37 | + await bluebird.mapSeries(users, async (user: User) => { |
| 38 | + await patchUser(user); |
| 39 | + const polls = await app.service('polls').find({ query: { authorId: user._id }}); |
| 40 | + await bluebird.mapSeries(polls, (poll: Poll) => patchPoll(poll)); |
| 41 | + return; |
| 42 | + }); |
| 43 | +}; |
| 44 | + |
| 45 | +update(); |
| 46 | + |
0 commit comments