-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata.js
More file actions
46 lines (40 loc) · 1.19 KB
/
Copy pathdata.js
File metadata and controls
46 lines (40 loc) · 1.19 KB
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
var path = require('path');
var fs = require('fs');
var Q = require('q');
var data = JSON.parse(fs.readFileSync(path.join(__dirname, './data.json'), {
encoding: 'utf8'
}));
var Data = function(services) {
Data.CategoryService = services.category;
Data.TeamService = services.team;
data.teams = checkForDoubles(data, 'teams');
data.categories = (checkForDoubles(data, 'categories'));
Q.allSettled(insertCategories()).then(insertTeams);
};
function insertCategories() {
return data.categories.map(function(category) {
return Data.CategoryService.findByName(category.name).then(function() {
return Data.CategoryService.save(category.name);
});
});
}
function insertTeams() {
return data.teams.map(function(team) {
return Data.CategoryService.findByName(team.category).then(function() {
return Data.TeamService.save(team);
});
});
}
function checkForDoubles(data, array) {
var doubles = [];
return data[array].filter(function(entry) {
if (doubles[entry.name]) {
console.log('Entry \"' + entry.name +
'\" is multiple times in ' + array);
return false;
}
doubles[entry.name] = true;
return true;
});
}
module.exports = Data;