@@ -2,6 +2,7 @@ import db from './db'
2
2
import * as util from '../util'
3
3
import config , { ServerConfig } from '../config/server'
4
4
import { DivisionACLError } from '../errors'
5
+ import { ExtractQueryType } from './util'
5
6
6
7
export interface User {
7
8
id : string ;
@@ -13,67 +14,67 @@ export interface User {
13
14
}
14
15
15
16
export const getAllUsers = ( ) : Promise < Pick < User , 'id' | 'name' | 'division' > [ ] > => {
16
- return db . query ( 'SELECT id, name, division FROM users' )
17
+ return db . query < ExtractQueryType < typeof getAllUsers > > ( 'SELECT id, name, division FROM users' )
17
18
. then ( res => res . rows )
18
19
}
19
20
20
21
export const getUserById = ( { id } : Pick < User , 'id' > ) : Promise < User | undefined > => {
21
- return db . query ( 'SELECT * FROM users WHERE id = $1' , [ id ] )
22
+ return db . query < User > ( 'SELECT * FROM users WHERE id = $1' , [ id ] )
22
23
. then ( res => res . rows [ 0 ] )
23
24
}
24
25
25
26
export const getUserByEmail = ( { email } : Pick < User , 'email' > ) : Promise < User | undefined > => {
26
- return db . query ( 'SELECT * FROM users WHERE email = $1' , [ email ] )
27
+ return db . query < User > ( 'SELECT * FROM users WHERE email = $1' , [ email ] )
27
28
. then ( res => res . rows [ 0 ] )
28
29
}
29
30
30
31
export const getUserByCtftimeId = ( { ctftimeId } : Pick < User , 'ctftimeId' > ) : Promise < User | undefined > => {
31
- return db . query ( 'SELECT * FROM users WHERE ctftime_id = $1' , [ ctftimeId ] )
32
+ return db . query < User > ( 'SELECT * FROM users WHERE ctftime_id = $1' , [ ctftimeId ] )
32
33
. then ( res => res . rows [ 0 ] )
33
34
}
34
35
35
36
export const getUserByIdAndEmail = ( { id, email } : Pick < User , 'id' | 'email' > ) : Promise < User | undefined > => {
36
- return db . query ( 'SELECT * FROM users WHERE id = $1 AND email = $2' , [ id , email ] )
37
+ return db . query < User > ( 'SELECT * FROM users WHERE id = $1 AND email = $2' , [ id , email ] )
37
38
. then ( res => res . rows [ 0 ] )
38
39
}
39
40
40
41
export const getUserByNameOrEmail = ( { name, email } : Pick < User , 'name' | 'email' > ) : Promise < User | undefined > => {
41
- return db . query ( 'SELECT * FROM users WHERE name = $1 OR email = $2' , [ name , email ] )
42
+ return db . query < User > ( 'SELECT * FROM users WHERE name = $1 OR email = $2' , [ name , email ] )
42
43
. then ( res => res . rows [ 0 ] )
43
44
}
44
45
45
46
export const getUserByNameOrCtftimeId = ( { name, ctftimeId } : Pick < User , 'name' | 'ctftimeId' > ) : Promise < User | undefined > => {
46
- return db . query ( 'SELECT * FROM users WHERE name = $1 OR ctftime_id = $2' , [ name , ctftimeId ] )
47
+ return db . query < User > ( 'SELECT * FROM users WHERE name = $1 OR ctftime_id = $2' , [ name , ctftimeId ] )
47
48
. then ( res => res . rows [ 0 ] )
48
49
}
49
50
50
51
export const removeUserByEmail = ( { email } : Pick < User , 'email' > ) : Promise < User | undefined > => {
51
- return db . query ( 'DELETE FROM users WHERE email = $1 RETURNING *' , [ email ] )
52
+ return db . query < User > ( 'DELETE FROM users WHERE email = $1 RETURNING *' , [ email ] )
52
53
. then ( res => res . rows [ 0 ] )
53
54
}
54
55
55
56
export const removeUserById = ( { id } : Pick < User , 'id' > ) : Promise < User | undefined > => {
56
- return db . query ( 'DELETE FROM users WHERE id = $1 RETURNING *' , [ id ] )
57
+ return db . query < User > ( 'DELETE FROM users WHERE id = $1 RETURNING *' , [ id ] )
57
58
. then ( res => res . rows [ 0 ] )
58
59
}
59
60
60
61
export const makeUser = ( { id, name, email, division, ctftimeId, perms } : User ) : Promise < User > => {
61
62
if ( config . email && config . divisionACLs && ! util . restrict . divisionAllowed ( email , division ) ) {
62
63
throw new DivisionACLError ( )
63
64
}
64
- return db . query ( 'INSERT INTO users (id, name, email, division, ctftime_id, perms) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *' ,
65
+ return db . query < User > ( 'INSERT INTO users (id, name, email, division, ctftime_id, perms) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *' ,
65
66
[ id , name , email , division , ctftimeId , perms ]
66
67
)
67
68
. then ( res => res . rows [ 0 ] )
68
69
}
69
70
70
71
export const removeCtftimeId = ( { id } : Pick < User , 'id' > ) : Promise < User | undefined > => {
71
- return db . query ( 'UPDATE users SET ctftime_id = NULL WHERE id = $1 AND ctftime_id IS NOT NULL RETURNING *' , [ id ] )
72
+ return db . query < User > ( 'UPDATE users SET ctftime_id = NULL WHERE id = $1 AND ctftime_id IS NOT NULL RETURNING *' , [ id ] )
72
73
. then ( res => res . rows [ 0 ] )
73
74
}
74
75
75
76
export const removeEmail = ( { id } : Pick < User , 'id' > ) : Promise < User | undefined > => {
76
- return db . query ( 'UPDATE users SET email = NULL WHERE id = $1 AND email IS NOT NULL RETURNING *' , [ id ] )
77
+ return db . query < User > ( 'UPDATE users SET email = NULL WHERE id = $1 AND email IS NOT NULL RETURNING *' , [ id ] )
77
78
. then ( res => res . rows [ 0 ] )
78
79
}
79
80
@@ -92,7 +93,7 @@ export const updateUser = async ({ id, name, email, division, ctftimeId, perms }
92
93
throw new DivisionACLError ( )
93
94
}
94
95
}
95
- return db . query ( `
96
+ return db . query < User > ( `
96
97
UPDATE users SET
97
98
name = COALESCE($1, name),
98
99
email = COALESCE($2, email),
0 commit comments