Last active
September 20, 2016 10:56
-
-
Save loopiezlol/0574fc17e08e681f50677ef22948314d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const co = require('co'); | |
module.exports = function() { | |
co(function* () { | |
const Workout = require('../../app/models/workout'); | |
const workouts = yield Workout.find({ | |
chosen: true, | |
}, { | |
reviews: 1, | |
userId: 1, | |
insights: 1, | |
}).populate('insights') | |
.lean(); | |
for (const workout of workouts) { | |
try { | |
const userReviewsOnWorkoutInsightsMap = {}; | |
const userRevisionsOnWorkoutInsightsMap = {}; | |
workout.insights.forEach(insight => { | |
insight.reviewedBy.forEach(r => { | |
if (r.userId === workout.userId) { | |
if (insight._id in Object.keys(userReviewsOnWorkoutInsightsMap)) { | |
userReviewsOnWorkoutInsightsMap[insight._id].push(r); | |
} else { | |
userReviewsOnWorkoutInsightsMap[insight._id] = []; | |
} | |
} | |
}); | |
insight.revisedBy.forEach(r => { | |
if (r.userId === workout.userId) { | |
if (insight._id in | |
Object.keys(userRevisionsOnWorkoutInsightsMap)) { | |
userRevisionsOnWorkoutInsightsMap[insight._id].push(r); | |
} else { | |
userRevisionsOnWorkoutInsightsMap[insight._id] = []; | |
} | |
} | |
}); | |
}); | |
// sort the reviews | |
Object.keys(userReviewsOnWorkoutInsightsMap).forEach(id => { | |
userReviewsOnWorkoutInsightsMap[id].sort((a, b) => { | |
return new Date(b.reviewedAt - a.reviewdAt); | |
}); | |
}); | |
Object.keys(userRevisionsOnWorkoutInsightsMap).forEach(id => { | |
userRevisionsOnWorkoutInsightsMap[id].sort((a, b) => { | |
return new Date(b.revisedAt - a.revisedAt); | |
}); | |
}); | |
if (!workout.reviews.length) { | |
workout.reviews = []; | |
} | |
// put latest reviews/ revisions on workout | |
workout.insights.forEach(insight => { | |
if (userReviewsOnWorkoutInsightsMap[insight._id]) { | |
userReviewsOnWorkoutInsightsMap[insight._id].forEach(review => { | |
if ((new Date(review.reviewedAt).getDay() === | |
new Date(workout.date).getDay()) | |
&& !isReviewInWorkout(workout, review, insight._id)) { | |
workout.reviews.push({ | |
insightId: insight._id, | |
review: review.review, | |
timeSpent: review.timeSpent, | |
reviewedAt: review.reviewedAt, | |
}); | |
} | |
}); | |
} | |
}); | |
workout.insights.forEach(insight => { | |
if (userRevisionsOnWorkoutInsightsMap[insight._id]) { | |
userRevisionsOnWorkoutInsightsMap[insight._id].forEach(revision => { | |
if ((new Date(revision.revisedAt).getDay() === | |
new Date(workout.date).getDay()) && | |
!isRevisionInWorkout(workout, revision, insight._id)) { // also check for existing review | |
workout.reviews.push({ | |
insightId: insight._id, | |
review: revision.review, | |
timeSpent: revision.timeSpent, | |
reviewedAt: revision.reviewedAt, | |
}); | |
} | |
}); | |
} | |
}); | |
yield workout.update().catch((err) => console.error(err)); | |
} catch (e) { | |
console.error(e); | |
} | |
} | |
process.exit(0); | |
}); | |
function isReviewInWorkout(workout, review, id) { | |
let found = false; | |
workout.reviews.forEach(existingReview => { | |
if (id === existingReview.insightId && | |
new Date(review.reviewedAt).getDay() === | |
new Date(existingReview.reviewedAt).getDay()) { | |
found = true; | |
} | |
}); | |
return found; | |
} | |
function isRevisionInWorkout(workout, revision, id) { | |
let found = false; | |
workout.reviews.forEach(existingReview => { | |
if (id === existingReview.insightId && | |
new Date(revision.revisedAt).getDay() === | |
new Date(existingReview.reviewedAt).getDay()) { | |
found = true; | |
} | |
}); | |
return found; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment