forked from idugalic/digital-restaurant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CourierOrderSaga.kt
42 lines (35 loc) · 2.36 KB
/
CourierOrderSaga.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.drestaurant.courier.domain
import org.axonframework.commandhandling.callbacks.LoggingCallback
import org.axonframework.commandhandling.gateway.CommandGateway
import org.axonframework.config.ProcessingGroup
import org.axonframework.modelling.saga.EndSaga
import org.axonframework.modelling.saga.SagaEventHandler
import org.axonframework.modelling.saga.StartSaga
import org.axonframework.spring.stereotype.Saga
import org.springframework.beans.factory.annotation.Autowired
/**
* Managing invariants (business transaction) of [CourierOrder] and [Courier] aggregates within 'Courier' bounded context
*
* Alternatively, you could choose to spawn [CourierOrder] aggregate from [Courier] aggregate directly (Axon provides this functionality), and not use saga.
* Benefits are that you check invariants internally, and you can do this with less internal (internal to the Kotlin module) events.
* Consequences are that you will introduce more coupling between this aggregates, which is fine as long as they belong to the same bounded context.
*/
@Saga
@ProcessingGroup("courierordersaga")
internal class CourierOrderSaga {
@Autowired
@Transient
private lateinit var commandGateway: CommandGateway
@StartSaga
@SagaEventHandler(associationProperty = "aggregateIdentifier")
fun on(event: CourierOrderAssigningInitiatedInternalEvent) = commandGateway.send(ValidateOrderByCourierInternalCommand(event.aggregateIdentifier, event.courierId, event.auditEntry), LoggingCallback.INSTANCE)
@EndSaga
@SagaEventHandler(associationProperty = "orderId", keyName = "aggregateIdentifier")
fun on(event: CourierNotFoundForOrderInternalEvent) = commandGateway.send(MarkCourierOrderAsNotAssignedInternalCommand(event.orderId, event.auditEntry), LoggingCallback.INSTANCE)
@EndSaga
@SagaEventHandler(associationProperty = "orderId", keyName = "aggregateIdentifier")
fun on(event: CourierValidatedOrderWithSuccessInternalEvent) = commandGateway.send(MarkCourierOrderAsAssignedInternalCommand(event.orderId, event.aggregateIdentifier, event.auditEntry), LoggingCallback.INSTANCE)
@EndSaga
@SagaEventHandler(associationProperty = "orderId", keyName = "aggregateIdentifier")
fun on(event: CourierValidatedOrderWithErrorInternalEvent) = commandGateway.send(MarkCourierOrderAsNotAssignedInternalCommand(event.orderId, event.auditEntry), LoggingCallback.INSTANCE)
}