Merge pull request #1 from mschwerz/feature/random-except-last-assigned

Random Assignment Except the Last Assigned Assignment Mode
This commit is contained in:
Mohamad Tarbin 2024-09-26 21:27:06 -04:00 committed by GitHub
commit 8343df8cd2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1140,7 +1140,13 @@ func checkNextAssignee(chore *chModel.Chore, choresHistory []*chModel.ChoreHisto
nextAssignee = chore.Assignees[rand.Intn(len(chore.Assignees))].UserID
case "keep_last_assigned":
// keep the last assignee
nextAssignee = history[len(history)-1].AssignedTo
nextAssignee = chore.AssignedTo
case "random_except_last_assigned":
var lastAssigned = chore.AssignedTo
AssigneesCopy := make([]chModel.ChoreAssignees, len(chore.Assignees))
copy(AssigneesCopy, chore.Assignees)
var removeLastAssigned = remove(AssigneesCopy, lastAssigned)
nextAssignee = removeLastAssigned[rand.Intn(len(removeLastAssigned))].UserID
default:
return chore.AssignedTo, fmt.Errorf("invalid assign strategy")
@ -1149,6 +1155,25 @@ func checkNextAssignee(chore *chModel.Chore, choresHistory []*chModel.ChoreHisto
return nextAssignee, nil
}
func remove(s []chModel.ChoreAssignees, i int) []chModel.ChoreAssignees {
var removalIndex = indexOf(s, i)
if removalIndex == -1 {
return s
}
s[removalIndex] = s[len(s)-1]
return s[:len(s)-1]
}
func indexOf(arr []chModel.ChoreAssignees, value int) int {
for i, v := range arr {
if v.UserID == value {
return i
}
}
return -1 // Return -1 if the value is not found
}
func Routes(router *gin.Engine, h *Handler, auth *jwt.GinJWTMiddleware) {
choresRoutes := router.Group("chores")