| 1 | package com.rbctcsworld.ecommerce.order; | |
| 2 | ||
| 3 | import com.rbctcsworld.ecommerce.common.exception.ConflictException; | |
| 4 | import jakarta.persistence.CascadeType; | |
| 5 | import jakarta.persistence.Column; | |
| 6 | import jakarta.persistence.Entity; | |
| 7 | import jakarta.persistence.GeneratedValue; | |
| 8 | import jakarta.persistence.GenerationType; | |
| 9 | import jakarta.persistence.Id; | |
| 10 | import jakarta.persistence.OneToMany; | |
| 11 | import jakarta.persistence.OrderBy; | |
| 12 | import jakarta.persistence.PrePersist; | |
| 13 | import jakarta.persistence.Table; | |
| 14 | ||
| 15 | import java.math.BigDecimal; | |
| 16 | import java.time.LocalDateTime; | |
| 17 | import java.util.ArrayList; | |
| 18 | import java.util.List; | |
| 19 | ||
| 20 | /** Named CustomerOrder because "Order" clashes with SQL ORDER BY and JPA's Order type. */ | |
| 21 | @Entity | |
| 22 | @Table(name = "orders") | |
| 23 | public class CustomerOrder { | |
| 24 | ||
| 25 | @Id | |
| 26 | @GeneratedValue(strategy = GenerationType.IDENTITY) | |
| 27 | private Long id; | |
| 28 | ||
| 29 | @Column(name = "order_number", nullable = false, unique = true, length = 30) | |
| 30 | private String orderNumber; | |
| 31 | ||
| 32 | @Column(name = "user_id", nullable = false) | |
| 33 | private Long userId; | |
| 34 | ||
| 35 | @Column(nullable = false, length = 20) | |
| 36 | private String status = OrderStatus.PLACED; | |
| 37 | ||
| 38 | @Column(name = "total_items", nullable = false) | |
| 39 | private int totalItems; | |
| 40 | ||
| 41 | @Column(nullable = false, precision = 12, scale = 2) | |
| 42 | private BigDecimal subtotal = BigDecimal.ZERO; | |
| 43 | ||
| 44 | @Column(nullable = false, precision = 12, scale = 2) | |
| 45 | private BigDecimal discount = BigDecimal.ZERO; | |
| 46 | ||
| 47 | @Column(name = "shipping_fee", nullable = false, precision = 12, scale = 2) | |
| 48 | private BigDecimal shippingFee = BigDecimal.ZERO; | |
| 49 | ||
| 50 | @Column(nullable = false, precision = 12, scale = 2) | |
| 51 | private BigDecimal tax = BigDecimal.ZERO; | |
| 52 | ||
| 53 | @Column(nullable = false, precision = 12, scale = 2) | |
| 54 | private BigDecimal total = BigDecimal.ZERO; | |
| 55 | ||
| 56 | @Column(name = "coupon_code", length = 40) | |
| 57 | private String couponCode; | |
| 58 | ||
| 59 | @Column(name = "paid_at") | |
| 60 | private LocalDateTime paidAt; | |
| 61 | ||
| 62 | @Column(length = 10) | |
| 63 | private String carrier; | |
| 64 | ||
| 65 | @Column(name = "tracking_number", unique = true, length = 30) | |
| 66 | private String trackingNumber; | |
| 67 | ||
| 68 | @Column(name = "shipped_at") | |
| 69 | private LocalDateTime shippedAt; | |
| 70 | ||
| 71 | @Column(name = "delivered_at") | |
| 72 | private LocalDateTime deliveredAt; | |
| 73 | ||
| 74 | @Column(name = "created_at", nullable = false) | |
| 75 | private LocalDateTime createdAt; | |
| 76 | ||
| 77 | @Column(name = "cancelled_at") | |
| 78 | private LocalDateTime cancelledAt; | |
| 79 | ||
| 80 | // delivery address: a copy taken when the order is placed (V12); null for orders placed before | |
| 81 | @Column(name = "ship_name", length = 80) | |
| 82 | private String shipName; | |
| 83 | ||
| 84 | @Column(name = "ship_line1", length = 100) | |
| 85 | private String shipLine1; | |
| 86 | ||
| 87 | @Column(name = "ship_line2", length = 100) | |
| 88 | private String shipLine2; | |
| 89 | ||
| 90 | @Column(name = "ship_city", length = 60) | |
| 91 | private String shipCity; | |
| 92 | ||
| 93 | @Column(name = "ship_state", length = 2) | |
| 94 | private String shipState; | |
| 95 | ||
| 96 | @Column(name = "ship_zip", length = 10) | |
| 97 | private String shipZip; | |
| 98 | ||
| 99 | @Column(name = "ship_phone", length = 20) | |
| 100 | private String shipPhone; | |
| 101 | ||
| 102 | @OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true) | |
| 103 | @OrderBy("id ASC") | |
| 104 | private List<OrderItem> items = new ArrayList<>(); | |
| 105 | ||
| 106 | @OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true) | |
| 107 | @OrderBy("id ASC") | |
| 108 | private List<OrderEvent> events = new ArrayList<>(); | |
| 109 | ||
| 110 | protected CustomerOrder() { | |
| 111 | } | |
| 112 | ||
| 113 | public CustomerOrder(String orderNumber, Long userId) { | |
| 114 | this.orderNumber = orderNumber; | |
| 115 | this.userId = userId; | |
| 116 | this.events.add(new OrderEvent(this, OrderStatus.PLACED, "Order placed")); | |
| 117 | } | |
| 118 | ||
| 119 | @PrePersist | |
| 120 | void onCreate() { | |
| 121 | createdAt = LocalDateTime.now(); | |
| 122 | } | |
| 123 | ||
| 124 | public void addItem(OrderItem item) { | |
| 125 |
1
1. addItem : removed call to com/rbctcsworld/ecommerce/order/OrderItem::attachTo → KILLED |
item.attachTo(this); |
| 126 | items.add(item); | |
| 127 |
1
1. addItem : Replaced integer addition with subtraction → KILLED |
totalItems += item.getQuantity(); |
| 128 | subtotal = subtotal.add(item.getLineTotal()); | |
| 129 | } | |
| 130 | ||
| 131 | /** Stores the final price breakdown (subtotal itself is accumulated by addItem). */ | |
| 132 | public void applyPricing(BigDecimal discount, BigDecimal shippingFee, BigDecimal tax, BigDecimal total, | |
| 133 | String couponCode) { | |
| 134 | this.discount = discount; | |
| 135 | this.shippingFee = shippingFee; | |
| 136 | this.tax = tax; | |
| 137 | this.total = total; | |
| 138 | this.couponCode = couponCode; | |
| 139 | } | |
| 140 | ||
| 141 | /** Stores where the order goes (expects a normalized address). */ | |
| 142 | public void shipTo(ShippingAddress a) { | |
| 143 | this.shipName = a.fullName(); | |
| 144 | this.shipLine1 = a.line1(); | |
| 145 | this.shipLine2 = a.line2(); | |
| 146 | this.shipCity = a.city(); | |
| 147 | this.shipState = a.state(); | |
| 148 | this.shipZip = a.zip(); | |
| 149 | this.shipPhone = a.phone(); | |
| 150 | } | |
| 151 | ||
| 152 | /** The delivery address, or null for an order placed before addresses existed. */ | |
| 153 | public ShippingAddress getShippingAddress() { | |
| 154 |
2
1. getShippingAddress : removed conditional - replaced equality check with false → KILLED 2. getShippingAddress : replaced return value with null for com/rbctcsworld/ecommerce/order/CustomerOrder::getShippingAddress → KILLED |
return shipName == null ? null |
| 155 | : new ShippingAddress(shipName, shipLine1, shipLine2, shipCity, shipState, shipZip, shipPhone); | |
| 156 | } | |
| 157 | ||
| 158 | // ------------------------------------------------------------ state machine | |
| 159 | ||
| 160 | /** The ONLY way the status changes: checks OrderStatus.ALLOWED (409 otherwise) and records a timeline event. */ | |
| 161 | public void transitionTo(String target, String note) { | |
| 162 |
1
1. transitionTo : removed conditional - replaced equality check with false → KILLED |
if (!OrderStatus.canMove(status, target)) { |
| 163 | throw new ConflictException("Order " + orderNumber + " cannot move from " + status + " to " + target); | |
| 164 | } | |
| 165 | status = target; | |
| 166 | events.add(new OrderEvent(this, target, note)); | |
| 167 | } | |
| 168 | ||
| 169 | public boolean isPaid() { | |
| 170 |
2
1. isPaid : replaced boolean return with false for com/rbctcsworld/ecommerce/order/CustomerOrder::isPaid → KILLED 2. isPaid : replaced boolean return with true for com/rbctcsworld/ecommerce/order/CustomerOrder::isPaid → KILLED |
return OrderStatus.PAID.equals(status); |
| 171 | } | |
| 172 | ||
| 173 | /** Throws 409 unless the order may currently move to {@code target} (used before side effects like charging). */ | |
| 174 | public void assertCanMoveTo(String target) { | |
| 175 |
1
1. assertCanMoveTo : removed conditional - replaced equality check with false → KILLED |
if (!OrderStatus.canMove(status, target)) { |
| 176 | throw new ConflictException("Order " + orderNumber + " cannot move from " + status + " to " + target); | |
| 177 | } | |
| 178 | } | |
| 179 | ||
| 180 | public void markPaid() { | |
| 181 |
1
1. markPaid : removed call to com/rbctcsworld/ecommerce/order/CustomerOrder::transitionTo → KILLED |
transitionTo(OrderStatus.PAID, "Payment received"); |
| 182 | paidAt = LocalDateTime.now(); | |
| 183 | } | |
| 184 | ||
| 185 | public void ship(String carrier, String trackingNumber) { | |
| 186 |
1
1. ship : removed call to com/rbctcsworld/ecommerce/order/CustomerOrder::transitionTo → KILLED |
transitionTo(OrderStatus.SHIPPED, "Shipped with " + carrier + ", tracking " + trackingNumber); |
| 187 | this.carrier = carrier; | |
| 188 | this.trackingNumber = trackingNumber; | |
| 189 | this.shippedAt = LocalDateTime.now(); | |
| 190 | } | |
| 191 | ||
| 192 | public void deliver() { | |
| 193 |
1
1. deliver : removed call to com/rbctcsworld/ecommerce/order/CustomerOrder::transitionTo → KILLED |
transitionTo(OrderStatus.DELIVERED, "Delivered"); |
| 194 | this.deliveredAt = LocalDateTime.now(); | |
| 195 | } | |
| 196 | ||
| 197 | public void cancel() { | |
| 198 |
1
1. cancel : removed call to com/rbctcsworld/ecommerce/order/CustomerOrder::transitionTo → KILLED |
transitionTo(OrderStatus.CANCELLED, "Order cancelled"); |
| 199 | cancelledAt = LocalDateTime.now(); | |
| 200 | } | |
| 201 | ||
| 202 |
1
1. getId : replaced Long return value with 0L for com/rbctcsworld/ecommerce/order/CustomerOrder::getId → KILLED |
public Long getId() { return id; } |
| 203 |
1
1. getOrderNumber : replaced return value with "" for com/rbctcsworld/ecommerce/order/CustomerOrder::getOrderNumber → KILLED |
public String getOrderNumber() { return orderNumber; } |
| 204 |
1
1. getUserId : replaced Long return value with 0L for com/rbctcsworld/ecommerce/order/CustomerOrder::getUserId → KILLED |
public Long getUserId() { return userId; } |
| 205 |
1
1. getStatus : replaced return value with "" for com/rbctcsworld/ecommerce/order/CustomerOrder::getStatus → KILLED |
public String getStatus() { return status; } |
| 206 |
1
1. getTotalItems : replaced int return with 0 for com/rbctcsworld/ecommerce/order/CustomerOrder::getTotalItems → KILLED |
public int getTotalItems() { return totalItems; } |
| 207 |
1
1. getSubtotal : replaced return value with null for com/rbctcsworld/ecommerce/order/CustomerOrder::getSubtotal → KILLED |
public BigDecimal getSubtotal() { return subtotal; } |
| 208 |
1
1. getDiscount : replaced return value with null for com/rbctcsworld/ecommerce/order/CustomerOrder::getDiscount → KILLED |
public BigDecimal getDiscount() { return discount; } |
| 209 |
1
1. getShippingFee : replaced return value with null for com/rbctcsworld/ecommerce/order/CustomerOrder::getShippingFee → KILLED |
public BigDecimal getShippingFee() { return shippingFee; } |
| 210 |
1
1. getTax : replaced return value with null for com/rbctcsworld/ecommerce/order/CustomerOrder::getTax → KILLED |
public BigDecimal getTax() { return tax; } |
| 211 |
1
1. getTotal : replaced return value with null for com/rbctcsworld/ecommerce/order/CustomerOrder::getTotal → KILLED |
public BigDecimal getTotal() { return total; } |
| 212 |
1
1. getCouponCode : replaced return value with "" for com/rbctcsworld/ecommerce/order/CustomerOrder::getCouponCode → KILLED |
public String getCouponCode() { return couponCode; } |
| 213 |
1
1. getPaidAt : replaced return value with null for com/rbctcsworld/ecommerce/order/CustomerOrder::getPaidAt → KILLED |
public LocalDateTime getPaidAt() { return paidAt; } |
| 214 |
1
1. getCarrier : replaced return value with "" for com/rbctcsworld/ecommerce/order/CustomerOrder::getCarrier → KILLED |
public String getCarrier() { return carrier; } |
| 215 |
1
1. getTrackingNumber : replaced return value with "" for com/rbctcsworld/ecommerce/order/CustomerOrder::getTrackingNumber → KILLED |
public String getTrackingNumber() { return trackingNumber; } |
| 216 |
1
1. getShippedAt : replaced return value with null for com/rbctcsworld/ecommerce/order/CustomerOrder::getShippedAt → KILLED |
public LocalDateTime getShippedAt() { return shippedAt; } |
| 217 |
1
1. getDeliveredAt : replaced return value with null for com/rbctcsworld/ecommerce/order/CustomerOrder::getDeliveredAt → KILLED |
public LocalDateTime getDeliveredAt() { return deliveredAt; } |
| 218 |
1
1. getEvents : replaced return value with Collections.emptyList for com/rbctcsworld/ecommerce/order/CustomerOrder::getEvents → KILLED |
public List<OrderEvent> getEvents() { return events; } |
| 219 |
1
1. getCreatedAt : replaced return value with null for com/rbctcsworld/ecommerce/order/CustomerOrder::getCreatedAt → KILLED |
public LocalDateTime getCreatedAt() { return createdAt; } |
| 220 |
1
1. getCancelledAt : replaced return value with null for com/rbctcsworld/ecommerce/order/CustomerOrder::getCancelledAt → KILLED |
public LocalDateTime getCancelledAt() { return cancelledAt; } |
| 221 |
1
1. getItems : replaced return value with Collections.emptyList for com/rbctcsworld/ecommerce/order/CustomerOrder::getItems → KILLED |
public List<OrderItem> getItems() { return items; } |
| 222 | } | |
Mutations | ||
| 125 |
1.1 |
|
| 127 |
1.1 |
|
| 154 |
1.1 2.2 |
|
| 162 |
1.1 |
|
| 170 |
1.1 2.2 |
|
| 175 |
1.1 |
|
| 181 |
1.1 |
|
| 186 |
1.1 |
|
| 193 |
1.1 |
|
| 198 |
1.1 |
|
| 202 |
1.1 |
|
| 203 |
1.1 |
|
| 204 |
1.1 |
|
| 205 |
1.1 |
|
| 206 |
1.1 |
|
| 207 |
1.1 |
|
| 208 |
1.1 |
|
| 209 |
1.1 |
|
| 210 |
1.1 |
|
| 211 |
1.1 |
|
| 212 |
1.1 |
|
| 213 |
1.1 |
|
| 214 |
1.1 |
|
| 215 |
1.1 |
|
| 216 |
1.1 |
|
| 217 |
1.1 |
|
| 218 |
1.1 |
|
| 219 |
1.1 |
|
| 220 |
1.1 |
|
| 221 |
1.1 |