-
Notifications
You must be signed in to change notification settings - Fork 0
/
change.info.go
86 lines (70 loc) · 1.61 KB
/
change.info.go
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
package repository
import (
"context"
"errors"
"time"
"github.com/e-politica/api/models/v1/user"
"github.com/e-politica/api/pkg/crypto"
"github.com/e-politica/api/pkg/database"
"github.com/e-politica/api/pkg/session"
)
var ErrNotDefaultAccount = errors.New("not a default account")
func ChangeInfo(ctx context.Context, db *database.Db, access string, params user.ChangeInfoParams) (err error) {
userId, err := session.GetUserId(ctx, access)
if err != nil {
return
}
password, err := getDefaultAccountPassword(db, userId)
if err != nil {
return
}
if !crypto.CheckPasswordHash(params.Password, password) {
err = ErrPasswordsDontMatch
return
}
query := `
UPDATE user_account
SET
user_name=(CASE WHEN LENGTH($2) = 0 THEN user_name ELSE $2 END),
user_picture_url=(CASE WHEN LENGTH($3) = 0 THEN user_picture_url ELSE $3 END),
user_update_timestamp=$4
WHERE user_id=$1
`
_, err = db.Exec(query, userId, params.Name, params.Picture, time.Now())
if err != nil {
return
}
if params.NewPassword == "" {
return
}
query = `
SELECT user_account_type
FROM user_account
WHERE user_id=$1
`
row, err := db.QueryRow(query, userId)
if err != nil {
return
}
var accountType string
if err = row.Scan(&accountType); err != nil {
return
}
if accountType != "default" {
err = ErrNotDefaultAccount
return
}
newPassword, err := crypto.HashPassword(params.NewPassword)
if err != nil {
return
}
query = `
UPDATE default_account
SET
df_account_password=$2,
df_account_update_timestamp=$3
WHERE df_account_user_id=$1
`
_, err = db.Exec(query, userId, newPassword, time.Now())
return
}