feat(tickets): ticket settings, ticket types and order tables
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
896ee3bcc5
commit
b8a1873266
23 files changed
+1171
-6
No files matched your search
+51
@@ -0,0 +1,51 @@
|
||||
package com.solvway.services.counter.controller;
|
||||
|
||||
import com.solvway.services.counter.model.Event;
|
||||
import com.solvway.services.counter.service.EventService;
|
||||
import com.solvway.services.counter.service.TicketTypeService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/** Ticket settings and types (add-event-ticket-orders). onSale is the only public path. */
|
||||
@RestController
|
||||
@RequestMapping("/counter/ticketType")
|
||||
@AllArgsConstructor
|
||||
public class TicketTypeController {
|
||||
private final TicketTypeService service;
|
||||
private final EventService eventService;
|
||||
|
||||
@PostMapping("/settings")
|
||||
public Map<String, Object> settings(@RequestBody EventService.TicketSettingsRequest req) {
|
||||
Event e = eventService.ticketSettings(req);
|
||||
return Map.of("ticketsEnabled", e.isTicketsEnabled(), "paymentInstructions", e.getPaymentInstructions() == null ? "" : e.getPaymentInstructions(),
|
||||
"holdDays", e.getHoldDays());
|
||||
}
|
||||
|
||||
@PostMapping("/list")
|
||||
public List<TicketTypeService.TypeRow> list(@RequestBody TicketTypeService.ListRequest req) {
|
||||
return service.list(req);
|
||||
}
|
||||
|
||||
@PostMapping("/save")
|
||||
public TicketTypeService.TypeRow save(@RequestBody TicketTypeService.SaveRequest req) {
|
||||
return service.save(req);
|
||||
}
|
||||
|
||||
@PostMapping("/delete")
|
||||
public void delete(@RequestBody TicketTypeService.DeleteRequest req) {
|
||||
service.delete(req);
|
||||
}
|
||||
|
||||
@GetMapping("/onSale/{eventUuid}")
|
||||
public TicketTypeService.OnSale onSale(@PathVariable("eventUuid") long eventUuid) {
|
||||
return service.onSale(eventUuid);
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,13 @@ public class Event implements Serializable {
|
||||
private Integer invitationCap;
|
||||
@Column(name = "checkin_token", updatable = false)
|
||||
private String checkinToken;
|
||||
/** Ticket settings (add-event-ticket-orders): changed only through EventService.ticketSettings. */
|
||||
@Column(name = "tickets_enabled")
|
||||
private boolean ticketsEnabled = false;
|
||||
@Column(name = "payment_instructions")
|
||||
private String paymentInstructions;
|
||||
@Column(name = "hold_days")
|
||||
private int holdDays = 3;
|
||||
private String uploadFloorPlan;
|
||||
private String uploadStageActivities;
|
||||
private String uploadLuckyDraw;
|
||||
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
package com.solvway.services.counter.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/** An order of a buyer: ticket lines now, merch lines later (add-event-ticket-orders). Money in sen. */
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
@Table(name = "shop_order", uniqueConstraints = {
|
||||
@UniqueConstraint(columnNames = {"company_id", "reference"}),
|
||||
@UniqueConstraint(columnNames = {"token"})
|
||||
})
|
||||
public class ShopOrder {
|
||||
public static final String AWAITING = "AWAITING";
|
||||
public static final String PAID = "PAID";
|
||||
public static final String CANCELLED = "CANCELLED";
|
||||
public static final String EXPIRED = "EXPIRED";
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "company_id")
|
||||
private long companyId;
|
||||
|
||||
@Column(name = "event_uuid")
|
||||
private Long eventUuid;
|
||||
|
||||
@Column(name = "reference")
|
||||
private String reference;
|
||||
|
||||
@Column(name = "token", length = 32)
|
||||
private String token;
|
||||
|
||||
@Column(name = "buyer_name")
|
||||
private String buyerName;
|
||||
|
||||
@Column(name = "buyer_email")
|
||||
private String buyerEmail;
|
||||
|
||||
@Column(name = "buyer_mobile_e164")
|
||||
private String buyerMobileE164;
|
||||
|
||||
@Column(name = "buyer_company")
|
||||
private String buyerCompany;
|
||||
|
||||
@Column(name = "lead_uuid")
|
||||
private Long leadUuid;
|
||||
|
||||
@Column(name = "event_lead_uuid")
|
||||
private Long eventLeadUuid;
|
||||
|
||||
@Column(name = "status")
|
||||
private String status;
|
||||
|
||||
@Column(name = "total_sen")
|
||||
private int totalSen;
|
||||
|
||||
@Column(name = "hold_until")
|
||||
private LocalDateTime holdUntil;
|
||||
|
||||
@Column(name = "paid_at")
|
||||
private LocalDateTime paidAt;
|
||||
|
||||
@Column(name = "paid_by")
|
||||
private String paidBy;
|
||||
|
||||
@Column(name = "payment_ref")
|
||||
private String paymentRef;
|
||||
|
||||
@Column(name = "cancelled_at")
|
||||
private LocalDateTime cancelledAt;
|
||||
|
||||
@Column(name = "cancelled_by")
|
||||
private String cancelledBy;
|
||||
|
||||
@Column(name = "expired_at")
|
||||
private LocalDateTime expiredAt;
|
||||
|
||||
@Column(name = "consent_at")
|
||||
private LocalDateTime consentAt;
|
||||
|
||||
@Column(name = "consent_notice_id")
|
||||
private Long consentNoticeId;
|
||||
|
||||
@Column(name = "page_path")
|
||||
private String pagePath;
|
||||
|
||||
@Column(name = "utm_source")
|
||||
private String utmSource;
|
||||
|
||||
@Column(name = "utm_medium")
|
||||
private String utmMedium;
|
||||
|
||||
@Column(name = "utm_campaign")
|
||||
private String utmCampaign;
|
||||
|
||||
@Column(name = "last_email")
|
||||
private String lastEmail;
|
||||
|
||||
@Column(name = "created_at", updatable = false)
|
||||
private LocalDateTime createdAt;
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.solvway.services.counter.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/** One line of an order, keeping the item's name and price at the time (add-event-ticket-orders). */
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
@Table(name = "shop_order_line")
|
||||
public class ShopOrderLine {
|
||||
public static final String TICKET = "TICKET";
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "order_id")
|
||||
private long orderId;
|
||||
|
||||
@Column(name = "kind")
|
||||
private String kind;
|
||||
|
||||
@Column(name = "item_id")
|
||||
private long itemId;
|
||||
|
||||
@Column(name = "name")
|
||||
private String name;
|
||||
|
||||
@Column(name = "unit_price_sen")
|
||||
private int unitPriceSen;
|
||||
|
||||
@Column(name = "quantity")
|
||||
private int quantity;
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.solvway.services.counter.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/** One seat of an order. The code, attendee and check-in columns are filled by add-event-tickets. */
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
@Table(name = "ticket")
|
||||
public class Ticket {
|
||||
public static final String ACTIVE = "ACTIVE";
|
||||
public static final String CANCELLED = "CANCELLED";
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "company_id")
|
||||
private long companyId;
|
||||
|
||||
@Column(name = "event_uuid")
|
||||
private long eventUuid;
|
||||
|
||||
@Column(name = "order_id")
|
||||
private long orderId;
|
||||
|
||||
@Column(name = "order_line_id")
|
||||
private long orderLineId;
|
||||
|
||||
@Column(name = "ticket_type_id")
|
||||
private long ticketTypeId;
|
||||
|
||||
@Column(name = "code", length = 32)
|
||||
private String code;
|
||||
|
||||
@Column(name = "attendee_name")
|
||||
private String attendeeName;
|
||||
|
||||
@Column(name = "attendee_mobile_e164")
|
||||
private String attendeeMobileE164;
|
||||
|
||||
@Column(name = "attendee_email")
|
||||
private String attendeeEmail;
|
||||
|
||||
@Column(name = "event_lead_uuid")
|
||||
private Long eventLeadUuid;
|
||||
|
||||
@Column(name = "status")
|
||||
private String status = ACTIVE;
|
||||
|
||||
@Column(name = "cancelled_at")
|
||||
private LocalDateTime cancelledAt;
|
||||
|
||||
@Column(name = "cancelled_by")
|
||||
private String cancelledBy;
|
||||
|
||||
@Column(name = "checked_in_at")
|
||||
private LocalDateTime checkedInAt;
|
||||
|
||||
@Column(name = "checked_in_by")
|
||||
private String checkedInBy;
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package com.solvway.services.counter.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/** A kind of ticket an event sells (add-event-ticket-orders). Price in sen. */
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
@Table(name = "ticket_type", uniqueConstraints = @UniqueConstraint(columnNames = {"event_uuid", "name"}))
|
||||
public class TicketType {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "company_id")
|
||||
private long companyId;
|
||||
|
||||
@Column(name = "event_uuid")
|
||||
private long eventUuid;
|
||||
|
||||
@Column(name = "name")
|
||||
private String name;
|
||||
|
||||
@Column(name = "price_sen")
|
||||
private int priceSen;
|
||||
|
||||
@Column(name = "quantity")
|
||||
private int quantity;
|
||||
|
||||
@Column(name = "sales_end_date")
|
||||
private LocalDate salesEndDate;
|
||||
|
||||
@Column(name = "sort")
|
||||
private int sort;
|
||||
|
||||
@Column(name = "enabled")
|
||||
private boolean enabled = true;
|
||||
|
||||
@Column(name = "created_by", updatable = false)
|
||||
private String createdBy;
|
||||
|
||||
@Column(name = "created_at", updatable = false)
|
||||
private LocalDateTime createdAt = LocalDateTime.now();
|
||||
|
||||
@Column(name = "updated_by")
|
||||
private String updatedBy;
|
||||
|
||||
@Column(name = "updated_at")
|
||||
private LocalDateTime updatedAt;
|
||||
}
|
||||
+5
@@ -18,6 +18,11 @@ public interface EventRepository extends JpaRepository<Event,Long> , JpaReposito
|
||||
Optional<Event> findByUuid(long uuid);
|
||||
Optional<Event> findByCheckinToken(String checkinToken);
|
||||
|
||||
/** Locks the event row for the rest of the transaction: ticket requests and seat changes of one event run one at a time. */
|
||||
@org.springframework.data.jpa.repository.Lock(jakarta.persistence.LockModeType.PESSIMISTIC_WRITE)
|
||||
@Query("SELECT e FROM Event e WHERE e.uuid = :uuid")
|
||||
Optional<Event> lockByUuid(@Param("uuid") long uuid);
|
||||
|
||||
/** Sets the check-in token only while none exists; returns rows changed. Atomic, so concurrent callers agree on one token. */
|
||||
@Modifying(clearAutomatically = true)
|
||||
@Transactional
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.solvway.services.counter.repo;
|
||||
|
||||
import com.solvway.services.counter.model.ShopOrderLine;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface ShopOrderLineRepository extends JpaRepository<ShopOrderLine, Long> {
|
||||
boolean existsByKindAndItemId(String kind, long itemId);
|
||||
|
||||
List<ShopOrderLine> findByOrderIdOrderByIdAsc(long orderId);
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.solvway.services.counter.repo;
|
||||
|
||||
import com.solvway.services.counter.model.ShopOrder;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface ShopOrderRepository extends JpaRepository<ShopOrder, Long> {
|
||||
Optional<ShopOrder> findByToken(String token);
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package com.solvway.services.counter.repo;
|
||||
|
||||
/** Seats a ticket type holds: active tickets of orders awaiting payment or paid. */
|
||||
public record TicketHeld(Long typeId, Long held) {}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.solvway.services.counter.repo;
|
||||
|
||||
import com.solvway.services.counter.model.Ticket;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface TicketRepository extends JpaRepository<Ticket, Long> {
|
||||
/** Held seats per ticket type of an event: one grouped count, so it never drifts. */
|
||||
@Query("select new com.solvway.services.counter.repo.TicketHeld(t.ticketTypeId, count(t)) from Ticket t, ShopOrder o"
|
||||
+ " where o.id = t.orderId and t.eventUuid = :eventUuid and t.status = 'ACTIVE' and o.status in ('AWAITING', 'PAID')"
|
||||
+ " group by t.ticketTypeId")
|
||||
List<TicketHeld> heldByType(@Param("eventUuid") long eventUuid);
|
||||
|
||||
List<Ticket> findByOrderIdOrderByIdAsc(long orderId);
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.solvway.services.counter.repo;
|
||||
|
||||
import com.solvway.services.counter.model.TicketType;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface TicketTypeRepository extends JpaRepository<TicketType, Long> {
|
||||
List<TicketType> findByEventUuidOrderBySortAscIdAsc(long eventUuid);
|
||||
|
||||
Optional<TicketType> findByEventUuidAndName(long eventUuid, String name);
|
||||
}
|
||||
+28
@@ -50,11 +50,17 @@ public class EventService {
|
||||
if (event.getInvitationCap() != null && event.getInvitationCap() < 1) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "INVITATION_CAP_INVALID");
|
||||
if (event.getId() == null) {
|
||||
if (event.getCompanyId() == null) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "COMPANY_REQUIRED");
|
||||
event.setTicketsEnabled(false); // ticket settings change only through ticketSettings
|
||||
event.setPaymentInstructions(null);
|
||||
event.setHoldDays(3);
|
||||
} else {
|
||||
// company is immutable: always carry the stored one so entity and cache match the DB
|
||||
Optional<Event> stored = eventRepository.findById(event.getId());
|
||||
if (stored.isPresent()) {
|
||||
event.setCompanyId(stored.get().getCompanyId());
|
||||
event.setTicketsEnabled(stored.get().isTicketsEnabled());
|
||||
event.setPaymentInstructions(stored.get().getPaymentInstructions());
|
||||
event.setHoldDays(stored.get().getHoldDays());
|
||||
if (event.getInvitationCap() != null && guestRepository != null
|
||||
&& guestRepository.countByEventUuid(stored.get().getUuid()) > event.getInvitationCap()) {
|
||||
throw new ResponseStatusException(HttpStatus.CONFLICT, "CAP_BELOW_GUESTS");
|
||||
@@ -65,6 +71,28 @@ public class EventService {
|
||||
updateEventCache(event);
|
||||
return event;
|
||||
}
|
||||
public record TicketSettingsRequest(Long companyId, long eventUuid, boolean ticketsEnabled, String paymentInstructions,
|
||||
Integer holdDays, String doneBy) {}
|
||||
|
||||
/** Tickets switch, payment instructions and hold days; the only way they change. */
|
||||
@Transactional
|
||||
public Event ticketSettings(TicketSettingsRequest r) {
|
||||
Event event = eventRepository.findByUuid(r.eventUuid())
|
||||
.filter(e -> r.companyId() != null && r.companyId().equals(e.getCompanyId()))
|
||||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "EVENT_NOT_FOUND"));
|
||||
String text = r.paymentInstructions() == null ? "" : r.paymentInstructions().strip();
|
||||
if (r.ticketsEnabled() && text.isEmpty()) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "PAYMENT_INSTRUCTIONS_REQUIRED");
|
||||
if (text.length() > 1000) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "PAYMENT_INSTRUCTIONS_TOO_LONG");
|
||||
if (r.holdDays() == null || r.holdDays() < 1 || r.holdDays() > 14) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "HOLD_DAYS_INVALID");
|
||||
event.setTicketsEnabled(r.ticketsEnabled());
|
||||
event.setPaymentInstructions(text.isEmpty() ? null : text);
|
||||
event.setHoldDays(r.holdDays());
|
||||
event.setDoneBy(r.doneBy());
|
||||
event = eventRepository.save(event);
|
||||
updateEventCache(event);
|
||||
return event;
|
||||
}
|
||||
|
||||
public List<Event> findAll(){
|
||||
return eventRepository.findAll();
|
||||
}
|
||||
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.core.enums.GeneralEnum;
|
||||
import com.solvway.services.counter.model.Event;
|
||||
import com.solvway.services.counter.model.ShopOrderLine;
|
||||
import com.solvway.services.counter.model.TicketType;
|
||||
import com.solvway.services.counter.repo.EventRepository;
|
||||
import com.solvway.services.counter.repo.ShopOrderLineRepository;
|
||||
import com.solvway.services.counter.repo.TicketHeld;
|
||||
import com.solvway.services.counter.repo.TicketRepository;
|
||||
import com.solvway.services.counter.repo.TicketTypeRepository;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/** Ticket types of an event, their held seats, and the public on-sale view (add-event-ticket-orders). */
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class TicketTypeService {
|
||||
static final ZoneId MALAYSIA = ZoneId.of("Asia/Kuala_Lumpur");
|
||||
private static final int MAX_PRICE_SEN = 9_999_999;
|
||||
|
||||
private final EventRepository events;
|
||||
private final TicketTypeRepository types;
|
||||
private final TicketRepository tickets;
|
||||
private final ShopOrderLineRepository lines;
|
||||
|
||||
public record ListRequest(Long companyId, long eventUuid) {}
|
||||
|
||||
public record TypeRow(Long id, String name, int priceSen, int quantity, LocalDate salesEndDate, int sort, boolean enabled,
|
||||
long held, long left) {}
|
||||
|
||||
public record SaveRequest(Long companyId, long eventUuid, Long id, String name, Integer priceSen, Integer quantity,
|
||||
LocalDate salesEndDate, Integer sort, Boolean enabled, String doneBy) {}
|
||||
|
||||
public record DeleteRequest(Long companyId, long eventUuid, Long id, String doneBy) {}
|
||||
|
||||
public record PublicType(Long id, String name, int priceSen, long left, String state) {}
|
||||
|
||||
public record OnSale(boolean ticketsOpen, String paymentInstructions, List<PublicType> types) {}
|
||||
|
||||
public List<TypeRow> list(ListRequest r) {
|
||||
event(r.companyId(), r.eventUuid(), false);
|
||||
Map<Long, Long> held = held(r.eventUuid());
|
||||
return types.findByEventUuidOrderBySortAscIdAsc(r.eventUuid()).stream().map(t -> row(t, held)).toList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public TypeRow save(SaveRequest r) {
|
||||
Event event = event(r.companyId(), r.eventUuid(), true); // seat changes run one at a time per event
|
||||
String name = r.name() == null ? "" : r.name().strip();
|
||||
if (name.isEmpty() || name.length() > 80) throw status(HttpStatus.BAD_REQUEST, "NAME_INVALID");
|
||||
if (r.priceSen() == null || r.priceSen() < 0 || r.priceSen() > MAX_PRICE_SEN) throw status(HttpStatus.BAD_REQUEST, "PRICE_INVALID");
|
||||
if (r.quantity() == null || r.quantity() < 1) throw status(HttpStatus.BAD_REQUEST, "QUANTITY_INVALID");
|
||||
TicketType t = r.id() == null ? new TicketType() : types.findById(r.id())
|
||||
.filter(x -> x.getEventUuid() == event.getUuid())
|
||||
.orElseThrow(() -> status(HttpStatus.NOT_FOUND, "TYPE_NOT_FOUND"));
|
||||
types.findByEventUuidAndName(event.getUuid(), name)
|
||||
.filter(other -> !other.getId().equals(t.getId()))
|
||||
.ifPresent(other -> { throw status(HttpStatus.CONFLICT, "NAME_TAKEN"); });
|
||||
Map<Long, Long> held = held(event.getUuid());
|
||||
if (t.getId() != null && r.quantity() < held.getOrDefault(t.getId(), 0L)) throw status(HttpStatus.CONFLICT, "QUANTITY_BELOW_HELD");
|
||||
if (t.getId() == null) {
|
||||
t.setCompanyId(event.getCompanyId());
|
||||
t.setEventUuid(event.getUuid());
|
||||
t.setCreatedBy(r.doneBy());
|
||||
} else {
|
||||
t.setUpdatedBy(r.doneBy());
|
||||
t.setUpdatedAt(LocalDateTime.now());
|
||||
}
|
||||
t.setName(name);
|
||||
t.setPriceSen(r.priceSen());
|
||||
t.setQuantity(r.quantity());
|
||||
t.setSalesEndDate(r.salesEndDate());
|
||||
t.setSort(r.sort() == null ? 0 : r.sort());
|
||||
t.setEnabled(r.enabled() == null || r.enabled());
|
||||
return row(types.save(t), held);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void delete(DeleteRequest r) {
|
||||
Event event = event(r.companyId(), r.eventUuid(), true);
|
||||
TicketType t = types.findById(r.id()).filter(x -> x.getEventUuid() == event.getUuid())
|
||||
.orElseThrow(() -> status(HttpStatus.NOT_FOUND, "TYPE_NOT_FOUND"));
|
||||
if (lines.existsByKindAndItemId(ShopOrderLine.TICKET, t.getId())) throw status(HttpStatus.CONFLICT, "TYPE_HAS_ORDERS");
|
||||
types.delete(t);
|
||||
}
|
||||
|
||||
/** What a visitor may see: enabled types only, no company data. */
|
||||
public OnSale onSale(long eventUuid) {
|
||||
Event event = events.findByUuid(eventUuid).orElseThrow(() -> status(HttpStatus.NOT_FOUND, "EVENT_NOT_FOUND"));
|
||||
LocalDate today = LocalDate.now(MALAYSIA);
|
||||
Map<Long, Long> held = held(eventUuid);
|
||||
List<PublicType> shown = types.findByEventUuidOrderBySortAscIdAsc(eventUuid).stream().filter(TicketType::isEnabled).map(t -> {
|
||||
long left = Math.max(0, t.getQuantity() - held.getOrDefault(t.getId(), 0L));
|
||||
String state = salesEnded(t, event, today) ? "ENDED" : left == 0 ? "SOLD_OUT" : "ON_SALE";
|
||||
return new PublicType(t.getId(), t.getName(), t.getPriceSen(), left, state);
|
||||
}).toList();
|
||||
return new OnSale(ticketsOpen(event, today), event.getPaymentInstructions(), shown);
|
||||
}
|
||||
|
||||
static boolean ticketsOpen(Event event, LocalDate today) {
|
||||
return event.isTicketsEnabled() && event.getEnabled() == GeneralEnum.FlagYesNo.Y
|
||||
&& event.getDateEnd() != null && !today.isAfter(event.getDateEnd().toLocalDate());
|
||||
}
|
||||
|
||||
/** A type's sales end on its own date, else on the event's last day. */
|
||||
static boolean salesEnded(TicketType t, Event event, LocalDate today) {
|
||||
LocalDate end = t.getSalesEndDate() != null ? t.getSalesEndDate()
|
||||
: event.getDateEnd() == null ? null : event.getDateEnd().toLocalDate();
|
||||
return end == null || today.isAfter(end);
|
||||
}
|
||||
|
||||
Map<Long, Long> held(long eventUuid) {
|
||||
return tickets.heldByType(eventUuid).stream().collect(Collectors.toMap(TicketHeld::typeId, TicketHeld::held));
|
||||
}
|
||||
|
||||
private Event event(Long companyId, long eventUuid, boolean lock) {
|
||||
return (lock ? events.lockByUuid(eventUuid) : events.findByUuid(eventUuid))
|
||||
.filter(e -> companyId != null && companyId.equals(e.getCompanyId()))
|
||||
.orElseThrow(() -> status(HttpStatus.NOT_FOUND, "EVENT_NOT_FOUND"));
|
||||
}
|
||||
|
||||
private static TypeRow row(TicketType t, Map<Long, Long> held) {
|
||||
long h = held.getOrDefault(t.getId(), 0L);
|
||||
return new TypeRow(t.getId(), t.getName(), t.getPriceSen(), t.getQuantity(), t.getSalesEndDate(), t.getSort(), t.isEnabled(),
|
||||
h, Math.max(0, t.getQuantity() - h));
|
||||
}
|
||||
|
||||
static ResponseStatusException status(HttpStatus s, String reason) {
|
||||
return new ResponseStatusException(s, reason);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user