-
Notifications
You must be signed in to change notification settings - Fork 0
/
user-service.js
177 lines (146 loc) · 5 KB
/
user-service.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
let mongoDBConnectionString = process.env.MONGO_URL;
let Schema = mongoose.Schema;
let userSchema = new Schema({
userName: {
type: String,
unique: true
},
password: String,
favourites: [String],
history: [String]
});
let User;
module.exports.connect = function () {
return new Promise(function (resolve, reject) {
let db = mongoose.createConnection(mongoDBConnectionString);
db.on('error', err => {
reject(err);
});
db.once('open', () => {
User = db.model("users", userSchema);
resolve();
});
});
};
module.exports.registerUser = function (userData) {
return new Promise(function (resolve, reject) {
if (userData.password != userData.password2) {
reject("Passwords do not match");
} else {
bcrypt.hash(userData.password, 10).then(hash => {
userData.password = hash;
let newUser = new User(userData);
newUser.save().then(() => {
resolve("User " + userData.userName + " successfully registered");
}).catch(err => {
if (err.code == 11000) {
reject("User Name already taken");
} else {
reject("There was an error creating the user: " + err);
}
})
}).catch(err => reject(err));
}
});
};
module.exports.checkUser = function (userData) {
return new Promise(function (resolve, reject) {
User.findOne({ userName: userData.userName })
.exec()
.then(user => {
bcrypt.compare(userData.password, user.password).then(res => {
if (res === true) {
resolve(user);
} else {
reject("Incorrect password for user " + userData.userName);
}
});
}).catch(err => {
reject("Unable to find user " + userData.userName);
});
});
};
module.exports.getFavourites = function (id) {
return new Promise(function (resolve, reject) {
User.findById(id)
.exec()
.then(user => {
resolve(user.favourites)
}).catch(err => {
reject(`Unable to get favourites for user with id: ${id}`);
});
});
}
module.exports.addFavourite = function (id, favId) {
return new Promise(function (resolve, reject) {
User.findById(id).exec().then(user => {
if (user.favourites.length < 50) {
User.findByIdAndUpdate(id,
{ $addToSet: { favourites: favId } },
{ new: true }
).exec()
.then(user => { resolve(user.favourites); })
.catch(err => { reject(`Unable to update favourites for user with id: ${id}`); })
} else {
reject(`Unable to update favourites for user with id: ${id}`);
}
})
});
}
module.exports.removeFavourite = function (id, favId) {
return new Promise(function (resolve, reject) {
User.findByIdAndUpdate(id,
{ $pull: { favourites: favId } },
{ new: true }
).exec()
.then(user => {
resolve(user.favourites);
})
.catch(err => {
reject(`Unable to update favourites for user with id: ${id}`);
})
});
}
module.exports.getHistory = function (id) {
return new Promise(function (resolve, reject) {
User.findById(id)
.exec()
.then(user => {
resolve(user.history)
}).catch(err => {
reject(`Unable to get history for user with id: ${id}`);
});
});
}
module.exports.addHistory = function (id, historyId) {
return new Promise(function (resolve, reject) {
User.findById(id).exec().then(user => {
if (user.favourites.length < 50) {
User.findByIdAndUpdate(id,
{ $addToSet: { history: historyId } },
{ new: true }
).exec()
.then(user => { resolve(user.history); })
.catch(err => { reject(`Unable to update history for user with id: ${id}`); })
} else {
reject(`Unable to update history for user with id: ${id}`);
}
})
});
}
module.exports.removeHistory = function (id, historyId) {
return new Promise(function (resolve, reject) {
User.findByIdAndUpdate(id,
{ $pull: { history: historyId } },
{ new: true }
).exec()
.then(user => {
resolve(user.history);
})
.catch(err => {
reject(`Unable to update history for user with id: ${id}`);
})
});
}