mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-16 14:17:22 +01:00
* Aded challenge migration sync * Fixed async grouping * Mapped promises and added error catching * Added placholders for syncing specific challenges * Prvented overriding of attribute and createdAt
48 lines
1.3 KiB
JavaScript
48 lines
1.3 KiB
JavaScript
import Bluebird from 'Bluebird';
|
|
|
|
import { model as Challenges } from '../../website/server/models/challenge';
|
|
import { model as User } from '../../website/server/models/user';
|
|
|
|
async function syncChallengeToMembers (challenges) {
|
|
let challengSyncPromises = challenges.map(async function (challenge) {
|
|
let users = await User.find({
|
|
// _id: '',
|
|
challenges: challenge._id,
|
|
}).exec();
|
|
|
|
let promises = [];
|
|
users.forEach(function (user) {
|
|
promises.push(challenge.syncToUser(user));
|
|
promises.push(challenge.save());
|
|
promises.push(user.save());
|
|
});
|
|
|
|
return Bluebird.all(promises);
|
|
});
|
|
|
|
return await Bluebird.all(challengSyncPromises);
|
|
}
|
|
|
|
async function syncChallenges (lastChallengeDate) {
|
|
let query = {
|
|
// _id: '',
|
|
};
|
|
|
|
if (lastChallengeDate) {
|
|
query.createdOn = { $lte: lastChallengeDate };
|
|
}
|
|
|
|
let challengesFound = await Challenges.find(query)
|
|
.limit(10)
|
|
.sort('-createdAt')
|
|
.exec();
|
|
|
|
let syncedChallenges = await syncChallengeToMembers(challengesFound)
|
|
.catch(reason => console.error(reason));
|
|
let lastChallenge = challengesFound[challengesFound.length - 1];
|
|
if (lastChallenge) syncChallenges(lastChallenge.createdAt);
|
|
return syncedChallenges;
|
|
};
|
|
|
|
module.exports = syncChallenges;
|