/
hotspots.go
318 lines (256 loc) · 9.26 KB
/
hotspots.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
/*
* Copyright (C) 2017 Nethesis S.r.l.
* http://www.nethesis.it - info@nethesis.it
*
* This file is part of Icaro project.
*
* Icaro is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or any later version.
*
* Icaro is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Icaro. If not, see COPYING.
*
* author: Edoardo Spadoni <edoardo.spadoni@nethesis.it>
*/
package methods
import (
"net/http"
"strconv"
"time"
"github.com/gin-gonic/gin"
_ "github.com/jinzhu/gorm/dialects/mysql"
uuid "github.com/satori/go.uuid"
"github.com/nethesis/icaro/sun/sun-api/database"
"github.com/nethesis/icaro/sun/sun-api/models"
"github.com/nethesis/icaro/sun/sun-api/utils"
)
func CreateHotspot(c *gin.Context) {
var json models.Hotspot
if err := c.BindJSON(&json); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": "Request fields malformed", "error": err.Error()})
return
}
accountId := c.MustGet("token").(models.AccessToken).AccountId
hotspot := models.Hotspot{
Uuid: uuid.Must(uuid.NewV4()).String(),
AccountId: accountId,
Name: json.Name,
Description: json.Description,
BusinessName: json.BusinessName,
BusinessVAT: json.BusinessVAT,
BusinessAddress: json.BusinessAddress,
BusinessEmail: json.BusinessEmail,
BusinessDPO: json.BusinessDPO,
BusinessDPOMail: json.BusinessDPOMail,
Created: time.Now().UTC(),
}
db := database.Instance()
db.Save(&hotspot)
if hotspot.Id == 0 {
c.JSON(http.StatusConflict, gin.H{"id": hotspot.Id, "status": "hotspot already exists"})
} else {
// associate disclaimers to hotspot
if json.PrivacyDisclaimerId > 0 {
db.Save(&models.DisclaimersHotspots{DisclaimerId: json.PrivacyDisclaimerId, HotspotId: hotspot.Id})
}
if json.TosDisclaimerId > 0 {
db.Save(&models.DisclaimersHotspots{DisclaimerId: json.TosDisclaimerId, HotspotId: hotspot.Id})
}
// initialize hotspot preferences
utils.SetDefaultHotspotPreferences(hotspot.Id)
c.JSON(http.StatusCreated, gin.H{"id": hotspot.Id, "status": "success"})
}
}
func updateDisclaimer(hotspotId int, disclaimerId int, disclaimerType string) {
var disclaimersHotspots models.DisclaimersHotspots
db := database.Instance()
db.Where("hotspot_id = ? AND disclaimers.type = ?", hotspotId, disclaimerType).Joins("JOIN disclaimers on disclaimers.id = disclaimers_hotspots.disclaimer_id").First(&disclaimersHotspots)
if disclaimersHotspots.Id != 0 {
// update preference
disclaimersHotspots.DisclaimerId = disclaimerId
db.Save(&disclaimersHotspots)
} else {
// preference not found, let's create it
disclaimersHotspots := models.DisclaimersHotspots{
DisclaimerId: disclaimerId,
HotspotId: hotspotId,
}
db.Save(&disclaimersHotspots)
}
}
func removeDisclaimer(hotspotId int, disclaimerType string) {
var disclaimersHotspots models.DisclaimersHotspots
db := database.Instance()
db.Where("hotspot_id = ? AND disclaimers.type = ?", hotspotId, disclaimerType).Joins("JOIN disclaimers on disclaimers.id = disclaimers_hotspots.disclaimer_id").First(&disclaimersHotspots)
if disclaimersHotspots.Id != 0 {
db.Delete(&disclaimersHotspots)
}
}
func UpdateHotspot(c *gin.Context) {
var hotspot models.Hotspot
accountId := c.MustGet("token").(models.AccessToken).AccountId
hotspotId := c.Param("hotspot_id")
var json models.Hotspot
if err := c.BindJSON(&json); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": "Request fields malformed", "error": err.Error()})
return
}
db := database.Instance()
if accountId == 1 {
db.Where("id = ?", hotspotId).First(&hotspot)
} else {
db.Where("id = ? AND account_id = ?", hotspotId, accountId).First(&hotspot)
}
if hotspot.Id == 0 {
c.JSON(http.StatusNotFound, gin.H{"message": "No hotspot found!"})
return
}
if len(json.Description) > 0 {
hotspot.Description = json.Description
}
if len(json.BusinessName) > 0 {
hotspot.BusinessName = json.BusinessName
}
if len(json.BusinessVAT) > 0 {
hotspot.BusinessVAT = json.BusinessVAT
}
if len(json.BusinessAddress) > 0 {
hotspot.BusinessAddress = json.BusinessAddress
}
if len(json.BusinessEmail) > 0 {
hotspot.BusinessEmail = json.BusinessEmail
}
hotspot.BusinessDPO = json.BusinessDPO
hotspot.BusinessDPOMail = json.BusinessDPOMail
db.Save(&hotspot)
// update hotspot disclaimers
if json.PrivacyDisclaimerId > 0 {
updateDisclaimer(hotspot.Id, json.PrivacyDisclaimerId, "privacy")
} else {
removeDisclaimer(hotspot.Id, "privacy")
}
if json.TosDisclaimerId > 0 {
updateDisclaimer(hotspot.Id, json.TosDisclaimerId, "tos")
} else {
removeDisclaimer(hotspot.Id, "tos")
}
c.JSON(http.StatusOK, gin.H{"status": "success"})
}
func GetHotspots(c *gin.Context) {
var hotspots []models.Hotspot
var total int
accountId := c.MustGet("token").(models.AccessToken).AccountId
page := c.Query("page")
limit := c.Query("limit")
q := c.Query("q")
offsets := utils.OffsetCalc(page, limit)
db := database.Instance()
chain := db.Order("name asc, description")
if len(q) > 0 {
chain = chain.Where("business_dpo_mail LIKE ? OR business_dpo LIKE ? OR business_address LIKE ? OR business_email LIKE ? OR business_name LIKE ? OR business_vat LIKE ? OR description LIKE ? OR name LIKE ?", "%"+q+"%", "%"+q+"%", "%"+q+"%", "%"+q+"%", "%"+q+"%", "%"+q+"%", "%"+q+"%", "%"+q+"%")
}
if accountId == 1 {
chain = chain.Find(&hotspots).Count(&total)
chain = chain.Offset(offsets[0]).Limit(offsets[1]).Find(&hotspots)
} else {
chain = chain.Where("account_id = ?", accountId).Find(&hotspots).Count(&total)
chain = chain.Where("account_id = ?", accountId).Offset(offsets[0]).Limit(offsets[1]).Find(&hotspots)
}
if len(hotspots) <= 0 {
c.JSON(http.StatusNotFound, gin.H{"message": "No hotspots found!"})
return
}
c.JSON(http.StatusOK, gin.H{"data": hotspots, "total": total})
}
func GetHotspot(c *gin.Context) {
var hotspot models.HotspotJSON
accountId := c.MustGet("token").(models.AccessToken).AccountId
hotspotId := c.Param("hotspot_id")
db := database.Instance()
if accountId == 1 {
db.Select("hotspots.*, accounts.name as account_name").Where("hotspots.id = ?", hotspotId).Joins("JOIN accounts on accounts.id = hotspots.account_id").First(&hotspot)
} else {
var account models.Account
db.Where("id = ?", accountId).First(&account)
if account.Type == "customer" || account.Type == "desk" {
accountId = account.CreatorId
}
db.Select("hotspots.*, accounts.name as account_name").Where("hotspots.id = ? AND account_id = ?", hotspotId, accountId).Joins("JOIN accounts on accounts.id = hotspots.account_id").First(&hotspot)
}
if hotspot.Id == 0 {
c.JSON(http.StatusNotFound, gin.H{"message": "No hotspot found!"})
return
}
c.JSON(http.StatusOK, hotspot)
}
func DeleteHotspot(c *gin.Context) {
var hotspot models.Hotspot
var accountsHotspot []models.AccountsHotspot
accountId := c.MustGet("token").(models.AccessToken).AccountId
hotspotId := c.Param("hotspot_id")
db := database.Instance()
if accountId == 1 {
db.Where("id = ?", hotspotId).First(&hotspot)
} else {
db.Where("id = ? AND account_id = ?", hotspotId, accountId).First(&hotspot)
}
if hotspot.Id == 0 {
c.JSON(http.StatusNotFound, gin.H{"message": "No hotspot found!"})
return
}
// delete all accounts related
db.Where("hotspot_id = ?", hotspotId).Find(&accountsHotspot)
for _, accountHotspot := range accountsHotspot {
var account models.Account
db.Where("id = ?", accountHotspot.AccountId).First(&account)
db.Delete(&account)
}
// delete hotspot
db.Delete(&hotspot)
c.JSON(http.StatusOK, gin.H{"status": "success"})
}
func StatsHotspotTotal(c *gin.Context) {
accountId := c.MustGet("token").(models.AccessToken).AccountId
var count int
db := database.Instance()
if accountId == 1 {
db.Table("hotspots").Count(&count)
} else {
db.Table("hotspots").Where("account_id = ?", accountId).Count(&count)
}
c.JSON(http.StatusOK, gin.H{"total": count})
}
func StatsSMSTotalSentForHotspot(c *gin.Context) {
var hotspotSmsCount []models.HotspotSmsCount
accountId := c.MustGet("token").(models.AccessToken).AccountId
db := database.Instance()
db.Where("hotspot_id in (?)", utils.ExtractHotspotIds(accountId, (accountId == 1), 0)).Find(&hotspotSmsCount)
if len(hotspotSmsCount) <= 0 {
c.JSON(http.StatusNotFound, gin.H{"message": "No sms stats found!"})
return
}
c.JSON(http.StatusOK, hotspotSmsCount)
}
func StatsSMSTotalSentForHotspotByHotspot(c *gin.Context) {
var hotspotSmsCount []models.HotspotSmsCount
accountId := c.MustGet("token").(models.AccessToken).AccountId
hotspotId := c.Param("hotspot_id")
hotspotIdInt, err := strconv.Atoi(hotspotId)
if err != nil {
hotspotIdInt = 0
}
db := database.Instance()
db.Where("hotspot_id in (?)", utils.ExtractHotspotIds(accountId, (accountId == 1), hotspotIdInt)).Find(&hotspotSmsCount)
if len(hotspotSmsCount) <= 0 {
c.JSON(http.StatusNotFound, gin.H{"message": "No sms stats found!"})
return
}
c.JSON(http.StatusOK, hotspotSmsCount)
}