| 1 | package planning |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/kernel" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "gorm.io/gorm" |
| 8 | "slices" |
| 9 | ) |
| 10 | |
| 11 | type ProjectConnectionView struct { |
| 12 | Project kernel.Project |
| 13 | Revision int64 |
| 14 | PinnedID string |
| 15 | Mode string |
| 16 | Options []ProjectConnectionOption |
| 17 | } |
| 18 | type ProjectConnectionOption struct { |
| 19 | ID, Label string |
| 20 | Enabled bool |
| 21 | } |
| 22 | |
| 23 | func ConnectionChoices(tx *gorm.DB, person string, rows []ConnectionRecord) ([]ProjectConnectionView, error) { |
| 24 | var result []ProjectConnectionView |
| 25 | var projects []kernel.Project |
| 26 | if err := tx.Order("id").Find(&projects).Error; err != nil { |
| 27 | return nil, err |
| 28 | } |
| 29 | for _, p := range projects { |
| 30 | v := ProjectConnectionView{Project: p, Mode: "best_fit"} |
| 31 | var choice ConnectionChoiceRecord |
| 32 | err := tx.Where("person=? AND project_id=?", person, p.ID).Take(&choice).Error |
| 33 | if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { |
| 34 | return nil, err |
| 35 | } |
| 36 | var ids []string |
| 37 | if err == nil { |
| 38 | if err = json.Unmarshal([]byte(choice.ConnectionIDsJSON), &ids); err != nil { |
| 39 | return nil, err |
| 40 | } |
| 41 | v.Revision = choice.Revision |
| 42 | v.PinnedID = choice.PinnedID |
| 43 | v.Mode = choice.Mode |
| 44 | } |
| 45 | for _, row := range rows { |
| 46 | if row.Person == person { |
| 47 | v.Options = append(v.Options, ProjectConnectionOption{ID: row.ID, Label: row.LocalID, Enabled: slices.Contains(ids, row.ID)}) |
| 48 | } |
| 49 | } |
| 50 | result = append(result, v) |
| 51 | } |
| 52 | return result, nil |
| 53 | } |
| 54 | |