This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand-admin-unlock.go
73 lines (63 loc) · 1.88 KB
/
command-admin-unlock.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
package main
import (
"strconv"
"strings"
)
const (
ADMIN_UNLOCK_USER_BE_SPECIFIED = "USER should be mentioned"
ADMIN_UNLOCK_SUCCESSFUL = "User is unlocked."
ADMIN_UNLOCK_USER_SHOULD_CONNECT_TO_STRAVA = "User should connect to Strava to finish unlocking."
)
type AdminUnlockCommand struct {
}
func (cmd *AdminUnlockCommand) Name() string {
return "admin_unlock"
}
func (cmd *AdminUnlockCommand) Execute(params []string, message *IncomingSlackMessage, executor *CommandExecutor) (string, error) {
checkResult, err := executor.checkFromAdmin(message)
if err != nil {
return "", err
}
if checkResult != "" {
return checkResult, nil
}
if len(params) != 1 {
return ADMIN_UNLOCK_USER_BE_SPECIFIED, nil
}
var accessDetails *AccessDetails
var slackOrStravaUserId string
if strings.HasPrefix(params[0], "<@") && strings.HasSuffix(params[0], ">") {
unlockSlackUserId := strings.TrimSuffix(strings.TrimPrefix(params[0], "<@"), ">")
slackOrStravaUserId = unlockSlackUserId
accessDetails, err = executor.repo.AccessDetails.GetForUser(message.TeamId, unlockSlackUserId)
} else {
stravaUserId, err := strconv.Atoi(params[0])
if err != nil {
return ADMIN_UNLOCK_USER_BE_SPECIFIED, nil
}
slackOrStravaUserId = params[0]
accessDetails, err = executor.repo.AccessDetails.GetByStravaUserId(stravaUserId)
}
if err != nil {
return "", err
}
if accessDetails != nil {
err := executor.unlockUser(accessDetails.StravaUserId, "ADMIN", message.TeamId)
if err != nil {
return "", err
}
return ADMIN_UNLOCK_SUCCESSFUL, nil
} else {
pendingUnlock, err := executor.repo.PendingUnlocks.Get(slackOrStravaUserId)
if err != nil {
return "", err
}
if pendingUnlock == nil {
err := executor.repo.PendingUnlocks.Create(slackOrStravaUserId)
if err != nil {
return "", err
}
}
return ADMIN_UNLOCK_USER_SHOULD_CONNECT_TO_STRAVA, nil
}
}