/
blocks.go
343 lines (317 loc) · 10.2 KB
/
blocks.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package api
import (
"encoding/json"
"errors"
"net/http"
)
// Block is a block of content, which has the details filled out in linked tables
type Block struct {
ID int64 `json:"id" db:"id"`
Name string `json:"name" db:"name"`
Summary string `json:"summary" db:"summary"`
BlockType string `json:"blockType" db:"blockType"`
Content interface{} `json:"content,omitempty"`
FoundInFlows int64 `json:"foundInFlows" db:"foundInFlows"`
AllowReset string `json:"allowReset" db:"allowReset"`
UserStatus string `json:"userStatus,omitempty" db:"userStatus"`
LastUpdatedOn string `json:"lastUpdatedOn,omitempty" db:"lastUpdatedOn"`
ProjectID int64 `json:"projectId,omitempty" db:"projectId"`
ProjectName string `json:"projectName,omitempty" db:"projectName"`
ModuleID int64 `json:"moduleId,omitempty" db:"moduleId"`
ModuleName string `json:"moduleName,omitempty" db:"moduleName"`
}
const (
BlockTypeExternal = "external"
BlockTypeEmbed = "embed"
BlockTypeForm = "form"
BlockTypeText = "text"
BlockTypeFile = "file"
)
var blockTypes = []string{
BlockTypeExternal,
BlockTypeEmbed,
BlockTypeForm,
BlockTypeText,
BlockTypeFile,
}
func isValidBlockType(search string) bool {
for i := range blockTypes {
if blockTypes[i] == search {
return true
}
}
return false
}
// CreateBlock creates a new block
func CreateBlock(input *Block) error {
input.processForDB()
defer input.processForAPI()
res, err := config.DBConnection.NamedExec(`INSERT INTO Blocks SET name = :name, summary = :summary, blockType = :blockType, allowReset = :allowReset`, input)
if err != nil {
return err
}
input.ID, _ = res.LastInsertId()
return nil
}
// GetBlocksForSite gets the blocks for the site, usually used in admin views for linking and setting up flows
func GetBlocksForSite() ([]Block, error) {
blocks := []Block{}
err := config.DBConnection.Select(&blocks, `SELECT b.*, COUNT(bmf.moduleId) AS foundInFlows FROM
Blocks b
LEFT JOIN BlockModuleFlows bmf ON bmf.blockId = b.id
GROUP BY b.id, b.name, b.summary, b.blockType
ORDER BY b.name`)
for i := range blocks {
blocks[i].processForAPI()
}
return blocks, err
}
// GetBlockByID gets a single block by id
func GetBlockByID(blockID int64) (*Block, error) {
block := &Block{}
defer block.processForAPI()
err := config.DBConnection.Get(block, `SELECT b.*, COUNT(bmf.moduleId) AS foundInFlows FROM
Blocks b
LEFT JOIN BlockModuleFlows bmf ON bmf.blockId = b.id
WHERE b.id = ?
GROUP BY b.id, b.name, b.summary, b.blockType`, blockID)
return block, err
}
// GetModuleBlockForParticipant gets a single block for a participant; we take in all three levels to
// ensure that the permissions are correct
func GetModuleBlockForParticipant(participantID, projectID, moduleID, blockID int64) (*Block, error) {
block := &Block{}
defer block.processForAPI()
err := config.DBConnection.Get(block, `SELECT b.*,
p.id AS projectId,
p.name AS projectName,
m.id AS moduleId,
m.name AS moduleName,
IFNULL(bus.status, 'not_started') AS userStatus,
IFNULL(bus.lastUpdatedOn, NOW()) AS lastUpdatedOn
FROM Blocks b, BlockModuleFlows bmf, Flows f, Modules m, Projects p
LEFT JOIN BlockUserStatus bus ON bus.userId = ? AND bus.blockId = ?
WHERE b.id = ? AND
b.id = bmf.blockId AND
bmf.moduleId = ? AND
bmf.moduleId = f.moduleId AND
f.projectId = ? AND
f.projectId = p.id AND
f.moduleId = m.id`, participantID, blockID, blockID, moduleID, projectID)
return block, err
}
// UpdateBlock updates a block
func UpdateBlock(input *Block) error {
input.processForDB()
defer input.processForAPI()
_, err := config.DBConnection.NamedExec(`UPDATE Blocks SET name = :name, blockType = :blockType, summary = :summary, allowReset = :allowReset WHERE id = :id`, input)
return err
}
// GetBlocksForModule gets all of the blocks for a module
func GetBlocksForModule(moduleID int64) ([]Block, error) {
blocks := []Block{}
err := config.DBConnection.Select(&blocks, `SELECT b.* FROM Blocks b, BlockModuleFlows bmf WHERE bmf.moduleId = ? AND bmf.blockId = b.id ORDER BY bmf.flowOrder`, moduleID)
for i := range blocks {
blocks[i].processForAPI()
}
return blocks, err
}
// DeleteBlock deletes a block
func DeleteBlock(blockID int64) error {
_, err := config.DBConnection.Exec(`DELETE FROM Blocks WHERE id = ?`, blockID)
if err != nil {
return err
}
_, err = config.DBConnection.Exec(`DELETE FROM BlockModuleFlows WHERE blockId = ?`, blockID)
if err != nil {
return err
}
return nil
}
// LinkBlockAndModule links a block and a module
func LinkBlockAndModule(moduleID int64, blockID int64, order int64) error {
_, err := config.DBConnection.Exec(`INSERT INTO BlockModuleFlows SET
moduleId = ?, blockId = ?, flowOrder = ? ON DUPLICATE KEY UPDATE flowOrder = ?`, moduleID, blockID, order, order)
return err
}
// UnlinkBlockAndModule unlinks a block and a module
func UnlinkBlockAndModule(moduleID int64, blockID int64) error {
_, err := config.DBConnection.Exec(`DELETE FROM BlockModuleFlows WHERE moduleId = ? AND blockId = ?`, moduleID, blockID)
return err
}
// UnlinkAllBlocksFromModule unlinks all blocks from a module
func UnlinkAllBlocksFromModule(moduleID int64) error {
_, err := config.DBConnection.Exec(`DELETE FROM BlockModuleFlows WHERE moduleId = ?`, moduleID)
return err
}
//
// these handles probably need to be refactored; we want to make sure the API can take in the arbitrary data, but converting
// using the json mapping seems... overkill
//
// handleBlockRequiredFields is a quick helper to validate data before a block is saved
func handleBlockRequiredFields(blockType string, rawData interface{}) error {
str, _ := json.Marshal(rawData)
switch blockType {
case BlockTypeExternal:
content := &BlockExternal{}
err := json.Unmarshal(str, content)
if err != nil || content.ExternalLink == "" {
return errors.New("invalid")
}
case BlockTypeEmbed:
content := &BlockEmbed{}
err := json.Unmarshal(str, content)
if err != nil || (content.EmbedLink == "" && content.FileID == 0) {
return errors.New("invalid")
}
case BlockTypeText:
content := &BlockText{}
err := json.Unmarshal(str, content)
if err != nil || content.Text == "" {
return errors.New("invalid")
}
case BlockTypeForm:
content := &BlockForm{}
err := json.Unmarshal(str, content)
if err != nil || content.Questions == nil || len(content.Questions) == 0 {
return errors.New("invalid")
}
case BlockTypeFile:
content := &BlockFile{}
err := json.Unmarshal(str, content)
if err != nil || content.FileID == 0 {
return errors.New("invalid")
}
default:
return errors.New("invalid type")
}
return nil
}
// handleBlockSave is a helper for creating and updating block content types
func handleBlockSave(blockType string, blockID int64, rawData interface{}) (interface{}, error) {
// since the content comes in as an interface, we have to unmarshal and THEN set the block id!
str, _ := json.Marshal(rawData)
switch blockType {
case BlockTypeExternal:
content := &BlockExternal{}
err := json.Unmarshal(str, content)
if err != nil {
return content, errors.New("could not convert")
}
content.BlockID = blockID
err = SaveBlockExternal(content)
return content, err
case BlockTypeEmbed:
content := &BlockEmbed{}
err := json.Unmarshal(str, content)
if err != nil {
return content, errors.New("could not convert")
}
content.BlockID = blockID
err = SaveBlockEmbed(content)
return content, err
case BlockTypeForm:
content := &BlockForm{}
err := json.Unmarshal(str, content)
if err != nil {
return content, errors.New("could not convert")
}
content.BlockID = blockID
err = HandleSaveBlockForm(content)
return content, err
case BlockTypeText:
content := &BlockText{}
err := json.Unmarshal(str, content)
if err != nil {
return content, errors.New("could not convert")
}
content.BlockID = blockID
err = SaveBlockText(content)
return content, err
case BlockTypeFile:
content := &BlockFile{}
err := json.Unmarshal(str, content)
if err != nil {
return content, errors.New("could not convert")
}
content.BlockID = blockID
// we want to make sure the file is available
err = UpdateFileVisibilityFromAdminOnly(content.FileID, FileVisibilityProject)
if err != nil {
return content, err
}
err = SaveBlockFile(content)
return content, err
}
return rawData, errors.New("unsupported type")
}
// handleBlockGet is a helper for getting the content for a block
func handleBlockGet(blockType string, blockID int64) (interface{}, error) {
switch blockType {
case BlockTypeExternal:
found, err := GetBlockExternalByBlockID(blockID)
return found, err
case BlockTypeEmbed:
found, err := GetBlockEmbedByBlockID(blockID)
return found, err
case BlockTypeForm:
found, err := GetBlockFormByBlockID(blockID)
if err != nil {
return found, err
}
questions, err := GetBlockFormQuestionsForBlockID(blockID)
if err != nil {
return found, err
}
found.Questions = questions
return found, err
case BlockTypeText:
found, err := GetBlockTextByBlockID(blockID)
return found, err
case BlockTypeFile:
found, err := GetBlockFileByBlockID(blockID)
return found, err
}
return map[string]string{}, errors.New("unsupported type")
}
// handleBlockDelete is a helper for deleting a block and its content
func handleBlockDelete(blockType string, blockID int64) error {
switch blockType {
case BlockTypeExternal:
err := DeleteBlockExternalByBlockID(blockID)
return err
case BlockTypeEmbed:
err := DeleteBlockEmbedByBlockID(blockID)
return err
case BlockTypeForm:
err := DeleteBlockFormByBlockID(blockID)
return err
case BlockTypeText:
err := DeleteBlockTextByBlockID(blockID)
return err
case BlockTypeFile:
err := DeleteBlockFileByBlockID(blockID)
return err
}
return errors.New("unsupported type")
}
func (input *Block) processForDB() {
if input.BlockType == "" {
input.BlockType = BlockTypeText
}
if input.AllowReset == "" {
input.AllowReset = Yes
}
}
func (input *Block) processForAPI() {
if input.Content == nil {
input.Content = map[string]string{}
}
if input.LastUpdatedOn != "" {
input.LastUpdatedOn, _ = parseTimeToTimeFormat(input.LastUpdatedOn, timeFormatAPI)
}
}
// Bind binds the data for the HTTP
func (data *Block) Bind(r *http.Request) error {
return nil
}