Merge pull request #1 from mschwerz/feature/random-except-last-assigned
Random Assignment Except the Last Assigned Assignment Mode
This commit is contained in:
commit
8343df8cd2
1 changed files with 26 additions and 1 deletions
|
@ -1140,7 +1140,13 @@ func checkNextAssignee(chore *chModel.Chore, choresHistory []*chModel.ChoreHisto
|
||||||
nextAssignee = chore.Assignees[rand.Intn(len(chore.Assignees))].UserID
|
nextAssignee = chore.Assignees[rand.Intn(len(chore.Assignees))].UserID
|
||||||
case "keep_last_assigned":
|
case "keep_last_assigned":
|
||||||
// keep the last assignee
|
// 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:
|
default:
|
||||||
return chore.AssignedTo, fmt.Errorf("invalid assign strategy")
|
return chore.AssignedTo, fmt.Errorf("invalid assign strategy")
|
||||||
|
@ -1149,6 +1155,25 @@ func checkNextAssignee(chore *chModel.Chore, choresHistory []*chModel.ChoreHisto
|
||||||
return nextAssignee, nil
|
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) {
|
func Routes(router *gin.Engine, h *Handler, auth *jwt.GinJWTMiddleware) {
|
||||||
|
|
||||||
choresRoutes := router.Group("chores")
|
choresRoutes := router.Group("chores")
|
||||||
|
|
Loading…
Add table
Reference in a new issue