# Example

This example models an example fulfilment process once a customer has purchased an item from an online store.

```typescript
// fulfiment-workflow-state.ts
import { WorkflowState } from '@node-ts/bus-core'

export class FulfilmentWorkflowState extends WorkflowState {
  static NAME = 'FulfilmentWorkflowState'
  $name = FulfilmentWorkflowState.NAME

  itemId: string
  customerId: string
  status: 'email-receipt' | 'shipping-item' | 'complete'
}
```

```typescript
import { BusInstance } from '@node-ts/bus-core'

export class FulfilmentWorkflow extends Workflow<FulfilmentWorkflowState> {

  constructor (bus: BusInstance) {}
  
  configureWorkflow (
    mapper: WorkflowMapper<FulfilmentWorkflowState, FulfilmentWorkflow>
  ): void {
    mapper
      .withState(FulfilmentWorkflowState)
      .startedBy(ItemPurchased, 'shipItem')
      .when(ItemShipped, 'emailReceipt')
      .when(ReceiptEmailed, 'complete')
  }
  
  async shipItem ({ itemId, customerId }: ItemPurchased) {
    await this.bus.send(new ShipItem(itemId, customerId))
    return { itemId, customerId, status: 'shipping-item' }
  }
  
  async emailReceipt (_: ItemShipped, { itemId, customerId }: FulfilmentWorkflowState) {
    await this.bus.send(new EmailReceipt(itemId, customerId))
    return { status: 'emailing-receipt' }
  }
  
  async complete () {
    return this.completeWorkflow({ status: 'complete' })
  }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bus.node-ts.com/guide/workflows/example.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
