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

Fix missing categories by matching short categories code in plan code #28

Merged
merged 1 commit into from
Jan 23, 2025
Merged
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
20 changes: 12 additions & 8 deletions cmd/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,18 @@ func runner(cmd *cobra.Command, args []string) error {

// Sort plans by category and price
sort.Slice(catalog.Plans, func(i, j int) bool {
if catalog.Plans[i].Blobs.Commercial.Range != catalog.Plans[j].Blobs.Commercial.Range {
planCategoryI := catalog.Plans[i].GetCategory()
planCategoryJ := catalog.Plans[j].GetCategory()
if planCategoryI != planCategoryJ {
// Group plans by category first
a := pkgcategory.GetDisplayName(catalog.Plans[i].Blobs.Commercial.Range)
a := pkgcategory.GetDisplayName(planCategoryI)
if a == "" {
a = catalog.Plans[i].Blobs.Commercial.Range
a = planCategoryI
}

b := pkgcategory.GetDisplayName(catalog.Plans[j].Blobs.Commercial.Range)
b := pkgcategory.GetDisplayName(planCategoryJ)
if b == "" {
b = catalog.Plans[j].Blobs.Commercial.Range
b = planCategoryJ
}

return a < b
Expand All @@ -100,8 +102,10 @@ func runner(cmd *cobra.Command, args []string) error {
continue
}

planCategory := plan.GetCategory()

// Filter plans by category
if category != "" && category != plan.Blobs.Commercial.Range {
if category != "" && category != planCategory {
continue
}

Expand All @@ -127,9 +131,9 @@ func runner(cmd *cobra.Command, args []string) error {
nothingAvailable = false
}

categoryDisplay := pkgcategory.GetDisplayName(plan.Blobs.Commercial.Range)
categoryDisplay := pkgcategory.GetDisplayName(planCategory)
if categoryDisplay == "" {
categoryDisplay = plan.Blobs.Commercial.Range
categoryDisplay = planCategory
}

// Display plan
Expand Down
9 changes: 5 additions & 4 deletions pkg/category/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package category
var (
// Categories is a list of known plan categories, including an empty string for uncategorized plans
Categories = []category{
{"kimsufi", "Kimsufi"},
{"soyoustart", "So you Start"},
{"rise", "Rise"},
{"", "uncategorized"},
{"kimsufi", "Kimsufi", "sk"},
{"soyoustart", "So you Start", "sys"},
{"rise", "Rise", "rise"},
{"", "uncategorized", ""},
}
)

type category struct {
Name string
DisplayName string
ShortCode string
}

func GetDisplayName(name string) string {
Expand Down
19 changes: 19 additions & 0 deletions pkg/kimsufi/catalog/plan_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package catalog
import (
"math"
"slices"
"strings"

pkgcategory "github.com/TheoBrigitte/kimsufi-notifier/pkg/category"
)

const (
Expand Down Expand Up @@ -42,6 +45,22 @@ func (p *Plan) GetAddon(name string) *PlanAddonFamily {
return nil
}

func (p *Plan) GetCategory() string {
if p.Blobs.Commercial.Range != "" {
return p.Blobs.Commercial.Range
}

if len(p.PlanCode) > 2 {
for _, category := range pkgcategory.Categories {
if category.ShortCode != "" && strings.HasPrefix(p.PlanCode[2:], category.ShortCode) {
return category.Name
}
}
}

return ""
}

// GetConfiguration returns the first configuration that matches the provided name.
func (p Plan) GetConfiguration(name string) *PlanConfiguration {
for _, config := range p.Configurations {
Expand Down
Loading