Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
<dependency>
<groupId>com.github.Podzilla</groupId>
<artifactId>podzilla-utils-lib</artifactId>
<version>v1.1.6</version>
<version>v1.1.7</version>
</dependency>
</dependencies>

Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/podzilla/courier/CourierApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
67 changes: 0 additions & 67 deletions src/main/java/com/podzilla/courier/config/RabbitMQConfig.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -115,28 +115,28 @@ public ResponseEntity<List<DeliveryTaskResponseDto>> 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<Pair<Double, Double>> 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<DeliveryTaskResponseDto> 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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NonNull;

@Getter
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.math.BigDecimal;

@Getter
@AllArgsConstructor
public class CreateDeliveryTaskRequestDto {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
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) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ public class LocationUpdateDto {

@NotNull(message = "Longitude is required")
private Double longitude;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
public class UpdateDeliveryStatusRequestDto {
@NotNull(message = "Status is required")
private DeliveryStatus status;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/podzilla/courier/models/DeliveryTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -34,17 +36,14 @@ public class DeliveryTaskService {

private final IDeliveryTaskRepository deliveryTaskRepository;
private final EventPublisher eventPublisher;
private final Map<ConfirmationType, DeliveryConfirmationStrategy> 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<ConfirmationType, DeliveryConfirmationStrategy> confirmationStrategies) {
final EventPublisher eventPublisher) {
this.deliveryTaskRepository = deliveryTaskRepository;
this.eventPublisher = eventPublisher;
this.confirmationStrategies = confirmationStrategies;
}

public DeliveryTaskResponseDto createDeliveryTask(final CreateDeliveryTaskRequestDto deliveryTaskRequestDto) {
Expand Down Expand Up @@ -140,32 +139,32 @@ public Optional<DeliveryTaskResponseDto> updateDeliveryTaskStatus(final String i
return Optional.empty();
}

public Pair<Double, Double> getDeliveryTaskLocation(final String id) {
LOGGER.info("Fetching location for delivery task with ID: {}", id);
Optional<DeliveryTask> deliveryTask = deliveryTaskRepository.findById(id);
public Pair<Double, Double> getDeliveryTaskLocation(final String orderId) {
LOGGER.info("Fetching location for delivery task with order id: {}", orderId);
Optional<DeliveryTask> 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<DeliveryTask> updatedDeliveryTask = deliveryTaskRepository.findById(id);
LOGGER.info("Updating location for delivery task with order id: {} to ({}, {})", orderId, latitude, longitude);
Optional<DeliveryTask> 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;
}

Expand Down Expand Up @@ -217,7 +216,11 @@ public Optional<String> 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");
Expand Down