Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions server/action/organisation/user/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/factly/x/validationx"
"github.com/go-chi/chi"
"github.com/spf13/viper"
"gorm.io/gorm"
)

type invites struct {
Expand Down Expand Up @@ -109,7 +110,13 @@ func create(w http.ResponseWriter, r *http.Request) {
}).First(&invitee).Error

if err != nil {
tx.Create(&invitee)
if err == gorm.ErrRecordNotFound {
tx.Create(&invitee)
} else {
loggerx.Error(errors.New("error creating invitee"))
errorx.Render(w, errorx.Parser(errorx.InternalServerError()))
return
}
}

invitation := &model.Invitation{
Expand All @@ -123,22 +130,8 @@ func create(w http.ResponseWriter, r *http.Request) {
ExpiredAt: time.Now().AddDate(0, 0, 7),
}

var invitationCount int64
err = tx.Model(&model.Invitation{}).
Where("invitee_id=? AND organisation_id=? AND status=? AND role=? AND expired_at<?", invitee.ID, uint(orgID), invitation.Status, invitation.Role, time.Now().AddDate(0, 0, 7)).
Count(&invitationCount).Error

if err != nil {
tx.Rollback()
loggerx.Error(err)
return
}
err = tx.Save(invitation).Error

if invitationCount > 0 {
loggerx.Error(err)
continue
}
err := tx.Model(&model.Invitation{}).Create(&invitation).Error
if err != nil {
tx.Rollback()
loggerx.Error(err)
Expand Down