From b67263ca6f30f8cd302422f01ae0d74d60999b8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Rasztabiga?= Date: Sat, 7 Oct 2023 21:21:54 +0200 Subject: [PATCH] feat(backend): Replace interceptors with calls to ports --- .../domain/command/aggregate/OrderDelivery.kt | 25 ++++- .../command/command/PickupDeliveryCommand.kt | 1 - .../command/port/CourierOnlineVerifierPort.kt | 6 ++ .../axon/AxonCourierOnlineVerifierAdapter.kt | 22 +++++ frontend/app/models/order.server.ts | 3 +- .../ordering.orders.$orderId.payment.tsx | 22 ++--- .../ordering.restaurants.$restaurantId.tsx | 1 + .../out/axon/AxonOrderVerificationAdapter.kt | 71 ++++++++++++++ .../order/config/CommandConfiguration.kt | 22 ----- .../order/domain/command/aggregate/Order.kt | 26 ++++- .../FinalizeOrderCommandInterceptor.kt | 95 ------------------- .../StartOrderCommandInterceptor.kt | 63 ------------ .../command/port/OrderVerificationPort.kt | 16 ++++ .../payment/config/CommandConfiguration.kt | 17 ---- 14 files changed, 171 insertions(+), 219 deletions(-) create mode 100644 delivery/src/main/kotlin/me/rasztabiga/thesis/delivery/domain/command/port/CourierOnlineVerifierPort.kt create mode 100644 delivery/src/main/kotlin/me/rasztabiga/thesis/delivery/infrastructure/axon/AxonCourierOnlineVerifierAdapter.kt create mode 100644 order/src/main/kotlin/me/rasztabiga/thesis/order/adapter/out/axon/AxonOrderVerificationAdapter.kt delete mode 100644 order/src/main/kotlin/me/rasztabiga/thesis/order/config/CommandConfiguration.kt delete mode 100644 order/src/main/kotlin/me/rasztabiga/thesis/order/domain/command/interceptor/FinalizeOrderCommandInterceptor.kt delete mode 100644 order/src/main/kotlin/me/rasztabiga/thesis/order/domain/command/interceptor/StartOrderCommandInterceptor.kt create mode 100644 order/src/main/kotlin/me/rasztabiga/thesis/order/domain/command/port/OrderVerificationPort.kt delete mode 100644 restaurant/src/main/kotlin/me/rasztabiga/thesis/payment/config/CommandConfiguration.kt diff --git a/delivery/src/main/kotlin/me/rasztabiga/thesis/delivery/domain/command/aggregate/OrderDelivery.kt b/delivery/src/main/kotlin/me/rasztabiga/thesis/delivery/domain/command/aggregate/OrderDelivery.kt index e0e46d8f..9abb2666 100644 --- a/delivery/src/main/kotlin/me/rasztabiga/thesis/delivery/domain/command/aggregate/OrderDelivery.kt +++ b/delivery/src/main/kotlin/me/rasztabiga/thesis/delivery/domain/command/aggregate/OrderDelivery.kt @@ -5,6 +5,7 @@ import me.rasztabiga.thesis.delivery.domain.command.command.DeliverDeliveryComma import me.rasztabiga.thesis.delivery.domain.command.command.PickupDeliveryCommand import me.rasztabiga.thesis.delivery.domain.command.command.RejectDeliveryOfferCommand import me.rasztabiga.thesis.delivery.domain.command.port.CalculateDeliveryFeePort +import me.rasztabiga.thesis.delivery.domain.command.port.CourierOnlineVerifierPort import me.rasztabiga.thesis.delivery.domain.command.port.OrderPreparedVerifierPort import me.rasztabiga.thesis.shared.domain.command.command.CreateOrderDeliveryOfferCommand import me.rasztabiga.thesis.shared.domain.command.event.OrderDeliveryAcceptedEvent @@ -52,8 +53,11 @@ class OrderDelivery { } @CommandHandler - fun handle(command: RejectDeliveryOfferCommand) { + fun handle(command: RejectDeliveryOfferCommand, courierOnlineVerifierPort: CourierOnlineVerifierPort) { require(this.status == DeliveryStatus.OFFER) { "Delivery can be rejected only if it's in OFFER status." } + require(courierOnlineVerifierPort.isCourierOnline(this.courierId!!)) { + "Delivery can be rejected only if the courier is online." + } apply( OrderDeliveryRejectedEvent( @@ -64,8 +68,11 @@ class OrderDelivery { } @CommandHandler - fun handle(command: AcceptDeliveryOfferCommand) { + fun handle(command: AcceptDeliveryOfferCommand, courierOnlineVerifierPort: CourierOnlineVerifierPort) { require(this.status == DeliveryStatus.OFFER) { "Delivery can be accepted only if it's in OFFER status." } + require(courierOnlineVerifierPort.isCourierOnline(this.courierId!!)) { + "Delivery can be accepted only if the courier is online." + } apply( OrderDeliveryAcceptedEvent( @@ -77,7 +84,11 @@ class OrderDelivery { } @CommandHandler - fun handle(command: PickupDeliveryCommand, orderPreparedVerifierPort: OrderPreparedVerifierPort) { + fun handle( + command: PickupDeliveryCommand, + orderPreparedVerifierPort: OrderPreparedVerifierPort, + courierOnlineVerifierPort: CourierOnlineVerifierPort + ) { require(this.status == DeliveryStatus.ACCEPTED) { "Delivery can be picked up only if it's in ACCEPTED status." } require(this.courierId == command.courierId) { "Delivery can be picked up only by the courier who accepted it." @@ -85,6 +96,9 @@ class OrderDelivery { require(orderPreparedVerifierPort.isOrderPrepared(this.orderId)) { "Delivery can be picked up only if the order is prepared." } + require(courierOnlineVerifierPort.isCourierOnline(this.courierId!!)) { + "Delivery can be picked up only if the courier is online." + } apply( OrderDeliveryPickedUpEvent( @@ -96,13 +110,16 @@ class OrderDelivery { } @CommandHandler - fun handle(command: DeliverDeliveryCommand) { + fun handle(command: DeliverDeliveryCommand, courierOnlineVerifierPort: CourierOnlineVerifierPort) { require(this.status == DeliveryStatus.PICKED_UP) { "Delivery can be delivered only if it's in PICKED_UP status." } require(this.courierId == command.courierId) { "Delivery can be delivered only by the courier who picked it up." } + require(courierOnlineVerifierPort.isCourierOnline(this.courierId!!)) { + "Delivery can be delivered only if the courier is online." + } apply( OrderDeliveryDeliveredEvent( diff --git a/delivery/src/main/kotlin/me/rasztabiga/thesis/delivery/domain/command/command/PickupDeliveryCommand.kt b/delivery/src/main/kotlin/me/rasztabiga/thesis/delivery/domain/command/command/PickupDeliveryCommand.kt index 8ed772f6..d9c69a4a 100644 --- a/delivery/src/main/kotlin/me/rasztabiga/thesis/delivery/domain/command/command/PickupDeliveryCommand.kt +++ b/delivery/src/main/kotlin/me/rasztabiga/thesis/delivery/domain/command/command/PickupDeliveryCommand.kt @@ -3,7 +3,6 @@ package me.rasztabiga.thesis.delivery.domain.command.command import org.axonframework.modelling.command.TargetAggregateIdentifier import java.util.UUID -// TODO verify that restaurant order is prepared data class PickupDeliveryCommand( @TargetAggregateIdentifier val id: UUID, val courierId: String diff --git a/delivery/src/main/kotlin/me/rasztabiga/thesis/delivery/domain/command/port/CourierOnlineVerifierPort.kt b/delivery/src/main/kotlin/me/rasztabiga/thesis/delivery/domain/command/port/CourierOnlineVerifierPort.kt new file mode 100644 index 00000000..138741e7 --- /dev/null +++ b/delivery/src/main/kotlin/me/rasztabiga/thesis/delivery/domain/command/port/CourierOnlineVerifierPort.kt @@ -0,0 +1,6 @@ +package me.rasztabiga.thesis.delivery.domain.command.port + +interface CourierOnlineVerifierPort { + + fun isCourierOnline(courierId: String): Boolean +} diff --git a/delivery/src/main/kotlin/me/rasztabiga/thesis/delivery/infrastructure/axon/AxonCourierOnlineVerifierAdapter.kt b/delivery/src/main/kotlin/me/rasztabiga/thesis/delivery/infrastructure/axon/AxonCourierOnlineVerifierAdapter.kt new file mode 100644 index 00000000..f5cccd94 --- /dev/null +++ b/delivery/src/main/kotlin/me/rasztabiga/thesis/delivery/infrastructure/axon/AxonCourierOnlineVerifierAdapter.kt @@ -0,0 +1,22 @@ +package me.rasztabiga.thesis.delivery.infrastructure.axon + +import me.rasztabiga.thesis.delivery.domain.command.port.CourierOnlineVerifierPort +import me.rasztabiga.thesis.shared.adapter.`in`.rest.api.CourierResponse +import me.rasztabiga.thesis.shared.domain.query.query.FindCourierByIdQuery +import org.axonframework.messaging.responsetypes.ResponseTypes +import org.axonframework.queryhandling.QueryGateway +import org.springframework.stereotype.Service + +@Service +class AxonCourierOnlineVerifierAdapter( + private val queryGateway: QueryGateway +) : CourierOnlineVerifierPort { + + override fun isCourierOnline(courierId: String): Boolean { + val courier = queryGateway.query( + FindCourierByIdQuery(courierId), + ResponseTypes.instanceOf(CourierResponse::class.java) + ).join() + return courier.availability == CourierResponse.Availability.ONLINE + } +} diff --git a/frontend/app/models/order.server.ts b/frontend/app/models/order.server.ts index bb760e5b..42940679 100644 --- a/frontend/app/models/order.server.ts +++ b/frontend/app/models/order.server.ts @@ -64,10 +64,11 @@ export interface OrderResponse { restaurantId: string; restaurantLocation: Location; deliveryLocation: Location; + deliveryFee: number; userId: string; status: OrderStatus; items: OrderItemResponse[]; - total: number; + itemsTotal: number; paymentId: string; paymentSessionUrl: string; } diff --git a/frontend/app/routes/ordering.orders.$orderId.payment.tsx b/frontend/app/routes/ordering.orders.$orderId.payment.tsx index 2ed3b672..4a53a3c3 100644 --- a/frontend/app/routes/ordering.orders.$orderId.payment.tsx +++ b/frontend/app/routes/ordering.orders.$orderId.payment.tsx @@ -88,25 +88,21 @@ export default function OrderPaymentPage() { ); })} +
+

+ {" "} + 1x Delivery fee, {data.order.deliveryFee} PLN +

+

-

Total: {data.order.total} PLN

-
-
-
-

Choose payment method

- +

Total: {data.order.itemsTotal + data.order.deliveryFee} PLN


-

Selected delivery address

- TODO +

Selected delivery address

+ {data.order.deliveryLocation.streetAddress}

{error && ( diff --git a/frontend/app/routes/ordering.restaurants.$restaurantId.tsx b/frontend/app/routes/ordering.restaurants.$restaurantId.tsx index 04f2a12e..0f590a02 100644 --- a/frontend/app/routes/ordering.restaurants.$restaurantId.tsx +++ b/frontend/app/routes/ordering.restaurants.$restaurantId.tsx @@ -193,6 +193,7 @@ export default function RestaurantPage() { ); })}

Total: {orderSum} PLN

+

+ delivery fee