1
|
|
|
import jwt from "jsonwebtoken"; |
2
|
|
|
import { db } from "./db.js" |
3
|
|
|
|
4
|
|
|
const user = { |
5
|
|
|
extractEmail: async function(githubToken) { |
|
|
|
|
6
|
|
|
// add logic here for extracting user email from Github |
7
|
|
|
// await .... |
8
|
|
|
}, |
9
|
|
|
insertIntoDB: async function(email, cardnr, cardtype) { |
10
|
|
|
|
11
|
|
|
const result = await db.queryWithArgs(`CALL new_user(?, ?, ?);`, [email, cardnr, cardtype]); |
12
|
|
|
|
13
|
|
|
return result[0][0]; |
14
|
|
|
|
15
|
|
|
}, |
16
|
|
|
/** |
17
|
|
|
* body should contain Github Token, |
18
|
|
|
* Card nr as string and card type as int |
19
|
|
|
* @param {*} req |
20
|
|
|
* @param {*} res |
21
|
|
|
* @param {*} next |
22
|
|
|
*/ |
23
|
|
|
register: async function(req, res, next) { |
|
|
|
|
24
|
|
|
const email = this.extractEmail(req.body.token) |
25
|
|
|
const payload = await this.newUser(email, req.body.cardnr, req.body.cardtype); |
26
|
|
|
const jwtToken = jwt.sign(payload, process.env.JWT_SECRET, { expiresIn: "24h" }); |
27
|
|
|
return res.json({ |
28
|
|
|
data: { |
29
|
|
|
type: "success", |
30
|
|
|
message: "User logged in", |
31
|
|
|
user: payload, |
32
|
|
|
token: jwtToken |
33
|
|
|
} |
34
|
|
|
}); |
35
|
|
|
}, |
36
|
|
|
/** |
37
|
|
|
* body should contain Github Token |
38
|
|
|
* @param {*} req |
39
|
|
|
* @param {*} res |
40
|
|
|
* @param {*} next |
41
|
|
|
*/ |
42
|
|
|
login: async function(req, res, next) { |
|
|
|
|
43
|
|
|
const email = this.extractEmail(req.body.token) |
44
|
|
|
const payload = await db.queryWithArgs(`CALL user_login(?);`, [email]); |
45
|
|
|
; |
46
|
|
|
const jwtToken = jwt.sign(payload, process.env.JWT_SECRET, { expiresIn: "24h" }); |
47
|
|
|
return res.json({ |
48
|
|
|
data: { |
49
|
|
|
type: "success", |
50
|
|
|
message: "User logged in", |
51
|
|
|
user: payload, |
52
|
|
|
token: jwtToken |
53
|
|
|
} |
54
|
|
|
}); |
55
|
|
|
}, |
56
|
|
|
/** |
57
|
|
|
* |
58
|
|
|
* @param {String | Int} what id or email to search for, for wilcard search add % before, after or both |
59
|
|
|
* @return {Array} if the search string is not a wildcard the array will only contain one object |
60
|
|
|
*/ |
61
|
|
|
userSearch: async function(what) { |
62
|
|
|
const result = await db.queryWithArgs(`CALL user_search(?);`, [what]); |
63
|
|
|
return result[0].map((user) => { |
64
|
|
|
return this.adjTypes(user); |
65
|
|
|
}); |
66
|
|
|
}, |
67
|
|
|
/** |
68
|
|
|
* |
69
|
|
|
* @param {Int} userId |
70
|
|
|
* @param {Boolean} active |
71
|
|
|
* @returns {Object} |
72
|
|
|
*/ |
73
|
|
|
updStatus: async function(userId, active) { |
74
|
|
|
const result = await db.queryWithArgs(`CALL upd_user_status(?, ?);`, [userId, active]); |
75
|
|
|
return this.adjTypes(result[0][0]); |
76
|
|
|
}, |
77
|
|
|
/** |
78
|
|
|
* |
79
|
|
|
* @param {Int} userId |
80
|
|
|
* @param {String} email |
81
|
|
|
* @returns {Object} |
82
|
|
|
*/ |
83
|
|
|
updEmail: async function(userId, email) { |
84
|
|
|
const result = await db.queryWithArgs(`CALL upd_user_email(?, ?);`, [userId, email]); |
85
|
|
|
return this.adjTypes(result[0][0]); |
86
|
|
|
}, |
87
|
|
|
/** |
88
|
|
|
* |
89
|
|
|
* @param {Int} offset |
90
|
|
|
* @param {Int} limit |
91
|
|
|
* @returns {Array} |
92
|
|
|
*/ |
93
|
|
|
allPag: async function(offset, limit) { |
94
|
|
|
const result = await db.queryWithArgs(`CALL upd_user_email(?, ?);`, [offset, limit]); |
95
|
|
|
return result[0].map((user) => { |
96
|
|
|
return this.adjTypes(user); |
97
|
|
|
}); |
98
|
|
|
}, |
99
|
|
|
all: async function() { |
100
|
|
|
const result = await db.queryNoArgs(`CALL all_users_pag();`, [offset, limit]); |
|
|
|
|
101
|
|
|
return result[0].map((user) => { |
102
|
|
|
return this.adjTypes(user); |
103
|
|
|
}); |
104
|
|
|
}, |
105
|
|
|
adjTypes(userObj) { |
106
|
|
|
userObj.balance = parseFloat(userObj.balance); |
107
|
|
|
userObj.active = userObj.active === 1; |
108
|
|
|
return userObj; |
109
|
|
|
}, |
110
|
|
|
}; |
111
|
|
|
|
112
|
|
|
export default user; |
113
|
|
|
|
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.