From beeb22dc428b173c7c4119581a214a427ee83fdb Mon Sep 17 00:00:00 2001 From: nikurasu Date: Sun, 26 May 2024 16:27:18 +0200 Subject: [PATCH] feat(attendies): New view and backend Add an importer for special events with a role selection --- src/app/controller/webhook_controller.go | 36 +++++++++++- src/app/routes/public_routes.go | 1 + src/app/service/db_attendie.go | 6 -- src/app/util/pretix.go | 74 +++++++++++++++++++++--- src/app/views/index.html | 2 + src/app/views/specialEventRoles.html | 67 +++++++++++++++++++++ 6 files changed, 172 insertions(+), 14 deletions(-) create mode 100644 src/app/views/specialEventRoles.html diff --git a/src/app/controller/webhook_controller.go b/src/app/controller/webhook_controller.go index d865694..06ddea2 100644 --- a/src/app/controller/webhook_controller.go +++ b/src/app/controller/webhook_controller.go @@ -75,7 +75,7 @@ func PostPretixRoleProduct(c *fiber.Ctx) error { func PostPretixRegularsTable(c *fiber.Ctx) error { data := new(entities.Pretix_Webhook) if err := c.BodyParser(data); err != nil { - return c.Status(fiber.ErrBadGateway.Code).SendString(err.Error()) + return c.Status(fiber.ErrBadRequest.Code).SendString(err.Error()) } pretixOrders, err := getAllPretixOrders(&data.Organizer, &data.Event) if err != nil { @@ -103,6 +103,40 @@ func PostPretixRegularsTable(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusOK) } +func PostPretixSpecialEventWithRoles(c *fiber.Ctx) error { + data := new(entities.Pretix_Webhook) + if err := c.BodyParser(data); err != nil { + return c.Status(fiber.ErrBadRequest.Code).SendString(err.Error()) + } + pretixOrders, err := getAllPretixOrders(&data.Organizer, &data.Event) + if err != nil { + return c.SendStatus(fiber.StatusInternalServerError) + } + event, err := service.Get_db_event_by_event(data.Event) + if err != nil { + return c.Status(fiber.StatusNotFound).SendString("Event not found") + } + attendies, err := util.GetAttendiesFromPretixJsonWithRoleQuestionNoSubproducts( + pretixOrders, + &event, + ) + if err != nil { + fmt.Println(err) + return c.SendStatus(fiber.StatusInternalServerError) + } + if err := service.DeleteAttendiesByEvent(event); err != nil { + fmt.Println(err) + return c.SendStatus(fiber.StatusInternalServerError) + } + for _, attendie := range attendies { + if err := service.CreateNewAttendie(attendie); err != nil { + fmt.Print(err) + return c.SendStatus(fiber.StatusInternalServerError) + } + } + return c.SendStatus(fiber.StatusOK) +} + func getAllPretixOrders(organizer, event *string) (*entities.Pretix_Event, error) { pretixOrders, err := util.GetPretixOrders(organizer, event) for link := pretixOrders.Next; link != nil; { diff --git a/src/app/routes/public_routes.go b/src/app/routes/public_routes.go index 2f61868..d0b3016 100644 --- a/src/app/routes/public_routes.go +++ b/src/app/routes/public_routes.go @@ -15,6 +15,7 @@ func PublicRoutes(app *fiber.App) { webhooks.Post("/pretix/role_question", controller.PostPretixRoleQuestion) webhooks.Post("/pretix/role_product", controller.PostPretixRoleProduct) webhooks.Post("/pretix/subproduct", controller.PostPretixRegularsTable) + webhooks.Post("pretix/speicalEventRoles", controller.PostPretixSpecialEventWithRoles) apiv1.Post("/login", controller.LoginUser) apiv1.Get("/event", controller.ReturnAllEventsPublic) apiv1.Get("/ping", controller.Ping) diff --git a/src/app/service/db_attendie.go b/src/app/service/db_attendie.go index aabe665..d20cb09 100644 --- a/src/app/service/db_attendie.go +++ b/src/app/service/db_attendie.go @@ -2,7 +2,6 @@ package service import ( "errors" - "fmt" "gorm.io/gorm" "ulmer-furs.de/pretix-proxy/v2/config" @@ -55,11 +54,6 @@ func GetAttendiesByEventPrivacy(event entities.Db_Event, privacy bool) (entities } func CreateNewAttendie(attendie entities.Db_Attendies) error { - if attendie.Privacy { - fmt.Println("Privacy activated") - } else { - fmt.Println("Noone needs privacy") - } result := config.Database.Create(&attendie) if result.Error != nil { return result.Error diff --git a/src/app/util/pretix.go b/src/app/util/pretix.go index bb185a1..956ee73 100644 --- a/src/app/util/pretix.go +++ b/src/app/util/pretix.go @@ -250,20 +250,80 @@ func GetAttendiesFromPretixJsonWithSeperateSubProducts(pretixEvent *entities.Pre } indexName := slices.IndexFunc(order.Positions[indexParticipation].Answers, func(a entities.Pretix_Answer) bool { return a.QuestionsIdentififer == *event.QuestionIdName }) indexPrivacy := slices.IndexFunc(order.Positions[indexParticipation].Answers, func(a entities.Pretix_Answer) bool { return a.QuestionsIdentififer == *event.QuestionIdPrivacy }) - + attendie.RegistrationTime = order.Datetime attendie.Nickname = order.Positions[indexParticipation].Answers[indexName].Answer var err error attendie.Privacy, err = strconv.ParseBool(order.Positions[indexParticipation].Answers[indexPrivacy].Answer) - if attendie.Privacy { - fmt.Println("Yey, privacy") - } else { - fmt.Println("no privacy") - } if err != nil { return attendies, err } attendies = append(attendies, attendie) } - fmt.Printf("%+v", attendies) return attendies, nil } + +func GetAttendiesFromPretixJsonWithRoleQuestionNoSubproducts(pretixEvent *entities.Pretix_Event, event *entities.Db_Event) (entities.AttendiesList, error) { + var attendies entities.AttendiesList + for _, order := range pretixEvent.Results { + if order.Status == "c" || order.Status == "e" { + continue + } + var attendie entities.Db_Attendies + attendie.Event = event + if event.ItemIdParticipation == nil { + return attendies, errors.New("event has no ItemIdParticipation") + } + indexParticipation := slices.IndexFunc(order.Positions, func(p entities.Pretix_Position) bool { return p.Item == *event.ItemIdParticipation }) + + indexName := slices.IndexFunc(order.Positions[indexParticipation].Answers, func(a entities.Pretix_Answer) bool { return a.QuestionsIdentififer == *event.QuestionIdName }) + indexPrivacy := slices.IndexFunc(order.Positions[indexParticipation].Answers, func(a entities.Pretix_Answer) bool { return a.QuestionsIdentififer == *event.QuestionIdPrivacy }) + indexRole := slices.IndexFunc(order.Positions[indexParticipation].Answers, func(a entities.Pretix_Answer) bool { return a.QuestionsIdentififer == *event.QuestionIdRole }) + attendie.Nickname = getAttendieNickname(&order, &indexName, &indexParticipation) + attendieRole, err := getAttendieRole(&order, event, &indexRole, &indexParticipation) + if err != nil { + return nil, err + } + attendie.Role = attendieRole + attendie.RegistrationTime = order.Datetime + privacy, err := getAttendiePrivacy(&order, &indexPrivacy, &indexParticipation) + if err != nil { + return nil, err + } + attendie.Privacy = privacy + attendies = append(attendies, attendie) + } + _, spotter, suiter, _, specialAnimal := GetRoles() + sort.Sort(entities.ByRegistrationTime(attendies)) + if *event.SuiterPerSpotter != -1 { + MarkWatingListAttendies(&attendies, (*event).SuiterPerSpotter, suiter, spotter, specialAnimal) + } + return attendies, nil +} + +func getAttendieRole(order *entities.Pretix_Result, event *entities.Db_Event, indexRole, indexParticipation *int) (*entities.Role, error) { + roleAnswer := order.Positions[*indexParticipation].Answers[*indexRole].OptionIdentifiers + if slices.Contains(roleAnswer, *event.OptionIdSuiter) { + return service.GetRoleByName("Suiter") + } else if slices.Contains(roleAnswer, *event.OptionIdSpotter) { + return service.GetRoleByName("Spotter") + } else if slices.Contains(roleAnswer, *event.OptionIdPhotograph) { + return service.GetRoleByName("Photograph") + } else if slices.Contains(roleAnswer, *event.OptionIdSpecialAnimal) { + return service.GetRoleByName("SpecialAnimal") + } else if slices.Contains(roleAnswer, *event.OptionIdGuest) { + return service.GetRoleByName("Guest") + } + return nil, fmt.Errorf("role not found") +} + +func getAttendieNickname(order *entities.Pretix_Result, indexName, indexParticipation *int) string { + return order.Positions[*indexParticipation].Answers[*indexName].Answer +} + +func getAttendiePrivacy(order *entities.Pretix_Result, indexPrivacy, indexParticipation *int) (bool, error) { + privacy, err := strconv.ParseBool(order.Positions[*indexParticipation].Answers[*indexPrivacy].Answer) + if err != nil { + return privacy, err + } + return privacy, err +} diff --git a/src/app/views/index.html b/src/app/views/index.html index 607d167..95e244d 100644 --- a/src/app/views/index.html +++ b/src/app/views/index.html @@ -13,6 +13,8 @@ {{template "suitwalk" .}} {{ else if eq .EventType "RegularsTable" }} {{ template "regularstable" .}} + {{ else if eq .EventType "SpecialEventRoles" }} + {{ template "specialEventRoles" . }} {{ end }} diff --git a/src/app/views/specialEventRoles.html b/src/app/views/specialEventRoles.html new file mode 100644 index 0000000..4e51951 --- /dev/null +++ b/src/app/views/specialEventRoles.html @@ -0,0 +1,67 @@ +{{define "specialEventRoles"}} +

{{ .EventName }} {{ .Locals.Attendie }}

+

{{ .Locals.Registered }}

+ + + + + + + + + {{range $suiter := .Attendies }} + {{if not $suiter.OnWaitingList }} + + + {{ if eq $suiter.Role.Name "Suiter" }} + + {{ else if eq $suiter.Role.Name "SpecialAnimal" }} + + {{ else if eq $suiter.Role.Name "Spotter" }} + + {{ else if eq $suiter.Role.Name "Photograph" }} + + {{ else if eq $suiter.Role.Name "Guest" }} + + {{ end }} + + {{end}} + {{end}} + +
{{ .Locals.Name }}{{ .Locals.Role }}
{{ $suiter.Nickname }}{{ $.Locals.Suiter }}{{ $.Locals.SpecialAnimal }}{{ $.Locals.Spotter }}{{ $.Locals.Photographer }}{{ $.Locals.Guest }}
+{{ if gt .Attendies.CountWaitingList 0 }} +

{{ .Locals.SuiterWaitinglist }}

+

{{ .Locals.WaitinglistDesc}}

+ + + + + + {{ if eq .EventType "Suitwalk" }} + + {{ end }} + + + + {{range $suiter := .Attendies }} + {{if $suiter.OnWaitingList }} + + + {{ if eq $suiter.Role.Name "Suiter" }} + + {{ else if eq $suiter.Role.Name "SpecialAnimal" }} + + {{ else if eq $suiter.Role.Name "Spotter" }} + + {{ else if eq $suiter.Role.Name "Photograph" }} + + {{ else if eq $suiter.Role.Name "Guest" }} + + {{ end }} + + {{end}} + {{end}} + +
{{ .Locals.Name }}{{ .Locals.Role }}{{ .Locals.Restaurant }}
{{ $suiter.Nickname }}{{ $.Locals.Suiter }}{{ $.Locals.SpecialAnimal }}{{ $.Locals.Spotter }}{{ $.Locals.Photographer }}{{ $.Locals.Guest }}
+{{end}} +{{end}} \ No newline at end of file