Skip to content

Commit e31aa82

Browse files
committed
feat(order,payment): more usecase validations
1 parent b86fde1 commit e31aa82

File tree

7 files changed

+28
-18
lines changed

7 files changed

+28
-18
lines changed

techchallenge/src/main/java/br/com/grupo63/techchallenge/adapter/in/controller/order/OrderController.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
import br.com.grupo63.techchallenge.adapter.in.controller.AbstractController;
44
import br.com.grupo63.techchallenge.adapter.in.controller.dto.DefaultResponseDTO;
55
import br.com.grupo63.techchallenge.adapter.in.controller.order.dto.CreateOrderRequestDTO;
6-
import br.com.grupo63.techchallenge.core.application.usecase.dto.ClientDTO;
76
import br.com.grupo63.techchallenge.core.application.usecase.dto.OrderDTO;
87
import br.com.grupo63.techchallenge.core.application.usecase.exception.NotFoundException;
98
import br.com.grupo63.techchallenge.core.application.usecase.exception.ValidationException;
109
import br.com.grupo63.techchallenge.core.application.usecase.order.IOrderUseCase;
1110
import io.swagger.v3.oas.annotations.Operation;
1211
import io.swagger.v3.oas.annotations.Parameter;
1312
import io.swagger.v3.oas.annotations.tags.Tag;
14-
import jakarta.validation.Valid;
1513
import lombok.RequiredArgsConstructor;
1614
import org.springframework.http.HttpStatus;
1715
import org.springframework.http.ResponseEntity;
@@ -43,7 +41,7 @@ public ResponseEntity<List<OrderDTO>> listUnfinishedOrders() {
4341
@PostMapping("/avancar-estado")
4442
@ResponseStatus(HttpStatus.OK)
4543
public void advanceOrderStatusFromOrderId(@Parameter(description = "Id do pedido.") @RequestParam Long orderId) throws NotFoundException, ValidationException {
46-
orderUseCase.advanceOrderStatus(orderId);
44+
orderUseCase.advanceStatus(orderId);
4745
}
4846

4947
@Operation(

techchallenge/src/main/java/br/com/grupo63/techchallenge/adapter/in/controller/payment/PaymentController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class PaymentController extends AbstractController {
2929
@PostMapping("/iniciar")
3030
public QRCodeResponseDTO startPayment(
3131
@Parameter(description = "Id do pedido") @RequestParam Long orderId
32-
) throws NotFoundException {
32+
) throws NotFoundException, ValidationException {
3333
String qrData = paymentUseCase.startPayment(orderId);
3434
return new QRCodeResponseDTO(qrData);
3535
}

techchallenge/src/main/java/br/com/grupo63/techchallenge/core/application/usecase/order/IOrderUseCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
public interface IOrderUseCase extends ICRUDUseCase<OrderDTO> {
1212

13-
void advanceOrderStatus(@NotNull Long orderId) throws NotFoundException, ValidationException;
13+
void advanceStatus(@NotNull Long orderId) throws NotFoundException, ValidationException;
1414

1515
List<OrderDTO> listUnfinishedOrders();
1616

techchallenge/src/main/java/br/com/grupo63/techchallenge/core/application/usecase/order/OrderUseCase.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import br.com.grupo63.techchallenge.core.domain.model.order.Order;
1010
import br.com.grupo63.techchallenge.core.domain.model.order.OrderItem;
1111
import br.com.grupo63.techchallenge.core.domain.model.order.OrderStatus;
12+
import br.com.grupo63.techchallenge.core.domain.model.payment.PaymentStatus;
1213
import jakarta.validation.constraints.NotNull;
1314
import lombok.RequiredArgsConstructor;
1415
import org.springframework.stereotype.Service;
@@ -31,7 +32,7 @@ public class OrderUseCase implements IOrderUseCase {
3132

3233
private void fillCurrentPrices(Order order) throws NotFoundException {
3334
double totalPrice = 0.0D;
34-
for (OrderItem orderItem: order.getItems()) {
35+
for (OrderItem orderItem : order.getItems()) {
3536
ProductDTO productDTO = productUseCase.read(orderItem.getProduct().getId());
3637

3738
orderItem.setPrice(productDTO.getPrice());
@@ -41,14 +42,16 @@ private void fillCurrentPrices(Order order) throws NotFoundException {
4142
}
4243

4344
@Override
44-
public void advanceOrderStatus(@NotNull Long orderId) throws NotFoundException, ValidationException {
45+
public void advanceStatus(@NotNull Long orderId) throws NotFoundException, ValidationException {
4546
Order order = orderRepository.findByIdAndDeletedFalse(orderId).orElseThrow(NotFoundException::new);
4647

47-
if (order.getStatus() == null) {
48-
order.setStatus(OrderStatus.RECEIVED);
48+
if (order.getPayment() == null || order.getPayment().getStatus() != PaymentStatus.PAID) {
49+
throw new ValidationException("order.advanceStatus.title", "order.advanceStatus.notPaid");
4950
} else if (order.getStatus() == OrderStatus.FINISHED) {
50-
throw new ValidationException("order.cantAdvance.done.title", "order.cantAdvance.done.description");
51-
} else {
51+
throw new ValidationException("order.advanceStatus.title", "order.advanceStatus.finished");
52+
} else if (order.getStatus() == null) {
53+
order.setStatus(OrderStatus.RECEIVED);
54+
} else {
5255
order.setStatus(nextOrderMap.get(order.getStatus()));
5356
}
5457

techchallenge/src/main/java/br/com/grupo63/techchallenge/core/application/usecase/payment/IPaymentUseCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
public interface IPaymentUseCase {
99

10-
String startPayment(@NotNull Long orderId) throws NotFoundException;
10+
String startPayment(@NotNull Long orderId) throws NotFoundException, ValidationException;
1111

1212
void finishPayment(@NotNull Long orderId) throws ValidationException, NotFoundException;
1313

techchallenge/src/main/java/br/com/grupo63/techchallenge/core/application/usecase/payment/PaymentUseCase.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@ public class PaymentUseCase implements IPaymentUseCase {
2020
private final OrderUseCase orderUseCase;
2121

2222
@Override
23-
public String startPayment(@NotNull(message = "payment.order.id.notNull") Long orderId) throws NotFoundException {
23+
public String startPayment(@NotNull(message = "payment.order.id.notNull") Long orderId) throws NotFoundException, ValidationException {
2424
OrderDTO orderDTO = orderUseCase.read(orderId);
2525

26+
if (orderDTO.getStatus() != null) {
27+
throw new ValidationException("payment.startPayment.title", "payment.startPayment.alreadyFinished");
28+
} else if (orderDTO.getPayment() != null) {
29+
throw new ValidationException("payment.startPayment.title", "payment.startPayment.alreadyStarted");
30+
}
31+
2632
String qrData = mercadoPagoService.generateQRCode(orderDTO.getId(), orderDTO.getTotalPrice());
2733

2834
orderDTO.setPayment(new PaymentDTO(PaymentStatus.PENDING, PaymentMethod.MERCADO_PAGO_QR_CODE, qrData));
@@ -43,7 +49,7 @@ public void finishPayment(@NotNull(message = "payment.order.id.notNull") Long or
4349

4450
orderDTO.getPayment().setStatus(PaymentStatus.PAID);
4551
orderUseCase.update(orderDTO, orderId);
46-
orderUseCase.advanceOrderStatus(orderId);
52+
orderUseCase.advanceStatus(orderId);
4753
}
4854

4955
@Override

techchallenge/src/main/resources/messages_pt_BR.properties

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ default.title.notFoundError=Não encontrado
66
default.title.notFoundError.description=O objeto que esta tentando acessar não foi encontrado
77

88
# Order
9-
order.cantAdvance.done.title=Ordem já finalizada
10-
order.cantAdvance.done.description=Não é possível avançar uma ordem ja finalizada
9+
order.advanceStatus.title=Erro ao avançar status do pedido
10+
order.advanceStatus.finished=Não é possível avançar um pedido já finalizado
11+
order.advanceStatus.notPaid=O pedido ainda não foi pago
1112

1213
# Payment
1314
payment.order.id.notNull=O id do pedido associado ao pagamento é obrigatório
14-
payment.qrCode.title=Erro ao criar QR Code
15-
payment.notStarted=O processo de pagamento ainda não foi iniciado para esse pedido
15+
payment.startPayment.title=Erro ao iniciar pagamento
16+
payment.startPayment.alreadyFinished=O pagamento já foi finalizado
17+
payment.startPayment.alreadyStarted=O pagamento já foi iniciado
18+
payment.notStarted=O processo de pagamento ainda não foi iniciado para esse pedido
1619
payment.confirm.title=Erro ao finalizar pagamento
1720
payment.confirm.alreadyPaid=O pagamento já foi finalizado
1821

0 commit comments

Comments
 (0)