|
1
|
import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"; |
|
2
|
import { |
|
3
|
IStoreCreditModuleService, |
|
4
|
ModuleDebitAccount, |
|
5
|
PluginModule, |
|
6
|
} from "../../../types"; |
|
7
|
|
|
8
|
/** |
|
9
|
* Data for debiting one or more store credit accounts. |
|
10
|
*/ |
|
11
|
export type DebitAccountStepInput = ModuleDebitAccount[] |
|
12
|
|
|
13
|
/** |
|
14
|
* This step debits one or more store credit accounts by creating debit transactions. |
|
15
|
* The step supports rollback by deleting the created transactions. |
|
16
|
* |
|
17
|
* @example |
|
18
|
* const data = debitAccountStep([ |
|
19
|
* { |
|
20
|
* account_id: "sca_123", |
|
21
|
* amount: 50, |
|
22
|
* reference: "cart", |
|
23
|
* reference_id: "cart_123", |
|
24
|
* }, |
|
25
|
* ]) |
|
26
|
*/ |
|
27
|
export const debitAccountStep = createStep( |
|
28
|
"debit-account", |
|
29
|
async (input: DebitAccountStepInput, { container }) => { |
|
30
|
const module = container.resolve<IStoreCreditModuleService>( |
|
31
|
PluginModule.STORE_CREDIT |
|
32
|
); |
|
33
|
|
|
34
|
const transactions = await module.debitAccounts(input); |
|
35
|
|
|
36
|
return new StepResponse( |
|
37
|
transactions, |
|
38
|
transactions.map((t) => t.id) |
|
39
|
); |
|
40
|
}, |
|
41
|
async (ids, { container }) => { |
|
42
|
if (!ids?.length) { |
|
43
|
return; |
|
44
|
} |
|
45
|
|
|
46
|
const module = container.resolve<IStoreCreditModuleService>( |
|
47
|
PluginModule.STORE_CREDIT |
|
48
|
); |
|
49
|
|
|
50
|
await module.deleteTransactions(ids); |
|
51
|
} |
|
52
|
); |