Files
habitica/migrations/challenges/sync-all-challenges.js
Carl Vuorinen cd90a281c2 Prevent duplicate challenge tasks (#11502)
* Implement atomic user challenges update

Prevents multiple concurrent requests from passing through
Fixes #11295

* Move user challenges update to separate method

* Rename challenge syncToUser to syncTasksToUser

Now that adding the challenge to user is separated, this methods main purpose is to sync the tasks

* Fix lint errors
2019-12-13 14:14:57 +01:00

46 lines
1.2 KiB
JavaScript

import { model as Challenges } from '../../website/server/models/challenge';
import { model as User } from '../../website/server/models/user';
async function syncChallengeToMembers (challenges) {
const challengSyncPromises = challenges.map(async challenge => {
const users = await User.find({
// _id: '',
challenges: challenge._id,
}).exec();
const promises = [];
users.forEach(user => {
promises.push(challenge.syncTasksToUser(user));
promises.push(challenge.save());
promises.push(user.save());
});
return Promise.all(promises);
});
return Promise.all(challengSyncPromises);
}
async function syncChallenges (lastChallengeDate) {
const query = {
// _id: '',
};
if (lastChallengeDate) {
query.createdOn = { $lte: lastChallengeDate };
}
const challengesFound = await Challenges.find(query)
.limit(10)
.sort('-createdAt')
.exec();
const syncedChallenges = await syncChallengeToMembers(challengesFound)
.catch(reason => console.error(reason));
const lastChallenge = challengesFound[challengesFound.length - 1];
if (lastChallenge) syncChallenges(lastChallenge.createdAt);
return syncedChallenges;
}
export default syncChallenges;