Skip to content

Commit 8cf383e

Browse files
committed
Document the code of notification and shipping microservices
1 parent 59adf7b commit 8cf383e

File tree

7 files changed

+113
-1
lines changed

7 files changed

+113
-1
lines changed

notification-service/src/main/java/com/example/notification_service/config/CustomerEventConsumerConfig.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313

1414
import java.util.function.Consumer;
1515

16+
/**
17+
* The Notification-Microservice gonna consume CustomerEvents
18+
* so that extract the infos it need to send notifications
19+
* It will receive the events wrapped into Messages
20+
* will convert them into custom Records
21+
* consume the Records messages, then acknowledge
22+
*/
23+
24+
1625
@Configuration
1726
@RequiredArgsConstructor
1827
@Slf4j

notification-service/src/main/java/com/example/notification_service/config/OrderEventConsumerConfig.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414

1515
import java.util.function.Consumer;
1616

17+
/**
18+
* The Notification-Microservice gonna consume OrderEvents
19+
* It will receive the events wrapped into Messages
20+
* will convert them into custom Records
21+
* consume the Records messages, then acknowledge
22+
*/
23+
24+
1725
@Configuration
1826
@RequiredArgsConstructor
1927
@Slf4j

notification-service/src/main/java/com/example/notification_service/consumer/OrderEventConsumerImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
import org.springframework.stereotype.Service;
99
import reactor.core.publisher.Mono;
1010

11+
/**
12+
* Based on the order-event type, the notification-microservice gonna consume the events
13+
*/
14+
1115
@Service
1216
@RequiredArgsConstructor
1317
@Slf4j

notification-service/src/main/java/com/example/notification_service/service/CustomerService.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,26 @@
1313

1414
import java.util.function.Predicate;
1515

16+
/**
17+
* Since the Customer-Microservice only implements POST requests regarding customer states
18+
* then the customer-service here is responsible for saving the newlyCreatedCustomers into the DB
19+
* it only save the customerId, username and email
20+
*/
21+
22+
1623
@Service
1724
@RequiredArgsConstructor
1825
@Slf4j
1926
public class CustomerService {
2027
private final CustomerRepo repo;
2128

29+
/**
30+
* Received the CreatedCustomerEvent
31+
* make sure the customer doesn't already exist
32+
* convert the event into an entity
33+
* save the entity.
34+
*/
35+
2236
@Transactional
2337
public Mono<Void> saveCustomer(CustomerEvent.Created event) {
2438
return repo.existsByCustomerId(event.customerId())

notification-service/src/main/java/com/example/notification_service/service/NotificationService.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
import java.util.function.Function;
2121
import java.util.function.Predicate;
2222

23+
/**
24+
* The notification-service is responsible for sending notifications
25+
* as soon as an order is either created, cancelled, or completed
26+
* the notification service will send messages
27+
*/
28+
29+
2330
@Service
2431
@RequiredArgsConstructor
2532
@Slf4j
@@ -34,6 +41,11 @@ public class NotificationService {
3441
private static final String CANCELLED_SUBJECT= "Order Cancelled Notification";
3542
private static final String COMPLETED_SUBJECT= "Order Completed Notification";
3643

44+
/**
45+
* Here the generic message
46+
* the subject, username, orderId, and the orderStatus gonna change based on the situation
47+
*/
48+
3749
private static final String GENERIC_MESSAGE = """
3850
==============================
3951
%s
@@ -74,6 +86,11 @@ private Mono<Boolean> sendEmail(String recipient, String subject, String content
7486
}
7587

7688

89+
/**
90+
* make sure the order with event orderId hasn't been processed before,
91+
* if so raise duplicate event exception
92+
* otherwise save the order while setting the status to CREATED
93+
*/
7794
private Function<OrderEvent.Created,Mono<OrderEvent.Created>> validateCreatedOrderEvent(){
7895
return event -> repo.existsByOrderId(event.orderId())
7996
.filter(Predicate.not(b->b))
@@ -82,6 +99,11 @@ private Function<OrderEvent.Created,Mono<OrderEvent.Created>> validateCreatedOrd
8299
.thenReturn(event);
83100
}
84101

102+
103+
/**
104+
* the order has to be of CREATED status before considering sending CANCELLED notification
105+
* set the status into CANCELLED and then save the order
106+
*/
85107
private Function<OrderEvent.Cancelled,Mono<OrderEvent.Cancelled>> validateCancelledOrderEvent(){
86108
return event -> repo.findByOrderIdAndStatus(event.orderId(),OrderStatus.CREATED)
87109
.flatMap(x -> repo.save(x.setStatus(OrderStatus.CANCELED)))
@@ -94,6 +116,11 @@ private Function<OrderEvent.Cancelled,Mono<OrderEvent.Cancelled>> validateCancel
94116

95117
}
96118

119+
120+
/**
121+
* the order has to be of CREATED status before considering sending COMPLETED notification
122+
* set the status into COMPLETED and then save the order
123+
*/
97124
private Function<OrderEvent.Completed,Mono<OrderEvent.Completed>> validateCompletedOrderEvent(){
98125
return event -> repo.findByOrderIdAndStatus(event.orderId(),OrderStatus.CREATED)
99126
.flatMap(x -> repo.save(x.setStatus(OrderStatus.COMPLETED)))
@@ -106,7 +133,13 @@ private Function<OrderEvent.Completed,Mono<OrderEvent.Completed>> validateComple
106133
}
107134

108135

109-
136+
/**
137+
* Receive the event
138+
* make sure it hasn't been processed before
139+
* then send the email to customer
140+
* if the operation failed, raise emailFailure exception
141+
* do nothing if either email failure or duplicate event exception were raised
142+
*/
110143
@Transactional
111144
public Mono<Void> sendEmailCreated(OrderEvent.Created event) {
112145
return validateCreatedOrderEvent().apply(event)

shipping-service/src/main/java/com/example/shipping_service/processor/OrderEventProcessorImpl.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ public class OrderEventProcessorImpl implements OrderEventProcessor {
2020

2121
private final ShippingService service;
2222

23+
/**
24+
* Receive the CreatedOrderEvent
25+
* Convert it into shipping Request dto
26+
* the shipping-service will return shipping response dto
27+
* convert the latter into ReadyShippingEvent
28+
* if duplicate event is raised, do nothing
29+
* return DeclinedShippingEvent in case the quantity limit exceeded
30+
*/
31+
2332
@Override
2433
public Mono<ShippingEvent> handle(OrderEvent.Created event) {
2534
return service.planShipping().apply(((ShippingDTO.Request) Mapper.toRequest().apply(event)))
@@ -29,13 +38,21 @@ public Mono<ShippingEvent> handle(OrderEvent.Created event) {
2938
.onErrorResume(QuantityLimitException.class, ex -> Mapper.toDeclined().apply(ex, event));
3039
}
3140

41+
42+
/**
43+
* If the order is completed, then we have to schedule the shipping
44+
*/
3245
@Override
3346
public Mono<ShippingEvent> handle(OrderEvent.Completed event) {
3447
return service.scheduleShipping().apply(event.orderId())
3548
.cast(ShippingDTO.Response.class)
3649
.map(Mapper.toScheduled());
3750
}
3851

52+
53+
/**
54+
* cancel shipping if order is cancelled
55+
*/
3956
@Override
4057
public Mono<ShippingEvent> handle(OrderEvent.Cancelled event) {
4158
return service.cancelShipping().apply(event.orderId())

shipping-service/src/main/java/com/example/shipping_service/service/ShippingService.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@
2525
public class ShippingService {
2626
private final ShippingRepo repo;
2727

28+
/**
29+
* receive the request
30+
* make sure the order hasn't bee processed before
31+
* of so raise duplicate event exception
32+
* also make sure the request quantity doesn't exceed the quantity limit
33+
* if s raise quantityLimit exception
34+
* if all went well, convert the request into entity
35+
* set the entity status to PENDING
36+
* save into the DB
37+
* convert the saved entity into DTO
38+
*/
2839

2940
public Function<ShippingDTO.Request, Mono<ShippingDTO>> planShipping() {
3041
return request -> {
@@ -41,12 +52,28 @@ public Function<ShippingDTO.Request, Mono<ShippingDTO>> planShipping() {
4152
};
4253
}
4354

55+
56+
/**
57+
* Receive the CancelledOrderEvent orderId
58+
* to cancel the shipping, it has to have PENDING status before
59+
* find the entity, set its status to CANCELLED
60+
* then save into the DB
61+
* finally convert the saved entity into dto
62+
*/
4463
public Function<UUID, Mono<ShippingDTO>> cancelShipping() {
4564
return orderId -> repo.findByOrderIdAndStatus(orderId,ShippingStatus.PENDING)
4665
.flatMap(entity -> repo.save(entity.setStatus(ShippingStatus.CANCELLED)))
4766
.map(Mapper.toDto());
4867
}
4968

69+
70+
/**
71+
* Receive the CancelledOrderEvent orderId
72+
* to schedule the shipping, it has to have PENDING status before
73+
* find the entity, set its status to SCHEDULED and set delivery date
74+
* then save into the DB
75+
* finally convert the saved entity into dto
76+
*/
5077
public Function<UUID, Mono<ShippingDTO>> scheduleShipping() {
5178
return orderId -> repo.findByOrderIdAndStatus(orderId,ShippingStatus.PENDING)
5279
.flatMap(e -> repo.save(e.setStatus(ShippingStatus.SCHEDULED).setDeliveryDate(Instant.now().plus(Duration.ofDays(3)))))

0 commit comments

Comments
 (0)