2022-01-13 19:10:24 +03:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2022-03-20 18:27:21 +03:00
|
|
|
datastructure "github.com/duke-git/lancet/v2/datastructure/set"
|
2022-01-13 19:10:24 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
"github.com/muety/wakapi/services"
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetEffectiveProjectsList returns the user's projects, including all alias targets and excluding all remapped project names (alias sources)
|
|
|
|
// Example: "A" mapped to "AB" using an alias
|
|
|
|
// -> "A" itself should not appear as a project anymore
|
|
|
|
// -> Instead, the "virtual" project "AB" shall appear
|
|
|
|
// See https://github.com/muety/wakapi/issues/231
|
|
|
|
func GetEffectiveProjectsList(user *models.User, heartbeatSrvc services.IHeartbeatService, aliasSrvc services.IAliasService) ([]string, error) {
|
|
|
|
// extract actual projects from heartbeats
|
|
|
|
realProjects, err := heartbeatSrvc.GetEntitySetByUser(models.SummaryProject, user)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch aliases
|
|
|
|
projectAliases, err := aliasSrvc.GetByUserAndType(user.ID, models.SummaryProject)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
|
2022-03-20 18:27:21 +03:00
|
|
|
projects := datastructure.NewSet[string](realProjects...)
|
|
|
|
|
2022-01-13 19:10:24 +03:00
|
|
|
// remove alias values (source of a mapping)
|
|
|
|
// add alias key (target of a mapping) instead
|
|
|
|
for _, a := range projectAliases {
|
2022-03-20 18:27:21 +03:00
|
|
|
projects.Delete(a.Value)
|
|
|
|
projects.Add(a.Key)
|
2022-01-13 19:10:24 +03:00
|
|
|
}
|
|
|
|
|
2022-03-20 18:27:21 +03:00
|
|
|
sorted := projects.Values()
|
|
|
|
sort.Strings(sorted)
|
|
|
|
return sorted, nil
|
2022-01-13 19:10:24 +03:00
|
|
|
}
|