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..cbfbe48 100644
--- a/src/main/java/com/podzilla/courier/CourierApplication.java
+++ b/src/main/java/com/podzilla/courier/CourierApplication.java
@@ -2,14 +2,16 @@
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) {
- 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/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
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/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/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..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
@@ -3,7 +3,9 @@
import com.podzilla.courier.models.DeliveryStatus;
-public record DeliveryTaskResponseDto(String id, String orderId, String courierId, Double price, DeliveryStatus status,
- Double orderLatitude, Double orderLongitude, Double courierLatitude,
- Double courierLongitude) {
-}
\ No newline at end of file
+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) {
+}
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
new file mode 100644
index 0000000..e4f2a6b
--- /dev/null
+++ b/src/main/java/com/podzilla/courier/events/CourierAssignmentEventConsumer.java
@@ -0,0 +1,34 @@
+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;
+import com.podzilla.mq.events.OrderAssignedToCourierEvent;
+import org.springframework.amqp.rabbit.annotation.RabbitListener;
+import org.springframework.stereotype.Component;
+
+@Component
+public class CourierAssignmentEventConsumer {
+ private final DeliveryTaskService deliveryTaskService;
+
+ public CourierAssignmentEventConsumer(final DeliveryTaskService deliveryTaskService) {
+ 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) {
+ OrderAssignedToCourierEvent courierEvent = (OrderAssignedToCourierEvent) event;
+ 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;
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..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
@@ -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,11 @@ 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");