-
Notifications
You must be signed in to change notification settings - Fork 46
/
006-event-teams.js
46 lines (39 loc) · 1.19 KB
/
006-event-teams.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const async = require('async')
, mongoose = require('mongoose')
, Team = require('../models/team')
, Event = require('../models/event');
exports.id = '006-event-teams';
exports.up = function(done) {
this.log('creating team for each event');
mongoose.model('Team').collection.dropIndexes(function (err) {
if (err) console.log('could not drop indexes', err);
Event.getEvents(function(err, events) {
async.each(events, function(event, done) {
Team.createTeamForEvent(event, function(err, eventTeam) {
if (err) {
console.log('Error creating team for event ' + event.name);
return done(err);
}
console.log('Created team ' + eventTeam.name + ' for event ' + event.name);
done();
});
}, function(err) {
done(err);
});
});
});
};
exports.down = function(done) {
Event.getEvents(function(err, events) {
if (err) return done(err);
events.forEach({populate: 'teamIds'}, function(event) {
event.teamIds.forEach(function(team) {
if (team.teamEventId) {
Team.deleteTeam(team, function(err) {
done(err);
});
}
});
});
});
};