diff --git a/openspec/changes/add-event-ticket-orders/tasks.md b/openspec/changes/add-event-ticket-orders/tasks.md index d624955..4c0ba4f 100644 --- a/openspec/changes/add-event-ticket-orders/tasks.md +++ b/openspec/changes/add-event-ticket-orders/tasks.md @@ -79,7 +79,7 @@ Design: `design.md` (technical contracts) and `docs/superpowers/specs/2026-12-04 - **`resend`:** sends the `last_email` kind. - Scope: `solvway-services-counter/src/**` - Verify: `mvn -T 1C -q -pl solvway-services-counter verify` -- [ ] 2.4 `CmsBlockValidator` TICKETS. +- [x] 2.4 `CmsBlockValidator` TICKETS. - Depends on: 2.1 - Tests first: - **Event checks:** diff --git a/solvway-services-counter/src/main/java/com/solvway/services/counter/service/CmsBlockValidator.java b/solvway-services-counter/src/main/java/com/solvway/services/counter/service/CmsBlockValidator.java index 28119cf..bbda952 100644 --- a/solvway-services-counter/src/main/java/com/solvway/services/counter/service/CmsBlockValidator.java +++ b/solvway-services-counter/src/main/java/com/solvway/services/counter/service/CmsBlockValidator.java @@ -17,6 +17,7 @@ import java.util.regex.Pattern; * Co-Lite changes: images are uploaded ids of the page's company, FORM points at an event of that company, * buttons may also use mailto: and tel:, and rich-text HTML holding scripts is refused (the public page sanitises as well). * The HTML block (custom HTML) is exempt from the script check: it is served only inside a sandboxed frame. + * add-event-ticket-orders adds TICKETS: an event of the page's company that sells tickets (ticketEvent) when saved. * add-landing-page-polish adds TOPBAR and section labels. add-landing-thanks-and-footer adds FOOTER and the thank-you text. add-page-sections adds FEATURES, STEPS, TRUST and CTA, a look on any block, and makes the HERO image optional. * Throws 422 on the first violation, with the reason "path|MessageCode". */ @@ -29,13 +30,18 @@ public final class CmsBlockValidator { static final Set ICONS = Set.of("wifi", "phone", "tv", "monitor", "lock", "shield-check", "cloud", "wrench", "headset", "users", "calendar", "star"); private static final Set CARD_TYPES = Set.of("FEATURES", "STEPS", "TRUST", "HERO", "FOOTER"); /** add-landing-page-polish: blocks that may carry a short label above their heading. */ - private static final Set EYEBROW_TYPES = Set.of("FEATURES", "STEPS", "IMAGE_TEXT", "FAQ", "CTA"); + private static final Set EYEBROW_TYPES = Set.of("FEATURES", "STEPS", "IMAGE_TEXT", "FAQ", "CTA", "TICKETS"); private static final Pattern SCRIPT = Pattern.compile("(?i)<\\s*script|\\son[a-z]+\\s*=|javascript\\s*:"); private CmsBlockValidator() { } + /** Without ticket-selling events: no TICKETS block passes. */ public static void validate(List blocks, LongPredicate ownImage, LongPredicate ownEvent) { + validate(blocks, ownImage, ownEvent, uuid -> false); + } + + public static void validate(List blocks, LongPredicate ownImage, LongPredicate ownEvent, LongPredicate ticketEvent) { if (blocks == null || blocks.isEmpty()) { return; } @@ -176,6 +182,25 @@ public final class CmsBlockValidator { unused(path, "look", b.getLook()); button(b, path); } + case "TICKETS" -> { + if (b.getEventUuid() == null || !ownEvent.test(b.getEventUuid())) { + throw refuse(path + ".eventUuid", "PagesBadEvent"); + } + if (!ticketEvent.test(b.getEventUuid())) { + throw refuse(path + ".eventUuid", "PagesTicketsClosed"); + } + max(b.getHeading(), path + ".heading", 120); + max(b.getText(), path + ".text", 1000); + unused(path, "html", b.getHtml()); + unused(path, "imageId", b.getImageId()); + unused(path, "imagePosition", b.getImagePosition()); + unused(path, "buttonLabel", b.getButtonLabel()); + unused(path, "buttonUrl", b.getButtonUrl()); + unused(path, "items", b.getItems()); + unused(path, "fullWidth", b.getFullWidth()); + unused(path, "formHeading", b.getFormHeading()); + unused(path, "formText", b.getFormText()); + } default -> throw refuse(path + ".type", "PagesUnknownType"); } } diff --git a/solvway-services-counter/src/main/java/com/solvway/services/counter/service/CmsPageService.java b/solvway-services-counter/src/main/java/com/solvway/services/counter/service/CmsPageService.java index 61ac51c..971f8a8 100644 --- a/solvway-services-counter/src/main/java/com/solvway/services/counter/service/CmsPageService.java +++ b/solvway-services-counter/src/main/java/com/solvway/services/counter/service/CmsPageService.java @@ -19,6 +19,7 @@ import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.web.server.ResponseStatusException; +import java.time.LocalDate; import java.time.LocalDateTime; import java.util.List; import java.util.Locale; @@ -91,7 +92,8 @@ public class CmsPageService { CmsBlockValidator.max(c.metaDescription(), "metaDescription", 160); CmsBlockValidator.validate(c.blocks(), id -> images.findCompanyIdById(id).map(co -> co == companyId).orElse(false), - uuid -> events.findByUuid(uuid).map(e -> e.getCompanyId() != null && e.getCompanyId() == companyId).orElse(false)); + uuid -> events.findByUuid(uuid).map(e -> e.getCompanyId() != null && e.getCompanyId() == companyId).orElse(false), + uuid -> events.findByUuid(uuid).map(e -> TicketTypeService.ticketsOpen(e, LocalDate.now(TicketTypeService.MALAYSIA))).orElse(false)); if (pages.existsByCompanyIdAndSlugAndIdNot(companyId, slug, row.getId() == null ? -1L : row.getId())) { throw slugTaken(); } diff --git a/solvway-services-counter/src/test/java/com/solvway/services/counter/service/CmsBlockValidatorTicketsTest.java b/solvway-services-counter/src/test/java/com/solvway/services/counter/service/CmsBlockValidatorTicketsTest.java new file mode 100644 index 0000000..4faf767 --- /dev/null +++ b/solvway-services-counter/src/test/java/com/solvway/services/counter/service/CmsBlockValidatorTicketsTest.java @@ -0,0 +1,78 @@ +package com.solvway.services.counter.service; + +import com.solvway.services.counter.dto.CmsBlock; +import org.junit.jupiter.api.Test; +import org.springframework.web.server.ResponseStatusException; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** add-event-ticket-orders: the TICKETS block (spec: Tickets Block Fields). Event 900 sells tickets, 901 is the company's but does not. */ +class CmsBlockValidatorTicketsTest { + + private static void validate(CmsBlock b) { + CmsBlockValidator.validate(List.of(b), id -> id == 5L, uuid -> uuid == 900L || uuid == 901L, uuid -> uuid == 900L); + } + + private static void refused(CmsBlock b, String path, String code) { + assertThatThrownBy(() -> validate(b)) + .isInstanceOfSatisfying(ResponseStatusException.class, e -> assertThat(e.getReason()).isEqualTo(path + "|" + code)); + } + + private static CmsBlock tickets(Long eventUuid) { + CmsBlock b = new CmsBlock(); + b.setType("TICKETS"); + b.setEventUuid(eventUuid); + return b; + } + + @Test + void aTicketsBlockNeedsAnEventOfThePageCompany() { + refused(tickets(null), "blocks[0].eventUuid", "PagesBadEvent"); + refused(tickets(902L), "blocks[0].eventUuid", "PagesBadEvent"); + } + + @Test + void anEventThatDoesNotSellTicketsIsRefused() { + // Tickets off, disabled or ended: the predicate is the counter's ticketsOpen check. + refused(tickets(901L), "blocks[0].eventUuid", "PagesTicketsClosed"); + } + + @Test + void headingAndTextHaveLimits() { + CmsBlock b = tickets(900L); + b.setHeading("h".repeat(121)); + refused(b, "blocks[0].heading", "PagesTooLong"); + b.setHeading("Get your tickets"); + b.setText("t".repeat(1001)); + refused(b, "blocks[0].text", "PagesTooLong"); + } + + @Test + void otherFieldsAreRefused() { + CmsBlock b = tickets(900L); + b.setButtonLabel("Buy"); + b.setButtonUrl("#x"); + refused(b, "blocks[0].buttonLabel", "PagesUnusedField"); + CmsBlock c = tickets(900L); + c.setFormHeading("Form"); + refused(c, "blocks[0].formHeading", "PagesUnusedField"); + CmsBlock d = tickets(900L); + d.setImageId(5L); + refused(d, "blocks[0].imageId", "PagesUnusedField"); + } + + @Test + void aValidBlockWithLabelLookAndLinkNameIsAccepted() { + CmsBlock b = tickets(900L); + b.setHeading("Get your tickets"); + b.setText("Seats are limited."); + b.setEyebrow("Tickets"); + b.setLook("MUTED"); + b.setAnchor("tickets"); + assertThatCode(() -> validate(b)).doesNotThrowAnyException(); + } +}