Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resending invitation upon expiration #368

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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