Parcourir la source

feat(core): New "Created" initial state for Fulfillments

Relates to #510.

BREAKING CHANGE: Fulfillments now start in the new "Created" state, and then _immediately_
transition to the "Pending" state. This allows e.g. event listeners to pick up newly-created
Fulfillments.
Michael Bromley il y a 5 ans
Parent
commit
a53f27efa4

+ 16 - 0
packages/core/e2e/order.e2e-spec.ts

@@ -564,12 +564,28 @@ describe('Orders resolver', () => {
                     },
                     type: HistoryEntryType.ORDER_FULFILLMENT,
                 },
+                {
+                    data: {
+                        from: 'Created',
+                        fulfillmentId: 'T_1',
+                        to: 'Pending',
+                    },
+                    type: HistoryEntryType.ORDER_FULFILLMENT_TRANSITION,
+                },
                 {
                     data: {
                         fulfillmentId: 'T_2',
                     },
                     type: HistoryEntryType.ORDER_FULFILLMENT,
                 },
+                {
+                    data: {
+                        from: 'Created',
+                        fulfillmentId: 'T_2',
+                        to: 'Pending',
+                    },
+                    type: HistoryEntryType.ORDER_FULFILLMENT_TRANSITION,
+                },
                 {
                     data: {
                         from: 'Pending',

+ 1 - 1
packages/core/src/service/helpers/fulfillment-state-machine/fulfillment-state-machine.ts

@@ -22,7 +22,7 @@ import {
 @Injectable()
 export class FulfillmentStateMachine {
     readonly config: StateMachineConfig<FulfillmentState, FulfillmentTransitionData>;
-    private readonly initialState: FulfillmentState = 'Pending';
+    private readonly initialState: FulfillmentState = 'Created';
 
     constructor(private configService: ConfigService, private historyService: HistoryService) {
         this.config = this.initConfig();

+ 4 - 1
packages/core/src/service/helpers/fulfillment-state-machine/fulfillment-state.ts

@@ -9,9 +9,12 @@ import { Order } from '../../../entity/order/order.entity';
  *
  * @docsCategory fulfillment
  */
-export type FulfillmentState = 'Pending' | 'Shipped' | 'Delivered' | 'Cancelled';
+export type FulfillmentState = 'Created' | 'Pending' | 'Shipped' | 'Delivered' | 'Cancelled';
 
 export const fulfillmentStateTransitions: Transitions<FulfillmentState> = {
+    Created: {
+        to: ['Pending'],
+    },
     Pending: {
         to: ['Shipped', 'Delivered', 'Cancelled'],
     },

+ 6 - 1
packages/core/src/service/services/order.service.ts

@@ -740,7 +740,12 @@ export class OrderService {
                 },
             });
         }
-        return fulfillment;
+        const { fulfillment: pendingFulfillment } = await this.fulfillmentService.transitionToState(
+            ctx,
+            fulfillment.id,
+            'Pending',
+        );
+        return pendingFulfillment;
     }
 
     private async ensureSufficientStockForFulfillment(