Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public record CreateOrderRequest(
String currency,
CustomerDetailsDTO customerDetails,
List<CreateOrderLineRequest> orderLines,
Double shippingCost,
AddressDTO shippingAddress,
AddressDTO billingAddress
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public record GetOrderResponse(
AddressDTO shippingAddress,
OrderStatus orderStatus,
String currency,
Double totalAmount,
Double subTotal,
Double shippingCost,
Instant createdAt,
Instant updatedAt,
List<OrderLineViewModel> orderLines
Expand All @@ -33,7 +34,8 @@ public static GetOrderResponse from(OrderDetailsDTO orderDetails) {
orderDetails.getShippingAddress(),
order.getOrderStatus(),
order.getCurrency(),
order.getTotalAmount(),
order.getSubTotal(),
order.getShippingCost(),
order.getCreatedAt(),
order.getUpdatedAt(),
orderDetails.getOrderLines().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ public class OrderDTO {
private long id;
private Long userId;
private String currency;
private double totalAmount;
private double subTotal;
private double shippingCost;
private OrderStatus orderStatus;
private int orderLinesAmount;
private Instant createdAt;
private Instant updatedAt;

public double getTotal() {
return subTotal + shippingCost;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public OrderDTO apply(OrderEntity order) {
.orderStatus(order.getStatus())
.createdAt(order.getCreatedAt())
.updatedAt(order.getUpdatedAt())
.shippingCost(order.getShippingCost() != null ? order.getShippingCost() : 0.0)
.currency(order.getCurrency() != null ? order.getCurrency() : null)
.totalAmount(order.getTotalAmount() != null ? order.getTotalAmount() : 0.0)
.subTotal(order.getSubTotal() != null ? order.getSubTotal() : 0.0)
.orderLinesAmount(order.getOrderLines() != null ? order.getOrderLines().size() : 0)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ public class OrderEntity {
private OrderStatus status;

private String currency;
@Column(name = "total_amount")
private Double totalAmount;
@Column(name = "sub_total")
private Double subTotal;

@Column(name = "shipping_cost")
private Double shippingCost;

@ToString.Exclude
@ManyToOne(cascade = CascadeType.ALL)
Expand All @@ -59,6 +62,10 @@ public class OrderEntity {
@UpdateTimestamp
private Instant updatedAt;

public double getTotal() {
return subTotal + shippingCost;
}

@Override
public final boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public PaymentResponse initiatePayment(PaymentRequest request) {
// Create and save payment entity
PaymentEntity payment = PaymentEntity.builder()
.orderId(order.getId())
.totalAmount(order.getTotalAmount())
.totalAmount(order.getTotal())
.paymentProvider(PaymentProvider.MOBILEPAY)
.status(PaymentStatus.PENDING)
.paymentMethod(request.paymentMethod()) // 'WALLET' or 'CARD'
Expand Down Expand Up @@ -158,7 +158,7 @@ public Map<String, Object> createMobilePayRequest(OrderEntity order, PaymentRequ

// Amount object
Map<String, Object> amount = new HashMap<>();
String value = String.valueOf(Math.round(order.getTotalAmount() * 100)); // Convert to minor units
String value = String.valueOf(Math.round(order.getTotal() * 100)); // Convert to minor units
amount.put("value", value); // Convert to minor units
amount.put("currency", "DKK");
paymentRequest.put("amount", amount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public PaymentResponse initiatePayment(PaymentRequest request) {

PaymentEntity payment = PaymentEntity.builder()
.orderId(order.getId())
.totalAmount(order.getTotalAmount())
.totalAmount(order.getTotal())
.paymentProvider(PaymentProvider.STRIPE)
.status(PaymentStatus.PENDING)
.paymentMethod(request.paymentMethod())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ private Map<String, Object> createOrderContext(OrderDetailsDTO orderDetails) {
orderContext.put("status", order.getOrderStatus());
orderContext.put("createdAt", order.getCreatedAt());
orderContext.put("currency", order.getCurrency());
orderContext.put("totalAmount", order.getTotalAmount());
orderContext.put("subTotal", order.getSubTotal());
orderContext.put("totalPrice", order.getTotal());
orderContext.put("shippingCost", order.getShippingCost());
orderContext.put("customerName", orderDetails.getCustomerDetails().getFullName());
orderContext.put("customerEmail", orderDetails.getCustomerDetails().getEmail());
orderContext.put("customerPhone", orderDetails.getCustomerDetails().getPhone());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,15 @@ private OrderEntity buildOrderEntity(Long userId,
line.variantId() != null ? variants.get(line.variantId()) : null))
.collect(Collectors.toSet());

double totalAmount = calculationService.calculateTotalAmount(orderLines);
double subTotal = calculationService.calculateTotalAmount(orderLines);

OrderEntity order = OrderEntity.builder()
.userId(userId)
.orderLines(orderLines)
.status(OrderStatus.PENDING)
.currency(request.currency())
.totalAmount(totalAmount)
.subTotal(subTotal)
.shippingCost(request.shippingCost())
.orderShippingInfo(shippingInfo)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static OrderViewModel fromDTO(OrderDTO orderDTO) {
orderDTO.getId(),
orderDTO.getUserId(),
orderDTO.getOrderLinesAmount(),
orderDTO.getTotalAmount(),
orderDTO.getSubTotal(),
orderDTO.getCurrency(),
orderDTO.getOrderStatus(),
orderDTO.getCreatedAt()
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/db/changelog/db.changelog-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
<include file="db/changelog/migrations/241230175909-changelog.xml"/>
<include file="db/changelog/migrations/241230180613-changelog.xml"/>
<include file="db/changelog/migrations/250108140403-changelog.xml"/>
<include file="db/changelog/migrations/250115175031-changelog.xml"/>
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.30.xsd"
objectQuotingStrategy="QUOTE_ONLY_RESERVED_WORDS">
<changeSet id="1736959831825-1" author="gkhaavik">
<addColumn tableName="orders">
<column name="shipping_cost" type="DOUBLE"/>
</addColumn>

<renameColumn tableName="orders" oldColumnName="total_amount" newColumnName="sub_total"
columnDataType="DOUBLE"/>
</changeSet>

</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ void setUp() {
.orderLines(orderLines)
.status(OrderStatus.PENDING)
.currency("USD")
.totalAmount(199.98)
.subTotal(199.98)
.shippingCost(39.0)
.createdAt(now)
.updatedAt(now)
.build();
Expand All @@ -61,7 +62,8 @@ void apply_Success() {
assertEquals(orderEntity.getUserId(), result.getUserId());
assertEquals(orderEntity.getStatus(), result.getOrderStatus());
assertEquals(orderEntity.getCurrency(), result.getCurrency());
assertEquals(orderEntity.getTotalAmount(), result.getTotalAmount());
assertEquals(orderEntity.getSubTotal(), result.getSubTotal());
assertEquals(orderEntity.getShippingCost(), result.getShippingCost());
assertEquals(orderEntity.getCreatedAt(), result.getCreatedAt());
assertEquals(orderEntity.getUpdatedAt(), result.getUpdatedAt());
assertEquals(orderEntity.getOrderLines().size(), result.getOrderLinesAmount());
Expand All @@ -81,18 +83,19 @@ void apply_HandlesNullValues() {
assertEquals(0, result.getOrderLinesAmount());
assertNull(result.getUserId());
assertNull(result.getCurrency());
assertEquals(0.0, result.getTotalAmount());
assertEquals(0.0, result.getSubTotal());
assertEquals(0.0, result.getShippingCost());
}

@Test
@DisplayName("Should handle null totalAmount correctly")
void apply_HandlesNullTotalAmount() {
orderEntity.setTotalAmount(null);
orderEntity.setSubTotal(null);

OrderDTO result = orderMapper.apply(orderEntity);

assertNotNull(result);
assertEquals(0.0, result.getTotalAmount());
assertEquals(0.0, result.getSubTotal());
}

@Test
Expand All @@ -105,4 +108,13 @@ void apply_HandlesNullOrderLines() {
assertNotNull(result);
assertEquals(0, result.getOrderLinesAmount());
}

@Test
@DisplayName("Should handle getTotalPrice correctly")
void getTotalPrice() {
OrderDTO result = orderMapper.apply(orderEntity);

assertNotNull(result);
assertEquals(238.98, result.getTotal());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ void setUp() {
.orderLines(orderLines)
.status(OrderStatus.PENDING)
.currency("USD")
.totalAmount(199.98)
.subTotal(199.98)
.shippingCost(39.0)
.build();

// Set up bidirectional relationship
Expand All @@ -49,7 +50,8 @@ void testOrderBuilder() {
assertEquals(1L, order.getUserId());
assertEquals(OrderStatus.PENDING, order.getStatus());
assertEquals("USD", order.getCurrency());
assertEquals(199.98, order.getTotalAmount());
assertEquals(199.98, order.getSubTotal());
assertEquals(39.0, order.getShippingCost());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ void setUp() {
.userId(1L)
.status(OrderStatus.PENDING)
.currency("USD")
.totalAmount(199.98)
.subTotal(199.98)
.shippingCost(39.0)
.orderLines(Set.of(orderLine))
.createdAt(Instant.now())
.build();
Expand All @@ -102,7 +103,7 @@ void setUp() {
.userId(1L)
.orderStatus(OrderStatus.PENDING)
.currency("USD")
.totalAmount(199.98)
.subTotal(199.98)
.build();

AddressDTO addressDTO = AddressDTO.builder()
Expand Down Expand Up @@ -134,7 +135,7 @@ void setUp() {
.build();

CreateOrderLineRequest orderLineRequest = new CreateOrderLineRequest(1L, null, 2);
createOrderRequest = new CreateOrderRequest("USD", customerDetailsDTO, List.of(orderLineRequest), addressDTO, null);
createOrderRequest = new CreateOrderRequest("USD", customerDetailsDTO, List.of(orderLineRequest), 39.0, addressDTO, null);
}

@Nested
Expand All @@ -154,7 +155,9 @@ void createOrder_Success() {

assertNotNull(result);
assertEquals(orderDTO.getId(), result.getId());
assertEquals(orderDTO.getTotalAmount(), result.getTotalAmount());
assertEquals(orderDTO.getSubTotal(), result.getSubTotal());
assertEquals(orderDTO.getShippingCost(), result.getShippingCost());
assertEquals(orderDTO.getTotal(), result.getTotal());
verify(validationService).validateCreateOrderRequest(createOrderRequest);
}

Expand Down
Loading