From 1f5a6493933829033096f75365823b22f449cc90 Mon Sep 17 00:00:00 2001 From: amir-awad Date: Sat, 17 May 2025 16:36:09 +0300 Subject: [PATCH 1/8] feat: add courier assignment event consumer (not working yet) --- pom.xml | 2 +- .../podzilla/courier/CourierApplication.java | 2 ++ .../controllers/DeliveryTaskController.java | 20 +++++------ .../CourierAssignmentEventConsumer.java | 26 +++++++++++++++ .../delivery_task/DeliveryTaskService.java | 33 ++++++++++--------- 5 files changed, 56 insertions(+), 27 deletions(-) create mode 100644 src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java diff --git a/pom.xml b/pom.xml index a16c4fb..9c12dc2 100644 --- a/pom.xml +++ b/pom.xml @@ -88,7 +88,7 @@ com.github.Podzilla podzilla-utils-lib - v1.1.6 + v1.1.7 diff --git a/src/main/java/com/podzilla/courier/CourierApplication.java b/src/main/java/com/podzilla/courier/CourierApplication.java index 8f346c2..8ed5d3f 100644 --- a/src/main/java/com/podzilla/courier/CourierApplication.java +++ b/src/main/java/com/podzilla/courier/CourierApplication.java @@ -2,10 +2,12 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; @EnableMongoRepositories(basePackages = "com.podzilla.courier.repositories") @SpringBootApplication +@ComponentScan(basePackages = {"com.podzilla.courier", "com.podzilla.mq"}) public class CourierApplication { public static void main(String[] args) { diff --git a/src/main/java/com/podzilla/courier/controllers/DeliveryTaskController.java b/src/main/java/com/podzilla/courier/controllers/DeliveryTaskController.java index f8e6851..56dcec7 100644 --- a/src/main/java/com/podzilla/courier/controllers/DeliveryTaskController.java +++ b/src/main/java/com/podzilla/courier/controllers/DeliveryTaskController.java @@ -115,28 +115,28 @@ public ResponseEntity> getDeliveryTasksByOrderId( return ResponseEntity.ok(deliveryTaskService.getDeliveryTasksByOrderId(orderId)); } - @GetMapping("/{id}/location") + @GetMapping("/{orderId}/location") @Operation(summary = "Get task location", description = "Retrieves coordinates of a delivery task") @ApiResponse(responseCode = "200", description = "Location retrieved successfully") @ApiResponse(responseCode = "404", description = "Task not found") public ResponseEntity> getDeliveryTaskLocation( - @Parameter(description = "ID of the delivery task") - @PathVariable final String id) { - LOGGER.info("Received request to get the location of delivery task with id {}", id); - return ResponseEntity.ok(deliveryTaskService.getDeliveryTaskLocation(id)); + @Parameter(description = "ID of the order") + @PathVariable final String orderId) { + LOGGER.info("Received request to get the location of delivery task with order id {}", orderId); + return ResponseEntity.ok(deliveryTaskService.getDeliveryTaskLocation(orderId)); } - @PatchMapping("/{id}/location") + @PatchMapping("/{orderId}/location") @Operation(summary = "Update task location", description = "Updates coordinates of a delivery task") @ApiResponse(responseCode = "200", description = "Location updated successfully") public ResponseEntity updateDeliveryTaskLocation( - @Parameter(description = "ID of the delivery task") - @PathVariable final String id, + @Parameter(description = "ID of the order") + @PathVariable final String orderId, @RequestBody(description = "New coordinates data") @org.springframework.web.bind.annotation.RequestBody final LocationUpdateDto locationUpdateDto) { - LOGGER.info("Received request to update the location of delivery task with id {}", id); + LOGGER.info("Received request to update the location of delivery task with order id {}", orderId); return ResponseEntity.ok(deliveryTaskService.updateDeliveryTaskLocation( - id, + orderId, locationUpdateDto.getLatitude(), locationUpdateDto.getLongitude())); } diff --git a/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java b/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java new file mode 100644 index 0000000..6412748 --- /dev/null +++ b/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java @@ -0,0 +1,26 @@ +package com.podzilla.courier.events; + +import com.podzilla.courier.services.delivery_task.DeliveryTaskService; +import com.podzilla.mq.EventsConstants; +import com.podzilla.mq.events.BaseEvent; +import com.podzilla.mq.events.OrderAssignedToCourierEvent; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.stereotype.Component; + +@Component +public class CourierAssignmentEventConsumer { + final DeliveryTaskService deliveryTaskService; + + public CourierAssignmentEventConsumer(DeliveryTaskService deliveryTaskService) { + this.deliveryTaskService = deliveryTaskService; + } + + @RabbitListener(queues = EventsConstants.COURIER_ORDER_EVENT_QUEUE) + public void handleEvent(BaseEvent event) { + if(event instanceof OrderAssignedToCourierEvent) { + OrderAssignedToCourierEvent courierEvent = (OrderAssignedToCourierEvent) event; +// deliveryTaskService.createDeliveryTask(courierEvent.getOrderId(), courierEvent.getCourierId(), +// courierEvent.getPrice(), courierEvent.getOrderLatitude(), courierEvent.getOrderLongitude()); + } + } +} 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 7be93de..c117ab4 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.confirmation_strategy.OtpConfirmationStrategy; +import com.podzilla.courier.services.delivery_task.confirmation_strategy.QrCodeConfirmationStrategy; +import com.podzilla.courier.services.delivery_task.confirmation_strategy.SignatureConfirmationStrategy; 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; @@ -24,7 +27,6 @@ import java.time.LocalDateTime; import java.util.List; -import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -34,17 +36,14 @@ public class DeliveryTaskService { private final IDeliveryTaskRepository deliveryTaskRepository; private final EventPublisher eventPublisher; - private final Map confirmationStrategies; private static final Logger LOGGER = LoggerFactory.getLogger(DeliveryTaskService.class); @Value("${otp.length}") private int otpLength; public DeliveryTaskService(final IDeliveryTaskRepository deliveryTaskRepository, - final EventPublisher eventPublisher, - final Map confirmationStrategies) { + final EventPublisher eventPublisher) { this.deliveryTaskRepository = deliveryTaskRepository; this.eventPublisher = eventPublisher; - this.confirmationStrategies = confirmationStrategies; } public DeliveryTaskResponseDto createDeliveryTask(final CreateDeliveryTaskRequestDto deliveryTaskRequestDto) { @@ -140,32 +139,32 @@ public Optional updateDeliveryTaskStatus(final String i return Optional.empty(); } - public Pair getDeliveryTaskLocation(final String id) { - LOGGER.info("Fetching location for delivery task with ID: {}", id); - Optional deliveryTask = deliveryTaskRepository.findById(id); + public Pair getDeliveryTaskLocation(final String orderId) { + LOGGER.info("Fetching location for delivery task with order id: {}", orderId); + Optional deliveryTask = deliveryTaskRepository.findByOrderId(orderId).stream().findFirst(); 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 with order id: {} is ({}, {})", orderId, 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 order id: {} for location", orderId); return Pair.of(0.0, 0.0); } - public DeliveryTaskResponseDto updateDeliveryTaskLocation(final String id, final Double latitude, + public DeliveryTaskResponseDto updateDeliveryTaskLocation(final String orderId, final Double latitude, final Double longitude) { - LOGGER.info("Updating location for delivery task with ID: {} to ({}, {})", id, latitude, longitude); - Optional updatedDeliveryTask = deliveryTaskRepository.findById(id); + LOGGER.info("Updating location for delivery task with order id: {} to ({}, {})", orderId, latitude, longitude); + Optional updatedDeliveryTask = deliveryTaskRepository.findByOrderId(orderId).stream().findFirst(); 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 with order id: {}", orderId); return DeliveryTaskMapper.toCreateResponseDto(deliveryTask); } - LOGGER.warn("Delivery task not found with ID: {} for location update", id); + LOGGER.warn("Delivery task not found with order id: {} for location update", orderId); return null; } @@ -217,7 +216,9 @@ public Optional confirmDelivery(final String id, final String confirmati } ConfirmationType confirmationType = task.getConfirmationType(); - DeliveryConfirmationStrategy strategy = confirmationStrategies.get(confirmationType); + DeliveryConfirmationStrategy strategy = confirmationType.equals(ConfirmationType.OTP)? new OtpConfirmationStrategy(eventPublisher): + confirmationType.equals(ConfirmationType.QR_CODE)? new QrCodeConfirmationStrategy(eventPublisher): + confirmationType.equals(ConfirmationType.SIGNATURE)? new SignatureConfirmationStrategy(eventPublisher): null; if (strategy == null) { LOGGER.error("No confirmation strategy found for type: {}", confirmationType); return Optional.of("Invalid confirmation type"); From 89ae901c2a5016a844ee87f9afae05ff27fa18b0 Mon Sep 17 00:00:00 2001 From: amir-awad Date: Sat, 17 May 2025 17:03:32 +0300 Subject: [PATCH 2/8] fix: changed type of price to be BigDecimal --- .../dtos/delivery_tasks/CreateDeliveryTaskRequestDto.java | 4 +++- .../dtos/delivery_tasks/DeliveryTaskResponseDto.java | 4 +++- .../courier/events/CourierAssignmentEventConsumer.java | 6 ++++-- src/main/java/com/podzilla/courier/models/DeliveryTask.java | 3 ++- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/CreateDeliveryTaskRequestDto.java b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/CreateDeliveryTaskRequestDto.java index 59ffb9d..1c89841 100644 --- a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/CreateDeliveryTaskRequestDto.java +++ b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/CreateDeliveryTaskRequestDto.java @@ -4,6 +4,8 @@ import lombok.AllArgsConstructor; import lombok.Getter; +import java.math.BigDecimal; + @Getter @AllArgsConstructor public class CreateDeliveryTaskRequestDto { @@ -14,7 +16,7 @@ public class CreateDeliveryTaskRequestDto { private String courierId; @NotNull(message = "Price is required") - private double price; + private BigDecimal price; @NotNull(message = "Order latitude is required") private double orderLatitude; diff --git a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/DeliveryTaskResponseDto.java b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/DeliveryTaskResponseDto.java index 769c005..7ee8699 100644 --- a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/DeliveryTaskResponseDto.java +++ b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/DeliveryTaskResponseDto.java @@ -3,7 +3,9 @@ import com.podzilla.courier.models.DeliveryStatus; -public record DeliveryTaskResponseDto(String id, String orderId, String courierId, Double price, DeliveryStatus status, +import java.math.BigDecimal; + +public record DeliveryTaskResponseDto(String id, String orderId, String courierId, BigDecimal price, DeliveryStatus status, Double orderLatitude, Double orderLongitude, Double courierLatitude, Double courierLongitude) { } \ No newline at end of file diff --git a/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java b/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java index 6412748..9336f21 100644 --- a/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java +++ b/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java @@ -1,5 +1,6 @@ package com.podzilla.courier.events; +import com.podzilla.courier.dtos.delivery_tasks.CreateDeliveryTaskRequestDto; import com.podzilla.courier.services.delivery_task.DeliveryTaskService; import com.podzilla.mq.EventsConstants; import com.podzilla.mq.events.BaseEvent; @@ -19,8 +20,9 @@ public CourierAssignmentEventConsumer(DeliveryTaskService deliveryTaskService) { public void handleEvent(BaseEvent event) { if(event instanceof OrderAssignedToCourierEvent) { OrderAssignedToCourierEvent courierEvent = (OrderAssignedToCourierEvent) event; -// deliveryTaskService.createDeliveryTask(courierEvent.getOrderId(), courierEvent.getCourierId(), -// courierEvent.getPrice(), courierEvent.getOrderLatitude(), courierEvent.getOrderLongitude()); + CreateDeliveryTaskRequestDto deliveryTask = new CreateDeliveryTaskRequestDto(courierEvent.getOrderId(), courierEvent.getCourierId(), + courierEvent.getPrice(), courierEvent.getOrderLatitude(), courierEvent.getOrderLongitude()); + deliveryTaskService.createDeliveryTask(deliveryTask); } } } diff --git a/src/main/java/com/podzilla/courier/models/DeliveryTask.java b/src/main/java/com/podzilla/courier/models/DeliveryTask.java index a4a6835..448a848 100644 --- a/src/main/java/com/podzilla/courier/models/DeliveryTask.java +++ b/src/main/java/com/podzilla/courier/models/DeliveryTask.java @@ -4,6 +4,7 @@ import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; +import java.math.BigDecimal; import java.time.LocalDateTime; @Data @@ -13,7 +14,7 @@ public class DeliveryTask { private String id; private String orderId; private String courierId; - private Double price; + private BigDecimal price; private DeliveryStatus status; private Double orderLatitude; private Double orderLongitude; From ce8f09ac1a5fd7f05bb3e58babb0d4109249a96c Mon Sep 17 00:00:00 2001 From: amir-awad Date: Sat, 17 May 2025 17:25:14 +0300 Subject: [PATCH 3/8] fix: removed config folder --- .../courier/config/RabbitMQConfig.java | 67 ------------------- 1 file changed, 67 deletions(-) delete mode 100644 src/main/java/com/podzilla/courier/config/RabbitMQConfig.java diff --git a/src/main/java/com/podzilla/courier/config/RabbitMQConfig.java b/src/main/java/com/podzilla/courier/config/RabbitMQConfig.java deleted file mode 100644 index aee4187..0000000 --- a/src/main/java/com/podzilla/courier/config/RabbitMQConfig.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.podzilla.courier.config; - -import org.springframework.amqp.core.*; -import org.springframework.amqp.rabbit.connection.ConnectionFactory; -import org.springframework.amqp.rabbit.core.RabbitTemplate; -import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class RabbitMQConfig { - - public static final String ORDER_EXCHANGE = "order.exchange"; - public static final String ORDER_SHIPPED_QUEUE = "order.shipped.queue"; - public static final String ORDER_DELIVERED_QUEUE = "order.delivered.queue"; - public static final String ORDER_FAILED_QUEUE = "order.failed.queue"; - public static final String ORDER_SHIPPED_KEY = "order.shipped"; - public static final String ORDER_DELIVERED_KEY = "order.delivered"; - public static final String ORDER_FAILED_KEY = "order.failed"; - - @Bean - public TopicExchange orderExchange() { - return new TopicExchange(ORDER_EXCHANGE); - } - - @Bean - public Queue orderShippedQueue() { - return new Queue(ORDER_SHIPPED_QUEUE, true); - } - - @Bean - public Queue orderDeliveredQueue() { - return new Queue(ORDER_DELIVERED_QUEUE, true); - } - - @Bean - public Queue orderFailedQueue() { - return new Queue(ORDER_FAILED_QUEUE, true); - } - - @Bean - public Binding orderShippedBinding(Queue orderShippedQueue, TopicExchange orderExchange) { - return BindingBuilder.bind(orderShippedQueue).to(orderExchange).with(ORDER_SHIPPED_KEY); - } - - @Bean - public Binding orderDeliveredBinding(Queue orderDeliveredQueue, TopicExchange orderExchange) { - return BindingBuilder.bind(orderDeliveredQueue).to(orderExchange).with(ORDER_DELIVERED_KEY); - } - - @Bean - public Binding orderFailedBinding(Queue orderFailedQueue, TopicExchange orderExchange) { - return BindingBuilder.bind(orderFailedQueue).to(orderExchange).with(ORDER_FAILED_KEY); - } - - @Bean - public Jackson2JsonMessageConverter jsonMessageConverter() { - return new Jackson2JsonMessageConverter(); - } - - @Bean - public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, Jackson2JsonMessageConverter jsonMessageConverter) { - RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); - rabbitTemplate.setMessageConverter(jsonMessageConverter); - return rabbitTemplate; - } -} \ No newline at end of file From 4afa8ec30d9a73efefdf5270fd842720bdd678d6 Mon Sep 17 00:00:00 2001 From: omar mohammed Date: Sat, 17 May 2025 17:34:30 +0300 Subject: [PATCH 4/8] update files to pass check-style --- .../dtos/delivery_tasks/DeliveryTaskResponseDto.java | 5 +++-- .../dtos/delivery_tasks/LocationUpdateDto.java | 2 +- .../SubmitCourierRatingRequestDto.java | 3 ++- .../UpdateDeliveryStatusRequestDto.java | 2 +- .../events/CourierAssignmentEventConsumer.java | 12 ++++++------ .../services/delivery_task/DeliveryTaskService.java | 8 +++++--- 6 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/DeliveryTaskResponseDto.java b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/DeliveryTaskResponseDto.java index 7ee8699..b374345 100644 --- a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/DeliveryTaskResponseDto.java +++ b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/DeliveryTaskResponseDto.java @@ -5,7 +5,8 @@ import java.math.BigDecimal; -public record DeliveryTaskResponseDto(String id, String orderId, String courierId, BigDecimal price, DeliveryStatus status, +public record DeliveryTaskResponseDto(String id, String orderId, String courierId, BigDecimal price, + DeliveryStatus status, Double orderLatitude, Double orderLongitude, Double courierLatitude, Double courierLongitude) { -} \ No newline at end of file +} diff --git a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/LocationUpdateDto.java b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/LocationUpdateDto.java index 4b07a0a..5b88697 100644 --- a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/LocationUpdateDto.java +++ b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/LocationUpdateDto.java @@ -12,4 +12,4 @@ public class LocationUpdateDto { @NotNull(message = "Longitude is required") private Double longitude; -} \ No newline at end of file +} diff --git a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/SubmitCourierRatingRequestDto.java b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/SubmitCourierRatingRequestDto.java index 3c0b118..76e40ea 100644 --- a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/SubmitCourierRatingRequestDto.java +++ b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/SubmitCourierRatingRequestDto.java @@ -9,8 +9,9 @@ @Getter @AllArgsConstructor public class SubmitCourierRatingRequestDto { + @SuppressWarnings("checkstyle:MagicNumber") @NotNull(message = "Rating is required") @Min(value = 1, message = "Rating must be at least 1") @Max(value = 5, message = "Rating must be at most 5") private Double rating; -} \ No newline at end of file +} diff --git a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/UpdateDeliveryStatusRequestDto.java b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/UpdateDeliveryStatusRequestDto.java index 474c1ab..f72d370 100644 --- a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/UpdateDeliveryStatusRequestDto.java +++ b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/UpdateDeliveryStatusRequestDto.java @@ -10,4 +10,4 @@ public class UpdateDeliveryStatusRequestDto { @NotNull(message = "Status is required") private DeliveryStatus status; -} \ No newline at end of file +} diff --git a/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java b/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java index 9336f21..4c7bf59 100644 --- a/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java +++ b/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java @@ -10,17 +10,17 @@ @Component public class CourierAssignmentEventConsumer { - final DeliveryTaskService deliveryTaskService; + private final DeliveryTaskService deliveryTaskService; - public CourierAssignmentEventConsumer(DeliveryTaskService deliveryTaskService) { + public CourierAssignmentEventConsumer(final DeliveryTaskService deliveryTaskService) { this.deliveryTaskService = deliveryTaskService; } @RabbitListener(queues = EventsConstants.COURIER_ORDER_EVENT_QUEUE) - public void handleEvent(BaseEvent event) { - if(event instanceof OrderAssignedToCourierEvent) { - OrderAssignedToCourierEvent courierEvent = (OrderAssignedToCourierEvent) event; - CreateDeliveryTaskRequestDto deliveryTask = new CreateDeliveryTaskRequestDto(courierEvent.getOrderId(), courierEvent.getCourierId(), + public void handleEvent(final BaseEvent event) { + if (event instanceof OrderAssignedToCourierEvent courierEvent) { + CreateDeliveryTaskRequestDto deliveryTask = new CreateDeliveryTaskRequestDto(courierEvent.getOrderId(), + courierEvent.getCourierId(), courierEvent.getPrice(), courierEvent.getOrderLatitude(), courierEvent.getOrderLongitude()); deliveryTaskService.createDeliveryTask(deliveryTask); } 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 c117ab4..af246cd 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 @@ -216,9 +216,11 @@ public Optional confirmDelivery(final String id, final String confirmati } ConfirmationType confirmationType = task.getConfirmationType(); - DeliveryConfirmationStrategy strategy = confirmationType.equals(ConfirmationType.OTP)? new OtpConfirmationStrategy(eventPublisher): - confirmationType.equals(ConfirmationType.QR_CODE)? new QrCodeConfirmationStrategy(eventPublisher): - confirmationType.equals(ConfirmationType.SIGNATURE)? new SignatureConfirmationStrategy(eventPublisher): null; + DeliveryConfirmationStrategy strategy = confirmationType.equals(ConfirmationType.OTP) + ? new OtpConfirmationStrategy(eventPublisher) + : confirmationType.equals(ConfirmationType.QR_CODE) + ? new QrCodeConfirmationStrategy(eventPublisher) : confirmationType.equals(ConfirmationType.SIGNATURE) + ? new SignatureConfirmationStrategy(eventPublisher) : null; if (strategy == null) { LOGGER.error("No confirmation strategy found for type: {}", confirmationType); return Optional.of("Invalid confirmation type"); From 740ab31eedfe6449235b5f7629963276b939c080 Mon Sep 17 00:00:00 2001 From: omar mohammed Date: Sat, 17 May 2025 17:45:11 +0300 Subject: [PATCH 5/8] update files to pass check-style --- src/main/java/com/podzilla/courier/CourierApplication.java | 6 +++--- .../courier/dtos/couriers/CreateCourierRequestDto.java | 1 - .../courier/dtos/couriers/UpdateCourierRequestDto.java | 2 ++ .../dtos/delivery_tasks/DeliveryTaskResponseDto.java | 5 ++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/podzilla/courier/CourierApplication.java b/src/main/java/com/podzilla/courier/CourierApplication.java index 8ed5d3f..cbfbe48 100644 --- a/src/main/java/com/podzilla/courier/CourierApplication.java +++ b/src/main/java/com/podzilla/courier/CourierApplication.java @@ -10,8 +10,8 @@ @ComponentScan(basePackages = {"com.podzilla.courier", "com.podzilla.mq"}) public class CourierApplication { - public static void main(String[] args) { - SpringApplication.run(CourierApplication.class, args); - } + public static void main(final String[] args) { + SpringApplication.run(CourierApplication.class, args); + } } diff --git a/src/main/java/com/podzilla/courier/dtos/couriers/CreateCourierRequestDto.java b/src/main/java/com/podzilla/courier/dtos/couriers/CreateCourierRequestDto.java index a378171..0a5fc54 100644 --- a/src/main/java/com/podzilla/courier/dtos/couriers/CreateCourierRequestDto.java +++ b/src/main/java/com/podzilla/courier/dtos/couriers/CreateCourierRequestDto.java @@ -3,7 +3,6 @@ import jakarta.validation.constraints.NotNull; import lombok.AllArgsConstructor; import lombok.Getter; -import lombok.NonNull; @Getter @AllArgsConstructor diff --git a/src/main/java/com/podzilla/courier/dtos/couriers/UpdateCourierRequestDto.java b/src/main/java/com/podzilla/courier/dtos/couriers/UpdateCourierRequestDto.java index 5ff5383..977365c 100644 --- a/src/main/java/com/podzilla/courier/dtos/couriers/UpdateCourierRequestDto.java +++ b/src/main/java/com/podzilla/courier/dtos/couriers/UpdateCourierRequestDto.java @@ -3,8 +3,10 @@ import com.podzilla.courier.models.CourierStatus; import jakarta.validation.constraints.NotNull; import lombok.Getter; +import lombok.Setter; @Getter +@Setter public class UpdateCourierRequestDto { @NotNull(message = "ID is required") private String id; diff --git a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/DeliveryTaskResponseDto.java b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/DeliveryTaskResponseDto.java index b374345..25c6f16 100644 --- a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/DeliveryTaskResponseDto.java +++ b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/DeliveryTaskResponseDto.java @@ -6,7 +6,6 @@ import java.math.BigDecimal; public record DeliveryTaskResponseDto(String id, String orderId, String courierId, BigDecimal price, - DeliveryStatus status, - Double orderLatitude, Double orderLongitude, Double courierLatitude, - Double courierLongitude) { + DeliveryStatus status, Double orderLatitude, Double orderLongitude, + Double courierLatitude, Double courierLongitude) { } From 9a027a73ee6e914a50b8b58c4373691c492390d5 Mon Sep 17 00:00:00 2001 From: omar mohammed Date: Sat, 17 May 2025 17:55:20 +0300 Subject: [PATCH 6/8] update files to pass check-style --- .../events/CourierAssignmentEventConsumer.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java b/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java index 4c7bf59..e4f2a6b 100644 --- a/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java +++ b/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java @@ -16,12 +16,18 @@ public CourierAssignmentEventConsumer(final DeliveryTaskService deliveryTaskServ this.deliveryTaskService = deliveryTaskService; } + @RabbitListener(queues = EventsConstants.COURIER_ORDER_EVENT_QUEUE) @RabbitListener(queues = EventsConstants.COURIER_ORDER_EVENT_QUEUE) public void handleEvent(final BaseEvent event) { - if (event instanceof OrderAssignedToCourierEvent courierEvent) { - CreateDeliveryTaskRequestDto deliveryTask = new CreateDeliveryTaskRequestDto(courierEvent.getOrderId(), + if (event instanceof OrderAssignedToCourierEvent) { + OrderAssignedToCourierEvent courierEvent = (OrderAssignedToCourierEvent) event; + CreateDeliveryTaskRequestDto deliveryTask = new CreateDeliveryTaskRequestDto( + courierEvent.getOrderId(), courierEvent.getCourierId(), - courierEvent.getPrice(), courierEvent.getOrderLatitude(), courierEvent.getOrderLongitude()); + courierEvent.getPrice(), + courierEvent.getOrderLatitude(), + courierEvent.getOrderLongitude() + ); deliveryTaskService.createDeliveryTask(deliveryTask); } } From d5c7963f7a137809b22a598f4bbef6cbd44c6958 Mon Sep 17 00:00:00 2001 From: amir-awad Date: Sat, 17 May 2025 19:14:33 +0300 Subject: [PATCH 7/8] fix: fix bug for otp & qr confirmation strategies --- pom.xml | 2 +- .../controllers/DeliveryTaskController.java | 13 +++------- .../delivery_tasks/ConfirmDeliveryDto.java | 12 +++++++++ .../CreateDeliveryTaskRequestDto.java | 10 ++++++-- .../DeliveryTaskResponseDto.java | 5 ++-- .../SubmitCourierRatingRequestDto.java | 4 ++- .../SubmitCourierRatingResponseDto.java | 4 ++- .../CourierAssignmentEventConsumer.java | 3 ++- .../courier/mappers/DeliveryTaskMapper.java | 8 +++--- .../courier/models/ConfirmationType.java | 7 ------ .../podzilla/courier/models/DeliveryTask.java | 5 ++-- .../delivery_task/DeliveryTaskService.java | 8 +++--- .../OtpConfirmationStrategy.java | 3 +-- .../QrCodeConfirmationStrategy.java | 3 +-- .../SignatureConfirmationStrategy.java | 3 +-- .../poll_command/StartPollingCommand.java | 25 ++++++++++++++++++- 16 files changed, 75 insertions(+), 40 deletions(-) create mode 100644 src/main/java/com/podzilla/courier/dtos/delivery_tasks/ConfirmDeliveryDto.java delete mode 100644 src/main/java/com/podzilla/courier/models/ConfirmationType.java diff --git a/pom.xml b/pom.xml index 9c12dc2..e54883c 100644 --- a/pom.xml +++ b/pom.xml @@ -88,7 +88,7 @@ com.github.Podzilla podzilla-utils-lib - v1.1.7 + v1.1.8 diff --git a/src/main/java/com/podzilla/courier/controllers/DeliveryTaskController.java b/src/main/java/com/podzilla/courier/controllers/DeliveryTaskController.java index 56dcec7..837ae0c 100644 --- a/src/main/java/com/podzilla/courier/controllers/DeliveryTaskController.java +++ b/src/main/java/com/podzilla/courier/controllers/DeliveryTaskController.java @@ -1,13 +1,6 @@ package com.podzilla.courier.controllers; -import com.podzilla.courier.dtos.delivery_tasks.SubmitCourierRatingResponseDto; -import com.podzilla.courier.dtos.delivery_tasks.SubmitCourierRatingRequestDto; -import com.podzilla.courier.dtos.delivery_tasks.CancelDeliveryTaskRequestDto; -import com.podzilla.courier.dtos.delivery_tasks.LocationUpdateDto; -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.UpdateDeliveryStatusRequestDto; +import com.podzilla.courier.dtos.delivery_tasks.*; import com.podzilla.courier.models.DeliveryStatus; import com.podzilla.courier.services.delivery_task.DeliveryTaskService; import io.swagger.v3.oas.annotations.Operation; @@ -183,9 +176,9 @@ public ResponseEntity confirmDelivery( @Parameter(description = "ID of the delivery task") @PathVariable final String id, @RequestBody(description = "Confirmation input (e.g., OTP, QR code, or signature)") - @org.springframework.web.bind.annotation.RequestBody final String confirmationInput) { + @org.springframework.web.bind.annotation.RequestBody final ConfirmDeliveryDto confirmDeliveryDto) { LOGGER.info("Received request to confirm delivery for task ID: {}", id); - return deliveryTaskService.confirmDelivery(id, confirmationInput) + return deliveryTaskService.confirmDelivery(id, confirmDeliveryDto.getConfirmationInput()) .map(ResponseEntity::ok) .orElse(ResponseEntity.notFound().build()); } diff --git a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/ConfirmDeliveryDto.java b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/ConfirmDeliveryDto.java new file mode 100644 index 0000000..65b9560 --- /dev/null +++ b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/ConfirmDeliveryDto.java @@ -0,0 +1,12 @@ +package com.podzilla.courier.dtos.delivery_tasks; + +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class ConfirmDeliveryDto { + @NotNull(message = "Confirmation input in required") + private String confirmationInput; +} diff --git a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/CreateDeliveryTaskRequestDto.java b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/CreateDeliveryTaskRequestDto.java index 1c89841..be29a6e 100644 --- a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/CreateDeliveryTaskRequestDto.java +++ b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/CreateDeliveryTaskRequestDto.java @@ -1,5 +1,6 @@ package com.podzilla.courier.dtos.delivery_tasks; +import com.podzilla.mq.events.OrderAssignedToCourierEvent.ConfirmationType; import jakarta.validation.constraints.NotNull; import lombok.AllArgsConstructor; import lombok.Getter; @@ -15,12 +16,17 @@ public class CreateDeliveryTaskRequestDto { @NotNull(message = "Courier ID is required") private String courierId; - @NotNull(message = "Price is required") - private BigDecimal price; + @NotNull(message = "Total amount is required") + private BigDecimal totalAmount; @NotNull(message = "Order latitude is required") private double orderLatitude; @NotNull(message = "Order longitude is required") private double orderLongitude; + + @NotNull(message = "Confirmation type is required") + private ConfirmationType confirmationType; + + private String signature; } diff --git a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/DeliveryTaskResponseDto.java b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/DeliveryTaskResponseDto.java index 7ee8699..6486095 100644 --- a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/DeliveryTaskResponseDto.java +++ b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/DeliveryTaskResponseDto.java @@ -2,10 +2,11 @@ import com.podzilla.courier.models.DeliveryStatus; +import com.podzilla.mq.events.OrderAssignedToCourierEvent.ConfirmationType; import java.math.BigDecimal; -public record DeliveryTaskResponseDto(String id, String orderId, String courierId, BigDecimal price, DeliveryStatus status, +public record DeliveryTaskResponseDto(String id, String orderId, String courierId, BigDecimal totalAmount, DeliveryStatus status, Double orderLatitude, Double orderLongitude, Double courierLatitude, - Double courierLongitude) { + Double courierLongitude, ConfirmationType confirmationType) { } \ No newline at end of file diff --git a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/SubmitCourierRatingRequestDto.java b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/SubmitCourierRatingRequestDto.java index 3c0b118..0382c92 100644 --- a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/SubmitCourierRatingRequestDto.java +++ b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/SubmitCourierRatingRequestDto.java @@ -6,11 +6,13 @@ import lombok.AllArgsConstructor; import lombok.Getter; +import java.math.BigDecimal; + @Getter @AllArgsConstructor public class SubmitCourierRatingRequestDto { @NotNull(message = "Rating is required") @Min(value = 1, message = "Rating must be at least 1") @Max(value = 5, message = "Rating must be at most 5") - private Double rating; + private BigDecimal rating; } \ No newline at end of file diff --git a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/SubmitCourierRatingResponseDto.java b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/SubmitCourierRatingResponseDto.java index f6ea97f..dfddda9 100644 --- a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/SubmitCourierRatingResponseDto.java +++ b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/SubmitCourierRatingResponseDto.java @@ -1,4 +1,6 @@ package com.podzilla.courier.dtos.delivery_tasks; -public record SubmitCourierRatingResponseDto(String id, String orderId, String courierId, Double courierRating) { +import java.math.BigDecimal; + +public record SubmitCourierRatingResponseDto(String id, String orderId, String courierId, BigDecimal courierRating) { } diff --git a/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java b/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java index 9336f21..e9cf31a 100644 --- a/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java +++ b/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java @@ -21,7 +21,8 @@ public void handleEvent(BaseEvent event) { if(event instanceof OrderAssignedToCourierEvent) { OrderAssignedToCourierEvent courierEvent = (OrderAssignedToCourierEvent) event; CreateDeliveryTaskRequestDto deliveryTask = new CreateDeliveryTaskRequestDto(courierEvent.getOrderId(), courierEvent.getCourierId(), - courierEvent.getPrice(), courierEvent.getOrderLatitude(), courierEvent.getOrderLongitude()); + courierEvent.getTotalAmount(), courierEvent.getOrderLatitude(), + courierEvent.getOrderLongitude(), courierEvent.getConfirmationType(), courierEvent.getSignature()); deliveryTaskService.createDeliveryTask(deliveryTask); } } diff --git a/src/main/java/com/podzilla/courier/mappers/DeliveryTaskMapper.java b/src/main/java/com/podzilla/courier/mappers/DeliveryTaskMapper.java index 48a6f9c..22a340d 100644 --- a/src/main/java/com/podzilla/courier/mappers/DeliveryTaskMapper.java +++ b/src/main/java/com/podzilla/courier/mappers/DeliveryTaskMapper.java @@ -12,9 +12,10 @@ public static DeliveryTask toEntity(CreateDeliveryTaskRequestDto dto) { DeliveryTask task = new DeliveryTask(); task.setOrderId(dto.getOrderId()); task.setCourierId(dto.getCourierId()); - task.setPrice(dto.getPrice()); + task.setTotalAmount(dto.getTotalAmount()); task.setOrderLatitude(dto.getOrderLatitude()); task.setOrderLongitude(dto.getOrderLongitude()); + task.setConfirmationType(dto.getConfirmationType()); return task; } @@ -23,12 +24,13 @@ public static DeliveryTaskResponseDto toCreateResponseDto(DeliveryTask task) { task.getId(), task.getOrderId(), task.getCourierId(), - task.getPrice(), + task.getTotalAmount(), task.getStatus(), task.getOrderLatitude(), task.getOrderLongitude(), task.getCourierLatitude(), - task.getCourierLongitude() + task.getCourierLongitude(), + task.getConfirmationType() ); } diff --git a/src/main/java/com/podzilla/courier/models/ConfirmationType.java b/src/main/java/com/podzilla/courier/models/ConfirmationType.java deleted file mode 100644 index 1fe3be3..0000000 --- a/src/main/java/com/podzilla/courier/models/ConfirmationType.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.podzilla.courier.models; - -public enum ConfirmationType { - OTP, - QR_CODE, - SIGNATURE -} diff --git a/src/main/java/com/podzilla/courier/models/DeliveryTask.java b/src/main/java/com/podzilla/courier/models/DeliveryTask.java index 448a848..831b3c2 100644 --- a/src/main/java/com/podzilla/courier/models/DeliveryTask.java +++ b/src/main/java/com/podzilla/courier/models/DeliveryTask.java @@ -1,5 +1,6 @@ package com.podzilla.courier.models; +import com.podzilla.mq.events.OrderAssignedToCourierEvent.ConfirmationType; import lombok.Data; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @@ -14,7 +15,7 @@ public class DeliveryTask { private String id; private String orderId; private String courierId; - private BigDecimal price; + private BigDecimal totalAmount; private DeliveryStatus status; private Double orderLatitude; private Double orderLongitude; @@ -24,7 +25,7 @@ public class DeliveryTask { private String qrCode; private String signature; private String cancellationReason; - private Double courierRating; + private BigDecimal courierRating; private LocalDateTime ratingTimestamp; private LocalDateTime createdAt; private LocalDateTime updatedAt; 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 c117ab4..85e65b3 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 @@ -5,7 +5,6 @@ import com.podzilla.courier.dtos.delivery_tasks.DeliveryTaskResponseDto; import com.podzilla.courier.dtos.delivery_tasks.SubmitCourierRatingResponseDto; 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; @@ -17,6 +16,7 @@ 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.OrderAssignedToCourierEvent.ConfirmationType; import com.podzilla.mq.events.OrderCancelledEvent; import com.podzilla.mq.events.OrderOutForDeliveryEvent; import org.slf4j.Logger; @@ -25,6 +25,7 @@ import org.springframework.data.util.Pair; import org.springframework.stereotype.Service; +import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.List; import java.util.Optional; @@ -119,7 +120,8 @@ public Optional updateDeliveryTaskStatus(final String i task.getCourierId()); Command startPollingCommand = new StartPollingCommand( eventPublisher, - event + event, + deliveryTaskRepository ); startPollingCommand.execute(); @@ -231,7 +233,7 @@ public Optional confirmDelivery(final String id, final String confirmati return result; } - public SubmitCourierRatingResponseDto submitCourierRating(final String id, final Double rating) { + public SubmitCourierRatingResponseDto submitCourierRating(final String id, final BigDecimal rating) { LOGGER.info("Submitting courier rating for delivery task with ID: {}", id); Optional deliveryTask = deliveryTaskRepository.findById(id); if (deliveryTask.isPresent()) { 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 5c23693..f97383c 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 @@ -9,7 +9,6 @@ import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; -import java.math.BigDecimal; import java.util.Optional; @Component @@ -35,7 +34,7 @@ public Optional confirmDelivery(final DeliveryTask task, final String co OrderDeliveredEvent event = new OrderDeliveredEvent( task.getOrderId(), task.getCourierId(), - task.getCourierRating() != null ? BigDecimal.valueOf(task.getCourierRating()) : null + task.getCourierRating() ); StopPollingCommand stopPollingCommand = new StopPollingCommand(eventPublisher, event); stopPollingCommand.execute(); 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 8b3a135..5cce106 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 @@ -9,7 +9,6 @@ import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; -import java.math.BigDecimal; import java.util.Optional; @Component @@ -38,7 +37,7 @@ public Optional confirmDelivery(final DeliveryTask task, final String co OrderDeliveredEvent event = new OrderDeliveredEvent( task.getOrderId(), task.getCourierId(), - task.getCourierRating() != null ? BigDecimal.valueOf(task.getCourierRating()) : null + task.getCourierRating() ); StopPollingCommand stopPollingCommand = new StopPollingCommand(eventPublisher, event); stopPollingCommand.execute(); 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 330fcdc..e6702a7 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 @@ -9,7 +9,6 @@ import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; -import java.math.BigDecimal; import java.util.Optional; @Component @@ -38,7 +37,7 @@ public Optional confirmDelivery(final DeliveryTask task, final String co OrderDeliveredEvent event = new OrderDeliveredEvent( task.getOrderId(), task.getCourierId(), - task.getCourierRating() != null ? BigDecimal.valueOf(task.getCourierRating()) : null + task.getCourierRating() ); StopPollingCommand stopPollingCommand = new StopPollingCommand(eventPublisher, event); stopPollingCommand.execute(); diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/poll_command/StartPollingCommand.java b/src/main/java/com/podzilla/courier/services/delivery_task/poll_command/StartPollingCommand.java index a3835c0..f8221f7 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/poll_command/StartPollingCommand.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/poll_command/StartPollingCommand.java @@ -1,5 +1,7 @@ package com.podzilla.courier.services.delivery_task.poll_command; +import com.podzilla.courier.models.DeliveryTask; +import com.podzilla.courier.repositories.delivery_task.IDeliveryTaskRepository; import com.podzilla.mq.EventPublisher; import com.podzilla.mq.EventsConstants; import com.podzilla.mq.events.OrderOutForDeliveryEvent; @@ -8,14 +10,35 @@ public class StartPollingCommand implements Command { private final EventPublisher eventPublisher; private final OrderOutForDeliveryEvent event; + private final IDeliveryTaskRepository deliveryTaskRepository; - public StartPollingCommand(final EventPublisher eventPublisher, final OrderOutForDeliveryEvent event) { + public StartPollingCommand(final EventPublisher eventPublisher, final OrderOutForDeliveryEvent event, + final IDeliveryTaskRepository deliveryTaskRepository) { this.eventPublisher = eventPublisher; this.event = event; + this.deliveryTaskRepository = deliveryTaskRepository; } @Override public void execute() { + DeliveryTask deliveryTask = deliveryTaskRepository.findByOrderId(event.getOrderId()) + .stream() + .findFirst() + .orElseThrow(() -> new IllegalStateException("No DeliveryTask found for orderId: " + event.getOrderId())); + + switch (deliveryTask.getConfirmationType()) { + case OTP: + String taskId = deliveryTask.getId(); + String otp = taskId.length() >= 4 ? taskId.substring(taskId.length() - 4) : taskId; + deliveryTask.setOtp(otp); + deliveryTaskRepository.save(deliveryTask); + break; + case QR_CODE: + deliveryTask.setQrCode("qr-code " + deliveryTask.getId()); + deliveryTaskRepository.save(deliveryTask); + break; + } + // publish out_for_delivery event so that the order service start tracking courier location eventPublisher.publishEvent(EventsConstants.ORDER_OUT_FOR_DELIVERY, event); } From 5a26c2b8a7268f3d00ca2721d7e20742a3781902 Mon Sep 17 00:00:00 2001 From: omar mohammed Date: Sat, 17 May 2025 20:17:25 +0300 Subject: [PATCH 8/8] update files to pass check-style --- .../courier/controllers/DeliveryTaskController.java | 10 +++++++++- .../dtos/delivery_tasks/DeliveryTaskResponseDto.java | 7 ++++--- .../courier/events/CourierAssignmentEventConsumer.java | 8 ++++---- .../podzilla/courier/mappers/DeliveryTaskMapper.java | 10 +++++----- .../poll_command/StartPollingCommand.java | 4 +++- 5 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/podzilla/courier/controllers/DeliveryTaskController.java b/src/main/java/com/podzilla/courier/controllers/DeliveryTaskController.java index 837ae0c..dba0b29 100644 --- a/src/main/java/com/podzilla/courier/controllers/DeliveryTaskController.java +++ b/src/main/java/com/podzilla/courier/controllers/DeliveryTaskController.java @@ -1,6 +1,14 @@ package com.podzilla.courier.controllers; -import com.podzilla.courier.dtos.delivery_tasks.*; +import com.podzilla.courier.dtos.delivery_tasks.CreateDeliveryTaskRequestDto; +import com.podzilla.courier.dtos.delivery_tasks.DeliveryTaskResponseDto; +import com.podzilla.courier.dtos.delivery_tasks.UpdateDeliveryStatusRequestDto; +import com.podzilla.courier.dtos.delivery_tasks.SubmitCourierRatingResponseDto; +import com.podzilla.courier.dtos.delivery_tasks.CancelDeliveryTaskRequestDto; +import com.podzilla.courier.dtos.delivery_tasks.CancelDeliveryTaskResponseDto; +import com.podzilla.courier.dtos.delivery_tasks.ConfirmDeliveryDto; +import com.podzilla.courier.dtos.delivery_tasks.LocationUpdateDto; +import com.podzilla.courier.dtos.delivery_tasks.SubmitCourierRatingRequestDto; import com.podzilla.courier.models.DeliveryStatus; import com.podzilla.courier.services.delivery_task.DeliveryTaskService; import io.swagger.v3.oas.annotations.Operation; diff --git a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/DeliveryTaskResponseDto.java b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/DeliveryTaskResponseDto.java index fd5b75a..ecfbeac 100644 --- a/src/main/java/com/podzilla/courier/dtos/delivery_tasks/DeliveryTaskResponseDto.java +++ b/src/main/java/com/podzilla/courier/dtos/delivery_tasks/DeliveryTaskResponseDto.java @@ -6,7 +6,8 @@ import java.math.BigDecimal; -public record DeliveryTaskResponseDto(String id, String orderId, String courierId, BigDecimal totalAmount, DeliveryStatus status, - Double orderLatitude, Double orderLongitude, Double courierLatitude, - Double courierLongitude, ConfirmationType confirmationType) { +public record DeliveryTaskResponseDto(String id, String orderId, String courierId, BigDecimal totalAmount, + DeliveryStatus status, Double orderLatitude, Double orderLongitude, + Double courierLatitude, Double courierLongitude, + ConfirmationType confirmationType) { } diff --git a/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java b/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java index afb42a6..1fc8444 100644 --- a/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java +++ b/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java @@ -10,15 +10,15 @@ @Component public class CourierAssignmentEventConsumer { - final DeliveryTaskService deliveryTaskService; + private final DeliveryTaskService deliveryTaskService; - public CourierAssignmentEventConsumer(DeliveryTaskService deliveryTaskService) { + public CourierAssignmentEventConsumer(final DeliveryTaskService deliveryTaskService) { this.deliveryTaskService = deliveryTaskService; } @RabbitListener(queues = EventsConstants.COURIER_ORDER_EVENT_QUEUE) - public void handleEvent(BaseEvent event) { - if(event instanceof OrderAssignedToCourierEvent) { + public void handleEvent(final BaseEvent event) { + if (event instanceof OrderAssignedToCourierEvent) { OrderAssignedToCourierEvent courierEvent = (OrderAssignedToCourierEvent) event; CreateDeliveryTaskRequestDto deliveryTask = new CreateDeliveryTaskRequestDto( courierEvent.getOrderId(), diff --git a/src/main/java/com/podzilla/courier/mappers/DeliveryTaskMapper.java b/src/main/java/com/podzilla/courier/mappers/DeliveryTaskMapper.java index 22a340d..8eac27f 100644 --- a/src/main/java/com/podzilla/courier/mappers/DeliveryTaskMapper.java +++ b/src/main/java/com/podzilla/courier/mappers/DeliveryTaskMapper.java @@ -8,7 +8,7 @@ public class DeliveryTaskMapper { - public static DeliveryTask toEntity(CreateDeliveryTaskRequestDto dto) { + public static DeliveryTask toEntity(final CreateDeliveryTaskRequestDto dto) { DeliveryTask task = new DeliveryTask(); task.setOrderId(dto.getOrderId()); task.setCourierId(dto.getCourierId()); @@ -19,7 +19,7 @@ public static DeliveryTask toEntity(CreateDeliveryTaskRequestDto dto) { return task; } - public static DeliveryTaskResponseDto toCreateResponseDto(DeliveryTask task) { + public static DeliveryTaskResponseDto toCreateResponseDto(final DeliveryTask task) { return new DeliveryTaskResponseDto( task.getId(), task.getOrderId(), @@ -34,7 +34,7 @@ public static DeliveryTaskResponseDto toCreateResponseDto(DeliveryTask task) { ); } - public static CancelDeliveryTaskResponseDto toCancelResponseDto(DeliveryTask task) { + public static CancelDeliveryTaskResponseDto toCancelResponseDto(final DeliveryTask task) { return new CancelDeliveryTaskResponseDto( task.getId(), task.getOrderId(), @@ -43,7 +43,7 @@ public static CancelDeliveryTaskResponseDto toCancelResponseDto(DeliveryTask tas ); } - public static SubmitCourierRatingResponseDto toSubmitCourierRatingResponseDto(DeliveryTask task) { + public static SubmitCourierRatingResponseDto toSubmitCourierRatingResponseDto(final DeliveryTask task) { return new SubmitCourierRatingResponseDto( task.getId(), task.getOrderId(), @@ -51,4 +51,4 @@ public static SubmitCourierRatingResponseDto toSubmitCourierRatingResponseDto(De task.getCourierRating() ); } -} \ No newline at end of file +} diff --git a/src/main/java/com/podzilla/courier/services/delivery_task/poll_command/StartPollingCommand.java b/src/main/java/com/podzilla/courier/services/delivery_task/poll_command/StartPollingCommand.java index f8221f7..900bd5e 100644 --- a/src/main/java/com/podzilla/courier/services/delivery_task/poll_command/StartPollingCommand.java +++ b/src/main/java/com/podzilla/courier/services/delivery_task/poll_command/StartPollingCommand.java @@ -19,12 +19,14 @@ public StartPollingCommand(final EventPublisher eventPublisher, final OrderOutFo this.deliveryTaskRepository = deliveryTaskRepository; } + @SuppressWarnings({"checkstyle:MagicNumber", "checkstyle:MissingSwitchDefault"}) @Override public void execute() { DeliveryTask deliveryTask = deliveryTaskRepository.findByOrderId(event.getOrderId()) .stream() .findFirst() - .orElseThrow(() -> new IllegalStateException("No DeliveryTask found for orderId: " + event.getOrderId())); + .orElseThrow(() -> new IllegalStateException("No DeliveryTask found for orderId: " + + event.getOrderId())); switch (deliveryTask.getConfirmationType()) { case OTP: