-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Extract hard-coded fulfillment state & process
This commit makes it possible to completely configure the fulfillment process by extracting all transition validation and allowing the developer to replace with custom logic.
- Loading branch information
1 parent
cff3b91
commit cdb2b75
Showing
10 changed files
with
154 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/core/src/config/fulfillment/default-fulfillment-process.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { HistoryEntryType } from '@vendure/common/lib/generated-types'; | ||
|
||
import { awaitPromiseOrObservable, Transitions } from '../../common/index'; | ||
import { FulfillmentState } from '../../service/index'; | ||
|
||
import { FulfillmentProcess } from './fulfillment-process'; | ||
|
||
declare module '../../service/helpers/fulfillment-state-machine/fulfillment-state' { | ||
interface FulfillmentStates { | ||
Shipped: never; | ||
Delivered: never; | ||
} | ||
} | ||
|
||
let configService: import('../config.service').ConfigService; | ||
let historyService: import('../../service/index').HistoryService; | ||
|
||
/** | ||
* @description | ||
* The default {@link FulfillmentProcess} | ||
* | ||
* @docsCategory fulfillment | ||
*/ | ||
export const defaultFulfillmentProcess: FulfillmentProcess<FulfillmentState> = { | ||
transitions: { | ||
Created: { | ||
to: ['Pending'], | ||
}, | ||
Pending: { | ||
to: ['Shipped', 'Delivered', 'Cancelled'], | ||
}, | ||
Shipped: { | ||
to: ['Delivered', 'Cancelled'], | ||
}, | ||
Delivered: { | ||
to: ['Cancelled'], | ||
}, | ||
Cancelled: { | ||
to: [], | ||
}, | ||
}, | ||
async init(injector) { | ||
// Lazily import these services to avoid a circular dependency error | ||
// due to this being used as part of the DefaultConfig | ||
const ConfigService = await import('../config.service').then(m => m.ConfigService); | ||
const HistoryService = await import('../../service/index').then(m => m.HistoryService); | ||
configService = injector.get(ConfigService); | ||
historyService = injector.get(HistoryService); | ||
}, | ||
async onTransitionStart(fromState, toState, data) { | ||
const { fulfillmentHandlers } = configService.shippingOptions; | ||
const fulfillmentHandler = fulfillmentHandlers.find(h => h.code === data.fulfillment.handlerCode); | ||
if (fulfillmentHandler) { | ||
const result = await awaitPromiseOrObservable( | ||
fulfillmentHandler.onFulfillmentTransition(fromState, toState, data), | ||
); | ||
if (result === false || typeof result === 'string') { | ||
return result; | ||
} | ||
} | ||
}, | ||
async onTransitionEnd(fromState, toState, data) { | ||
const historyEntryPromises = data.orders.map(order => | ||
historyService.createHistoryEntryForOrder({ | ||
orderId: order.id, | ||
type: HistoryEntryType.ORDER_FULFILLMENT_TRANSITION, | ||
ctx: data.ctx, | ||
data: { | ||
fulfillmentId: data.fulfillment.id, | ||
from: fromState, | ||
to: toState, | ||
}, | ||
}), | ||
); | ||
await Promise.all(historyEntryPromises); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 14 additions & 24 deletions
38
packages/core/src/service/helpers/fulfillment-state-machine/fulfillment-state.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.