1 import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk";
2 import {
3 IStoreCreditModuleService,
4 ModuleCreditAccount,
5 PluginModule,
6 } from "../../../types";
7
8 /**
9 * Data for crediting one or more store credit accounts.
10 */
11 export type CreditAccountStepInput = ModuleCreditAccount[]
12
13 /**
14 * This step credits one or more store credit accounts with transactions.
15 * The step supports rollback by deleting the created transactions.
16 *
17 * @example
18 * const data = creditAccountStep([
19 * {
20 * account_id: "sca_123",
21 * amount: 100,
22 * reference: "gift_card",
23 * reference_id: "gc_123",
24 * },
25 * ])
26 */
27 export const creditAccountStep = createStep(
28 "credit-account",
29 async (input: CreditAccountStepInput, { container }) => {
30 const module = container.resolve<IStoreCreditModuleService>(
31 PluginModule.STORE_CREDIT
32 );
33
34 const transactions = await module.creditAccounts(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 );