-
-
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): Make Refund process configurable (#2942)
- Loading branch information
Showing
9 changed files
with
234 additions
and
48 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
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,53 @@ | ||
import { HistoryEntryType } from '@vendure/common/lib/generated-types'; | ||
|
||
import { RefundState } from '../../service/helpers/refund-state-machine/refund-state'; | ||
|
||
import { RefundProcess } from './refund-process'; | ||
|
||
let configService: import('../config.service').ConfigService; | ||
let historyService: import('../../service/index').HistoryService; | ||
|
||
/** | ||
* @description | ||
* The default {@link RefundProcess}. | ||
* | ||
* @docsCategory refund | ||
*/ | ||
export const defaultRefundProcess: RefundProcess<RefundState> = { | ||
transitions: { | ||
Pending: { | ||
to: ['Settled', 'Failed'], | ||
}, | ||
Settled: { | ||
to: [], | ||
}, | ||
Failed: { | ||
to: [], | ||
}, | ||
}, | ||
async init(injector) { | ||
const ConfigService = await import('../config.service.js').then(m => m.ConfigService); | ||
const HistoryService = await import('../../service/index.js').then(m => m.HistoryService); | ||
configService = injector.get(ConfigService); | ||
historyService = injector.get(HistoryService); | ||
}, | ||
onTransitionStart: async (fromState, toState, data) => { | ||
return true; | ||
}, | ||
onTransitionEnd: async (fromState, toState, data) => { | ||
if (!historyService) { | ||
throw new Error('HistoryService has not been initialized'); | ||
} | ||
await historyService.createHistoryEntryForOrder({ | ||
ctx: data.ctx, | ||
orderId: data.order.id, | ||
type: HistoryEntryType.ORDER_REFUND_TRANSITION, | ||
data: { | ||
refundId: data.refund.id, | ||
from: fromState, | ||
to: toState, | ||
reason: data.refund.reason, | ||
}, | ||
}); | ||
}, | ||
}; |
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,41 @@ | ||
import { | ||
OnTransitionEndFn, | ||
OnTransitionErrorFn, | ||
OnTransitionStartFn, | ||
Transitions, | ||
} from '../../common/finite-state-machine/types'; | ||
import { InjectableStrategy } from '../../common/types/injectable-strategy'; | ||
import { | ||
CustomRefundStates, | ||
RefundState, | ||
RefundTransitionData, | ||
} from '../../service/helpers/refund-state-machine/refund-state'; | ||
|
||
/** | ||
* @description | ||
* A RefundProcess is used to define the way the refund process works as in: what states a Refund can be | ||
* in, and how it may transition from one state to another. Using the `onTransitionStart()` hook, a | ||
* RefundProcess can perform checks before allowing a state transition to occur, and the `onTransitionEnd()` | ||
* hook allows logic to be executed after a state change. | ||
* | ||
* For detailed description of the interface members, see the {@link StateMachineConfig} docs. | ||
* | ||
* @docsCategory refund | ||
*/ | ||
export interface RefundProcess<State extends keyof CustomRefundStates | string> extends InjectableStrategy { | ||
transitions?: Transitions<State, State | RefundState> & Partial<Transitions<RefundState | State>>; | ||
onTransitionStart?: OnTransitionStartFn<State | RefundState, RefundTransitionData>; | ||
onTransitionEnd?: OnTransitionEndFn<State | RefundState, RefundTransitionData>; | ||
onTransitionError?: OnTransitionErrorFn<State | RefundState>; | ||
} | ||
|
||
/** | ||
* @description | ||
* Used to define extensions to or modifications of the default refund process. | ||
* | ||
* For detailed description of the interface members, see the {@link StateMachineConfig} docs. | ||
* | ||
* @deprecated Use RefundProcess | ||
*/ | ||
export interface CustomRefundProcess<State extends keyof CustomRefundStates | string> | ||
extends RefundProcess<State> {} |
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
34 changes: 18 additions & 16 deletions
34
packages/core/src/service/helpers/refund-state-machine/refund-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
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