|
1
|
import { StepResponse } from "@medusajs/framework/workflows-sdk"; |
|
2
|
|
|
3
|
import { createStep } from "@medusajs/framework/workflows-sdk"; |
|
4
|
import { PluginModule } from "../../../types"; |
|
5
|
import { ModuleGiftCard } from "../../../types/loyalty"; |
|
6
|
import { |
|
7
|
IStoreCreditModuleService, |
|
8
|
ModuleAccountStats, |
|
9
|
} from "../../../types/store-credit"; |
|
10
|
|
|
11
|
/** |
|
12
|
* Input to retrieve the balance of gift cards from their associated store credit accounts. |
|
13
|
*/ |
|
14
|
export interface RetrieveGiftCardsBalanceStepInput { |
|
15
|
/** |
|
16
|
* A map of gift card IDs to their associated store credit account IDs. |
|
17
|
*/ |
|
18
|
giftCardStoreCreditAccountMap: Record<string, string> |
|
19
|
/** |
|
20
|
* The gift cards whose balances should be retrieved. |
|
21
|
*/ |
|
22
|
giftCards: ModuleGiftCard[] |
|
23
|
} |
|
24
|
|
|
25
|
/** |
|
26
|
* This step retrieves the balance of gift cards from their associated store credit accounts. |
|
27
|
* Returns a map of gift card codes to their account stats. |
|
28
|
* |
|
29
|
* @example |
|
30
|
* const data = retrieveGiftCardsBalanceStep({ |
|
31
|
* giftCards: [ |
|
32
|
* { |
|
33
|
* id: "gc_123", |
|
34
|
* code: "GC-XXXX", |
|
35
|
* // other gift card properties... |
|
36
|
* } |
|
37
|
* ], |
|
38
|
* giftCardStoreCreditAccountMap: { "gc_123": "sca_456" }, |
|
39
|
* }) |
|
40
|
*/ |
|
41
|
export const retrieveGiftCardsBalanceStep = createStep( |
|
42
|
"retrieve-gift-cards-balance", |
|
43
|
async function ( |
|
44
|
{ |
|
45
|
giftCardStoreCreditAccountMap, |
|
46
|
giftCards, |
|
47
|
}: RetrieveGiftCardsBalanceStepInput, |
|
48
|
{ container } |
|
49
|
) { |
|
50
|
const accountBalanceMap: Record<string, ModuleAccountStats> = {}; |
|
51
|
const module = container.resolve<IStoreCreditModuleService>( |
|
52
|
PluginModule.STORE_CREDIT |
|
53
|
); |
|
54
|
|
|
55
|
for (const giftCard of giftCards) { |
|
56
|
const giftCardBalance = await module.retrieveAccountStats({ |
|
57
|
account_id: giftCardStoreCreditAccountMap[giftCard.id], |
|
58
|
}); |
|
59
|
|
|
60
|
accountBalanceMap[giftCard.code] = giftCardBalance; |
|
61
|
} |
|
62
|
|
|
63
|
return new StepResponse(accountBalanceMap); |
|
64
|
} |
|
65
|
); |