diff --git a/src/app/controller/webhook_controller.go b/src/app/controller/webhook_controller.go index 004aefb..856fcbd 100644 --- a/src/app/controller/webhook_controller.go +++ b/src/app/controller/webhook_controller.go @@ -14,12 +14,18 @@ func PostPretixRoleQuestion(c *fiber.Ctx) error { if err := c.BodyParser(data); err != nil { return c.Status(fiber.ErrBadRequest.Code).SendString(err.Error()) } - //TODO: Update attendies from pretix pretixOrders, err := util.GetPretixOrders(&data.Organizer, &data.Event) - _ = pretixOrders if err != nil { return c.Status(500).SendString("Internal Server Error") } + for link := pretixOrders.Next; link != nil; { + next_page, err := util.GetPretixOrdersByLink(link) + if err != nil { + return c.Status(500).SendString("Internal Server Error") + } + pretixOrders.Results = append(pretixOrders.Results, next_page.Results...) + link = next_page.Next + } event, err := service.Get_db_event_by_event(data.Event) if err != nil { return c.Status(fiber.StatusNotFound).SendString("Event not found") @@ -54,9 +60,7 @@ func PostPretixRoleProduct(c *fiber.Ctx) error { if err != nil { return c.Status(fiber.StatusNotFound).SendString("Event not found") } - fmt.Printf("%+v\n", pretixOrders) attendies, err := util.GetAttendiesFromPretixJsonWithRoleProduct(pretixOrders, &event) - fmt.Printf("%+v\n", attendies) if err != nil { fmt.Println(err) } diff --git a/src/app/service/role.go b/src/app/service/role.go index 02a8e81..df45903 100644 --- a/src/app/service/role.go +++ b/src/app/service/role.go @@ -8,17 +8,17 @@ import ( "ulmer-furs.de/pretix-proxy/v2/entities" ) -func GetRoleById(id int) (entities.Role, error) { - var role entities.Role - result := config.Database.Find(&role, id) +func GetRoleById(id int) (*entities.Role, error) { + var role *entities.Role + result := config.Database.Find(role, id) if result.RowsAffected == 0 { return role, errors.New("role not found") } return role, nil } -func GetRoleByName(name string) (entities.Role, error) { - var role entities.Role +func GetRoleByName(name string) (*entities.Role, error) { + var role *entities.Role result := config.Database.First(&role, "name = ?", name) if result.RowsAffected == 0 { return role, errors.New("role not found") diff --git a/src/app/util/attendiesList.go b/src/app/util/attendiesList.go index 69a4f1c..3e7261b 100644 --- a/src/app/util/attendiesList.go +++ b/src/app/util/attendiesList.go @@ -11,10 +11,10 @@ func CreateSuiterWaitingList(attendiesList *entities.AttendiesList, suiterPerSpo var suiterWatinglist entities.AttendiesList var i int for i < len(*attendiesList) { - if (*attendiesList)[i].Role == *suiter || (*attendiesList)[i].Role == *specialAnimal { + if *(*attendiesList)[i].Role == *suiter || *(*attendiesList)[i].Role == *specialAnimal { suiterCounter++ } - if suiterCounter > suiterCount && ((*attendiesList)[i].Role == *suiter || (*attendiesList)[i].Role == *specialAnimal) { + if suiterCounter > suiterCount && (*(*attendiesList)[i].Role == *suiter || *(*attendiesList)[i].Role == *specialAnimal) { suiterWatinglist = append(suiterWatinglist, (*attendiesList)[i]) *attendiesList = append((*attendiesList)[:i], (*attendiesList)[i+1:]...) } else { @@ -30,10 +30,10 @@ func MarkWatingListAttendies(attendiesList *entities.AttendiesList, suiterPerSpo var suiterCounter int var i int for i < len(*attendiesList) { - if (*attendiesList)[i].Role == *suiter || (*attendiesList)[i].Role == *specialAnimal { + if *(*attendiesList)[i].Role == *suiter || *(*attendiesList)[i].Role == *specialAnimal { suiterCounter++ } - if suiterCounter > suiterCount && ((*attendiesList)[i].Role == *suiter || (*attendiesList)[i].Role == *specialAnimal) { + if suiterCounter > suiterCount && (*(*attendiesList)[i].Role == *suiter || *(*attendiesList)[i].Role == *specialAnimal) { (*attendiesList)[i].OnWaitingList = true } i++ diff --git a/src/app/util/pretix.go b/src/app/util/pretix.go index df8e637..c6a37fb 100644 --- a/src/app/util/pretix.go +++ b/src/app/util/pretix.go @@ -37,6 +37,28 @@ func GetPretixOrders(organizer *string, event *string) (*entities.Pretix_Event, return &unmarshaled_resp, nil } +func GetPretixOrdersByLink(link *string) (*entities.Pretix_Event, error) { + req, err := http.NewRequest("GET", fmt.Sprintf(*link), nil) + if err != nil { + return nil, errors.New("request error") + } + req.Header.Add("Authorization", fmt.Sprintf("Token %s", config.Env.ApiKeyPretix)) + resp, err := config.Client.Do(req) + if err != nil { + return nil, errors.New("request error") + } + respbody, err := io.ReadAll(resp.Body) + defer resp.Body.Close() + if err != nil { + return nil, errors.New("request error") + } + var unmarshaled_resp entities.Pretix_Event + if err := json.Unmarshal(respbody, &unmarshaled_resp); err != nil { + return nil, errors.New("request error") + } + return &unmarshaled_resp, nil +} + func GetAttendieListFromPretixJson(pretixEvent entities.Pretix_Event, event entities.Db_Event) entities.Attendies { var attendies entities.Attendies var name string @@ -90,6 +112,9 @@ func GetAttendiesFromPretixJsonWithRoleQuestion(pretixEvent *entities.Pretix_Eve return attendies, errors.New("no participation found") } indexParticipation := slices.IndexFunc(order.Positions, func(p entities.Pretix_Position) bool { return p.Item == *event.ItemIdParticipation }) + if indexParticipation == -1 { + continue + } if event.ItemIdRestaurant == nil { attendie.AttendsRestaurant = false } else { @@ -100,7 +125,6 @@ func GetAttendiesFromPretixJsonWithRoleQuestion(pretixEvent *entities.Pretix_Eve attendie.AttendsRestaurant = false } } - indexName := slices.IndexFunc(order.Positions[indexParticipation].Answers, func(a entities.Pretix_Answer) bool { return a.QuestionsIdentififer == *event.QuestionIdName }) attendie.Nickname = order.Positions[indexParticipation].Answers[indexName].Answer @@ -132,7 +156,7 @@ func GetAttendiesFromPretixJsonWithRoleQuestion(pretixEvent *entities.Pretix_Eve } _, spotter, suiter, _, specialAnimal := GetRoles() sort.Sort(entities.ByRegistrationTime(attendies)) - MarkWatingListAttendies(&attendies, (*event).SuiterPerSpotter, &suiter, &spotter, &specialAnimal) + MarkWatingListAttendies(&attendies, (*event).SuiterPerSpotter, suiter, spotter, specialAnimal) return attendies, nil } @@ -188,6 +212,6 @@ func GetAttendiesFromPretixJsonWithRoleProduct(pretixEvent *entities.Pretix_Even } _, spotter, suiter, _, specialAnimal := GetRoles() sort.Sort(entities.ByRegistrationTime(attendies)) - MarkWatingListAttendies(&attendies, (*event).SuiterPerSpotter, &suiter, &spotter, &specialAnimal) + MarkWatingListAttendies(&attendies, (*event).SuiterPerSpotter, suiter, spotter, specialAnimal) return attendies, nil } diff --git a/src/app/util/roleHelper.go b/src/app/util/roleHelper.go index 405188f..77ff151 100644 --- a/src/app/util/roleHelper.go +++ b/src/app/util/roleHelper.go @@ -5,7 +5,7 @@ import ( "ulmer-furs.de/pretix-proxy/v2/entities" ) -func GetRoles() (guest, spotter, suiter, photograph, specialAnimal entities.Role) { +func GetRoles() (guest, spotter, suiter, photograph, specialAnimal *entities.Role) { guest, _ = service.GetRoleByName("Guest") spotter, _ = service.GetRoleByName("Spotter") suiter, _ = service.GetRoleByName("Suiter") diff --git a/src/entities/attendiesList.go b/src/entities/attendiesList.go index 595b2ea..ba2cb75 100644 --- a/src/entities/attendiesList.go +++ b/src/entities/attendiesList.go @@ -5,7 +5,7 @@ type AttendiesList []Db_Attendies func (a AttendiesList) CountByRole(role Role) int { var count int for _, attendie := range a { - if attendie.Role == role { + if *attendie.Role == role { count++ } } diff --git a/src/entities/db_attendies.go b/src/entities/db_attendies.go index c5bdd99..1db1c07 100644 --- a/src/entities/db_attendies.go +++ b/src/entities/db_attendies.go @@ -26,7 +26,7 @@ type Db_Attendies struct { EventID int `gorm:"not null"` Event Db_Event RoleID int `gorm:"not null"` - Role Role + Role *Role AttendsRestaurant bool `gorm:"column:attends_restaurant;not null"` Privacy bool `gorm:"column:privacy;not null"` RegistrationTime time.Time `gorm:"column:registration_time;not null;default:CURRENT_TIMESTAMP"` diff --git a/src/entities/pretix.go b/src/entities/pretix.go index 4dc397b..00a3d5c 100644 --- a/src/entities/pretix.go +++ b/src/entities/pretix.go @@ -5,6 +5,7 @@ import "time" type Pretix_Event struct { Count int `json:"count"` Results []Pretix_Result `json:"results"` + Next *string `json:"next"` } type Pretix_Result struct {