/
controller.js
40 lines (34 loc) · 1.11 KB
/
controller.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
// get user schema
const USER = require('./mongo-schemas/user');
module.exports = (ACTIONS) => ({
/**
* User authorization handler
* @param {String} login - user login
* @param {String} password - user password
* @param {String} email - user email
* @return {Promise} - user object
*/
authorization: async({ login, password, email }) => {
const userOptions = { model: USER.model, payload: { login } };
const user = await ACTIONS.send('database.read', userOptions);
if (!user) {
userOptions.payload = { login, password, email };
return ACTIONS.send('database.create', userOptions);
} else {
if (user.password !== password) throw new Error('Incorrect password!');
return user;
}
},
/**
* User get info handler
* @param {String} id - user id
* @return {Promise} - user object
*/
getUser: async({ id }) => {
const userOptions = { model: USER.model, payload: { id } };
console.log(userOptions);
const user = await ACTIONS.send('database.read', userOptions);
if (!user) throw new Error(`Incorrect ID: ${id}!`);
return user;
},
});