-
Notifications
You must be signed in to change notification settings - Fork 145
/
update_invite.go
46 lines (37 loc) · 1.52 KB
/
update_invite.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
package tenants
import (
"github.com/labstack/echo/v4"
"github.com/hatchet-dev/hatchet/api/v1/server/oas/apierrors"
"github.com/hatchet-dev/hatchet/api/v1/server/oas/gen"
"github.com/hatchet-dev/hatchet/api/v1/server/oas/transformers"
"github.com/hatchet-dev/hatchet/internal/repository"
"github.com/hatchet-dev/hatchet/internal/repository/prisma/db"
)
func (t *TenantService) TenantInviteUpdate(ctx echo.Context, request gen.TenantInviteUpdateRequestObject) (gen.TenantInviteUpdateResponseObject, error) {
tenantMember := ctx.Get("tenant-member").(*db.TenantMemberModel)
invite := ctx.Get("tenant-invite").(*db.TenantInviteLinkModel)
// validate the request
if apiErrors, err := t.config.Validator.ValidateAPI(request.Body); err != nil {
return nil, err
} else if apiErrors != nil {
return gen.TenantInviteUpdate400JSONResponse(*apiErrors), nil
}
// if user is not an owner, they cannot change a role to owner
if tenantMember.Role != db.TenantMemberRoleOwner && request.Body.Role == gen.OWNER {
return gen.TenantInviteUpdate400JSONResponse(
apierrors.NewAPIErrors("only an owner can change a role to owner"),
), nil
}
// construct the database query
updateOpts := &repository.UpdateTenantInviteOpts{
Role: repository.StringPtr(string(request.Body.Role)),
}
// update the invite
invite, err := t.config.Repository.TenantInvite().UpdateTenantInvite(invite.ID, updateOpts)
if err != nil {
return nil, err
}
return gen.TenantInviteUpdate200JSONResponse(
*transformers.ToTenantInviteLink(invite),
), nil
}