From 95ec54afa80037140b858941392d8d014a9867e2 Mon Sep 17 00:00:00 2001 From: amir-awad Date: Mon, 12 May 2025 14:47:00 +0300 Subject: [PATCH 01/14] chore: utilize strategy desgin pattern --- .../controllers/CourierController.java | 2 +- .../controllers/DeliveryTaskController.java | 33 +++------- .../courier/models/ConfirmationType.java | 7 +++ .../podzilla/courier/models/DeliveryTask.java | 3 + .../{ => courier}/CourierService.java | 2 +- .../DeliveryTaskService.java | 63 +++++++++++-------- .../DeliveryConfirmationStrategy.java | 9 +++ .../OtpConfirmationStrategy.java | 45 +++++++++++++ .../QrCodeConfirmationStrategy.java | 48 ++++++++++++++ .../SignatureConfirmationStrategy.java | 47 ++++++++++++++ .../courier/CourierApplicationTests.java | 2 +- 11 files changed, 208 insertions(+), 53 deletions(-) create mode 100644 src/main/java/com/podzilla/courier/models/ConfirmationType.java rename src/main/java/com/podzilla/courier/services/{ => courier}/CourierService.java (98%) rename src/main/java/com/podzilla/courier/services/{ => delivery_task}/DeliveryTaskService.java (84%) create mode 100644 src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/DeliveryConfirmationStrategy.java create mode 100644 src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/OtpConfirmationStrategy.java create mode 100644 src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/QrCodeConfirmationStrategy.java create mode 100644 src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/SignatureConfirmationStrategy.java diff --git a/src/main/java/com/podzilla/courier/controllers/CourierController.java b/src/main/java/com/podzilla/courier/controllers/CourierController.java index 5ebfa56..5a15093 100644 --- a/src/main/java/com/podzilla/courier/controllers/CourierController.java +++ b/src/main/java/com/podzilla/courier/controllers/CourierController.java @@ -3,7 +3,7 @@ import com.podzilla.courier.dtos.couriers.CourierResponseDto; import com.podzilla.courier.dtos.couriers.CreateCourierRequestDto; import com.podzilla.courier.dtos.couriers.UpdateCourierRequestDto; -import com.podzilla.courier.services.CourierService; +import com.podzilla.courier.services.courier.CourierService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.responses.ApiResponse; diff --git a/src/main/java/com/podzilla/courier/controllers/DeliveryTaskController.java b/src/main/java/com/podzilla/courier/controllers/DeliveryTaskController.java index 27997b8..8cd9376 100644 --- a/src/main/java/com/podzilla/courier/controllers/DeliveryTaskController.java +++ b/src/main/java/com/podzilla/courier/controllers/DeliveryTaskController.java @@ -9,7 +9,7 @@ import com.podzilla.courier.dtos.delivery_tasks.DeliveryTaskResponseDto; import com.podzilla.courier.dtos.delivery_tasks.UpdateDeliveryStatusRequestDto; import com.podzilla.courier.models.DeliveryStatus; -import com.podzilla.courier.services.DeliveryTaskService; +import com.podzilla.courier.services.delivery_task.DeliveryTaskService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.parameters.RequestBody; @@ -174,30 +174,17 @@ public ResponseEntity deleteDeliveryTask( return ResponseEntity.ok(deliveryTaskService.deleteDeliveryTask(id)); } - @PutMapping("/{id}/otp") - @Operation(summary = "Update OTP", description = "Updates the One-Time Password for a delivery task") - @ApiResponse(responseCode = "200", description = "OTP updated successfully") - @ApiResponse(responseCode = "404", description = "Task not found") - public ResponseEntity updateOtp( - @Parameter(description = "ID of the delivery task") - @PathVariable final String id, - @RequestBody(description = "New OTP value") - @org.springframework.web.bind.annotation.RequestBody final String otp) { - return deliveryTaskService.updateOtp(id, otp) - .map(ResponseEntity::ok) - .orElse(ResponseEntity.notFound().build()); - } - - @PutMapping("/{id}/otp-confirmation") - @Operation(summary = "Confirm OTP", description = "Validates the One-Time Password for a delivery task") - @ApiResponse(responseCode = "200", description = "OTP confirmed successfully") - @ApiResponse(responseCode = "404", description = "Invalid OTP or task not found") - public ResponseEntity confirmOTP( + @PutMapping("/{id}/confirmation") + @Operation(summary = "Confirm delivery", description = "Validates the delivery confirmation input (e.g., OTP, QR code, or signature)") + @ApiResponse(responseCode = "200", description = "Delivery confirmed successfully") + @ApiResponse(responseCode = "404", description = "Invalid confirmation input or task not found") + public ResponseEntity confirmDelivery( @Parameter(description = "ID of the delivery task") @PathVariable final String id, - @RequestBody(description = "OTP to validate") - @org.springframework.web.bind.annotation.RequestBody final String otp) { - return deliveryTaskService.confirmOTP(id, otp) + @RequestBody(description = "Confirmation input (e.g., OTP, QR code, or signature)") + @org.springframework.web.bind.annotation.RequestBody final String confirmationInput) { + LOGGER.info("Received request to confirm delivery for task ID: {}", id); + return deliveryTaskService.confirmDelivery(id, confirmationInput) .map(ResponseEntity::ok) .orElse(ResponseEntity.notFound().build()); } diff --git a/src/main/java/com/podzilla/courier/models/ConfirmationType.java b/src/main/java/com/podzilla/courier/models/ConfirmationType.java new file mode 100644 index 0000000..252f19c --- /dev/null +++ b/src/main/java/com/podzilla/courier/models/ConfirmationType.java @@ -0,0 +1,7 @@ +package com.podzilla.courier.models; + +public enum ConfirmationType { + OTP, + QR_CODE, + SIGNATURE +} \ No newline at end of file diff --git a/src/main/java/com/podzilla/courier/models/DeliveryTask.java b/src/main/java/com/podzilla/courier/models/DeliveryTask.java index 688d402..ba6063b 100644 --- a/src/main/java/com/podzilla/courier/models/DeliveryTask.java +++ b/src/main/java/com/podzilla/courier/models/DeliveryTask.java @@ -20,11 +20,14 @@ public class DeliveryTask { private Double courierLatitude; private Double courierLongitude; private String otp; + private String qrCode; + private String signature; private String cancellationReason; private Double courierRating; private LocalDateTime ratingTimestamp; private LocalDateTime createdAt; private LocalDateTime updatedAt; + private ConfirmationType confirmationType; public DeliveryTask() { this.status = DeliveryStatus.ASSIGNED; diff --git a/src/main/java/com/podzilla/courier/services/CourierService.java b/src/main/java/com/podzilla/courier/services/courier/CourierService.java similarity index 98% rename from src/main/java/com/podzilla/courier/services/CourierService.java rename to src/main/java/com/podzilla/courier/services/courier/CourierService.java index e856005..b78abd8 100644 --- a/src/main/java/com/podzilla/courier/services/CourierService.java +++ b/src/main/java/com/podzilla/courier/services/courier/CourierService.java @@ -1,4 +1,4 @@ -package com.podzilla.courier.services; +package com.podzilla.courier.services.courier; import com.podzilla.courier.dtos.couriers.*; import com.podzilla.courier.mappers.CourierMapper; diff --git a/src/main/java/com/podzilla/courier/services/DeliveryTaskService.java b/src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java similarity index 84% rename from src/main/java/com/podzilla/courier/services/DeliveryTaskService.java rename to src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java index 847cc0d..1807fb3 100644 --- a/src/main/java/com/podzilla/courier/services/DeliveryTaskService.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java @@ -1,43 +1,45 @@ -package com.podzilla.courier.services; +package com.podzilla.courier.services.delivery_task; -import com.podzilla.courier.config.RabbitMQConfig; import com.podzilla.courier.dtos.delivery_tasks.CancelDeliveryTaskResponseDto; import com.podzilla.courier.dtos.delivery_tasks.CreateDeliveryTaskRequestDto; import com.podzilla.courier.dtos.delivery_tasks.DeliveryTaskResponseDto; import com.podzilla.courier.dtos.delivery_tasks.SubmitCourierRatingResponseDto; -import com.podzilla.courier.dtos.events.OrderDeliveredEvent; import com.podzilla.courier.dtos.events.OrderFailedEvent; import com.podzilla.courier.dtos.events.OrderShippedEvent; import com.podzilla.courier.events.EventPublisher; import com.podzilla.courier.mappers.DeliveryTaskMapper; +import com.podzilla.courier.models.ConfirmationType; import com.podzilla.courier.models.DeliveryStatus; import com.podzilla.courier.models.DeliveryTask; import com.podzilla.courier.repositories.delivery_task.IDeliveryTaskRepository; +import com.podzilla.courier.services.delivery_task.confirmation_strategy.DeliveryConfirmationStrategy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.data.util.Pair; import org.springframework.stereotype.Service; -import java.math.BigDecimal; import java.time.Instant; import java.time.LocalDateTime; import java.util.List; +import java.util.Map; import java.util.Optional; -import java.util.UUID; import java.util.stream.Collectors; +import java.util.stream.IntStream; @Service public class DeliveryTaskService { private final IDeliveryTaskRepository deliveryTaskRepository; private final EventPublisher eventPublisher; + private final Map confirmationStrategies; private static final Logger logger = LoggerFactory.getLogger(DeliveryTaskService.class); - public DeliveryTaskService(IDeliveryTaskRepository deliveryTaskRepository, EventPublisher eventPublisher) { + public DeliveryTaskService(IDeliveryTaskRepository deliveryTaskRepository, EventPublisher eventPublisher, + Map confirmationStrategies) { this.deliveryTaskRepository = deliveryTaskRepository; this.eventPublisher = eventPublisher; + this.confirmationStrategies = confirmationStrategies; } public DeliveryTaskResponseDto createDeliveryTask(CreateDeliveryTaskRequestDto deliveryTaskRequestDto) { @@ -111,6 +113,16 @@ public Optional updateDeliveryTaskStatus(String id, Del if (status == DeliveryStatus.OUT_FOR_DELIVERY) { OrderShippedEvent event = new OrderShippedEvent(task.getOrderId(), task.getCourierId(), Instant.now()); eventPublisher.publishOrderShipped(event); + + if (task.getConfirmationType() == ConfirmationType.OTP) { + String otp = IntStream.range(0, 4) + .mapToObj(i -> String.valueOf(task.getId().charAt(i))) + .collect(Collectors.joining()); + task.setOtp(otp); + } else if (task.getConfirmationType() == ConfirmationType.QR_CODE) { + String qrContent = "QR-" + task.getId(); + task.setQrCode(qrContent); + } } return Optional.of(DeliveryTaskMapper.toCreateResponseDto(updatedDeliveryTask.get())); } @@ -192,29 +204,26 @@ public Optional updateOtp(String id, String otp) { return Optional.of("Updated OTP"); } - public Optional confirmOTP(String id, String otp) { - logger.info("Confirming otp for delivery task with ID: {}", id); + public Optional confirmDelivery(String id, String confirmationInput) { + logger.info("Confirming delivery for task ID: {}", id); DeliveryTask task = deliveryTaskRepository.findById(id).orElse(null); - if (task == null) + if (task == null) { + logger.warn("Delivery task not found with ID: {} for customer delivery confirmation", id); return Optional.empty(); - String message = "Wrong OTP"; - if (task.getOtp().equals(otp)) { - task.setStatus(DeliveryStatus.DELIVERED); - message = "OTP confirmed"; - logger.debug("OTP confirmed for delivery task ID: {}", id); - // publish order.delivered event - OrderDeliveredEvent event = new OrderDeliveredEvent( - task.getOrderId(), - task.getCourierId(), - Instant.now(), - task.getCourierRating() != null ? BigDecimal.valueOf(task.getCourierRating()) : null - ); - eventPublisher.publishOrderDelivered(event); - } else { - logger.debug("OTP not confirmed for delivery task ID: {}", id); } - deliveryTaskRepository.save(task); - return Optional.of(message); + + ConfirmationType confirmationType = task.getConfirmationType(); + DeliveryConfirmationStrategy strategy = confirmationStrategies.get(confirmationType); + if (strategy == null) { + logger.error("No confirmation strategy found for type: {}", confirmationType); + return Optional.of("Invalid confirmation type"); + } + + Optional result = strategy.confirmDelivery(task, confirmationInput); + if (result.isPresent() && result.get().contains("confirmed")) { + deliveryTaskRepository.save(task); + } + return result; } public SubmitCourierRatingResponseDto submitCourierRating(String id, Double rating) { diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/DeliveryConfirmationStrategy.java b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/DeliveryConfirmationStrategy.java new file mode 100644 index 0000000..5ca010d --- /dev/null +++ b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/DeliveryConfirmationStrategy.java @@ -0,0 +1,9 @@ +package com.podzilla.courier.services.delivery_task.confirmation_strategy; + +import com.podzilla.courier.models.DeliveryTask; + +import java.util.Optional; + +public interface DeliveryConfirmationStrategy { + Optional confirmDelivery(DeliveryTask task, String confirmationInput); +} \ No newline at end of file diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/OtpConfirmationStrategy.java b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/OtpConfirmationStrategy.java new file mode 100644 index 0000000..8588c22 --- /dev/null +++ b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/OtpConfirmationStrategy.java @@ -0,0 +1,45 @@ +package com.podzilla.courier.services.delivery_task.confirmation_strategy; + +import com.podzilla.courier.dtos.events.OrderDeliveredEvent; +import com.podzilla.courier.events.EventPublisher; +import com.podzilla.courier.models.DeliveryStatus; +import com.podzilla.courier.models.DeliveryTask; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import java.math.BigDecimal; +import java.time.Instant; +import java.util.Optional; + +@Component +public class OtpConfirmationStrategy implements DeliveryConfirmationStrategy { + private static final Logger logger = LoggerFactory.getLogger(OtpConfirmationStrategy.class); + private final EventPublisher eventPublisher; + + public OtpConfirmationStrategy(EventPublisher eventPublisher) { + this.eventPublisher = eventPublisher; + } + + @Override + public Optional confirmDelivery(DeliveryTask task, String confirmationInput) { + logger.info("Confirming delivery with OTP for task ID: {}", task.getId()); + if (task.getOtp() == null || !task.getOtp().equals(confirmationInput)) { + logger.debug("Invalid OTP for task ID: {}", task.getId()); + return Optional.of("Wrong OTP"); + } + + task.setStatus(DeliveryStatus.DELIVERED); + logger.debug("OTP confirmed for task ID: {}", task.getId()); + + OrderDeliveredEvent event = new OrderDeliveredEvent( + task.getOrderId(), + task.getCourierId(), + Instant.now(), + task.getCourierRating() != null ? BigDecimal.valueOf(task.getCourierRating()) : null + ); + eventPublisher.publishOrderDelivered(event); + + return Optional.of("OTP confirmed"); + } +} \ No newline at end of file diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/QrCodeConfirmationStrategy.java b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/QrCodeConfirmationStrategy.java new file mode 100644 index 0000000..4e85b5a --- /dev/null +++ b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/QrCodeConfirmationStrategy.java @@ -0,0 +1,48 @@ +package com.podzilla.courier.services.delivery_task.confirmation_strategy; + +import com.podzilla.courier.dtos.events.OrderDeliveredEvent; +import com.podzilla.courier.events.EventPublisher; +import com.podzilla.courier.models.DeliveryStatus; +import com.podzilla.courier.models.DeliveryTask; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import java.math.BigDecimal; +import java.time.Instant; +import java.util.Optional; + +@Component +public class QrCodeConfirmationStrategy implements DeliveryConfirmationStrategy { + private static final Logger logger = LoggerFactory.getLogger(QrCodeConfirmationStrategy.class); + private final EventPublisher eventPublisher; + + public QrCodeConfirmationStrategy(EventPublisher eventPublisher) { + this.eventPublisher = eventPublisher; + } + + @Override + public Optional confirmDelivery(DeliveryTask task, String confirmationInput) { + logger.info("Confirming delivery with QR code for task ID: {}", task.getId()); + // assume QR code is valid if it matches a predefined value or logic + String expectedQrCode = task.getQrCode(); + if (expectedQrCode == null || !expectedQrCode.equals(confirmationInput)) { + logger.debug("Invalid QR code for task ID: {}", task.getId()); + return Optional.of("Invalid QR code"); + } + + task.setStatus(DeliveryStatus.DELIVERED); + logger.debug("QR code confirmed for task ID: {}", task.getId()); + + // publish order.delivered event + OrderDeliveredEvent event = new OrderDeliveredEvent( + task.getOrderId(), + task.getCourierId(), + Instant.now(), + task.getCourierRating() != null ? BigDecimal.valueOf(task.getCourierRating()) : null + ); + eventPublisher.publishOrderDelivered(event); + + return Optional.of("QR code confirmed"); + } +} \ No newline at end of file diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/SignatureConfirmationStrategy.java b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/SignatureConfirmationStrategy.java new file mode 100644 index 0000000..c248908 --- /dev/null +++ b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/SignatureConfirmationStrategy.java @@ -0,0 +1,47 @@ +package com.podzilla.courier.services.delivery_task.confirmation_strategy; + +import com.podzilla.courier.dtos.events.OrderDeliveredEvent; +import com.podzilla.courier.events.EventPublisher; +import com.podzilla.courier.models.DeliveryStatus; +import com.podzilla.courier.models.DeliveryTask; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import java.math.BigDecimal; +import java.time.Instant; +import java.util.Optional; + +@Component +public class SignatureConfirmationStrategy implements DeliveryConfirmationStrategy { + private static final Logger logger = LoggerFactory.getLogger(SignatureConfirmationStrategy.class); + private final EventPublisher eventPublisher; + + public SignatureConfirmationStrategy(EventPublisher eventPublisher) { + this.eventPublisher = eventPublisher; + } + + @Override + public Optional confirmDelivery(DeliveryTask task, String confirmationInput) { + logger.info("Confirming delivery with signature for task ID: {}", task.getId()); + // assume signature is valid if input is non-empty + if (confirmationInput == null || confirmationInput.isEmpty() || !task.getSignature().equals(confirmationInput)) { + logger.debug("Invalid signature for task ID: {}", task.getId()); + return Optional.of("Invalid signature"); + } + + task.setStatus(DeliveryStatus.DELIVERED); + logger.debug("Signature confirmed for task ID: {}", task.getId()); + + // publish order.delivered event + OrderDeliveredEvent event = new OrderDeliveredEvent( + task.getOrderId(), + task.getCourierId(), + Instant.now(), + task.getCourierRating() != null ? BigDecimal.valueOf(task.getCourierRating()) : null + ); + eventPublisher.publishOrderDelivered(event); + + return Optional.of("Signature confirmed"); + } +} \ No newline at end of file diff --git a/src/test/java/com/podzilla/courier/CourierApplicationTests.java b/src/test/java/com/podzilla/courier/CourierApplicationTests.java index 500e026..e7df220 100644 --- a/src/test/java/com/podzilla/courier/CourierApplicationTests.java +++ b/src/test/java/com/podzilla/courier/CourierApplicationTests.java @@ -6,7 +6,7 @@ import com.podzilla.courier.dtos.couriers.UpdateCourierRequestDto; import com.podzilla.courier.dtos.couriers.CourierResponseDto; import com.podzilla.courier.models.CourierStatus; -import com.podzilla.courier.services.CourierService; +import com.podzilla.courier.services.courier.CourierService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; From 05990c6816da9f04bc62aa149ebedd98eef217d0 Mon Sep 17 00:00:00 2001 From: amir-awad Date: Mon, 12 May 2025 21:08:08 +0300 Subject: [PATCH 02/14] fix: migrated to the mq shared lib --- pom.xml | 11 ++++ .../dtos/events/OrderDeliveredEvent.java | 7 --- .../courier/dtos/events/OrderFailedEvent.java | 6 -- .../dtos/events/OrderShippedEvent.java | 6 -- .../courier/events/EventPublisher.java | 11 ---- .../events/RabbitMQEventPublisher.java | 59 ------------------- .../delivery_task/DeliveryTaskService.java | 19 +++--- .../OtpConfirmationStrategy.java | 11 ++-- .../QrCodeConfirmationStrategy.java | 8 +-- .../SignatureConfirmationStrategy.java | 8 +-- 10 files changed, 33 insertions(+), 113 deletions(-) delete mode 100644 src/main/java/com/podzilla/courier/dtos/events/OrderDeliveredEvent.java delete mode 100644 src/main/java/com/podzilla/courier/dtos/events/OrderFailedEvent.java delete mode 100644 src/main/java/com/podzilla/courier/dtos/events/OrderShippedEvent.java delete mode 100644 src/main/java/com/podzilla/courier/events/EventPublisher.java delete mode 100644 src/main/java/com/podzilla/courier/events/RabbitMQEventPublisher.java diff --git a/pom.xml b/pom.xml index 937a7fd..f857355 100644 --- a/pom.xml +++ b/pom.xml @@ -30,6 +30,12 @@ 21 + + + jitpack.io + [https://jitpack.io](https://jitpack.io) + + org.springframework.boot @@ -79,6 +85,11 @@ springdoc-openapi-starter-webmvc-ui 2.8.3 + + com.github.Podzilla + podzilla-utils-lib + v1.1.6 + diff --git a/src/main/java/com/podzilla/courier/dtos/events/OrderDeliveredEvent.java b/src/main/java/com/podzilla/courier/dtos/events/OrderDeliveredEvent.java deleted file mode 100644 index c11abc8..0000000 --- a/src/main/java/com/podzilla/courier/dtos/events/OrderDeliveredEvent.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.podzilla.courier.dtos.events; - -import java.math.BigDecimal; -import java.time.Instant; - -public record OrderDeliveredEvent(String orderId, String courierId, Instant timestamp, BigDecimal courierRating) { -} diff --git a/src/main/java/com/podzilla/courier/dtos/events/OrderFailedEvent.java b/src/main/java/com/podzilla/courier/dtos/events/OrderFailedEvent.java deleted file mode 100644 index a4bd019..0000000 --- a/src/main/java/com/podzilla/courier/dtos/events/OrderFailedEvent.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.podzilla.courier.dtos.events; - -import java.time.Instant; - -public record OrderFailedEvent(String orderId, String courierId, String reason, Instant timestamp) { -} diff --git a/src/main/java/com/podzilla/courier/dtos/events/OrderShippedEvent.java b/src/main/java/com/podzilla/courier/dtos/events/OrderShippedEvent.java deleted file mode 100644 index ee14987..0000000 --- a/src/main/java/com/podzilla/courier/dtos/events/OrderShippedEvent.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.podzilla.courier.dtos.events; - -import java.time.Instant; - -public record OrderShippedEvent(String orderId, String courierId, Instant timestamp) { -} diff --git a/src/main/java/com/podzilla/courier/events/EventPublisher.java b/src/main/java/com/podzilla/courier/events/EventPublisher.java deleted file mode 100644 index 600cb64..0000000 --- a/src/main/java/com/podzilla/courier/events/EventPublisher.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.podzilla.courier.events; - -import com.podzilla.courier.dtos.events.OrderDeliveredEvent; -import com.podzilla.courier.dtos.events.OrderFailedEvent; -import com.podzilla.courier.dtos.events.OrderShippedEvent; - -public interface EventPublisher { - void publishOrderShipped(OrderShippedEvent event); - void publishOrderDelivered(OrderDeliveredEvent event); - void publishOrderFailed(OrderFailedEvent event); -} \ No newline at end of file diff --git a/src/main/java/com/podzilla/courier/events/RabbitMQEventPublisher.java b/src/main/java/com/podzilla/courier/events/RabbitMQEventPublisher.java deleted file mode 100644 index b8f9eb7..0000000 --- a/src/main/java/com/podzilla/courier/events/RabbitMQEventPublisher.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.podzilla.courier.events; - -import com.podzilla.courier.config.RabbitMQConfig; -import com.podzilla.courier.dtos.events.OrderDeliveredEvent; -import com.podzilla.courier.dtos.events.OrderFailedEvent; -import com.podzilla.courier.dtos.events.OrderShippedEvent; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.amqp.rabbit.core.RabbitTemplate; -import org.springframework.stereotype.Service; - -@Service -public class RabbitMQEventPublisher implements EventPublisher { - private static final Logger logger = LoggerFactory.getLogger(RabbitMQEventPublisher.class); - private final RabbitTemplate rabbitTemplate; - - public RabbitMQEventPublisher(RabbitTemplate rabbitTemplate) { - this.rabbitTemplate = rabbitTemplate; - } - - @Override - public void publishOrderShipped(OrderShippedEvent event) { - try { - rabbitTemplate.convertAndSend(RabbitMQConfig.ORDER_EXCHANGE, RabbitMQConfig.ORDER_SHIPPED_KEY, event, message -> { - message.getMessageProperties().setCorrelationId(java.util.UUID.randomUUID().toString()); - return message; - }); - logger.info("Published order.shipped event for order ID: {}", event.orderId()); - } catch (Exception e) { - logger.error("Failed to publish order.shipped event for order ID: {}", event.orderId(), e); - } - } - - @Override - public void publishOrderDelivered(OrderDeliveredEvent event) { - try { - rabbitTemplate.convertAndSend(RabbitMQConfig.ORDER_EXCHANGE, RabbitMQConfig.ORDER_DELIVERED_KEY, event, message -> { - message.getMessageProperties().setCorrelationId(java.util.UUID.randomUUID().toString()); - return message; - }); - logger.info("Published order.delivered event for order ID: {}", event.orderId()); - } catch (Exception e) { - logger.error("Failed to publish order.delivered event for order ID: {}", event.orderId(), e); - } - } - - @Override - public void publishOrderFailed(OrderFailedEvent event) { - try { - rabbitTemplate.convertAndSend(RabbitMQConfig.ORDER_EXCHANGE, RabbitMQConfig.ORDER_FAILED_KEY, event, message -> { - message.getMessageProperties().setCorrelationId(java.util.UUID.randomUUID().toString()); - return message; - }); - logger.info("Published order.failed event for order ID: {}", event.orderId()); - } catch (Exception e) { - logger.error("Failed to publish order.failed event for order ID: {}", event.orderId(), e); - } - } -} \ No newline at end of file diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java b/src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java index 1807fb3..aa8cea6 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java @@ -4,21 +4,21 @@ import com.podzilla.courier.dtos.delivery_tasks.CreateDeliveryTaskRequestDto; import com.podzilla.courier.dtos.delivery_tasks.DeliveryTaskResponseDto; import com.podzilla.courier.dtos.delivery_tasks.SubmitCourierRatingResponseDto; -import com.podzilla.courier.dtos.events.OrderFailedEvent; -import com.podzilla.courier.dtos.events.OrderShippedEvent; -import com.podzilla.courier.events.EventPublisher; import com.podzilla.courier.mappers.DeliveryTaskMapper; import com.podzilla.courier.models.ConfirmationType; import com.podzilla.courier.models.DeliveryStatus; import com.podzilla.courier.models.DeliveryTask; import com.podzilla.courier.repositories.delivery_task.IDeliveryTaskRepository; import com.podzilla.courier.services.delivery_task.confirmation_strategy.DeliveryConfirmationStrategy; +import com.podzilla.mq.EventPublisher; +import com.podzilla.mq.EventsConstants; +import com.podzilla.mq.events.OrderCancelledEvent; +import com.podzilla.mq.events.OrderOutForDeliveryEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.util.Pair; import org.springframework.stereotype.Service; -import java.time.Instant; import java.time.LocalDateTime; import java.util.List; import java.util.Map; @@ -111,8 +111,8 @@ public Optional updateDeliveryTaskStatus(String id, Del logger.debug("Delivery task ID: {} updated to status: {}", id, status); // publish order.shipped event if status is OUT_FOR_DELIVERY if (status == DeliveryStatus.OUT_FOR_DELIVERY) { - OrderShippedEvent event = new OrderShippedEvent(task.getOrderId(), task.getCourierId(), Instant.now()); - eventPublisher.publishOrderShipped(event); + OrderOutForDeliveryEvent event = new OrderOutForDeliveryEvent(task.getOrderId(), task.getCourierId()); + eventPublisher.publishEvent(EventsConstants.ORDER_OUT_FOR_DELIVERY, event); if (task.getConfirmationType() == ConfirmationType.OTP) { String otp = IntStream.range(0, 4) @@ -168,13 +168,12 @@ public CancelDeliveryTaskResponseDto cancelDeliveryTask(String id, String cancel deliveryTaskRepository.save(deliveryTaskToCancel); logger.debug("Delivery task cancelled for delivery task ID: {}", id); // publish order.failed event - OrderFailedEvent event = new OrderFailedEvent( + OrderCancelledEvent event = new OrderCancelledEvent( deliveryTaskToCancel.getOrderId(), deliveryTaskToCancel.getCourierId(), - cancellationReason, - Instant.now() + cancellationReason ); - eventPublisher.publishOrderFailed(event); + eventPublisher.publishEvent(EventsConstants.ORDER_CANCELLED, event); return DeliveryTaskMapper.toCancelResponseDto(deliveryTaskToCancel); } logger.warn("Delivery task not found with ID: {}", id); diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/OtpConfirmationStrategy.java b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/OtpConfirmationStrategy.java index 8588c22..e1f403f 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/OtpConfirmationStrategy.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/OtpConfirmationStrategy.java @@ -1,15 +1,15 @@ package com.podzilla.courier.services.delivery_task.confirmation_strategy; -import com.podzilla.courier.dtos.events.OrderDeliveredEvent; -import com.podzilla.courier.events.EventPublisher; import com.podzilla.courier.models.DeliveryStatus; import com.podzilla.courier.models.DeliveryTask; +import com.podzilla.mq.EventPublisher; +import com.podzilla.mq.EventsConstants; +import com.podzilla.mq.events.OrderDeliveredEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import java.math.BigDecimal; -import java.time.Instant; import java.util.Optional; @Component @@ -32,13 +32,12 @@ public Optional confirmDelivery(DeliveryTask task, String confirmationIn task.setStatus(DeliveryStatus.DELIVERED); logger.debug("OTP confirmed for task ID: {}", task.getId()); - OrderDeliveredEvent event = new OrderDeliveredEvent( + com.podzilla.mq.events.OrderDeliveredEvent event = new OrderDeliveredEvent( task.getOrderId(), task.getCourierId(), - Instant.now(), task.getCourierRating() != null ? BigDecimal.valueOf(task.getCourierRating()) : null ); - eventPublisher.publishOrderDelivered(event); + eventPublisher.publishEvent(EventsConstants.ORDER_DELIVERED, event); return Optional.of("OTP confirmed"); } diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/QrCodeConfirmationStrategy.java b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/QrCodeConfirmationStrategy.java index 4e85b5a..e852628 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/QrCodeConfirmationStrategy.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/QrCodeConfirmationStrategy.java @@ -1,9 +1,10 @@ package com.podzilla.courier.services.delivery_task.confirmation_strategy; -import com.podzilla.courier.dtos.events.OrderDeliveredEvent; -import com.podzilla.courier.events.EventPublisher; import com.podzilla.courier.models.DeliveryStatus; import com.podzilla.courier.models.DeliveryTask; +import com.podzilla.mq.EventPublisher; +import com.podzilla.mq.EventsConstants; +import com.podzilla.mq.events.OrderDeliveredEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @@ -38,10 +39,9 @@ public Optional confirmDelivery(DeliveryTask task, String confirmationIn OrderDeliveredEvent event = new OrderDeliveredEvent( task.getOrderId(), task.getCourierId(), - Instant.now(), task.getCourierRating() != null ? BigDecimal.valueOf(task.getCourierRating()) : null ); - eventPublisher.publishOrderDelivered(event); + eventPublisher.publishEvent(EventsConstants.ORDER_DELIVERED, event); return Optional.of("QR code confirmed"); } diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/SignatureConfirmationStrategy.java b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/SignatureConfirmationStrategy.java index c248908..961ff73 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/SignatureConfirmationStrategy.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/SignatureConfirmationStrategy.java @@ -1,9 +1,10 @@ package com.podzilla.courier.services.delivery_task.confirmation_strategy; -import com.podzilla.courier.dtos.events.OrderDeliveredEvent; -import com.podzilla.courier.events.EventPublisher; import com.podzilla.courier.models.DeliveryStatus; import com.podzilla.courier.models.DeliveryTask; +import com.podzilla.mq.EventPublisher; +import com.podzilla.mq.EventsConstants; +import com.podzilla.mq.events.OrderDeliveredEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @@ -37,10 +38,9 @@ public Optional confirmDelivery(DeliveryTask task, String confirmationIn OrderDeliveredEvent event = new OrderDeliveredEvent( task.getOrderId(), task.getCourierId(), - Instant.now(), task.getCourierRating() != null ? BigDecimal.valueOf(task.getCourierRating()) : null ); - eventPublisher.publishOrderDelivered(event); + eventPublisher.publishEvent(EventsConstants.ORDER_DELIVERED, event); return Optional.of("Signature confirmed"); } From 17f2537579523f171ea9fab24a60c44a3eb55630 Mon Sep 17 00:00:00 2001 From: omar mohammed Date: Tue, 13 May 2025 22:11:42 +0300 Subject: [PATCH 03/14] create order command design patterns --- .../OrderDeliveredEventCommand.java | 36 +++++++++++++++++++ .../designPatterns/OrderEventCommand.java | 5 +++ .../OrderFailedEventCommand.java | 35 ++++++++++++++++++ .../OrderShippedEventCommand.java | 25 +++++++++++++ 4 files changed, 101 insertions(+) create mode 100644 src/main/java/com/podzilla/courier/designPatterns/OrderDeliveredEventCommand.java create mode 100644 src/main/java/com/podzilla/courier/designPatterns/OrderEventCommand.java create mode 100644 src/main/java/com/podzilla/courier/designPatterns/OrderFailedEventCommand.java create mode 100644 src/main/java/com/podzilla/courier/designPatterns/OrderShippedEventCommand.java diff --git a/src/main/java/com/podzilla/courier/designPatterns/OrderDeliveredEventCommand.java b/src/main/java/com/podzilla/courier/designPatterns/OrderDeliveredEventCommand.java new file mode 100644 index 0000000..ffad733 --- /dev/null +++ b/src/main/java/com/podzilla/courier/designPatterns/OrderDeliveredEventCommand.java @@ -0,0 +1,36 @@ +package com.podzilla.courier.designPatterns; + +import com.podzilla.courier.dtos.events.OrderDeliveredEvent; +import com.podzilla.courier.events.EventPublisher; + +import java.math.BigDecimal; +import java.time.Instant; + +public class OrderDeliveredEventCommand implements OrderEventCommand { + + private final String orderId; + private final String courierId; + private final Instant deliveredAt; + private final BigDecimal courierRating; + private final EventPublisher eventPublisher; + + public OrderDeliveredEventCommand( + final String orderId, + final String courierId, + final Instant deliveredAt, + final BigDecimal courierRating, + final EventPublisher eventPublisher + ) { + this.orderId = orderId; + this.courierId = courierId; + this.deliveredAt = deliveredAt; + this.courierRating = courierRating; + this.eventPublisher = eventPublisher; + } + + @Override + public void execute() { + OrderDeliveredEvent event = new OrderDeliveredEvent(orderId, courierId, deliveredAt, courierRating); + eventPublisher.publishOrderDelivered(event); + } +} diff --git a/src/main/java/com/podzilla/courier/designPatterns/OrderEventCommand.java b/src/main/java/com/podzilla/courier/designPatterns/OrderEventCommand.java new file mode 100644 index 0000000..fbc09ca --- /dev/null +++ b/src/main/java/com/podzilla/courier/designPatterns/OrderEventCommand.java @@ -0,0 +1,5 @@ +package com.podzilla.courier.designPatterns; + +public interface OrderEventCommand { + void execute(); +} diff --git a/src/main/java/com/podzilla/courier/designPatterns/OrderFailedEventCommand.java b/src/main/java/com/podzilla/courier/designPatterns/OrderFailedEventCommand.java new file mode 100644 index 0000000..537da24 --- /dev/null +++ b/src/main/java/com/podzilla/courier/designPatterns/OrderFailedEventCommand.java @@ -0,0 +1,35 @@ +package com.podzilla.courier.designPatterns; + +import com.podzilla.courier.dtos.events.OrderFailedEvent; +import com.podzilla.courier.events.EventPublisher; + +import java.time.Instant; + +public class OrderFailedEventCommand implements OrderEventCommand { + + private final String orderId; + private final String courierId; + private final String cancellationReason; + private final Instant failedAt; + private final EventPublisher eventPublisher; + + public OrderFailedEventCommand( + final String orderId, + final String courierId, + final String cancellationReason, + final Instant failedAt, + final EventPublisher eventPublisher + ) { + this.orderId = orderId; + this.courierId = courierId; + this.cancellationReason = cancellationReason; + this.failedAt = failedAt; + this.eventPublisher = eventPublisher; + } + + @Override + public void execute() { + OrderFailedEvent event = new OrderFailedEvent(orderId, courierId, cancellationReason, failedAt); + eventPublisher.publishOrderFailed(event); + } +} diff --git a/src/main/java/com/podzilla/courier/designPatterns/OrderShippedEventCommand.java b/src/main/java/com/podzilla/courier/designPatterns/OrderShippedEventCommand.java new file mode 100644 index 0000000..8722d79 --- /dev/null +++ b/src/main/java/com/podzilla/courier/designPatterns/OrderShippedEventCommand.java @@ -0,0 +1,25 @@ +package com.podzilla.courier.designPatterns; + +import com.podzilla.courier.dtos.events.OrderShippedEvent; +import com.podzilla.courier.events.EventPublisher; + +import java.time.Instant; + +public class OrderShippedEventCommand implements OrderEventCommand { + + private final String orderId; + private final String courierId; + private final EventPublisher eventPublisher; + + public OrderShippedEventCommand(final String orderId, final String courierId, final EventPublisher eventPublisher) { + this.orderId = orderId; + this.courierId = courierId; + this.eventPublisher = eventPublisher; + } + + @Override + public void execute() { + OrderShippedEvent event = new OrderShippedEvent(orderId, courierId, Instant.now()); + eventPublisher.publishOrderShipped(event); + } +} From 76438b0285f5658a139afe86aeafe51fc41de972 Mon Sep 17 00:00:00 2001 From: omar mohammed Date: Fri, 16 May 2025 19:56:24 +0300 Subject: [PATCH 04/14] move files to relevant path --- pom.xml | 236 +++++++++--------- .../OrderDeliveredEventCommand.java | 36 --- .../designPatterns/OrderEventCommand.java | 5 - .../OrderFailedEventCommand.java | 35 --- .../OrderShippedEventCommand.java | 25 -- 5 files changed, 118 insertions(+), 219 deletions(-) delete mode 100644 src/main/java/com/podzilla/courier/designPatterns/OrderDeliveredEventCommand.java delete mode 100644 src/main/java/com/podzilla/courier/designPatterns/OrderEventCommand.java delete mode 100644 src/main/java/com/podzilla/courier/designPatterns/OrderFailedEventCommand.java delete mode 100644 src/main/java/com/podzilla/courier/designPatterns/OrderShippedEventCommand.java diff --git a/pom.xml b/pom.xml index f857355..a16c4fb 100644 --- a/pom.xml +++ b/pom.xml @@ -1,124 +1,124 @@ - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 3.4.5 - - - com.podzilla - courier - 0.0.1-SNAPSHOT - courier - Courier Service - - - - - - - - - - - - - - - 21 - - - - jitpack.io - [https://jitpack.io](https://jitpack.io) - - - - - org.springframework.boot - spring-boot-starter-amqp - - - org.springframework.boot - spring-boot-starter-data-mongodb - - - org.springframework.boot - spring-boot-starter-data-rest - - - org.springframework.boot - spring-boot-starter-web - + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.4.5 + + + com.podzilla + courier + 0.0.1-SNAPSHOT + courier + Courier Service + + + + + + + + + + + + + + + 21 + + + + jitpack.io + https://jitpack.io + + + + + org.springframework.boot + spring-boot-starter-amqp + + + org.springframework.boot + spring-boot-starter-data-mongodb + + + org.springframework.boot + spring-boot-starter-data-rest + + + org.springframework.boot + spring-boot-starter-web + - - org.springframework.boot - spring-boot-devtools - runtime - true - - - org.projectlombok - lombok - true - - - org.springframework.boot - spring-boot-starter-test - test - - - org.springframework.amqp - spring-rabbit-test - test - - - jakarta.validation - jakarta.validation-api - 3.0.2 - - - org.springdoc - springdoc-openapi-starter-webmvc-ui - 2.8.3 - - - com.github.Podzilla - podzilla-utils-lib - v1.1.6 - - + + org.springframework.boot + spring-boot-devtools + runtime + true + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.amqp + spring-rabbit-test + test + + + jakarta.validation + jakarta.validation-api + 3.0.2 + + + org.springdoc + springdoc-openapi-starter-webmvc-ui + 2.8.3 + + + com.github.Podzilla + podzilla-utils-lib + v1.1.6 + + - - - - org.apache.maven.plugins - maven-compiler-plugin - - - - org.projectlombok - lombok - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - org.projectlombok - lombok - - - - - - + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + org.projectlombok + lombok + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + \ No newline at end of file diff --git a/src/main/java/com/podzilla/courier/designPatterns/OrderDeliveredEventCommand.java b/src/main/java/com/podzilla/courier/designPatterns/OrderDeliveredEventCommand.java deleted file mode 100644 index ffad733..0000000 --- a/src/main/java/com/podzilla/courier/designPatterns/OrderDeliveredEventCommand.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.podzilla.courier.designPatterns; - -import com.podzilla.courier.dtos.events.OrderDeliveredEvent; -import com.podzilla.courier.events.EventPublisher; - -import java.math.BigDecimal; -import java.time.Instant; - -public class OrderDeliveredEventCommand implements OrderEventCommand { - - private final String orderId; - private final String courierId; - private final Instant deliveredAt; - private final BigDecimal courierRating; - private final EventPublisher eventPublisher; - - public OrderDeliveredEventCommand( - final String orderId, - final String courierId, - final Instant deliveredAt, - final BigDecimal courierRating, - final EventPublisher eventPublisher - ) { - this.orderId = orderId; - this.courierId = courierId; - this.deliveredAt = deliveredAt; - this.courierRating = courierRating; - this.eventPublisher = eventPublisher; - } - - @Override - public void execute() { - OrderDeliveredEvent event = new OrderDeliveredEvent(orderId, courierId, deliveredAt, courierRating); - eventPublisher.publishOrderDelivered(event); - } -} diff --git a/src/main/java/com/podzilla/courier/designPatterns/OrderEventCommand.java b/src/main/java/com/podzilla/courier/designPatterns/OrderEventCommand.java deleted file mode 100644 index fbc09ca..0000000 --- a/src/main/java/com/podzilla/courier/designPatterns/OrderEventCommand.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.podzilla.courier.designPatterns; - -public interface OrderEventCommand { - void execute(); -} diff --git a/src/main/java/com/podzilla/courier/designPatterns/OrderFailedEventCommand.java b/src/main/java/com/podzilla/courier/designPatterns/OrderFailedEventCommand.java deleted file mode 100644 index 537da24..0000000 --- a/src/main/java/com/podzilla/courier/designPatterns/OrderFailedEventCommand.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.podzilla.courier.designPatterns; - -import com.podzilla.courier.dtos.events.OrderFailedEvent; -import com.podzilla.courier.events.EventPublisher; - -import java.time.Instant; - -public class OrderFailedEventCommand implements OrderEventCommand { - - private final String orderId; - private final String courierId; - private final String cancellationReason; - private final Instant failedAt; - private final EventPublisher eventPublisher; - - public OrderFailedEventCommand( - final String orderId, - final String courierId, - final String cancellationReason, - final Instant failedAt, - final EventPublisher eventPublisher - ) { - this.orderId = orderId; - this.courierId = courierId; - this.cancellationReason = cancellationReason; - this.failedAt = failedAt; - this.eventPublisher = eventPublisher; - } - - @Override - public void execute() { - OrderFailedEvent event = new OrderFailedEvent(orderId, courierId, cancellationReason, failedAt); - eventPublisher.publishOrderFailed(event); - } -} diff --git a/src/main/java/com/podzilla/courier/designPatterns/OrderShippedEventCommand.java b/src/main/java/com/podzilla/courier/designPatterns/OrderShippedEventCommand.java deleted file mode 100644 index 8722d79..0000000 --- a/src/main/java/com/podzilla/courier/designPatterns/OrderShippedEventCommand.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.podzilla.courier.designPatterns; - -import com.podzilla.courier.dtos.events.OrderShippedEvent; -import com.podzilla.courier.events.EventPublisher; - -import java.time.Instant; - -public class OrderShippedEventCommand implements OrderEventCommand { - - private final String orderId; - private final String courierId; - private final EventPublisher eventPublisher; - - public OrderShippedEventCommand(final String orderId, final String courierId, final EventPublisher eventPublisher) { - this.orderId = orderId; - this.courierId = courierId; - this.eventPublisher = eventPublisher; - } - - @Override - public void execute() { - OrderShippedEvent event = new OrderShippedEvent(orderId, courierId, Instant.now()); - eventPublisher.publishOrderShipped(event); - } -} From 377b9b3b27510b81ce4813e57ca18826cc5b107c Mon Sep 17 00:00:00 2001 From: omar mohammed Date: Fri, 16 May 2025 19:56:48 +0300 Subject: [PATCH 05/14] add command pattern files --- .../publish_command/Command.java | 5 ++++ .../PublishOrderCancelledEventCommand.java | 23 +++++++++++++++++++ ...ublishOrderOutForDeliveryEventCommand.java | 23 +++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 src/main/java/com/podzilla/courier/services/delivery_task/publish_command/Command.java create mode 100644 src/main/java/com/podzilla/courier/services/delivery_task/publish_command/PublishOrderCancelledEventCommand.java create mode 100644 src/main/java/com/podzilla/courier/services/delivery_task/publish_command/PublishOrderOutForDeliveryEventCommand.java diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/Command.java b/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/Command.java new file mode 100644 index 0000000..86a10e3 --- /dev/null +++ b/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/Command.java @@ -0,0 +1,5 @@ +package com.podzilla.courier.services.delivery_task.publish_command; + +public interface Command { + void execute(); +} diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/PublishOrderCancelledEventCommand.java b/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/PublishOrderCancelledEventCommand.java new file mode 100644 index 0000000..7fc312c --- /dev/null +++ b/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/PublishOrderCancelledEventCommand.java @@ -0,0 +1,23 @@ +package com.podzilla.courier.services.delivery_task.publish_command; + +import com.podzilla.mq.EventPublisher; +import com.podzilla.mq.EventsConstants; +import com.podzilla.mq.events.OrderCancelledEvent; + +public class PublishOrderCancelledEventCommand implements Command { + private final EventPublisher eventPublisher; + private final OrderCancelledEvent event; + + public PublishOrderCancelledEventCommand(final EventPublisher eventPublisher, + final String orderId, + final String courierId, + final String cancellationReason) { + this.eventPublisher = eventPublisher; + this.event = new OrderCancelledEvent(orderId, courierId, cancellationReason); + } + + @Override + public void execute() { + eventPublisher.publishEvent(EventsConstants.ORDER_CANCELLED, event); + } +} diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/PublishOrderOutForDeliveryEventCommand.java b/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/PublishOrderOutForDeliveryEventCommand.java new file mode 100644 index 0000000..0c10cfe --- /dev/null +++ b/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/PublishOrderOutForDeliveryEventCommand.java @@ -0,0 +1,23 @@ +package com.podzilla.courier.services.delivery_task.publish_command; + +import com.podzilla.mq.EventPublisher; +import com.podzilla.mq.EventsConstants; +import com.podzilla.mq.events.OrderOutForDeliveryEvent; + +public class PublishOrderOutForDeliveryEventCommand implements Command { + + private final EventPublisher eventPublisher; + private final OrderOutForDeliveryEvent event; + + public PublishOrderOutForDeliveryEventCommand(final EventPublisher eventPublisher, + final String orderId, + final String courierId) { + this.eventPublisher = eventPublisher; + this.event = new OrderOutForDeliveryEvent(orderId, courierId); + } + + @Override + public void execute() { + eventPublisher.publishEvent(EventsConstants.ORDER_OUT_FOR_DELIVERY, event); + } +} From 20e7c025913fbf5c9612def3b696183b15d6842f Mon Sep 17 00:00:00 2001 From: omar mohammed Date: Fri, 16 May 2025 19:57:17 +0300 Subject: [PATCH 06/14] update delivey task to reflect the usage of command pattern --- .../delivery_task/DeliveryTaskService.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java b/src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java index aa8cea6..d183762 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java @@ -10,6 +10,9 @@ import com.podzilla.courier.models.DeliveryTask; import com.podzilla.courier.repositories.delivery_task.IDeliveryTaskRepository; import com.podzilla.courier.services.delivery_task.confirmation_strategy.DeliveryConfirmationStrategy; +import com.podzilla.courier.services.delivery_task.publish_command.Command; +import com.podzilla.courier.services.delivery_task.publish_command.PublishOrderCancelledEventCommand; +import com.podzilla.courier.services.delivery_task.publish_command.PublishOrderOutForDeliveryEventCommand; import com.podzilla.mq.EventPublisher; import com.podzilla.mq.EventsConstants; import com.podzilla.mq.events.OrderCancelledEvent; @@ -111,8 +114,13 @@ public Optional updateDeliveryTaskStatus(String id, Del logger.debug("Delivery task ID: {} updated to status: {}", id, status); // publish order.shipped event if status is OUT_FOR_DELIVERY if (status == DeliveryStatus.OUT_FOR_DELIVERY) { - OrderOutForDeliveryEvent event = new OrderOutForDeliveryEvent(task.getOrderId(), task.getCourierId()); - eventPublisher.publishEvent(EventsConstants.ORDER_OUT_FOR_DELIVERY, event); + Command outForDeliveryCommand = new PublishOrderOutForDeliveryEventCommand( + eventPublisher, + task.getOrderId(), + task.getCourierId() + ); + + outForDeliveryCommand.execute(); if (task.getConfirmationType() == ConfirmationType.OTP) { String otp = IntStream.range(0, 4) @@ -168,12 +176,15 @@ public CancelDeliveryTaskResponseDto cancelDeliveryTask(String id, String cancel deliveryTaskRepository.save(deliveryTaskToCancel); logger.debug("Delivery task cancelled for delivery task ID: {}", id); // publish order.failed event - OrderCancelledEvent event = new OrderCancelledEvent( + Command cancelOrderCommand = new PublishOrderCancelledEventCommand( + eventPublisher, deliveryTaskToCancel.getOrderId(), deliveryTaskToCancel.getCourierId(), cancellationReason ); - eventPublisher.publishEvent(EventsConstants.ORDER_CANCELLED, event); + + cancelOrderCommand.execute(); + return DeliveryTaskMapper.toCancelResponseDto(deliveryTaskToCancel); } logger.warn("Delivery task not found with ID: {}", id); From ff0fdfaea74d5de6078473e20ba4ca0c3c4ec10c Mon Sep 17 00:00:00 2001 From: omar mohammed Date: Fri, 16 May 2025 20:03:01 +0300 Subject: [PATCH 07/14] update delivey task to pass check-style --- .../delivery_task/DeliveryTaskService.java | 124 ++++++++---------- src/main/resources/application.properties | 2 + 2 files changed, 59 insertions(+), 67 deletions(-) diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java b/src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java index d183762..438bb64 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java @@ -14,11 +14,9 @@ import com.podzilla.courier.services.delivery_task.publish_command.PublishOrderCancelledEventCommand; import com.podzilla.courier.services.delivery_task.publish_command.PublishOrderOutForDeliveryEventCommand; import com.podzilla.mq.EventPublisher; -import com.podzilla.mq.EventsConstants; -import com.podzilla.mq.events.OrderCancelledEvent; -import com.podzilla.mq.events.OrderOutForDeliveryEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; import org.springframework.data.util.Pair; import org.springframework.stereotype.Service; @@ -35,83 +33,85 @@ public class DeliveryTaskService { private final IDeliveryTaskRepository deliveryTaskRepository; private final EventPublisher eventPublisher; private final Map confirmationStrategies; - private static final Logger logger = LoggerFactory.getLogger(DeliveryTaskService.class); + private static final Logger LOGGER = LoggerFactory.getLogger(DeliveryTaskService.class); + @Value("${otp.length}") + private int otpLength; - - public DeliveryTaskService(IDeliveryTaskRepository deliveryTaskRepository, EventPublisher eventPublisher, - Map confirmationStrategies) { + public DeliveryTaskService(final IDeliveryTaskRepository deliveryTaskRepository, + final EventPublisher eventPublisher, + final Map confirmationStrategies) { this.deliveryTaskRepository = deliveryTaskRepository; this.eventPublisher = eventPublisher; this.confirmationStrategies = confirmationStrategies; } - public DeliveryTaskResponseDto createDeliveryTask(CreateDeliveryTaskRequestDto deliveryTaskRequestDto) { - logger.info("Creating delivery task for order ID: {}", deliveryTaskRequestDto.getOrderId()); + public DeliveryTaskResponseDto createDeliveryTask(final CreateDeliveryTaskRequestDto deliveryTaskRequestDto) { + LOGGER.info("Creating delivery task for order ID: {}", deliveryTaskRequestDto.getOrderId()); DeliveryTask deliveryTask = DeliveryTaskMapper.toEntity(deliveryTaskRequestDto); DeliveryTask savedTask = deliveryTaskRepository.save(deliveryTask); - logger.debug("Delivery task created with ID: {}", savedTask.getId()); + LOGGER.debug("Delivery task created with ID: {}", savedTask.getId()); return DeliveryTaskMapper.toCreateResponseDto(savedTask); } public List getAllDeliveryTasks() { - logger.info("Fetching all delivery tasks"); + LOGGER.info("Fetching all delivery tasks"); List deliveryTasks = deliveryTaskRepository.findAll() .stream() .map(DeliveryTaskMapper::toCreateResponseDto) .collect(Collectors.toList()); - logger.debug("Delivery tasks fetched: {}", deliveryTasks); + LOGGER.debug("Delivery tasks fetched: {}", deliveryTasks); return deliveryTasks; } - public Optional getDeliveryTaskById(String id) { - logger.info("Fetching delivery task with ID: {}", id); + public Optional getDeliveryTaskById(final String id) { + LOGGER.info("Fetching delivery task with ID: {}", id); Optional deliveryTask = deliveryTaskRepository.findById(id); if (deliveryTask.isPresent()) { - logger.debug("Delivery task found with ID: {}", deliveryTask.get().getId()); + LOGGER.debug("Delivery task found with ID: {}", deliveryTask.get().getId()); } else { - logger.debug("Delivery task not found with ID: {}", id); + LOGGER.debug("Delivery task not found with ID: {}", id); } return deliveryTask.map(DeliveryTaskMapper::toCreateResponseDto); } - public List getDeliveryTasksByCourierId(String courierId) { - logger.info("Fetching delivery tasks by courier ID: {}", courierId); + public List getDeliveryTasksByCourierId(final String courierId) { + LOGGER.info("Fetching delivery tasks by courier ID: {}", courierId); List deliveryTasks = deliveryTaskRepository.findByCourierId(courierId) .stream() .map(DeliveryTaskMapper::toCreateResponseDto) .collect(Collectors.toList()); - logger.debug("Retrieved {} delivery tasks for courier ID: {}", deliveryTasks.size(), courierId); + LOGGER.debug("Retrieved {} delivery tasks for courier ID: {}", deliveryTasks.size(), courierId); return deliveryTasks; } - public List getDeliveryTasksByStatus(DeliveryStatus status) { - logger.info("Fetching delivery tasks by status: {}", status); + public List getDeliveryTasksByStatus(final DeliveryStatus status) { + LOGGER.info("Fetching delivery tasks by status: {}", status); List deliveryTasks = deliveryTaskRepository.findByStatus(status) .stream() .map(DeliveryTaskMapper::toCreateResponseDto) .collect(Collectors.toList()); - logger.debug("Retrieved {} delivery tasks for status: {}", deliveryTasks.size(), status); + LOGGER.debug("Retrieved {} delivery tasks for status: {}", deliveryTasks.size(), status); return deliveryTasks; } - public List getDeliveryTasksByOrderId(String orderId) { - logger.info("Fetching delivery tasks by order ID: {}", orderId); + public List getDeliveryTasksByOrderId(final String orderId) { + LOGGER.info("Fetching delivery tasks by order ID: {}", orderId); List deliveryTasks = deliveryTaskRepository.findByOrderId(orderId) .stream() .map(DeliveryTaskMapper::toCreateResponseDto) .collect(Collectors.toList()); - logger.debug("Retrieved {} delivery tasks for order ID: {}", deliveryTasks.size(), orderId); + LOGGER.debug("Retrieved {} delivery tasks for order ID: {}", deliveryTasks.size(), orderId); return deliveryTasks; } - public Optional updateDeliveryTaskStatus(String id, DeliveryStatus status) { - logger.info("Updating delivery task with ID: {} to {}", id, status); + public Optional updateDeliveryTaskStatus(final String id, final DeliveryStatus status) { + LOGGER.info("Updating delivery task with ID: {} to {}", id, status); Optional updatedDeliveryTask = deliveryTaskRepository.findById(id); if (updatedDeliveryTask.isPresent()) { DeliveryTask task = updatedDeliveryTask.get(); task.setStatus(status); deliveryTaskRepository.save(task); - logger.debug("Delivery task ID: {} updated to status: {}", id, status); + LOGGER.debug("Delivery task ID: {} updated to status: {}", id, status); // publish order.shipped event if status is OUT_FOR_DELIVERY if (status == DeliveryStatus.OUT_FOR_DELIVERY) { Command outForDeliveryCommand = new PublishOrderOutForDeliveryEventCommand( @@ -123,7 +123,7 @@ public Optional updateDeliveryTaskStatus(String id, Del outForDeliveryCommand.execute(); if (task.getConfirmationType() == ConfirmationType.OTP) { - String otp = IntStream.range(0, 4) + String otp = IntStream.range(0, otpLength) .mapToObj(i -> String.valueOf(task.getId().charAt(i))) .collect(Collectors.joining()); task.setOtp(otp); @@ -134,47 +134,48 @@ public Optional updateDeliveryTaskStatus(String id, Del } return Optional.of(DeliveryTaskMapper.toCreateResponseDto(updatedDeliveryTask.get())); } - logger.warn("Delivery task not found with ID: {} for status update", id); + LOGGER.warn("Delivery task not found with ID: {} for status update", id); return Optional.empty(); } - public Pair getDeliveryTaskLocation(String id) { - logger.info("Fetching location for delivery task with ID: {}", id); + public Pair getDeliveryTaskLocation(final String id) { + LOGGER.info("Fetching location for delivery task with ID: {}", id); Optional deliveryTask = deliveryTaskRepository.findById(id); if (deliveryTask.isPresent()) { Double latitude = deliveryTask.get().getCourierLatitude(); Double longitude = deliveryTask.get().getCourierLongitude(); - logger.debug("Location for delivery task ID: {} is ({}, {})", id, latitude, longitude); + LOGGER.debug("Location for delivery task ID: {} is ({}, {})", id, latitude, longitude); return Pair.of(latitude, longitude); } - logger.warn("Delivery task not found with ID for location: {}", id); + LOGGER.warn("Delivery task not found with ID for location: {}", id); return Pair.of(0.0, 0.0); } - public DeliveryTaskResponseDto updateDeliveryTaskLocation(String id, Double latitude, Double longitude) { - logger.info("Updating location for delivery task with ID: {} to ({}, {})", id, latitude, longitude); + public DeliveryTaskResponseDto updateDeliveryTaskLocation(final String id, final Double latitude, + final Double longitude) { + LOGGER.info("Updating location for delivery task with ID: {} to ({}, {})", id, latitude, longitude); Optional updatedDeliveryTask = deliveryTaskRepository.findById(id); if (updatedDeliveryTask.isPresent()) { DeliveryTask deliveryTask = updatedDeliveryTask.get(); deliveryTask.setCourierLongitude(latitude); deliveryTask.setCourierLongitude(longitude); deliveryTaskRepository.save(deliveryTask); - logger.debug("Location updated for delivery task ID: {}", id); + LOGGER.debug("Location updated for delivery task ID: {}", id); return DeliveryTaskMapper.toCreateResponseDto(deliveryTask); } - logger.warn("Delivery task not found with ID: {} for location update", id); + LOGGER.warn("Delivery task not found with ID: {} for location update", id); return null; } - public CancelDeliveryTaskResponseDto cancelDeliveryTask(String id, String cancellationReason) { - logger.info("Cancelling delivery task with ID: {}", id); + public CancelDeliveryTaskResponseDto cancelDeliveryTask(final String id, final String cancellationReason) { + LOGGER.info("Cancelling delivery task with ID: {}", id); Optional deliveryTask = deliveryTaskRepository.findById(id); if (deliveryTask.isPresent()) { DeliveryTask deliveryTaskToCancel = deliveryTask.get(); deliveryTaskToCancel.setStatus(DeliveryStatus.CANCELLED); deliveryTaskToCancel.setCancellationReason(cancellationReason); deliveryTaskRepository.save(deliveryTaskToCancel); - logger.debug("Delivery task cancelled for delivery task ID: {}", id); + LOGGER.debug("Delivery task cancelled for delivery task ID: {}", id); // publish order.failed event Command cancelOrderCommand = new PublishOrderCancelledEventCommand( eventPublisher, @@ -187,45 +188,34 @@ public CancelDeliveryTaskResponseDto cancelDeliveryTask(String id, String cancel return DeliveryTaskMapper.toCancelResponseDto(deliveryTaskToCancel); } - logger.warn("Delivery task not found with ID: {}", id); + LOGGER.warn("Delivery task not found with ID: {}", id); return null; } - public DeliveryTaskResponseDto deleteDeliveryTask(String id) { - logger.info("Deleting delivery task with ID: {}", id); + public DeliveryTaskResponseDto deleteDeliveryTask(final String id) { + LOGGER.info("Deleting delivery task with ID: {}", id); Optional deliveryTask = deliveryTaskRepository.findById(id); if (deliveryTask.isPresent()) { deliveryTaskRepository.delete(deliveryTask.get()); - logger.debug("Delivery task with ID: {} deleted", id); + LOGGER.debug("Delivery task with ID: {} deleted", id); return DeliveryTaskMapper.toCreateResponseDto(deliveryTask.get()); } - logger.warn("Delivery task not found with ID: {} for deletion", id); + LOGGER.warn("Delivery task not found with ID: {} for deletion", id); return null; } - public Optional updateOtp(String id, String otp) { - logger.info("Updating otp for delivery task with ID: {}", id); - DeliveryTask task = deliveryTaskRepository.findById(id).orElse(null); - if (task == null) - return Optional.empty(); - task.setOtp(otp); - deliveryTaskRepository.save(task); - logger.debug("OTP updated for delivery task ID: {}", id); - return Optional.of("Updated OTP"); - } - - public Optional confirmDelivery(String id, String confirmationInput) { - logger.info("Confirming delivery for task ID: {}", id); + public Optional confirmDelivery(final String id, final String confirmationInput) { + LOGGER.info("Confirming delivery for task ID: {}", id); DeliveryTask task = deliveryTaskRepository.findById(id).orElse(null); if (task == null) { - logger.warn("Delivery task not found with ID: {} for customer delivery confirmation", id); + LOGGER.warn("Delivery task not found with ID: {} for customer delivery confirmation", id); return Optional.empty(); } ConfirmationType confirmationType = task.getConfirmationType(); DeliveryConfirmationStrategy strategy = confirmationStrategies.get(confirmationType); if (strategy == null) { - logger.error("No confirmation strategy found for type: {}", confirmationType); + LOGGER.error("No confirmation strategy found for type: {}", confirmationType); return Optional.of("Invalid confirmation type"); } @@ -236,13 +226,13 @@ public Optional confirmDelivery(String id, String confirmationInput) { return result; } - public SubmitCourierRatingResponseDto submitCourierRating(String id, Double rating) { - logger.info("Submitting courier rating for delivery task with ID: {}", id); + public SubmitCourierRatingResponseDto submitCourierRating(final String id, final Double rating) { + LOGGER.info("Submitting courier rating for delivery task with ID: {}", id); Optional deliveryTask = deliveryTaskRepository.findById(id); if (deliveryTask.isPresent()) { DeliveryTask deliveryTaskToSubmit = deliveryTask.get(); - if(!deliveryTaskToSubmit.getStatus().equals(DeliveryStatus.DELIVERED)) { - logger.error("Delivery task status is not DELIVERED"); + if (!deliveryTaskToSubmit.getStatus().equals(DeliveryStatus.DELIVERED)) { + LOGGER.error("Delivery task status is not DELIVERED"); throw new IllegalStateException("Task must be delivered to submit a rating"); } deliveryTaskToSubmit.setCourierRating(rating); @@ -251,7 +241,7 @@ public SubmitCourierRatingResponseDto submitCourierRating(String id, Double rati deliveryTaskRepository.save(deliveryTaskToSubmit); return DeliveryTaskMapper.toSubmitCourierRatingResponseDto(deliveryTaskToSubmit); } - logger.warn("Delivery task not found with ID: {} for courier rating", id); + LOGGER.warn("Delivery task not found with ID: {} for courier rating", id); return null; } -} \ No newline at end of file +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 71de7b7..12daa26 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -14,3 +14,5 @@ spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest +otp.length=4 + From 3584a57771d6dbd01cfc5a14cbbfe445f766583b Mon Sep 17 00:00:00 2001 From: omar mohammed Date: Fri, 16 May 2025 20:09:02 +0300 Subject: [PATCH 08/14] remove unused variable --- src/main/resources/application.properties | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 12daa26..84a2b2f 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -3,7 +3,6 @@ spring.application.name=courier spring.data.mongodb.uri=mongodb://courier:courier-password@mongodb:27017/courier-db?authSource=admin springdoc.swagger-ui.path=/swagger-ui.html springdoc.api-docs.path=/v3/api-docs -springdoc.show-spring-data-rest-sort-parameter=false logging.level.org.springdoc=DEBUG logging.level.org.springframework=INFO From 41cd1e41bd56b3da93d7db2e555c438cf98e5d1f Mon Sep 17 00:00:00 2001 From: omar mohammed Date: Fri, 16 May 2025 20:10:04 +0300 Subject: [PATCH 09/14] fix exceeding line length constraint --- .../podzilla/courier/controllers/DeliveryTaskController.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/podzilla/courier/controllers/DeliveryTaskController.java b/src/main/java/com/podzilla/courier/controllers/DeliveryTaskController.java index 8cd9376..f8e6851 100644 --- a/src/main/java/com/podzilla/courier/controllers/DeliveryTaskController.java +++ b/src/main/java/com/podzilla/courier/controllers/DeliveryTaskController.java @@ -175,7 +175,8 @@ public ResponseEntity deleteDeliveryTask( } @PutMapping("/{id}/confirmation") - @Operation(summary = "Confirm delivery", description = "Validates the delivery confirmation input (e.g., OTP, QR code, or signature)") + @Operation(summary = "Confirm delivery", + description = "Validates the delivery confirmation input (e.g., OTP, QR code, or signature)") @ApiResponse(responseCode = "200", description = "Delivery confirmed successfully") @ApiResponse(responseCode = "404", description = "Invalid confirmation input or task not found") public ResponseEntity confirmDelivery( From a074a6ee8ea259bb41396b9ca82ae8fab5558d81 Mon Sep 17 00:00:00 2001 From: omar mohammed Date: Fri, 16 May 2025 20:18:09 +0300 Subject: [PATCH 10/14] update files to pass check-style --- .../courier/models/ConfirmationType.java | 2 +- .../podzilla/courier/models/DeliveryTask.java | 2 +- .../DeliveryConfirmationStrategy.java | 2 +- .../OtpConfirmationStrategy.java | 14 +++++++------- .../QrCodeConfirmationStrategy.java | 15 +++++++-------- .../SignatureConfirmationStrategy.java | 18 +++++++++--------- 6 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/podzilla/courier/models/ConfirmationType.java b/src/main/java/com/podzilla/courier/models/ConfirmationType.java index 252f19c..1fe3be3 100644 --- a/src/main/java/com/podzilla/courier/models/ConfirmationType.java +++ b/src/main/java/com/podzilla/courier/models/ConfirmationType.java @@ -4,4 +4,4 @@ public enum ConfirmationType { OTP, QR_CODE, SIGNATURE -} \ No newline at end of file +} diff --git a/src/main/java/com/podzilla/courier/models/DeliveryTask.java b/src/main/java/com/podzilla/courier/models/DeliveryTask.java index ba6063b..a4a6835 100644 --- a/src/main/java/com/podzilla/courier/models/DeliveryTask.java +++ b/src/main/java/com/podzilla/courier/models/DeliveryTask.java @@ -36,4 +36,4 @@ public DeliveryTask() { this.courierLatitude = 0.0; // warehouse lat this.courierLongitude = 0.0; // warehouse long } -} \ No newline at end of file +} diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/DeliveryConfirmationStrategy.java b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/DeliveryConfirmationStrategy.java index 5ca010d..266e5b4 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/DeliveryConfirmationStrategy.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/DeliveryConfirmationStrategy.java @@ -6,4 +6,4 @@ public interface DeliveryConfirmationStrategy { Optional confirmDelivery(DeliveryTask task, String confirmationInput); -} \ No newline at end of file +} diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/OtpConfirmationStrategy.java b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/OtpConfirmationStrategy.java index e1f403f..bdb0500 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/OtpConfirmationStrategy.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/OtpConfirmationStrategy.java @@ -14,23 +14,23 @@ @Component public class OtpConfirmationStrategy implements DeliveryConfirmationStrategy { - private static final Logger logger = LoggerFactory.getLogger(OtpConfirmationStrategy.class); + private static final Logger LOGGER = LoggerFactory.getLogger(OtpConfirmationStrategy.class); private final EventPublisher eventPublisher; - public OtpConfirmationStrategy(EventPublisher eventPublisher) { + public OtpConfirmationStrategy(final EventPublisher eventPublisher) { this.eventPublisher = eventPublisher; } @Override - public Optional confirmDelivery(DeliveryTask task, String confirmationInput) { - logger.info("Confirming delivery with OTP for task ID: {}", task.getId()); + public Optional confirmDelivery(final DeliveryTask task, final String confirmationInput) { + LOGGER.info("Confirming delivery with OTP for task ID: {}", task.getId()); if (task.getOtp() == null || !task.getOtp().equals(confirmationInput)) { - logger.debug("Invalid OTP for task ID: {}", task.getId()); + LOGGER.debug("Invalid OTP for task ID: {}", task.getId()); return Optional.of("Wrong OTP"); } task.setStatus(DeliveryStatus.DELIVERED); - logger.debug("OTP confirmed for task ID: {}", task.getId()); + LOGGER.debug("OTP confirmed for task ID: {}", task.getId()); com.podzilla.mq.events.OrderDeliveredEvent event = new OrderDeliveredEvent( task.getOrderId(), @@ -41,4 +41,4 @@ public Optional confirmDelivery(DeliveryTask task, String confirmationIn return Optional.of("OTP confirmed"); } -} \ No newline at end of file +} diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/QrCodeConfirmationStrategy.java b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/QrCodeConfirmationStrategy.java index e852628..8e1104d 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/QrCodeConfirmationStrategy.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/QrCodeConfirmationStrategy.java @@ -10,30 +10,29 @@ import org.springframework.stereotype.Component; import java.math.BigDecimal; -import java.time.Instant; import java.util.Optional; @Component public class QrCodeConfirmationStrategy implements DeliveryConfirmationStrategy { - private static final Logger logger = LoggerFactory.getLogger(QrCodeConfirmationStrategy.class); + private static final Logger LOGGER = LoggerFactory.getLogger(QrCodeConfirmationStrategy.class); private final EventPublisher eventPublisher; - public QrCodeConfirmationStrategy(EventPublisher eventPublisher) { + public QrCodeConfirmationStrategy(final EventPublisher eventPublisher) { this.eventPublisher = eventPublisher; } @Override - public Optional confirmDelivery(DeliveryTask task, String confirmationInput) { - logger.info("Confirming delivery with QR code for task ID: {}", task.getId()); + public Optional confirmDelivery(final DeliveryTask task, final String confirmationInput) { + LOGGER.info("Confirming delivery with QR code for task ID: {}", task.getId()); // assume QR code is valid if it matches a predefined value or logic String expectedQrCode = task.getQrCode(); if (expectedQrCode == null || !expectedQrCode.equals(confirmationInput)) { - logger.debug("Invalid QR code for task ID: {}", task.getId()); + LOGGER.debug("Invalid QR code for task ID: {}", task.getId()); return Optional.of("Invalid QR code"); } task.setStatus(DeliveryStatus.DELIVERED); - logger.debug("QR code confirmed for task ID: {}", task.getId()); + LOGGER.debug("QR code confirmed for task ID: {}", task.getId()); // publish order.delivered event OrderDeliveredEvent event = new OrderDeliveredEvent( @@ -45,4 +44,4 @@ public Optional confirmDelivery(DeliveryTask task, String confirmationIn return Optional.of("QR code confirmed"); } -} \ No newline at end of file +} diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/SignatureConfirmationStrategy.java b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/SignatureConfirmationStrategy.java index 961ff73..933d0b8 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/SignatureConfirmationStrategy.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/SignatureConfirmationStrategy.java @@ -10,29 +10,29 @@ import org.springframework.stereotype.Component; import java.math.BigDecimal; -import java.time.Instant; import java.util.Optional; @Component public class SignatureConfirmationStrategy implements DeliveryConfirmationStrategy { - private static final Logger logger = LoggerFactory.getLogger(SignatureConfirmationStrategy.class); + private static final Logger LOGGER = LoggerFactory.getLogger(SignatureConfirmationStrategy.class); private final EventPublisher eventPublisher; - public SignatureConfirmationStrategy(EventPublisher eventPublisher) { + public SignatureConfirmationStrategy(final EventPublisher eventPublisher) { this.eventPublisher = eventPublisher; } @Override - public Optional confirmDelivery(DeliveryTask task, String confirmationInput) { - logger.info("Confirming delivery with signature for task ID: {}", task.getId()); + public Optional confirmDelivery(final DeliveryTask task, final String confirmationInput) { + LOGGER.info("Confirming delivery with signature for task ID: {}", task.getId()); // assume signature is valid if input is non-empty - if (confirmationInput == null || confirmationInput.isEmpty() || !task.getSignature().equals(confirmationInput)) { - logger.debug("Invalid signature for task ID: {}", task.getId()); + if (confirmationInput == null || confirmationInput.isEmpty() + || !task.getSignature().equals(confirmationInput)) { + LOGGER.debug("Invalid signature for task ID: {}", task.getId()); return Optional.of("Invalid signature"); } task.setStatus(DeliveryStatus.DELIVERED); - logger.debug("Signature confirmed for task ID: {}", task.getId()); + LOGGER.debug("Signature confirmed for task ID: {}", task.getId()); // publish order.delivered event OrderDeliveredEvent event = new OrderDeliveredEvent( @@ -44,4 +44,4 @@ public Optional confirmDelivery(DeliveryTask task, String confirmationIn return Optional.of("Signature confirmed"); } -} \ No newline at end of file +} From 5f8029314470ef90f091f9e79928768e91b2897d Mon Sep 17 00:00:00 2001 From: omar mohammed Date: Fri, 16 May 2025 20:23:12 +0300 Subject: [PATCH 11/14] update files to pass check-style --- .../services/courier/CourierService.java | 55 +++++++++++-------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/podzilla/courier/services/courier/CourierService.java b/src/main/java/com/podzilla/courier/services/courier/CourierService.java index b78abd8..45ef08d 100644 --- a/src/main/java/com/podzilla/courier/services/courier/CourierService.java +++ b/src/main/java/com/podzilla/courier/services/courier/CourierService.java @@ -1,6 +1,8 @@ package com.podzilla.courier.services.courier; -import com.podzilla.courier.dtos.couriers.*; +import com.podzilla.courier.dtos.couriers.CourierResponseDto; +import com.podzilla.courier.dtos.couriers.CreateCourierRequestDto; +import com.podzilla.courier.dtos.couriers.UpdateCourierRequestDto; import com.podzilla.courier.mappers.CourierMapper; import com.podzilla.courier.models.Courier; import com.podzilla.courier.repositories.courier.CourierRepository; @@ -16,64 +18,71 @@ public class CourierService { private final CourierRepository courierRepository; - private static final Logger logger = LoggerFactory.getLogger(CourierService.class); + private static final Logger LOGGER = LoggerFactory.getLogger(CourierService.class); - public CourierService(CourierRepository courierRepository) { + public CourierService(final CourierRepository courierRepository) { this.courierRepository = courierRepository; } public List getAllCouriers() { - logger.info("Fetching all couriers"); + LOGGER.info("Fetching all couriers"); List couriers = courierRepository.findAll().stream() .map(CourierMapper::toCreateResponseDto).collect(Collectors.toList()); - logger.info("Couriers Fetched: {}", couriers); + LOGGER.info("Couriers Fetched: {}", couriers); return couriers; } - public Optional getCourierById(String id) { - logger.info("Fetching courier with ID: {}", id); + public Optional getCourierById(final String id) { + LOGGER.info("Fetching courier with ID: {}", id); Optional courier = courierRepository.findById(id); if (courier.isPresent()) { - logger.debug("Courier found with ID: {}", courier.get().getId()); + LOGGER.debug("Courier found with ID: {}", courier.get().getId()); } else { - logger.debug("Courier not found with ID: {}", id); + LOGGER.debug("Courier not found with ID: {}", id); } return courier.map(CourierMapper::toCreateResponseDto); } - public CourierResponseDto createCourier(CreateCourierRequestDto courier) { - logger.info("Creating new courier"); + public CourierResponseDto createCourier(final CreateCourierRequestDto courier) { + LOGGER.info("Creating new courier"); Courier newCourier = CourierMapper.toEntity(courier); Courier savedCourier = courierRepository.save(newCourier); - logger.info("Created courier with ID: {}", savedCourier.getId()); + LOGGER.info("Created courier with ID: {}", savedCourier.getId()); return CourierMapper.toCreateResponseDto(savedCourier); } - public Optional updateCourier(String id, UpdateCourierRequestDto courierDto) { - logger.info("Updating courier with ID: {}", id); + public Optional updateCourier(final String id, final UpdateCourierRequestDto courierDto) { + LOGGER.info("Updating courier with ID: {}", id); Optional existingCourier = courierRepository.findById(id); if (existingCourier.isEmpty()) { - logger.debug("Courier not found with ID: {}", id); + LOGGER.debug("Courier not found with ID: {}", id); return Optional.empty(); } Courier courier = existingCourier.get(); - if (courierDto.getName() != null) courier.setName(courierDto.getName()); - if (courierDto.getMobileNo() != null) courier.setMobileNo(courierDto.getMobileNo()); - if (courierDto.getStatus() != null) courier.setStatus(courierDto.getStatus()); + if (courierDto.getName() != null) { + courier.setName(courierDto.getName()); + } + if (courierDto.getMobileNo() != null) { + courier.setMobileNo(courierDto.getMobileNo()); + } + if (courierDto.getStatus() != null) { + courier.setStatus(courierDto.getStatus()); + } Courier savedCourier = courierRepository.save(courier); - logger.info("Updated courier with ID: {}", savedCourier.getId()); + LOGGER.info("Updated courier with ID: {}", savedCourier.getId()); return Optional.of(CourierMapper.toCreateResponseDto(savedCourier)); } - public Optional deleteCourier(String id) { - logger.info("Deleting courier with ID: {}", id); + public Optional deleteCourier(final String id) { + LOGGER.info("Deleting courier with ID: {}", id); Optional courier = courierRepository.findById(id); if (courier.isPresent()) { courierRepository.deleteById(id); - logger.info("Deleted courier with ID: {}", id); + LOGGER.info("Deleted courier with ID: {}", id); return courier.map(CourierMapper::toCreateResponseDto); } - logger.debug("Courier not found with ID: {}", id); + LOGGER.debug("Courier not found with ID: {}", id); return Optional.empty(); } } + From 5056e42f6fe742015c524f90b6ea98491aa94420 Mon Sep 17 00:00:00 2001 From: amir-awad Date: Fri, 16 May 2025 22:16:33 +0300 Subject: [PATCH 12/14] fix: adjust command design pattern to a meaningful behaviour --- .../delivery_task/DeliveryTaskService.java | 26 ++++++++++------- .../OtpConfirmationStrategy.java | 7 +++-- .../QrCodeConfirmationStrategy.java | 5 ++-- .../SignatureConfirmationStrategy.java | 5 ++-- .../PublishOrderCancelledEventCommand.java | 23 --------------- ...tCommand.java => StartPollingCommand.java} | 9 +++--- .../publish_command/StopPollingCommand.java | 29 +++++++++++++++++++ 7 files changed, 58 insertions(+), 46 deletions(-) delete mode 100644 src/main/java/com/podzilla/courier/services/delivery_task/publish_command/PublishOrderCancelledEventCommand.java rename src/main/java/com/podzilla/courier/services/delivery_task/publish_command/{PublishOrderOutForDeliveryEventCommand.java => StartPollingCommand.java} (56%) create mode 100644 src/main/java/com/podzilla/courier/services/delivery_task/publish_command/StopPollingCommand.java diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java b/src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java index 438bb64..ec6843d 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java @@ -11,9 +11,11 @@ import com.podzilla.courier.repositories.delivery_task.IDeliveryTaskRepository; import com.podzilla.courier.services.delivery_task.confirmation_strategy.DeliveryConfirmationStrategy; import com.podzilla.courier.services.delivery_task.publish_command.Command; -import com.podzilla.courier.services.delivery_task.publish_command.PublishOrderCancelledEventCommand; -import com.podzilla.courier.services.delivery_task.publish_command.PublishOrderOutForDeliveryEventCommand; +import com.podzilla.courier.services.delivery_task.publish_command.StopPollingCommand; +import com.podzilla.courier.services.delivery_task.publish_command.StartPollingCommand; import com.podzilla.mq.EventPublisher; +import com.podzilla.mq.events.OrderCancelledEvent; +import com.podzilla.mq.events.OrderOutForDeliveryEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; @@ -114,13 +116,13 @@ public Optional updateDeliveryTaskStatus(final String i LOGGER.debug("Delivery task ID: {} updated to status: {}", id, status); // publish order.shipped event if status is OUT_FOR_DELIVERY if (status == DeliveryStatus.OUT_FOR_DELIVERY) { - Command outForDeliveryCommand = new PublishOrderOutForDeliveryEventCommand( + OrderOutForDeliveryEvent event = new OrderOutForDeliveryEvent(task.getOrderId(), + task.getCourierId()); + Command startPollingCommand = new StartPollingCommand( eventPublisher, - task.getOrderId(), - task.getCourierId() + event ); - - outForDeliveryCommand.execute(); + startPollingCommand.execute(); if (task.getConfirmationType() == ConfirmationType.OTP) { String otp = IntStream.range(0, otpLength) @@ -177,14 +179,16 @@ public CancelDeliveryTaskResponseDto cancelDeliveryTask(final String id, final S deliveryTaskRepository.save(deliveryTaskToCancel); LOGGER.debug("Delivery task cancelled for delivery task ID: {}", id); // publish order.failed event - Command cancelOrderCommand = new PublishOrderCancelledEventCommand( - eventPublisher, + OrderCancelledEvent event = new OrderCancelledEvent( deliveryTaskToCancel.getOrderId(), deliveryTaskToCancel.getCourierId(), cancellationReason ); - - cancelOrderCommand.execute(); + Command stopPollingCommand = new StopPollingCommand( + eventPublisher, + event + ); + stopPollingCommand.execute(); return DeliveryTaskMapper.toCancelResponseDto(deliveryTaskToCancel); } diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/OtpConfirmationStrategy.java b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/OtpConfirmationStrategy.java index bdb0500..f6bc510 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/OtpConfirmationStrategy.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/OtpConfirmationStrategy.java @@ -2,8 +2,8 @@ import com.podzilla.courier.models.DeliveryStatus; import com.podzilla.courier.models.DeliveryTask; +import com.podzilla.courier.services.delivery_task.publish_command.StopPollingCommand; import com.podzilla.mq.EventPublisher; -import com.podzilla.mq.EventsConstants; import com.podzilla.mq.events.OrderDeliveredEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,12 +32,13 @@ public Optional confirmDelivery(final DeliveryTask task, final String co task.setStatus(DeliveryStatus.DELIVERED); LOGGER.debug("OTP confirmed for task ID: {}", task.getId()); - com.podzilla.mq.events.OrderDeliveredEvent event = new OrderDeliveredEvent( + OrderDeliveredEvent event = new OrderDeliveredEvent( task.getOrderId(), task.getCourierId(), task.getCourierRating() != null ? BigDecimal.valueOf(task.getCourierRating()) : null ); - eventPublisher.publishEvent(EventsConstants.ORDER_DELIVERED, event); + StopPollingCommand stopPollingCommand = new StopPollingCommand(eventPublisher, event); + stopPollingCommand.execute(); return Optional.of("OTP confirmed"); } diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/QrCodeConfirmationStrategy.java b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/QrCodeConfirmationStrategy.java index 8e1104d..949714a 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/QrCodeConfirmationStrategy.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/QrCodeConfirmationStrategy.java @@ -2,8 +2,8 @@ import com.podzilla.courier.models.DeliveryStatus; import com.podzilla.courier.models.DeliveryTask; +import com.podzilla.courier.services.delivery_task.publish_command.StopPollingCommand; import com.podzilla.mq.EventPublisher; -import com.podzilla.mq.EventsConstants; import com.podzilla.mq.events.OrderDeliveredEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,7 +40,8 @@ public Optional confirmDelivery(final DeliveryTask task, final String co task.getCourierId(), task.getCourierRating() != null ? BigDecimal.valueOf(task.getCourierRating()) : null ); - eventPublisher.publishEvent(EventsConstants.ORDER_DELIVERED, event); + StopPollingCommand stopPollingCommand = new StopPollingCommand(eventPublisher, event); + stopPollingCommand.execute(); return Optional.of("QR code confirmed"); } diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/SignatureConfirmationStrategy.java b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/SignatureConfirmationStrategy.java index 933d0b8..4ca87fb 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/SignatureConfirmationStrategy.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/SignatureConfirmationStrategy.java @@ -2,8 +2,8 @@ import com.podzilla.courier.models.DeliveryStatus; import com.podzilla.courier.models.DeliveryTask; +import com.podzilla.courier.services.delivery_task.publish_command.StopPollingCommand; import com.podzilla.mq.EventPublisher; -import com.podzilla.mq.EventsConstants; import com.podzilla.mq.events.OrderDeliveredEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,7 +40,8 @@ public Optional confirmDelivery(final DeliveryTask task, final String co task.getCourierId(), task.getCourierRating() != null ? BigDecimal.valueOf(task.getCourierRating()) : null ); - eventPublisher.publishEvent(EventsConstants.ORDER_DELIVERED, event); + StopPollingCommand stopPollingCommand = new StopPollingCommand(eventPublisher, event); + stopPollingCommand.execute(); return Optional.of("Signature confirmed"); } diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/PublishOrderCancelledEventCommand.java b/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/PublishOrderCancelledEventCommand.java deleted file mode 100644 index 7fc312c..0000000 --- a/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/PublishOrderCancelledEventCommand.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.podzilla.courier.services.delivery_task.publish_command; - -import com.podzilla.mq.EventPublisher; -import com.podzilla.mq.EventsConstants; -import com.podzilla.mq.events.OrderCancelledEvent; - -public class PublishOrderCancelledEventCommand implements Command { - private final EventPublisher eventPublisher; - private final OrderCancelledEvent event; - - public PublishOrderCancelledEventCommand(final EventPublisher eventPublisher, - final String orderId, - final String courierId, - final String cancellationReason) { - this.eventPublisher = eventPublisher; - this.event = new OrderCancelledEvent(orderId, courierId, cancellationReason); - } - - @Override - public void execute() { - eventPublisher.publishEvent(EventsConstants.ORDER_CANCELLED, event); - } -} diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/PublishOrderOutForDeliveryEventCommand.java b/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/StartPollingCommand.java similarity index 56% rename from src/main/java/com/podzilla/courier/services/delivery_task/publish_command/PublishOrderOutForDeliveryEventCommand.java rename to src/main/java/com/podzilla/courier/services/delivery_task/publish_command/StartPollingCommand.java index 0c10cfe..47946e0 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/PublishOrderOutForDeliveryEventCommand.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/StartPollingCommand.java @@ -4,20 +4,19 @@ import com.podzilla.mq.EventsConstants; import com.podzilla.mq.events.OrderOutForDeliveryEvent; -public class PublishOrderOutForDeliveryEventCommand implements Command { +public class StartPollingCommand implements Command { private final EventPublisher eventPublisher; private final OrderOutForDeliveryEvent event; - public PublishOrderOutForDeliveryEventCommand(final EventPublisher eventPublisher, - final String orderId, - final String courierId) { + public StartPollingCommand(final EventPublisher eventPublisher, OrderOutForDeliveryEvent event) { this.eventPublisher = eventPublisher; - this.event = new OrderOutForDeliveryEvent(orderId, courierId); + this.event = event; } @Override public void execute() { + // publish out_for_delivery event so that the order service start tracking courier location eventPublisher.publishEvent(EventsConstants.ORDER_OUT_FOR_DELIVERY, event); } } diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/StopPollingCommand.java b/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/StopPollingCommand.java new file mode 100644 index 0000000..e95bfd1 --- /dev/null +++ b/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/StopPollingCommand.java @@ -0,0 +1,29 @@ +package com.podzilla.courier.services.delivery_task.publish_command; + +import com.podzilla.mq.EventPublisher; +import com.podzilla.mq.EventsConstants; +import com.podzilla.mq.events.OrderCancelledEvent; +import com.podzilla.mq.events.OrderDeliveredEvent; + +public class StopPollingCommand implements Command { + private final EventPublisher eventPublisher; + private final Object event; + + public StopPollingCommand(final EventPublisher eventPublisher, + Object event) { + this.eventPublisher = eventPublisher; + this.event = event; + } + + + @Override + public void execute() { + if (event instanceof OrderCancelledEvent cancelledEvent) { + // publish order_cancelled event so that the order service stop tracking courier location + eventPublisher.publishEvent(EventsConstants.ORDER_CANCELLED, cancelledEvent); + } else if (event instanceof OrderDeliveredEvent deliveredEvent) { + // publish order_delivered event so that the order service stop tracking courier location + eventPublisher.publishEvent(EventsConstants.ORDER_DELIVERED, deliveredEvent); + } + } +} From e41a7af0326392500ed6b63dedb74f7cd917e9f8 Mon Sep 17 00:00:00 2001 From: omar mohammed Date: Fri, 16 May 2025 22:33:02 +0300 Subject: [PATCH 13/14] rename package --- .../courier/services/delivery_task/DeliveryTaskService.java | 6 +++--- .../confirmation_strategy/OtpConfirmationStrategy.java | 2 +- .../confirmation_strategy/QrCodeConfirmationStrategy.java | 2 +- .../SignatureConfirmationStrategy.java | 2 +- .../services/delivery_task/poll_command/Command.java | 5 +++++ .../StartPollingCommand.java | 4 ++-- .../StopPollingCommand.java | 5 ++--- .../services/delivery_task/publish_command/Command.java | 5 ----- 8 files changed, 15 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/podzilla/courier/services/delivery_task/poll_command/Command.java rename src/main/java/com/podzilla/courier/services/delivery_task/{publish_command => poll_command}/StartPollingCommand.java (85%) rename src/main/java/com/podzilla/courier/services/delivery_task/{publish_command => poll_command}/StopPollingCommand.java (89%) delete mode 100644 src/main/java/com/podzilla/courier/services/delivery_task/publish_command/Command.java diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java b/src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java index ec6843d..7be93de 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/DeliveryTaskService.java @@ -10,9 +10,9 @@ import com.podzilla.courier.models.DeliveryTask; import com.podzilla.courier.repositories.delivery_task.IDeliveryTaskRepository; import com.podzilla.courier.services.delivery_task.confirmation_strategy.DeliveryConfirmationStrategy; -import com.podzilla.courier.services.delivery_task.publish_command.Command; -import com.podzilla.courier.services.delivery_task.publish_command.StopPollingCommand; -import com.podzilla.courier.services.delivery_task.publish_command.StartPollingCommand; +import com.podzilla.courier.services.delivery_task.poll_command.Command; +import com.podzilla.courier.services.delivery_task.poll_command.StopPollingCommand; +import com.podzilla.courier.services.delivery_task.poll_command.StartPollingCommand; import com.podzilla.mq.EventPublisher; import com.podzilla.mq.events.OrderCancelledEvent; import com.podzilla.mq.events.OrderOutForDeliveryEvent; diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/OtpConfirmationStrategy.java b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/OtpConfirmationStrategy.java index f6bc510..5c23693 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/OtpConfirmationStrategy.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/OtpConfirmationStrategy.java @@ -2,7 +2,7 @@ import com.podzilla.courier.models.DeliveryStatus; import com.podzilla.courier.models.DeliveryTask; -import com.podzilla.courier.services.delivery_task.publish_command.StopPollingCommand; +import com.podzilla.courier.services.delivery_task.poll_command.StopPollingCommand; import com.podzilla.mq.EventPublisher; import com.podzilla.mq.events.OrderDeliveredEvent; import org.slf4j.Logger; diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/QrCodeConfirmationStrategy.java b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/QrCodeConfirmationStrategy.java index 949714a..8b3a135 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/QrCodeConfirmationStrategy.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/QrCodeConfirmationStrategy.java @@ -2,7 +2,7 @@ import com.podzilla.courier.models.DeliveryStatus; import com.podzilla.courier.models.DeliveryTask; -import com.podzilla.courier.services.delivery_task.publish_command.StopPollingCommand; +import com.podzilla.courier.services.delivery_task.poll_command.StopPollingCommand; import com.podzilla.mq.EventPublisher; import com.podzilla.mq.events.OrderDeliveredEvent; import org.slf4j.Logger; diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/SignatureConfirmationStrategy.java b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/SignatureConfirmationStrategy.java index 4ca87fb..330fcdc 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/SignatureConfirmationStrategy.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/confirmation_strategy/SignatureConfirmationStrategy.java @@ -2,7 +2,7 @@ import com.podzilla.courier.models.DeliveryStatus; import com.podzilla.courier.models.DeliveryTask; -import com.podzilla.courier.services.delivery_task.publish_command.StopPollingCommand; +import com.podzilla.courier.services.delivery_task.poll_command.StopPollingCommand; import com.podzilla.mq.EventPublisher; import com.podzilla.mq.events.OrderDeliveredEvent; import org.slf4j.Logger; diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/poll_command/Command.java b/src/main/java/com/podzilla/courier/services/delivery_task/poll_command/Command.java new file mode 100644 index 0000000..a79be50 --- /dev/null +++ b/src/main/java/com/podzilla/courier/services/delivery_task/poll_command/Command.java @@ -0,0 +1,5 @@ +package com.podzilla.courier.services.delivery_task.poll_command; + +public interface Command { + void execute(); +} diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/StartPollingCommand.java b/src/main/java/com/podzilla/courier/services/delivery_task/poll_command/StartPollingCommand.java similarity index 85% rename from src/main/java/com/podzilla/courier/services/delivery_task/publish_command/StartPollingCommand.java rename to src/main/java/com/podzilla/courier/services/delivery_task/poll_command/StartPollingCommand.java index 47946e0..a3835c0 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/StartPollingCommand.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/poll_command/StartPollingCommand.java @@ -1,4 +1,4 @@ -package com.podzilla.courier.services.delivery_task.publish_command; +package com.podzilla.courier.services.delivery_task.poll_command; import com.podzilla.mq.EventPublisher; import com.podzilla.mq.EventsConstants; @@ -9,7 +9,7 @@ public class StartPollingCommand implements Command { private final EventPublisher eventPublisher; private final OrderOutForDeliveryEvent event; - public StartPollingCommand(final EventPublisher eventPublisher, OrderOutForDeliveryEvent event) { + public StartPollingCommand(final EventPublisher eventPublisher, final OrderOutForDeliveryEvent event) { this.eventPublisher = eventPublisher; this.event = event; } diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/StopPollingCommand.java b/src/main/java/com/podzilla/courier/services/delivery_task/poll_command/StopPollingCommand.java similarity index 89% rename from src/main/java/com/podzilla/courier/services/delivery_task/publish_command/StopPollingCommand.java rename to src/main/java/com/podzilla/courier/services/delivery_task/poll_command/StopPollingCommand.java index e95bfd1..1a13bbd 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/StopPollingCommand.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/poll_command/StopPollingCommand.java @@ -1,4 +1,4 @@ -package com.podzilla.courier.services.delivery_task.publish_command; +package com.podzilla.courier.services.delivery_task.poll_command; import com.podzilla.mq.EventPublisher; import com.podzilla.mq.EventsConstants; @@ -9,8 +9,7 @@ public class StopPollingCommand implements Command { private final EventPublisher eventPublisher; private final Object event; - public StopPollingCommand(final EventPublisher eventPublisher, - Object event) { + public StopPollingCommand(final EventPublisher eventPublisher, final Object event) { this.eventPublisher = eventPublisher; this.event = event; } diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/Command.java b/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/Command.java deleted file mode 100644 index 86a10e3..0000000 --- a/src/main/java/com/podzilla/courier/services/delivery_task/publish_command/Command.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.podzilla.courier.services.delivery_task.publish_command; - -public interface Command { - void execute(); -} From 8b5a3d06de608838cbdb18bacb3c7537651f8b99 Mon Sep 17 00:00:00 2001 From: omar mohammed Date: Fri, 16 May 2025 22:39:07 +0300 Subject: [PATCH 14/14] update files to pass check-style --- .../delivery_task/poll_command/StopPollingCommand.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/poll_command/StopPollingCommand.java b/src/main/java/com/podzilla/courier/services/delivery_task/poll_command/StopPollingCommand.java index 1a13bbd..fe65603 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/poll_command/StopPollingCommand.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/poll_command/StopPollingCommand.java @@ -17,11 +17,13 @@ public StopPollingCommand(final EventPublisher eventPublisher, final Object even @Override public void execute() { - if (event instanceof OrderCancelledEvent cancelledEvent) { - // publish order_cancelled event so that the order service stop tracking courier location + if (event instanceof OrderCancelledEvent) { + OrderCancelledEvent cancelledEvent = (OrderCancelledEvent) event; + // publish order_cancelled event so that the order service stops tracking courier location eventPublisher.publishEvent(EventsConstants.ORDER_CANCELLED, cancelledEvent); - } else if (event instanceof OrderDeliveredEvent deliveredEvent) { - // publish order_delivered event so that the order service stop tracking courier location + } else if (event instanceof OrderDeliveredEvent) { + OrderDeliveredEvent deliveredEvent = (OrderDeliveredEvent) event; + // publish order_delivered event so that the order service stops tracking courier location eventPublisher.publishEvent(EventsConstants.ORDER_DELIVERED, deliveredEvent); } }