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

Status field to enum #383

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions server/action/organisation/user/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ func create(w http.ResponseWriter, r *http.Request) {
CreatedByID: uint(currentUID),
},
InviteeID: invitee.ID,
Status: false,
Status: model.Pending,
Role: user.Role,
OrganisationID: uint(orgID),
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)).
Where("invitee_id=? AND organisation_id=? AND role=? AND expired_at<? AND status IN (?)", invitee.ID, uint(orgID), invitation.Role, time.Now().AddDate(0, 0, 7), []model.Status{model.Pending, model.Accepted}).
Count(&invitationCount).Error

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion server/action/profile/invite/accept.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func accept(w http.ResponseWriter, r *http.Request) {
}

tx := model.DB.Begin()
err = tx.Model(&model.Invitation{}).Where(&filter).Update("status", true).Error
err = tx.Model(&model.Invitation{}).Where(&filter).Update("status", model.Accepted).Error
if err != nil {
loggerx.Error(err)
tx.Rollback()
Expand Down
6 changes: 5 additions & 1 deletion server/action/profile/invite/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ func delete(w http.ResponseWriter, r *http.Request) {
},
}

tx.Where(&invites).Delete(&invites)
// invitationed is being deleted
// instead of deleting, updating the status to rejected
// tx.Where(&invites).Delete(&invites)
tx.Where(&invites).Update("Status", model.Rejected)
tx.Commit()

renderx.JSON(w, http.StatusOK, nil)
}
2 changes: 1 addition & 1 deletion server/action/profile/invite/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func listInvitations(w http.ResponseWriter, r *http.Request) {
var invitationContext model.ContextKey = "invitation_user"
tx := model.DB.WithContext(context.WithValue(r.Context(), invitationContext, userID)).Begin()
invitationList := make([]model.Invitation, 0)
err = tx.Model(model.Invitation{}).Where("invitee_id=? AND status=? and expired_at>?", uint(userID), false, time.Now()).Find(&invitationList).Error
err = tx.Model(model.Invitation{}).Where("invitee_id=? AND status=? and expired_at>?", uint(userID), model.Pending, time.Now()).Find(&invitationList).Error
if err != nil {
tx.Rollback()
loggerx.Error(err)
Expand Down
12 changes: 10 additions & 2 deletions server/model/invitation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ package model

import "time"

//Invitation model definition
type Status string

const (
Pending Status = "pending"
Rejected Status = "rejected"
Accepted Status = "accepted"
)

// Invitation model definition
type Invitation struct {
Base
InviteeID uint `gorm:"column:invitee_id" json:"invitee_id"`
OrganisationID uint `gorm:"column:organisation_id" json:"organisation_id"`
Role string `gorm:"column:role" json:"role"`
Status bool `gorm:"column:status" json:"status"`
Status Status `gorm:"column:status" json:"status"`
ExpiredAt time.Time `json:"expired_at"`
}

Expand Down