forked from HackIllinois/api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkin_service.go
127 lines (98 loc) · 2.73 KB
/
checkin_service.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package service
import (
"errors"
"github.com/HackIllinois/api/common/database"
"github.com/HackIllinois/api/services/checkin/config"
"github.com/HackIllinois/api/services/checkin/models"
)
var db database.Database
func init() {
db_connection, err := database.InitDatabase(config.CHECKIN_DB_HOST, config.CHECKIN_DB_NAME)
if err != nil {
panic(err)
}
db = db_connection
}
/*
Returns the checkin associated with the given user id
*/
func GetUserCheckin(id string) (*models.UserCheckin, error) {
query := database.QuerySelector{
"id": id,
}
var user_checkin models.UserCheckin
err := db.FindOne("checkins", query, &user_checkin)
if err != nil {
return nil, err
}
return &user_checkin, nil
}
/*
Create the checkin associated with the given user id
*/
func CreateUserCheckin(id string, user_checkin models.UserCheckin) error {
_, err := GetUserCheckin(id)
if err != database.ErrNotFound {
if err != nil {
return err
}
return errors.New("Checkin already exists")
}
err = db.Insert("checkins", &user_checkin)
return err
}
/*
Update the checkin associated with the given user id
*/
func UpdateUserCheckin(id string, user_checkin models.UserCheckin) error {
selector := database.QuerySelector{
"id": id,
}
err := db.Update("checkins", selector, &user_checkin)
return err
}
/*
Returns true, nil if a user with specified ID is allowed to checkin, and false, nil if not allowed.
*/
func CanUserCheckin(id string, user_has_override bool) (bool, error) {
is_user_registered, err := IsUserRegistered(id)
if err != nil {
return false, err
}
// To checkin, the user must either (have RSVPed) or (have registered and got an override)
if is_user_registered && user_has_override {
return true, nil
}
// We do not want to call the below service function if the above condition is met, as it results
// in a 400 (Bad Request) / error if the user's RSVP info cannot be found.
// Therefore, we do not combine the conditions, and return as early as possible.
is_user_rsvped, err := IsAttendeeRsvped(id)
if err != nil {
return false, err
}
return is_user_rsvped, nil
}
/*
Returns a list of all checked in user IDs
*/
func GetAllCheckedInUsers() (*models.CheckinList, error) {
query := database.QuerySelector{
"hascheckedin": true,
}
var check_ins []models.UserCheckin
err := db.FindAll("checkins", query, &check_ins)
if err != nil {
return nil, err
}
var checkin_list models.CheckinList
for _, check_in := range check_ins {
checkin_list.CheckedInUsers = append(checkin_list.CheckedInUsers, check_in.ID)
}
return &checkin_list, nil
}
/*
Returns all checkin stats
*/
func GetStats() (map[string]interface{}, error) {
return db.GetStats("checkins", []string{"override", "hascheckedin", "haspickedupswag"})
}