-
Notifications
You must be signed in to change notification settings - Fork 12
/
team_user.go
57 lines (50 loc) · 1.71 KB
/
team_user.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
package makeless_go
import (
"errors"
"github.com/gin-gonic/gin"
"github.com/makeless/makeless-go/http"
"github.com/makeless/makeless-go/model"
"gorm.io/gorm"
h "net/http"
"strconv"
"sync"
)
func (makeless *Makeless) deleteTeamUser(http makeless_go_http.Http) error {
http.GetRouter().GetEngine().DELETE(
"/api/auth/team-user",
http.GetAuthenticator().GetMiddleware().MiddlewareFunc(),
http.EmailVerificationMiddleware(makeless.GetConfig().GetConfiguration().GetEmailVerification()),
http.TeamUserMiddleware(),
http.NotTeamCreatorMiddleware(),
func(c *gin.Context) {
var err error
var userId = http.GetAuthenticator().GetAuthUserId(c)
var teamId, _ = strconv.Atoi(c.GetHeader("Team"))
var teamUser = &makeless_go_model.TeamUser{
RWMutex: new(sync.RWMutex),
}
if teamUser, err = http.GetDatabase().GetTeamUserByFields(http.GetDatabase().GetConnection().WithContext(c), teamUser, map[string]interface{}{
"team_id": teamId,
"user_id": userId,
}); err != nil {
switch errors.Is(err, gorm.ErrRecordNotFound) {
case true:
c.AbortWithStatusJSON(h.StatusBadRequest, http.Response(err, nil))
default:
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
}
return
}
if err = http.GetDatabase().DeleteTeamUser(http.GetDatabase().GetConnection().WithContext(c), teamUser); err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
if err = http.GetEvent().Trigger(userId, "makeless", "team-user:delete", nil); err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
c.JSON(h.StatusOK, http.Response(nil, nil))
},
)
return nil
}