-
Notifications
You must be signed in to change notification settings - Fork 6
/
wingController.js
96 lines (88 loc) · 3.31 KB
/
wingController.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"use strict";
const util = require('util');
const eventEmitter = require('events').EventEmitter;
const logger = require('../modules/logger/logger');
function WingController() {
eventEmitter.call(this);
this.joinWing = (searchParams, wingName) => {
return new Promise((resolve, reject) => {
require('../models/user')
.then(user => {
user.findOneAndUpdate(
searchParams,
{
$addToSet: { wings: { name: wingName } }
})
.then((user) => {
if (user) {
this.emit('joinWing', { user, wingName });
return resolve(user);
} else {
return reject(new Error("No such user"));
}
})
.catch(err => {
logger.log(err);
reject(err);
})
})
.catch(err => {
logger.log(err);
reject(err);
})
})
}
this.leaveWing = (searchParams, wingName) => {
return new Promise((resolve, reject) => {
require('../models/user')
.then(userModel => {
userModel.findOneAndUpdate(searchParams, { $pull: { wings: { name: wingName } } })
.then(user => {
if (!user) {
return reject(false);
}
//see if wing was in original set anyway
let exists = user.wings.filter(wing => {
return wing.name === wingName;
})
if (exists.length > 0) {
this.emit('leaveWing', { user, wingName });
return resolve(true);
} else {
reject(false);
}
})
.catch(err => {
logger.log(err);
reject(err);
})
})
.catch(err => {
logger.log(err);
reject(err);
})
})
}
this.listMembers = (wingName) => {
return new Promise((resolve, reject) => {
require('../models/user')
.then(userModel => {
userModel.find({ "wings.name": { $eq: wingName } }, { username: 1 })
.then(users => {
logger.log(users);
resolve(users);
})
.catch(err => {
logger.log(err);
reject(err);
})
})
.catch(err => {
logger.log(err);
reject(err);
})
})
}
}
util.inherits(WingController, eventEmitter);
module.exports = new WingController();