-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathloadApps.js
78 lines (70 loc) · 2.42 KB
/
loadApps.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
'use strict';
var applist = require('./applist.json');
var mongoose = require('mongoose');
var User = mongoose.model('User');
var Application = mongoose.model('Application');
module.exports = function() {
return new Promise(function(resolve, reject) {
var entries = [];
var count = 0;
var doUserWork = function(application, _createdBy) {
return new Promise(function(resolve, reject) {
User.findOne({ username: _createdBy }, function(err, res) {
if (res !== null) {
// assume user exists!
application.setAuditFields(res);
application.save().then(resolve, reject);
} else {
reject(Error('User does not exist'));
}
});
});
};
var doApplWork = function(item, query) {
return new Promise(function(resolve, reject) {
Application.findOne({ name: item.name }, function(err, res) {
if (res === null) {
// assume application doesn't exist!
count++;
var a = new Application(item);
a.save().then(resolve, reject);
} else {
reject(Error('Application already exists'));
}
});
});
};
// build list of entries
applist.forEach(function(item, index) {
entries.push(item);
});
// resolve promises
Promise.resolve()
.then(function() {
return entries.reduce(function(previousItem, currentItem) {
return previousItem.then(function() {
currentItem.tags = [['public'], ['admin']];
return (
doApplWork(currentItem)
//
// Sequential reduction of work moving from the tail of the original promise
// array to the head, by returning a promise for the next 'then' clause each time
// until the final then completes. Only then will this promise reduction
// finally resolve for the .then of the original resolving Promise.resolve().
//
.then(function(application) {
return doUserWork(application, currentItem._createdBy);
})
);
});
}, Promise.resolve());
})
.then(function() {
console.log('Applications loaded:', count);
})
.then(resolve, reject)
.catch(function(err) {
console.error('Error loading applications:', err);
});
});
};