This repository has been archived by the owner on Jul 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
create.go
235 lines (206 loc) · 5.95 KB
/
create.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
package space
import (
"context"
"encoding/json"
"errors"
"net/http"
"strconv"
"github.com/spf13/viper"
"github.com/factly/dega-server/config"
"github.com/factly/dega-server/service/core/model"
"github.com/factly/dega-server/util"
"github.com/factly/x/errorx"
"github.com/factly/x/loggerx"
"github.com/factly/x/meilisearchx"
"github.com/factly/x/middlewarex"
"github.com/factly/x/renderx"
"github.com/factly/x/slugx"
"github.com/factly/x/validationx"
)
// create - Create space
// @Summary Create space
// @Description Create space
// @Tags Space
// @ID add-space
// @Consume json
// @Produce json
// @Param X-User header string true "User ID"
// @Param Space body space true "Space Object"
// @Success 201 {object} model.Space
// @Router /core/spaces [post]
func create(w http.ResponseWriter, r *http.Request) {
uID, err := middlewarex.GetUser(r.Context())
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.Unauthorized()))
return
}
space := &space{}
err = json.NewDecoder(r.Body).Decode(&space)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DecodeError()))
return
}
validationError := validationx.Check(space)
if validationError != nil {
loggerx.Error(errors.New("validation error"))
errorx.Render(w, validationError)
return
}
if space.OrganisationID == 0 {
return
}
err = util.CheckSpaceKetoPermission("create", uint(space.OrganisationID), uint(uID))
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.GetMessage(err.Error(), http.StatusUnauthorized)))
return
}
var superOrgID int
if viper.GetBool("create_super_organisation") {
superOrgID, err = middlewarex.GetSuperOrganisationID("dega")
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.InternalServerError()))
return
}
// Fetch organisation permissions
permission := model.OrganisationPermission{}
err = config.DB.Model(&model.OrganisationPermission{}).Where(&model.OrganisationPermission{
OrganisationID: uint(space.OrganisationID),
}).First(&permission).Error
if err != nil && space.OrganisationID != superOrgID {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.GetMessage("cannot create more spaces", http.StatusUnprocessableEntity)))
return
}
if err == nil {
// Fetch total number of spaces in organisation
var totSpaces int64
config.DB.Model(&model.Space{}).Where(&model.Space{
OrganisationID: space.OrganisationID,
}).Count(&totSpaces)
if totSpaces >= permission.Spaces && permission.Spaces > 0 {
errorx.Render(w, errorx.Parser(errorx.GetMessage("cannot create more spaces", http.StatusUnprocessableEntity)))
return
}
}
}
var spaceSlug string
if space.Slug != "" && slugx.Check(space.Slug) {
spaceSlug = space.Slug
} else {
spaceSlug = slugx.Make(space.Name)
}
result := model.Space{
Name: space.Name,
SiteTitle: space.SiteTitle,
Slug: approveSpaceSlug(spaceSlug),
Description: space.Description,
TagLine: space.TagLine,
SiteAddress: space.SiteAddress,
Analytics: space.Analytics,
VerificationCodes: space.VerificationCodes,
SocialMediaURLs: space.SocialMediaURLs,
OrganisationID: space.OrganisationID,
ContactInfo: space.ContactInfo,
HeaderCode: space.HeaderCode,
FooterCode: space.FooterCode,
MetaFields: space.MetaFields,
}
tx := config.DB.WithContext(context.WithValue(r.Context(), userContext, uID)).Begin()
err = tx.Create(&result).Error
if err != nil {
tx.Rollback()
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
return
}
if viper.GetBool("create_super_organisation") {
// Create SpacePermission for super organisation
var spacePermission model.SpacePermission
if superOrgID == space.OrganisationID {
spacePermission = model.SpacePermission{
SpaceID: result.ID,
Media: -1,
Posts: -1,
Podcast: true,
Episodes: -1,
FactCheck: true,
}
} else {
spacePermission = model.SpacePermission{
SpaceID: result.ID,
Media: viper.GetInt64("default_number_of_media"),
Posts: viper.GetInt64("default_number_of_posts"),
Episodes: viper.GetInt64("default_number_of_episodes"),
Podcast: false,
FactCheck: false,
}
}
var spacePermContext config.ContextKey = "space_perm_user"
if err = tx.WithContext(context.WithValue(r.Context(), spacePermContext, uID)).Create(&spacePermission).Error; err != nil {
tx.Rollback()
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
return
}
}
// Insert into meili index
meiliObj := map[string]interface{}{
"id": result.ID,
"kind": "space",
"name": result.Name,
"slug": result.Slug,
"description": result.Description,
"site_title": result.SiteTitle,
"site_address": result.SiteAddress,
"tag_line": result.TagLine,
"organisation_id": result.OrganisationID,
"analytics": result.Analytics,
}
err = meilisearchx.AddDocument("dega", meiliObj)
if err != nil {
tx.Rollback()
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.InternalServerError()))
return
}
tx.Commit()
if util.CheckNats() {
if err = util.NC.Publish("space.created", result); err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.InternalServerError()))
return
}
}
renderx.JSON(w, http.StatusCreated, result)
}
func approveSpaceSlug(slug string) string {
spaceList := make([]model.Space, 0)
config.DB.Model(&model.Space{}).Where("slug LIKE ? AND deleted_at IS NULL", slug+"%").Find(&spaceList)
count := 0
for {
flag := true
for _, each := range spaceList {
temp := slug
if count != 0 {
temp = temp + "-" + strconv.Itoa(count)
}
if each.Slug == temp {
flag = false
break
}
}
if flag {
break
}
count++
}
temp := slug
if count != 0 {
temp = temp + "-" + strconv.Itoa(count)
}
return temp
}