Example
This example models an example fulfilment process once a customer has purchased an item from an online store.
1
// fulfiment-workflow-state.ts
2
import { WorkflowState } from '@node-ts/bus-core'
3
​
4
export class FulfilmentWorkflowState extends WorkflowState {
5
static NAME = 'FulfilmentWorkflowState'
6
$name = FulfilmentWorkflowState.NAME
7
​
8
itemId: string
9
customerId: string
10
status: 'email-receipt' | 'shipping-item' | 'complete'
11
}
Copied!
1
import { BusInstance } from '@node-ts/bus-core'
2
​
3
export class FulfilmentWorkflow extends Workflow<FulfilmentWorkflowState> {
4
​
5
constructor (bus: BusInstance) {}
6
7
configureWorkflow (
8
mapper: WorkflowMapper<FulfilmentWorkflowState, FulfilmentWorkflow>
9
): void {
10
mapper
11
.withState(FulfilmentWorkflowState)
12
.startedBy(ItemPurchased, 'shipItem')
13
.when(ItemShipped, 'emailReceipt')
14
.when(ReceiptEmailed, 'complete')
15
}
16
17
async shipItem ({ itemId, customerId }: ItemPurchased) {
18
await this.bus.send(new ShipItem(itemId, customerId))
19
return { itemId, customerId, status: 'shipping-item' }
20
}
21
22
async emailReceipt (_: ItemShipped, { itemId, customerId }: FulfilmentWorkflowState) {
23
await this.bus.send(new EmailReceipt(itemId, customerId))
24
return { status: 'emailing-receipt' }
25
}
26
27
async complete () {
28
return this.completeWorkflow({ status: 'complete' })
29
}
30
}
Copied!
Copy link