-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkflow_student.go
241 lines (210 loc) · 8.07 KB
/
workflow_student.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"encoding/json"
"fmt"
"net/http"
"sort"
"time"
"github.com/gin-gonic/gin"
"github.com/rs/xid"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func studentGenerateCodeHandler(ctx *gin.Context) {
params := ginContextRequestParameter(ctx)
response := GinResponse{
Status: http.StatusOK,
}
defer func() {
ginContextProcessResponse(ctx, &response)
}()
var studentRelativeBindInfo StudentRelativeBindInfo
var err error
if err = json.Unmarshal(params.Data, &studentRelativeBindInfo); err != nil {
response.Status = http.StatusBadRequest
response.Message = fmt.Sprintf("[%s] - %s", serverErrorMessages[seInputJSONNotValid], err.Error())
return
}
// check input student PID
if studentRelativeBindInfo.StudentPID.IsZero() {
response.Status = http.StatusConflict
response.Message = fmt.Sprintf("[%s] - Please specified student PID", serverErrorMessages[seInputJSONNotValid])
return
}
// find student by PID
var students []*Student
students, err = findStudent(studentRelativeBindInfo.StudentPID)
if err != nil || len(students) == 0 {
response.Status = http.StatusConflict
response.Message = fmt.Sprintf("[%s] - No student found with PID %s", serverErrorMessages[seResourceNotFound], studentRelativeBindInfo.StudentPID.Hex())
return
}
var studentFound = students[0]
// do not generate code if a main relative relationship exists
studentReferences, err := findStudentRelativeRef(studentFound.PID, primitive.NilObjectID)
var mainReference *StudentRelativeRef = nil
if err == nil && len(studentReferences) > 0 {
for i := 0; i < len(studentReferences); i++ {
if studentReferences[i].IsMain {
mainReference = studentReferences[i]
break
}
}
}
if mainReference != nil {
response.Status = http.StatusConflict
response.Message = fmt.Sprintf("[%s] - Student (PID %s) already has a main relative binding (PID %s) -> no binding code generated",
serverErrorMessages[seResourceConflict], studentFound.PID.Hex(), mainReference.RelativePID.Hex())
return
}
// generate binding code
studentFound.BindingCode = xid.New().String()
studentFound.BindingExpire = int64(time.Now().Unix()) + int64(3600*serverConfig.StudentBindingCodeLifeTime) // expired after one week
updateStudent(studentFound)
response.Payload = studentFound
return
}
// bind student with a main relative
func studentBindingRelativeHandler(ctx *gin.Context) {
params := ginContextRequestParameter(ctx)
response := GinResponse{
Status: http.StatusOK,
}
defer func() {
ginContextProcessResponse(ctx, &response)
}()
var studentRelativeBindInfo StudentRelativeBindInfo
var err error
if err = json.Unmarshal(params.Data, &studentRelativeBindInfo); err != nil {
response.Status = http.StatusBadRequest
response.Message = fmt.Sprintf("[%s] - %s", serverErrorMessages[seInputJSONNotValid], err.Error())
return
}
// check student binding code and relative wechat id
if studentRelativeBindInfo.RelativeWXID == "" || studentRelativeBindInfo.BindingCode == "" {
response.Status = http.StatusConflict
response.Message = fmt.Sprintf("[%s] - Please specified valid relative_wxid and binding_code", serverErrorMessages[seInputJSONNotValid])
return
}
// find relative by wechat id
var relativeFound *Relative
relativeFound, err = findRelativeByWXID(studentRelativeBindInfo.RelativeWXID)
if err != nil || relativeFound == nil {
response.Status = http.StatusConflict
response.Message = fmt.Sprintf("[%s] - No relative found with wechat id \"%s\"", serverErrorMessages[seResourceNotFound], studentRelativeBindInfo.RelativeWXID)
return
}
// find student by binding code
var studentFound *Student
studentFound, err = findStudentByBindingCode(studentRelativeBindInfo.BindingCode)
if err != nil || studentFound == nil {
response.Status = http.StatusConflict
response.Message = fmt.Sprintf("[%s] - No student found with binding code \"%s\"", serverErrorMessages[seResourceNotFound], studentRelativeBindInfo.BindingCode)
return
}
// check if binding code is expired
var curTS = int64(time.Now().Unix())
if curTS > studentFound.BindingExpire {
response.Status = http.StatusConflict
response.Message = fmt.Sprintf("[%s] - Binding code (%s) for student (PID %s) is expired at %v", serverErrorMessages[seResourceExpired],
studentFound.BindingCode, studentFound.PID.Hex(), time.Unix(studentFound.BindingExpire, 0).Format(time.RFC3339))
return
}
var reference StudentRelativeRef
reference.StudentPID = studentFound.PID
reference.RelativePID = relativeFound.PID
reference.IsMain = true
if studentRelativeBindInfo.Relationship == "" {
reference.Relationship = "Unknown"
} else {
reference.Relationship = studentRelativeBindInfo.Relationship
}
var referencePID = primitive.NilObjectID
referencePID, err = createStudentRelativeRef(&reference)
if err != nil {
response.Status = http.StatusConflict
response.Message = fmt.Sprintf("[%s] - %s --> could not bind student (PID %s) and relative (PID %s)", serverErrorMessages[seResourceConflict],
err.Error(), studentFound.PID.Hex(), relativeFound.PID.Hex())
return
}
// remove binding code from student information
studentFound.BindingCode = ""
studentFound.BindingExpire = 0
updateStudent(studentFound)
response.Payload = referencePID
return
}
// unbind all relatives for a student (delete main relative-> other should be deleted)
func studentUnbindingRelativeHandler(ctx *gin.Context) {
params := ginContextRequestParameter(ctx)
response := GinResponse{
Status: http.StatusOK,
}
defer func() {
ginContextProcessResponse(ctx, &response)
}()
var studentRelativeBindInfo StudentRelativeBindInfo
var err error
if err = json.Unmarshal(params.Data, &studentRelativeBindInfo); err != nil {
response.Status = http.StatusBadRequest
response.Message = fmt.Sprintf("[%s] - %s", serverErrorMessages[seInputJSONNotValid], err.Error())
return
}
// check student PID
if studentRelativeBindInfo.StudentPID.IsZero() {
response.Status = http.StatusConflict
response.Message = fmt.Sprintf("[%s] - Please specified valid student PID", serverErrorMessages[seInputJSONNotValid])
return
}
var deleteCnt int = 0
deleteCnt, err = deleteStudentRelativeRef(studentRelativeBindInfo.StudentPID, primitive.NilObjectID)
if err != nil {
response.Status = http.StatusConflict
response.Message = fmt.Sprintf("[%s] - Error occurs during unbind student (PID %s) relative references - %s",
serverErrorMessages[seInputJSONNotValid], studentRelativeBindInfo.StudentPID.Hex(), err.Error())
return
}
response.Payload = fmt.Sprintf("Unbind %d relatives for student (PID %s)", deleteCnt, studentRelativeBindInfo.StudentPID.Hex())
return
}
func studentMediaQueryHandler(ctx *gin.Context) {
params := ginContextRequestParameter(ctx)
response := GinResponse{
Status: http.StatusOK,
}
defer func() {
ginContextProcessResponse(ctx, &response)
}()
var mediaReq StudentMediaQueryReq
var err error
if err = json.Unmarshal(params.Data, &mediaReq); err != nil {
response.Status = http.StatusBadRequest
response.Message = fmt.Sprintf("[%s] - %s", serverErrorMessages[seInputJSONNotValid], err.Error())
return
}
// find student by PID
var students []*Student
students, err = findStudent(mediaReq.StudentPID)
if err != nil || len(students) == 0 {
response.Status = http.StatusConflict
response.Message = fmt.Sprintf("[%s] - No student found with PID %s", serverErrorMessages[seResourceNotFound], mediaReq.StudentPID.Hex())
return
}
cloudMediaSlice, err := findCloudMediaByStudentPID(mediaReq.StudentPID, false)
if err != nil {
response.Status = http.StatusConflict
response.Message = fmt.Sprintf("[%s] - Error occurs when searching cloud media for student (PID %s): %s",
serverErrorMessages[seResourceNotFound], mediaReq.StudentPID.Hex(), err.Error())
return
}
cloudMediaRes := []*CloudMedia{}
for i := range cloudMediaSlice {
if cloudMediaSlice[i].CreateTS <= mediaReq.EndTS && cloudMediaSlice[i].CreateTS >= mediaReq.StartTS {
cloudMediaRes = append(cloudMediaRes, cloudMediaSlice[i])
}
}
sort.Slice(cloudMediaRes, func(i, j int) bool {
return cloudMediaRes[i].CreateTS < cloudMediaRes[j].CreateTS
})
response.Payload = cloudMediaRes
return
}