|
| 1 | +package net.furizon.backend.feature.pretix.order; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import lombok.Builder; |
| 7 | +import lombok.Data; |
| 8 | +import lombok.RequiredArgsConstructor; |
| 9 | +import lombok.extern.slf4j.Slf4j; |
| 10 | +import net.furizon.backend.db.entities.pretix.Event; |
| 11 | +import net.furizon.backend.db.entities.users.User; |
| 12 | +import net.furizon.backend.infrastructure.pretix.Const; |
| 13 | +import net.furizon.backend.infrastructure.pretix.model.ExtraDays; |
| 14 | +import net.furizon.backend.infrastructure.pretix.model.OrderStatus; |
| 15 | +import net.furizon.backend.infrastructure.pretix.model.Sponsorship; |
| 16 | +import net.furizon.backend.infrastructure.pretix.service.PretixInformation; |
| 17 | +import org.jetbrains.annotations.NotNull; |
| 18 | +import org.jetbrains.annotations.Nullable; |
| 19 | + |
| 20 | +import java.time.LocalDate; |
| 21 | +import java.time.LocalTime; |
| 22 | +import java.time.ZonedDateTime; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Set; |
| 28 | +import java.util.TreeSet; |
| 29 | + |
| 30 | +@RequiredArgsConstructor |
| 31 | +@Data |
| 32 | +@Builder |
| 33 | +@Slf4j |
| 34 | +public class Order { |
| 35 | + |
| 36 | + @NotNull |
| 37 | + private String code; |
| 38 | + |
| 39 | + @NotNull |
| 40 | + private OrderStatus orderStatus; |
| 41 | + |
| 42 | + @NotNull |
| 43 | + private Sponsorship sponsorship = Sponsorship.NONE; |
| 44 | + |
| 45 | + @NotNull |
| 46 | + private ExtraDays extraDays = ExtraDays.NONE; |
| 47 | + |
| 48 | + @NotNull |
| 49 | + private final Set<Integer> dailyDays; |
| 50 | + |
| 51 | + private int roomCapacity = 0; // 0 = has no room |
| 52 | + |
| 53 | + @Nullable |
| 54 | + private String hotelLocation; |
| 55 | + |
| 56 | + @NotNull |
| 57 | + private String pretixOrderSecret; |
| 58 | + |
| 59 | + private boolean hasMembership = false; |
| 60 | + |
| 61 | + private int answersMainPositionId = -1; |
| 62 | + |
| 63 | + private User orderOwner; //TODO load from db! (lazy load?) |
| 64 | + |
| 65 | + private Event orderEvent; //TODO load from db! (lazy load?) |
| 66 | + |
| 67 | + @NotNull |
| 68 | + private Map<String, Object> answers; |
| 69 | + |
| 70 | + public boolean isDaily() { |
| 71 | + return !dailyDays.isEmpty(); |
| 72 | + } |
| 73 | + |
| 74 | + public boolean hasDay(int day) { |
| 75 | + return dailyDays.contains(day); |
| 76 | + } |
| 77 | + |
| 78 | + @NotNull |
| 79 | + public String getAnswersJson(PretixInformation pi) { |
| 80 | + List<PretixAnswer> answers = new ArrayList<>(); |
| 81 | + for (String key : this.answers.keySet()) { |
| 82 | + Object o = this.answers.get(key); |
| 83 | + var identifierOpt = pi.getQuestionIdFromIdentifier(key); |
| 84 | + if (identifierOpt.isPresent()) { |
| 85 | + int id = identifierOpt.get(); |
| 86 | + var type = pi.getQuestionTypeFromId(id); |
| 87 | + if (type.isPresent()) { |
| 88 | + String out = switch (type.get()) { |
| 89 | + case NUMBER -> String.valueOf(o); |
| 90 | + case STRING_ONE_LINE -> (String) o; |
| 91 | + case STRING_MULTI_LINE -> (String) o; |
| 92 | + case BOOLEAN -> ((boolean) o) ? "true" : "false"; |
| 93 | + case LIST_SINGLE_CHOICE -> (String) o; |
| 94 | + case LIST_MULTIPLE_CHOICE -> (String) o; |
| 95 | + case FILE -> (String) o; |
| 96 | + case DATE -> o.toString(); |
| 97 | + case TIME -> o.toString(); |
| 98 | + case DATE_TIME -> o.toString(); |
| 99 | + case COUNTRY_CODE -> (String) o; |
| 100 | + case PHONE_NUMBER -> (String) o; |
| 101 | + }; |
| 102 | + |
| 103 | + if (out != null && !(out = out.strip()).isEmpty()) { |
| 104 | + answers.add(new PretixAnswer(id, out)); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + ObjectMapper om = new ObjectMapper(); |
| 111 | + try { |
| 112 | + return om.writeValueAsString(answers); |
| 113 | + } catch (JsonProcessingException e) { |
| 114 | + log.error("Error while serializing answers for order {}", getCode(), e); |
| 115 | + return "[]"; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public static class OrderBuilder { |
| 120 | + |
| 121 | + public OrderBuilder dailyDays(long days) { |
| 122 | + dailyDays = new TreeSet<>(); |
| 123 | + for (int i = 0; i < 63; i++) { |
| 124 | + if ((days & (1L << i)) != 0L) { |
| 125 | + dailyDays.add(i); |
| 126 | + } |
| 127 | + } |
| 128 | + return this; |
| 129 | + } |
| 130 | + |
| 131 | + public OrderBuilder answers(@NotNull String answersStr, @NotNull PretixInformation pi) { |
| 132 | + this.answers = new HashMap<String, Object>(); |
| 133 | + try { |
| 134 | + ObjectMapper om = new ObjectMapper(); |
| 135 | + List<PretixAnswer> answers = om.readValue(answersStr, new TypeReference<List<PretixAnswer>>() {}); |
| 136 | + for (PretixAnswer answer : answers) { |
| 137 | + int questionId = answer.getQuestionId(); |
| 138 | + var identifier = pi.getQuestionIdentifierFromId(questionId); |
| 139 | + if (identifier.isPresent()) { |
| 140 | + String answerIdentifier = identifier.get(); |
| 141 | + String value = answer.getAnswer(); |
| 142 | + if (value != null) { |
| 143 | + var type = pi.getQuestionTypeFromId(questionId); |
| 144 | + if (type.isPresent()) { |
| 145 | + Object o = switch (type.get()) { |
| 146 | + case NUMBER -> Float.parseFloat(value); |
| 147 | + case STRING_ONE_LINE -> value; |
| 148 | + case STRING_MULTI_LINE -> value; |
| 149 | + case BOOLEAN -> value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"); |
| 150 | + case LIST_SINGLE_CHOICE -> value; |
| 151 | + case LIST_MULTIPLE_CHOICE -> value; |
| 152 | + case FILE -> Const.QUESTIONS_FILE_KEEP; |
| 153 | + case DATE -> LocalDate.parse(value); |
| 154 | + case TIME -> LocalTime.parse(value); |
| 155 | + case DATE_TIME -> ZonedDateTime.parse(value); |
| 156 | + case COUNTRY_CODE -> value; |
| 157 | + case PHONE_NUMBER -> value; |
| 158 | + }; |
| 159 | + this.answers.put(answerIdentifier, o); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + } catch (JsonProcessingException e) { |
| 165 | + log.error("Unable to parse answers json", e); |
| 166 | + } |
| 167 | + return this; |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments