diff --git a/src/app/controller/webhook_controller.go b/src/app/controller/webhook_controller.go index 06ddea2..5425b8e 100644 --- a/src/app/controller/webhook_controller.go +++ b/src/app/controller/webhook_controller.go @@ -137,6 +137,37 @@ func PostPretixSpecialEventWithRoles(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusOK) } +func PostPretixEventWithoutMainProduct(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.GetAttendiesFromPretixJsonFindNameInSubproducts(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 b9c9018..de86831 100644 --- a/src/app/routes/public_routes.go +++ b/src/app/routes/public_routes.go @@ -16,6 +16,7 @@ func PublicRoutes(app *fiber.App) { webhooks.Post("/pretix/role_product", controller.PostPretixRoleProduct) webhooks.Post("/pretix/subproduct", controller.PostPretixRegularsTable) webhooks.Post("/pretix/speicalEventRoles", controller.PostPretixSpecialEventWithRoles) + webhooks.Post("/pretix/specialEventWithoutMain", controller.PostPretixEventWithoutMainProduct) apiv1.Post("/login", controller.LoginUser) apiv1.Get("/event", controller.ReturnAllEventsPublic) apiv1.Get("/ping", controller.Ping) diff --git a/src/app/util/pretix.go b/src/app/util/pretix.go index c2cbfa6..2f6ea59 100644 --- a/src/app/util/pretix.go +++ b/src/app/util/pretix.go @@ -300,6 +300,56 @@ func GetAttendiesFromPretixJsonWithRoleQuestionNoSubproducts(pretixEvent *entiti return attendies, nil } +func GetAttendiesFromPretixJsonFindNameInSubproducts(pretixEvent *entities.Pretix_Event, event *entities.Db_Event) (entities.AttendiesList, error) { + var attendies entities.AttendiesList + for _, order := range pretixEvent.Results { + if checkCanceled(order) { + continue + } + var attendie entities.Db_Attendies + attendie.Event = event + if event.ItemIdEvent == nil { + return attendies, errors.New("event has no ItemIdEvent") + } + if event.ItemIdRestaurant == nil { + return attendies, errors.New("event has no ItemIdRestaurant") + } + indexEvent := slices.IndexFunc(order.Positions, func(p entities.Pretix_Position) bool { return p.Item == *event.ItemIdEvent }) + indexRestaurant := slices.IndexFunc(order.Positions, func(p entities.Pretix_Position) bool { return p.Item == *event.ItemIdRestaurant }) + var indexName int + var indexPrivacy int + if indexEvent != -1 { + indexName = slices.IndexFunc(order.Positions[indexEvent].Answers, func(a entities.Pretix_Answer) bool { return a.QuestionsIdentififer == *event.QuestionIdName }) + indexPrivacy = slices.IndexFunc(order.Positions[indexEvent].Answers, func(a entities.Pretix_Answer) bool { return a.QuestionsIdentififer == *event.QuestionIdName }) + attendie.AttendsEvent = true + attendie.Nickname = getAttendieNickname(&order, &indexName, &indexEvent) + privacy, err := getAttendiePrivacy(&order, &indexPrivacy, &indexEvent) + if err != nil { + return nil, err + } + attendie.Privacy = privacy + } + if indexRestaurant != -1 { + indexName = slices.IndexFunc(order.Positions[indexRestaurant].Answers, func(a entities.Pretix_Answer) bool { return a.QuestionsIdentififer == *event.QuestionIdName }) + indexPrivacy = slices.IndexFunc(order.Positions[indexRestaurant].Answers, func(a entities.Pretix_Answer) bool { return a.QuestionsIdentififer == *event.QuestionIdName }) + attendie.AttendsRestaurant = true + attendie.Nickname = getAttendieNickname(&order, &indexName, &indexRestaurant) + privacy, err := getAttendiePrivacy(&order, &indexPrivacy, &indexRestaurant) + if err != nil { + return nil, err + } + attendie.Privacy = privacy + } + attendie.RegistrationTime = order.Datetime + attendies = append(attendies, attendie) + } + return attendies, nil +} + +func checkCanceled(pretixResult entities.Pretix_Result) bool { + return pretixResult.Status == "c" || pretixResult.Status == "e" +} + 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) { diff --git a/src/entities/db_attendies.go b/src/entities/db_attendies.go index 2d6df32..6aed97e 100644 --- a/src/entities/db_attendies.go +++ b/src/entities/db_attendies.go @@ -29,6 +29,7 @@ type Db_Attendies struct { Role *Role AttendsRestaurant bool `gorm:"column:attends_restaurant;not null;default:false"` AttendsBar bool `gorm:"attends_restaurant;not null;default:false"` + AttendsEvent bool `gorm:"attends_event;not null;default:false"` Privacy bool `gorm:"column:privacy;not null;default:false"` RegistrationTime time.Time `gorm:"column:registration_time;not null;default:CURRENT_TIMESTAMP"` OnWaitingList bool `gorm:"column:on_wating_list;not null;default:false"` diff --git a/src/entities/db_event.go b/src/entities/db_event.go index b9fd1f7..258a15c 100644 --- a/src/entities/db_event.go +++ b/src/entities/db_event.go @@ -14,6 +14,7 @@ type Db_Event struct { ItemIdBar *int `gorm:"column:item_id_bar"` ItemIdParticipation *int `gorm:"column:item_id_participation"` ItemIdRole *int `gorm:"column:item_id_role"` + ItemIdEvent *int `gorm:"column:item_id_event"` QuestionIdRole *string `gorm:"column:question_id_role"` QuestionIdName *string `gorm:"column:question_id_name"` QuestionIdPrivacy *string `gorm:"column:question_id_is_private"`