fix(get-attendies): get all orders from pretix
Pretix paginates if there are over 50 orders in one request, request the following orders
This commit is contained in:
parent
123f9c5957
commit
5e2fc7f87b
8 changed files with 48 additions and 19 deletions
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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++
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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++
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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"`
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue