Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 4dfb8e2

Browse files
committed
making userId optional on orders to add compatibility with guest checkouts
adding missing customer details to shipping info adding customer details validation
1 parent 86dbec7 commit 4dfb8e2

7 files changed

Lines changed: 63 additions & 12 deletions

File tree

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
<dependency>
136136
<groupId>org.liquibase</groupId>
137137
<artifactId>liquibase-core</artifactId>
138+
<version>4.30.0</version>
138139
</dependency>
139140
<dependency>
140141
<groupId>org.springframework.retry</groupId>

src/main/java/com/zenfulcode/commercify/commercify/controller/OrderController.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ public class OrderController {
3737
"id", "userId", "status", "currency", "totalAmount", "createdAt", "updatedAt"
3838
);
3939

40-
@PreAuthorize("hasRole('USER') and #userId == authentication.principal.id")
4140
@PostMapping("/{userId}")
42-
public ResponseEntity<?> createOrder(@PathVariable Long userId, @Validated @RequestBody CreateOrderRequest orderRequest) {
41+
public ResponseEntity<?> createOrder(@PathVariable Long userId, @RequestBody CreateOrderRequest orderRequest) {
4342
try {
4443
OrderDTO orderDTO = orderService.createOrder(userId, orderRequest);
4544
return ResponseEntity.ok(CreateOrderResponse.from(OrderViewModel.fromDTO(orderDTO)));
@@ -62,6 +61,30 @@ public ResponseEntity<?> createOrder(@PathVariable Long userId, @Validated @Requ
6261
}
6362
}
6463

64+
@PostMapping("/")
65+
public ResponseEntity<?> createOrder(@RequestBody CreateOrderRequest orderRequest) {
66+
try {
67+
OrderDTO orderDTO = orderService.createOrder(orderRequest);
68+
return ResponseEntity.ok(CreateOrderResponse.from(OrderViewModel.fromDTO(orderDTO)));
69+
} catch (IllegalArgumentException e) {
70+
return ResponseEntity.badRequest()
71+
.body(CreateOrderResponse.from("Invalid request: " + e.getMessage()));
72+
} catch (ProductNotFoundException e) {
73+
return ResponseEntity.badRequest()
74+
.body(CreateOrderResponse.from("Product not found: " + e.getMessage()));
75+
} catch (InsufficientStockException e) {
76+
return ResponseEntity.badRequest()
77+
.body(CreateOrderResponse.from("Insufficient stock: " + e.getMessage()));
78+
} catch (OrderValidationException e) {
79+
return ResponseEntity.badRequest()
80+
.body(CreateOrderResponse.from(e.getMessage()));
81+
} catch (Exception e) {
82+
log.error("Error creating order", e);
83+
return ResponseEntity.internalServerError()
84+
.body(CreateOrderResponse.from("Error creating order: " + e.getMessage()));
85+
}
86+
}
87+
6588
@PreAuthorize("hasRole('USER') and #userId == authentication.principal.id or hasRole('ADMIN')")
6689
@GetMapping("/user/{userId}")
6790
public ResponseEntity<?> getOrdersByUserId(

src/main/java/com/zenfulcode/commercify/commercify/entity/OrderEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class OrderEntity {
3030
@Column(name = "id", nullable = false)
3131
private Long id;
3232

33-
@Column(name = "user_id", nullable = false)
33+
@Column(name = "user_id")
3434
private Long userId;
3535

3636
@ToString.Exclude

src/main/java/com/zenfulcode/commercify/commercify/service/order/OrderService.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.zenfulcode.commercify.commercify.api.requests.orders.CreateOrderLineRequest;
55
import com.zenfulcode.commercify.commercify.api.requests.orders.CreateOrderRequest;
66
import com.zenfulcode.commercify.commercify.dto.*;
7-
import com.zenfulcode.commercify.commercify.dto.mapper.AddressMapper;
87
import com.zenfulcode.commercify.commercify.dto.mapper.OrderMapper;
98
import com.zenfulcode.commercify.commercify.dto.mapper.ProductMapper;
109
import com.zenfulcode.commercify.commercify.dto.mapper.ProductVariantMapper;
@@ -42,6 +41,11 @@ public class OrderService {
4241
private final ProductVariantMapper productVariantMapper;
4342
private final OrderShippingInfoRepository orderShippingInfoRepository;
4443

44+
@Transactional
45+
public OrderDTO createOrder(CreateOrderRequest request) {
46+
return createOrder(null, request);
47+
}
48+
4549
@Transactional
4650
public OrderDTO createOrder(Long userId, CreateOrderRequest request) {
4751
// Validate request and check stock
@@ -81,6 +85,11 @@ private OrderShippingInfo getShippingInformation(CreateOrderRequest request) {
8185
.billingCountry(billingAddress.getCountry());
8286
}
8387

88+
shippingInfo.customerEmail(request.customerDetails().getEmail())
89+
.customerFirstName(request.customerDetails().getFirstName())
90+
.customerLastName(request.customerDetails().getLastName())
91+
.customerPhone(request.customerDetails().getPhone());
92+
8493
return shippingInfo.build();
8594
}
8695

src/main/java/com/zenfulcode/commercify/commercify/service/order/OrderValidationService.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ public void validateCreateOrderRequest(CreateOrderRequest request) {
5454
}
5555
}
5656

57+
if (request.customerDetails() == null) {
58+
errors.add("Customer details are required");
59+
}
60+
61+
if (request.customerDetails() != null) {
62+
if (request.customerDetails().getFirstName() == null || request.customerDetails().getFirstName().isBlank()) {
63+
errors.add("Customer first name is required");
64+
}
65+
if (request.customerDetails().getLastName() == null || request.customerDetails().getLastName().isBlank()) {
66+
errors.add("Customer last name is required");
67+
}
68+
if (request.customerDetails().getEmail() == null || request.customerDetails().getEmail().isBlank()) {
69+
errors.add("Customer email is required");
70+
}
71+
}
72+
5773
if (!errors.isEmpty()) {
5874
throw new OrderValidationException("Order validation failed: " + String.join(", ", errors));
5975
}

src/main/resources/db/changelog/db.changelog-master.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@
1212
<include file="db/changelog/migrations/241127144629-changelog.xml"/>
1313
<include file="db/changelog/migrations/241201215506-changelog.xml"/>
1414
<include file="db/changelog/migrations/241223151914-changelog.xml"/>
15-
<include file="db/changelog/migrations/241228215224-changelog.xml"/>
16-
<!-- <include file="db/changelog/migrations/241228214702-additional-fields-to-order-info-changelog.xml"/>-->
15+
<include file="db/changelog/migrations/241228232009-changelog.xml"/>
1716
</databaseChangeLog>

src/main/resources/db/changelog/migrations/241228215224-changelog.xml renamed to src/main/resources/db/changelog/migrations/241228232009-changelog.xml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,36 @@
33
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
55
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
6-
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.27.xsd"
6+
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.30.xsd"
77
objectQuotingStrategy="QUOTE_ONLY_RESERVED_WORDS">
8-
<changeSet id="1735419144093-1" author="gkhaavik">
8+
<changeSet id="1735424409802-2" author="gkhaavik">
99
<addColumn tableName="order_shipping_info">
1010
<column name="customer_email" type="VARCHAR(255)"/>
1111
<column name="customer_first_name" type="VARCHAR(255)"/>
1212
<column name="customer_last_name" type="VARCHAR(255)"/>
1313
<column name="customer_phone" type="VARCHAR(255)"/>
1414
</addColumn>
1515
</changeSet>
16-
<changeSet id="1735419144093-2" author="gkhaavik">
16+
<changeSet id="1735424409802-3" author="gkhaavik">
1717
<addNotNullConstraint columnDataType="VARCHAR(255)" columnName="customer_email" tableName="order_shipping_info"
1818
defaultNullValue="NIL"/>
1919
</changeSet>
20-
<changeSet id="1735419144093-4" author="gkhaavik">
20+
<changeSet id="1735424409802-5" author="gkhaavik">
2121
<addNotNullConstraint columnDataType="VARCHAR(255)" columnName="customer_first_name"
2222
tableName="order_shipping_info"
2323
defaultNullValue="NIL"/>
2424
</changeSet>
25-
<changeSet id="1735419144093-6" author="gkhaavik">
25+
<changeSet id="1735424409802-7" author="gkhaavik">
2626
<addNotNullConstraint columnDataType="VARCHAR(255)" columnName="customer_last_name"
2727
tableName="order_shipping_info"
2828
defaultNullValue="NIL"/>
2929
</changeSet>
30-
<changeSet id="1735419144093-8" author="gkhaavik">
30+
<changeSet id="1735424409802-9" author="gkhaavik">
3131
<addNotNullConstraint columnDataType="VARCHAR(255)" columnName="customer_phone" tableName="order_shipping_info"
3232
defaultNullValue="NIL"/>
3333
</changeSet>
34+
<changeSet id="1735424409802-1" author="gkhaavik">
35+
<dropNotNullConstraint columnDataType="bigint" columnName="user_id" tableName="orders"/>
36+
</changeSet>
3437

3538
</databaseChangeLog>

0 commit comments

Comments
 (0)