feat(pages): landing pages, page images and web sign-ups in services-counter
Pages and block rules are ported from babyboo's CMS, scoped per company. Images are checked by their magic bytes. A WEB registration keeps its page address and UTM tags and never counts as attended. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
1 parent
9741707156
commit
b164110367
21 files changed
+1116
-13
No files matched your search
+71
@@ -0,0 +1,71 @@
|
||||
package com.solvway.services.counter.controller;
|
||||
|
||||
import com.solvway.services.counter.dto.CmsPageDto;
|
||||
import com.solvway.services.counter.dto.CmsPageRequests;
|
||||
import com.solvway.services.counter.model.PageImage;
|
||||
import com.solvway.services.counter.service.CmsPageService;
|
||||
import com.solvway.services.counter.service.PageImageService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.http.CacheControl;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Map;
|
||||
|
||||
/** Landing pages and their images. mobile-api owns the module and company checks; visitor reads go through findPublished and the image read. */
|
||||
@RestController
|
||||
@RequestMapping("/counter")
|
||||
@AllArgsConstructor
|
||||
public class CmsPageController {
|
||||
private final CmsPageService pages;
|
||||
private final PageImageService images;
|
||||
|
||||
@PostMapping("/cmsPage/save")
|
||||
public CmsPageDto save(@RequestBody CmsPageRequests.Save req) {
|
||||
return pages.save(req.page(), req.user());
|
||||
}
|
||||
|
||||
@GetMapping("/cmsPage/findById/{id}")
|
||||
public CmsPageDto findById(@PathVariable("id") long id) {
|
||||
return pages.findById(id);
|
||||
}
|
||||
|
||||
@PostMapping("/cmsPage/search")
|
||||
public Page<CmsPageDto> search(@RequestBody CmsPageRequests.Search req) {
|
||||
return pages.search(req.companyId(), req.searchKey(), req.page(), req.size());
|
||||
}
|
||||
|
||||
@PostMapping("/cmsPage/delete")
|
||||
public void delete(@RequestBody Long id) {
|
||||
pages.delete(id);
|
||||
}
|
||||
|
||||
@GetMapping("/cmsPage/findPublished/{companyId}/{slug}")
|
||||
public CmsPageDto findPublished(@PathVariable("companyId") long companyId, @PathVariable("slug") String slug) {
|
||||
return pages.findPublished(companyId, slug).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "PagesNotFound"));
|
||||
}
|
||||
|
||||
@PostMapping("/pageImage/upload")
|
||||
public Map<String, Long> upload(@RequestBody CmsPageRequests.ImageUpload req) {
|
||||
return Map.of("id", images.upload(req.companyId(), req.data(), req.user()));
|
||||
}
|
||||
|
||||
/** Image ids are not secret: public pages show them to anyone. */
|
||||
@GetMapping("/pageImage/{id}")
|
||||
public ResponseEntity<byte[]> image(@PathVariable("id") long id) {
|
||||
PageImage image = images.find(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "PagesNotFound"));
|
||||
return ResponseEntity.ok().contentType(MediaType.parseMediaType(image.getContentType()))
|
||||
.cacheControl(CacheControl.maxAge(Duration.ofDays(365)).cachePublic().immutable())
|
||||
.body(image.getBytes());
|
||||
}
|
||||
|
||||
@GetMapping("/pageImage/findCompany/{id}")
|
||||
public Long imageCompany(@PathVariable("id") long id) {
|
||||
return images.companyOf(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "PagesNotFound"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.solvway.services.counter.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* One landing-page block. Which fields apply depends on {@code type}:
|
||||
* HERO (imageId, heading, text, button), RICH_TEXT (html), IMAGE_TEXT (imageId, imagePosition, heading, html, button),
|
||||
* FAQ (heading, items), FORM (heading, eventUuid).
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public class CmsBlock {
|
||||
private String type;
|
||||
private Long imageId;
|
||||
private String heading;
|
||||
private String text;
|
||||
private String html;
|
||||
private String imagePosition;
|
||||
private String buttonLabel;
|
||||
private String buttonUrl;
|
||||
private List<CmsFaqItem> items;
|
||||
private Long eventUuid;
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package com.solvway.services.counter.dto;
|
||||
|
||||
/** One FAQ question and its plain-text answer. */
|
||||
public record CmsFaqItem(String question, String answer) {}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.solvway.services.counter.dto;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/** A landing page as the API sends and receives it; blocks are omitted (null) in list rows. */
|
||||
public record CmsPageDto(Long id, Long companyId, String slug, String title, String metaDescription, String status,
|
||||
List<CmsBlock> blocks, LocalDateTime updatedAt) {}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.solvway.services.counter.dto;
|
||||
|
||||
/** Request bodies for the landing-page endpoints. */
|
||||
public final class CmsPageRequests {
|
||||
private CmsPageRequests() {
|
||||
}
|
||||
|
||||
public record Save(CmsPageDto page, String user) {}
|
||||
|
||||
/** companyId null searches every company (super admin). */
|
||||
public record Search(Long companyId, String searchKey, int page, int size) {}
|
||||
|
||||
public record ImageUpload(Long companyId, String data, String user) {}
|
||||
}
|
||||
+5
-1
@@ -24,5 +24,9 @@ public record EventLeadDetail(
|
||||
List<Long> interestIds,
|
||||
Long companyId,
|
||||
LocalDateTime attendedAt,
|
||||
String source
|
||||
String source,
|
||||
String pagePath,
|
||||
String utmSource,
|
||||
String utmMedium,
|
||||
String utmCampaign
|
||||
) {}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.solvway.services.counter.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
@Table(name = "cms_page")
|
||||
public class CmsPage {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private Long companyId;
|
||||
private String slug;
|
||||
private String title;
|
||||
private String metaDescription;
|
||||
private String status = "DRAFT";
|
||||
/** JSON array of blocks, replaced as a whole on every save. */
|
||||
@Column(columnDefinition = "json")
|
||||
private String blocks;
|
||||
private String createdBy;
|
||||
private LocalDateTime createdAt = LocalDateTime.now();
|
||||
private String updatedBy;
|
||||
private LocalDateTime updatedAt;
|
||||
}
|
||||
+6
@@ -51,6 +51,12 @@ public class EventLead {
|
||||
/** STAFF (captured in the portal) or QR (self-registered). */
|
||||
private String source = "STAFF";
|
||||
|
||||
/** Web page sign-ups only: the landing page's public address and the visitor's UTM tags. */
|
||||
private String pagePath;
|
||||
private String utmSource;
|
||||
private String utmMedium;
|
||||
private String utmCampaign;
|
||||
|
||||
private LocalDateTime consentAt;
|
||||
|
||||
private Long consentNoticeId;
|
||||
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.solvway.services.counter.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
@Table(name = "page_image")
|
||||
public class PageImage {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private Long companyId;
|
||||
private String contentType;
|
||||
@Lob
|
||||
@Column(columnDefinition = "mediumblob")
|
||||
private byte[] bytes;
|
||||
private String createdBy;
|
||||
private LocalDateTime createdAt = LocalDateTime.now();
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.solvway.services.counter.repo;
|
||||
|
||||
import com.solvway.services.counter.model.CmsPage;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface CmsPageRepository extends JpaRepository<CmsPage, Long> {
|
||||
boolean existsByCompanyIdAndSlugAndIdNot(Long companyId, String slug, Long id);
|
||||
|
||||
Optional<CmsPage> findByCompanyIdAndSlug(Long companyId, String slug);
|
||||
|
||||
/** companyId null lists every company (super admin); key null lists every page. */
|
||||
@Query("select p from CmsPage p where (:companyId is null or p.companyId = :companyId)"
|
||||
+ " and (:key is null or lower(p.title) like :key or p.slug like :key)")
|
||||
Page<CmsPage> search(@Param("companyId") Long companyId, @Param("key") String key, Pageable pageable);
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.solvway.services.counter.repo;
|
||||
|
||||
import com.solvway.services.counter.model.PageImage;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface PageImageRepository extends JpaRepository<PageImage, Long> {
|
||||
/** The owning company without loading the bytes. */
|
||||
@Query("select i.companyId from PageImage i where i.id = :id")
|
||||
Optional<Long> findCompanyIdById(@Param("id") Long id);
|
||||
}
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.services.counter.dto.CmsBlock;
|
||||
import com.solvway.services.counter.dto.CmsFaqItem;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.LongPredicate;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Landing-page block rules, ported from babyboo's CmsBlockValidator (specs/cms-pages) without the product blocks.
|
||||
* 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 HTML holding scripts is refused (the public page sanitises as well).
|
||||
* Throws 422 on the first violation, with the reason "path|MessageCode".
|
||||
*/
|
||||
public final class CmsBlockValidator {
|
||||
private static final int MAX_BLOCKS = 30;
|
||||
private static final Pattern SCRIPT = Pattern.compile("(?i)<\\s*script|\\son[a-z]+\\s*=|javascript\\s*:");
|
||||
|
||||
private CmsBlockValidator() {
|
||||
}
|
||||
|
||||
public static void validate(List<CmsBlock> blocks, LongPredicate ownImage, LongPredicate ownEvent) {
|
||||
if (blocks == null || blocks.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (blocks.size() > MAX_BLOCKS) {
|
||||
throw refuse("blocks", "PagesTooManyBlocks");
|
||||
}
|
||||
for (int i = 0; i < blocks.size(); i++) {
|
||||
CmsBlock b = blocks.get(i);
|
||||
String path = "blocks[" + i + "]";
|
||||
if (b == null || b.getType() == null) {
|
||||
throw refuse(path + ".type", "PagesUnknownType");
|
||||
}
|
||||
switch (b.getType()) {
|
||||
case "HERO" -> {
|
||||
image(b, path, ownImage);
|
||||
required(b.getHeading(), path + ".heading");
|
||||
max(b.getHeading(), path + ".heading", 120);
|
||||
max(b.getText(), path + ".text", 300);
|
||||
button(b, path);
|
||||
}
|
||||
case "RICH_TEXT" -> html(b.getHtml(), path + ".html");
|
||||
case "IMAGE_TEXT" -> {
|
||||
image(b, path, ownImage);
|
||||
max(b.getHeading(), path + ".heading", 120);
|
||||
if (!"LEFT".equals(b.getImagePosition()) && !"RIGHT".equals(b.getImagePosition())) {
|
||||
throw refuse(path + ".imagePosition", "PagesBadImagePosition");
|
||||
}
|
||||
html(b.getHtml(), path + ".html");
|
||||
button(b, path);
|
||||
}
|
||||
case "FAQ" -> faq(b, path);
|
||||
case "FORM" -> {
|
||||
max(b.getHeading(), path + ".heading", 120);
|
||||
if (b.getEventUuid() == null || !ownEvent.test(b.getEventUuid())) {
|
||||
throw refuse(path + ".eventUuid", "PagesBadEvent");
|
||||
}
|
||||
}
|
||||
default -> throw refuse(path + ".type", "PagesUnknownType");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static ResponseStatusException refuse(String path, String code) {
|
||||
return new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY, path + "|" + code);
|
||||
}
|
||||
|
||||
static void required(String value, String path) {
|
||||
if (value == null || value.isBlank()) {
|
||||
throw refuse(path, "PagesRequired");
|
||||
}
|
||||
}
|
||||
|
||||
static void max(String value, String path, int limit) {
|
||||
if (value != null && value.length() > limit) {
|
||||
throw refuse(path, "PagesTooLong");
|
||||
}
|
||||
}
|
||||
|
||||
private static void image(CmsBlock b, String path, LongPredicate ownImage) {
|
||||
if (b.getImageId() == null || !ownImage.test(b.getImageId())) {
|
||||
throw refuse(path + ".imageId", "PagesBadImage");
|
||||
}
|
||||
}
|
||||
|
||||
private static void html(String value, String path) {
|
||||
required(value, path);
|
||||
max(value, path, 100_000);
|
||||
if (SCRIPT.matcher(value).find()) {
|
||||
throw refuse(path, "PagesScript");
|
||||
}
|
||||
}
|
||||
|
||||
private static void faq(CmsBlock b, String path) {
|
||||
max(b.getHeading(), path + ".heading", 120);
|
||||
List<CmsFaqItem> items = b.getItems();
|
||||
if (items == null || items.isEmpty() || items.size() > 30) {
|
||||
throw refuse(path + ".items", "PagesFaqCount");
|
||||
}
|
||||
for (int i = 0; i < items.size(); i++) {
|
||||
CmsFaqItem item = items.get(i);
|
||||
String itemPath = path + ".items[" + i + "]";
|
||||
required(item == null ? null : item.question(), itemPath + ".question");
|
||||
max(item.question(), itemPath + ".question", 300);
|
||||
required(item.answer(), itemPath + ".answer");
|
||||
max(item.answer(), itemPath + ".answer", 2000);
|
||||
}
|
||||
}
|
||||
|
||||
private static void button(CmsBlock b, String path) {
|
||||
boolean hasLabel = b.getButtonLabel() != null && !b.getButtonLabel().isBlank();
|
||||
boolean hasUrl = b.getButtonUrl() != null && !b.getButtonUrl().isBlank();
|
||||
if (hasLabel != hasUrl) {
|
||||
throw refuse(path + (hasLabel ? ".buttonUrl" : ".buttonLabel"), "PagesButtonPair");
|
||||
}
|
||||
if (hasLabel) {
|
||||
max(b.getButtonLabel(), path + ".buttonLabel", 40);
|
||||
if (!safeLink(b.getButtonUrl())) {
|
||||
throw refuse(path + ".buttonUrl", "PagesBadLink");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A same-site path, https, mailto or tel link, at most 1000 characters. Browsers turn "\" into "/" and drop
|
||||
* whitespace inside URLs, so "/\evil" would become protocol-relative: such characters are refused outright.
|
||||
*/
|
||||
static boolean safeLink(String url) {
|
||||
if (url.length() > 1000 || url.chars().anyMatch(c -> c == '\\' || c <= 0x20 || c == 0x7f || Character.isSpaceChar(c) || c == 0xFEFF)) {
|
||||
return false;
|
||||
}
|
||||
if (url.startsWith("//")) {
|
||||
return false;
|
||||
}
|
||||
return url.startsWith("/") || url.startsWith("https://") || url.startsWith("mailto:") || url.startsWith("tel:");
|
||||
}
|
||||
}
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.solvway.services.counter.dto.CmsBlock;
|
||||
import com.solvway.services.counter.dto.CmsPageDto;
|
||||
import com.solvway.services.counter.model.CmsPage;
|
||||
import com.solvway.services.counter.repo.CmsPageRepository;
|
||||
import com.solvway.services.counter.repo.EventRepository;
|
||||
import com.solvway.services.counter.repo.PageImageRepository;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/** Landing pages, ported from babyboo's CmsPageService (specs/cms-pages) and scoped per company. Reasons are "path|MessageCode". */
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class CmsPageService {
|
||||
private static final Pattern SLUG = Pattern.compile("^[a-z0-9]+(-[a-z0-9]+)*$");
|
||||
private static final TypeReference<List<CmsBlock>> BLOCKS = new TypeReference<>() {};
|
||||
|
||||
private final CmsPageRepository pages;
|
||||
private final PageImageRepository images;
|
||||
private final EventRepository events;
|
||||
private final ObjectMapper json;
|
||||
|
||||
public CmsPageDto save(CmsPageDto dto, String user) {
|
||||
CmsPage row;
|
||||
if (dto.id() == null) {
|
||||
if (dto.companyId() == null) {
|
||||
throw CmsBlockValidator.refuse("companyId", "PagesRequired");
|
||||
}
|
||||
row = new CmsPage();
|
||||
row.setCompanyId(dto.companyId());
|
||||
row.setCreatedBy(user);
|
||||
} else {
|
||||
row = pages.findById(dto.id()).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "PagesNotFound"));
|
||||
}
|
||||
long companyId = row.getCompanyId();
|
||||
String slug = dto.slug() == null ? "" : dto.slug();
|
||||
if (slug.length() > 80 || !SLUG.matcher(slug).matches()) {
|
||||
throw CmsBlockValidator.refuse("slug", "PagesBadSlug");
|
||||
}
|
||||
CmsBlockValidator.required(dto.title(), "title");
|
||||
CmsBlockValidator.max(dto.title(), "title", 120);
|
||||
CmsBlockValidator.max(dto.metaDescription(), "metaDescription", 160);
|
||||
if (!"DRAFT".equals(dto.status()) && !"PUBLISHED".equals(dto.status())) {
|
||||
throw CmsBlockValidator.refuse("status", "PagesBadStatus");
|
||||
}
|
||||
CmsBlockValidator.validate(dto.blocks(),
|
||||
id -> images.findCompanyIdById(id).map(c -> c == companyId).orElse(false),
|
||||
uuid -> events.findByUuid(uuid).map(e -> e.getCompanyId() != null && e.getCompanyId() == companyId).orElse(false));
|
||||
if (pages.existsByCompanyIdAndSlugAndIdNot(companyId, slug, dto.id() == null ? -1L : dto.id())) {
|
||||
throw slugTaken();
|
||||
}
|
||||
row.setSlug(slug);
|
||||
row.setTitle(dto.title().trim());
|
||||
row.setMetaDescription(dto.metaDescription() == null || dto.metaDescription().isBlank() ? null : dto.metaDescription().trim());
|
||||
row.setStatus(dto.status());
|
||||
row.setBlocks(write(dto.blocks() == null ? List.of() : dto.blocks()));
|
||||
row.setUpdatedBy(user);
|
||||
row.setUpdatedAt(LocalDateTime.now());
|
||||
try {
|
||||
return toDto(pages.saveAndFlush(row), true);
|
||||
} catch (DataIntegrityViolationException e) {
|
||||
throw slugTaken();
|
||||
}
|
||||
}
|
||||
|
||||
public CmsPageDto findById(long id) {
|
||||
return pages.findById(id).map(p -> toDto(p, true))
|
||||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "PagesNotFound"));
|
||||
}
|
||||
|
||||
/** Newest update first; rows carry no blocks. */
|
||||
public Page<CmsPageDto> search(Long companyId, String searchKey, int page, int size) {
|
||||
String key = searchKey == null || searchKey.isBlank() ? null : "%" + searchKey.trim().toLowerCase(Locale.ROOT) + "%";
|
||||
PageRequest request = PageRequest.of(Math.max(page, 0), size >= 1 ? Math.min(size, 100) : 20,
|
||||
Sort.by(Sort.Order.desc("updatedAt"), Sort.Order.desc("id")));
|
||||
return pages.search(companyId, key, request).map(p -> toDto(p, false));
|
||||
}
|
||||
|
||||
public void delete(long id) {
|
||||
pages.deleteById(id);
|
||||
}
|
||||
|
||||
/** A visitor's read: only published pages, the slug matched ignoring case. */
|
||||
public Optional<CmsPageDto> findPublished(long companyId, String slug) {
|
||||
return pages.findByCompanyIdAndSlug(companyId, slug.toLowerCase(Locale.ROOT))
|
||||
.filter(p -> "PUBLISHED".equals(p.getStatus()))
|
||||
.map(p -> toDto(p, true));
|
||||
}
|
||||
|
||||
private static ResponseStatusException slugTaken() {
|
||||
return new ResponseStatusException(HttpStatus.CONFLICT, "slug|PagesSlugTaken");
|
||||
}
|
||||
|
||||
private CmsPageDto toDto(CmsPage p, boolean withBlocks) {
|
||||
return new CmsPageDto(p.getId(), p.getCompanyId(), p.getSlug(), p.getTitle(), p.getMetaDescription(), p.getStatus(),
|
||||
withBlocks ? read(p.getBlocks()) : null, p.getUpdatedAt());
|
||||
}
|
||||
|
||||
private String write(List<CmsBlock> blocks) {
|
||||
try {
|
||||
return json.writeValueAsString(blocks);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private List<CmsBlock> read(String blocks) {
|
||||
if (blocks == null || blocks.isBlank()) {
|
||||
return List.of();
|
||||
}
|
||||
try {
|
||||
return json.readValue(blocks, BLOCKS);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new IllegalStateException("Unreadable page blocks", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -255,7 +255,7 @@ public class EventLeadService {
|
||||
return new EventLeadDetail(e.getUuid(), e.getEventUuid(), l.getUuid(), l.getName(), l.getMobileE164(),
|
||||
l.getEmailNormalised(), l.getCompanyName(), l.getJobTitle(), e.getLeadType(), e.getSalespersonUserId(),
|
||||
e.getTemperature(), e.getStatus(), e.getNextFollowUpDate(), e.getNotes(), e.getCapturedAt(), interestIds, e.getCompanyId(),
|
||||
e.getAttendedAt(), e.getSource());
|
||||
e.getAttendedAt(), e.getSource(), e.getPagePath(), e.getUtmSource(), e.getUtmMedium(), e.getUtmCampaign());
|
||||
}
|
||||
|
||||
private static void fillBlank(String current, String offered, Consumer<String> setter) {
|
||||
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.services.counter.model.PageImage;
|
||||
import com.solvway.services.counter.repo.PageImageRepository;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.Base64;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Landing-page images, kept in MySQL. ponytail: fine for a few hundred images; move the bytes to object storage past that.
|
||||
* The type comes from the magic bytes, never from the file name.
|
||||
*/
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class PageImageService {
|
||||
static final int MAX_BYTES = 2 * 1024 * 1024;
|
||||
private final PageImageRepository repository;
|
||||
|
||||
public long upload(Long companyId, String base64, String user) {
|
||||
if (companyId == null) {
|
||||
throw bad("companyId|PagesRequired");
|
||||
}
|
||||
byte[] bytes;
|
||||
try {
|
||||
bytes = Base64.getDecoder().decode(base64 == null ? "" : base64);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw bad("file|PagesBadImageType");
|
||||
}
|
||||
if (bytes.length > MAX_BYTES) {
|
||||
throw bad("file|PagesImageTooLarge");
|
||||
}
|
||||
String type = typeOf(bytes);
|
||||
if (type == null) {
|
||||
throw bad("file|PagesBadImageType");
|
||||
}
|
||||
PageImage image = new PageImage();
|
||||
image.setCompanyId(companyId);
|
||||
image.setContentType(type);
|
||||
image.setBytes(bytes);
|
||||
image.setCreatedBy(user);
|
||||
return repository.save(image).getId();
|
||||
}
|
||||
|
||||
public Optional<PageImage> find(long id) {
|
||||
return repository.findById(id);
|
||||
}
|
||||
|
||||
public Optional<Long> companyOf(long id) {
|
||||
return repository.findCompanyIdById(id);
|
||||
}
|
||||
|
||||
static String typeOf(byte[] b) {
|
||||
if (b.length >= 8 && (b[0] & 0xFF) == 0x89 && b[1] == 'P' && b[2] == 'N' && b[3] == 'G'
|
||||
&& b[4] == 0x0D && b[5] == 0x0A && b[6] == 0x1A && b[7] == 0x0A) {
|
||||
return "image/png";
|
||||
}
|
||||
if (b.length >= 3 && (b[0] & 0xFF) == 0xFF && (b[1] & 0xFF) == 0xD8 && (b[2] & 0xFF) == 0xFF) {
|
||||
return "image/jpeg";
|
||||
}
|
||||
if (b.length >= 12 && b[0] == 'R' && b[1] == 'I' && b[2] == 'F' && b[3] == 'F'
|
||||
&& b[8] == 'W' && b[9] == 'E' && b[10] == 'B' && b[11] == 'P') {
|
||||
return "image/webp";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static ResponseStatusException bad(String reason) {
|
||||
return new ResponseStatusException(HttpStatus.BAD_REQUEST, reason);
|
||||
}
|
||||
}
|
||||
+26
-8
@@ -25,24 +25,33 @@ public class RegistrationService {
|
||||
public record RegisterRequest(Long companyId, long eventUuid, Long salespersonUserId, String name, String mobileE164,
|
||||
String email, String companyName, String jobTitle, List<Long> interestIds,
|
||||
Long consentNoticeId, LocalDateTime consentAt, boolean attend,
|
||||
String source, Long guestId) {
|
||||
/** source null means QR; guestId, when set, is the invited guest this registration links to. */
|
||||
String source, Long guestId,
|
||||
String pagePath, String utmSource, String utmMedium, String utmCampaign) {
|
||||
/** source null means QR; guestId, when set, is the invited guest this registration links to. pagePath and utm* come with source WEB. */
|
||||
public RegisterRequest(Long companyId, long eventUuid, Long salespersonUserId, String name, String mobileE164,
|
||||
String email, String companyName, String jobTitle, List<Long> interestIds,
|
||||
Long consentNoticeId, LocalDateTime consentAt, boolean attend, String source, Long guestId) {
|
||||
this(companyId, eventUuid, salespersonUserId, name, mobileE164, email, companyName, jobTitle, interestIds,
|
||||
consentNoticeId, consentAt, attend, source, guestId, null, null, null, null);
|
||||
}
|
||||
public RegisterRequest(Long companyId, long eventUuid, Long salespersonUserId, String name, String mobileE164,
|
||||
String email, String companyName, String jobTitle, List<Long> interestIds,
|
||||
Long consentNoticeId, LocalDateTime consentAt, boolean attend) {
|
||||
this(companyId, eventUuid, salespersonUserId, name, mobileE164, email, companyName, jobTitle, interestIds,
|
||||
consentNoticeId, consentAt, attend, null, null);
|
||||
consentNoticeId, consentAt, attend, null, null, null, null, null, null);
|
||||
}
|
||||
public RegisterRequest(Long companyId, long eventUuid, Long salespersonUserId, String name, String mobileE164,
|
||||
String email, String companyName, String jobTitle, List<Long> interestIds,
|
||||
Long consentNoticeId, LocalDateTime consentAt) {
|
||||
this(companyId, eventUuid, salespersonUserId, name, mobileE164, email, companyName, jobTitle, interestIds,
|
||||
consentNoticeId, consentAt, false, null, null);
|
||||
consentNoticeId, consentAt, false, null, null, null, null, null, null);
|
||||
}
|
||||
}
|
||||
public record RegisterResult(String outcome, String eventLeadUuid) {}
|
||||
|
||||
private static final String QR = "qr";
|
||||
private static final String WEB = "WEB";
|
||||
private static final int UTM_MAX = 100;
|
||||
|
||||
private final TransactionTemplate transactionTemplate;
|
||||
private final LeadRepository leadRepository;
|
||||
@@ -96,7 +105,7 @@ public class RegistrationService {
|
||||
lead = match.get();
|
||||
Optional<EventLead> captured = eventLeadRepository.findByEventUuidAndLeadUuid(req.eventUuid(), lead.getUuid());
|
||||
if (captured.isPresent()) return alreadyCaptured(captured.get(), lead, req);
|
||||
notes = differences(lead, req);
|
||||
notes = differences(lead, req, WEB.equals(req.source()) ? "web page" : "QR");
|
||||
leadType = "EXISTING";
|
||||
}
|
||||
|
||||
@@ -112,8 +121,13 @@ public class RegistrationService {
|
||||
e.setCapturedBy(QR);
|
||||
e.setConsentAt(req.consentAt());
|
||||
e.setConsentNoticeId(req.consentNoticeId());
|
||||
e.setPagePath(req.pagePath());
|
||||
e.setUtmSource(cut(req.utmSource()));
|
||||
e.setUtmMedium(cut(req.utmMedium()));
|
||||
e.setUtmCampaign(cut(req.utmCampaign()));
|
||||
// An invite link can be opened anywhere, so an invite registration never counts as attending; the guest checks in.
|
||||
boolean atVenue = !"INVITE".equals(req.source()) && eventLeadService.isDuringEvent(req.eventUuid());
|
||||
// A web page sign-up neither: a standing enquiries event is always running.
|
||||
boolean atVenue = !"INVITE".equals(req.source()) && !WEB.equals(req.source()) && eventLeadService.isDuringEvent(req.eventUuid());
|
||||
if (req.attend() || atVenue) e.setAttendedAt(LocalDateTime.now());
|
||||
e = eventLeadRepository.saveAndFlush(e);
|
||||
eventLeadService.recordStatusChange(e, null, "OPEN", e.getCapturedAt(), QR);
|
||||
@@ -145,14 +159,18 @@ public class RegistrationService {
|
||||
}
|
||||
|
||||
/** Submitted non-blank values that differ from the lead's, as an event-lead note; null when nothing differs. */
|
||||
private static String differences(Lead lead, RegisterRequest req) {
|
||||
private static String cut(String value) {
|
||||
return value == null || value.isBlank() ? null : value.length() > UTM_MAX ? value.substring(0, UTM_MAX) : value;
|
||||
}
|
||||
|
||||
private static String differences(Lead lead, RegisterRequest req, String channel) {
|
||||
List<String> parts = new ArrayList<>();
|
||||
diff(parts, "name", req.name(), lead.getName());
|
||||
diff(parts, "mobile", req.mobileE164(), lead.getMobileE164());
|
||||
diff(parts, "email", req.email(), lead.getEmailNormalised());
|
||||
diff(parts, "company", req.companyName(), lead.getCompanyName());
|
||||
diff(parts, "job title", req.jobTitle(), lead.getJobTitle());
|
||||
return parts.isEmpty() ? null : "Registered by QR with: " + String.join("; ", parts);
|
||||
return parts.isEmpty() ? null : "Registered by " + channel + " with: " + String.join("; ", parts);
|
||||
}
|
||||
|
||||
private static void diff(List<String> parts, String label, String submitted, String current) {
|
||||
|
||||
Reference in new issue
Block a user