Skip to content
Merged
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.7</version>
<version>v1.1.8</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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.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;
Expand Down Expand Up @@ -183,9 +184,9 @@ public ResponseEntity<String> 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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@


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,
public record DeliveryTaskResponseDto(String id, String orderId, String courierId, BigDecimal totalAmount,
DeliveryStatus status, Double orderLatitude, Double orderLongitude,
Double courierLatitude, Double courierLongitude) {
Double courierLatitude, Double courierLongitude,
ConfirmationType confirmationType) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.math.BigDecimal;

@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;
private BigDecimal rating;
}
Original file line number Diff line number Diff line change
@@ -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) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +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) {
OrderAssignedToCourierEvent courierEvent = (OrderAssignedToCourierEvent) event;
CreateDeliveryTaskRequestDto deliveryTask = new CreateDeliveryTaskRequestDto(
courierEvent.getOrderId(),
courierEvent.getCourierId(),
courierEvent.getPrice(),
courierEvent.getTotalAmount(),
courierEvent.getOrderLatitude(),
courierEvent.getOrderLongitude()
);
courierEvent.getOrderLongitude(),
courierEvent.getConfirmationType(),
courierEvent.getSignature());
deliveryTaskService.createDeliveryTask(deliveryTask);
}
}
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/com/podzilla/courier/mappers/DeliveryTaskMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,33 @@

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());
task.setPrice(dto.getPrice());
task.setTotalAmount(dto.getTotalAmount());
task.setOrderLatitude(dto.getOrderLatitude());
task.setOrderLongitude(dto.getOrderLongitude());
task.setConfirmationType(dto.getConfirmationType());
return task;
}

public static DeliveryTaskResponseDto toCreateResponseDto(DeliveryTask task) {
public static DeliveryTaskResponseDto toCreateResponseDto(final DeliveryTask task) {
return new DeliveryTaskResponseDto(
task.getId(),
task.getOrderId(),
task.getCourierId(),
task.getPrice(),
task.getTotalAmount(),
task.getStatus(),
task.getOrderLatitude(),
task.getOrderLongitude(),
task.getCourierLatitude(),
task.getCourierLongitude()
task.getCourierLongitude(),
task.getConfirmationType()
);
}

public static CancelDeliveryTaskResponseDto toCancelResponseDto(DeliveryTask task) {
public static CancelDeliveryTaskResponseDto toCancelResponseDto(final DeliveryTask task) {
return new CancelDeliveryTaskResponseDto(
task.getId(),
task.getOrderId(),
Expand All @@ -41,12 +43,12 @@ 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(),
task.getCourierId(),
task.getCourierRating()
);
}
}
}

This file was deleted.

5 changes: 3 additions & 2 deletions src/main/java/com/podzilla/courier/models/DeliveryTask.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -119,7 +120,8 @@ public Optional<DeliveryTaskResponseDto> updateDeliveryTaskStatus(final String i
task.getCourierId());
Command startPollingCommand = new StartPollingCommand(
eventPublisher,
event
event,
deliveryTaskRepository
);
startPollingCommand.execute();

Expand Down Expand Up @@ -233,7 +235,7 @@ public Optional<String> 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> deliveryTask = deliveryTaskRepository.findById(id);
if (deliveryTask.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Optional;

@Component
Expand All @@ -35,7 +34,7 @@ public Optional<String> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Optional;

@Component
Expand Down Expand Up @@ -38,7 +37,7 @@ public Optional<String> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Optional;

@Component
Expand Down Expand Up @@ -38,7 +37,7 @@ public Optional<String> 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,14 +10,37 @@ 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;
}

@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()));

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);
}
Expand Down