1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import { createGiftCardsWorkflow } from "@medusajs/loyalty-plugin/workflows"
3 import {
4 ILoyaltyModuleService,
5 PluginModule,
6 } from "@medusajs/loyalty-plugin/types"
7 import {
8 adminHeaders,
9 createAdminUser,
10 } from "../../../../helpers/create-admin-user"
11
12 jest.setTimeout(60 * 1000)
13
14 const giftCardPayload = {
15 currency_code: "USD",
16 value: 1000,
17 code: "TEST1",
18 line_item_id: "lin_123",
19 }
20
21 const giftCardResponse = {
22 id: expect.any(String),
23 code: "TEST1",
24 currency_code: "USD",
25 expires_at: null,
26 line_item_id: "lin_123",
27 status: "redeemed", // cards are automatically redeemed i.e. SCA is created upon creation
28 value: 1000,
29 }
30
31 medusaIntegrationTestRunner({
32 testSuite: ({ dbConnection, api, getContainer, dbUtils }) => {
33 beforeAll(async () => {
34 await createAdminUser(dbConnection, adminHeaders, getContainer())
35
36 await dbUtils.snapshot()
37 })
38
39 describe("GET /admin/gift-cards", () => {
40 beforeEach(async () => {
41 await api.post(
42 `/admin/customers`,
43 { email: "test@test.com" },
44 adminHeaders
45 )
46
47 await api.post(
48 `/admin/gift-cards`,
49 { ...giftCardPayload },
50 adminHeaders
51 )
52 })
53
54 it("successfully returns all gift cards", async () => {
55 const {
56 data: { gift_cards: giftCards },
57 } = await api.get(`/admin/gift-cards`, adminHeaders)
58
59 expect(giftCards).toEqual([expect.objectContaining({ code: "TEST1" })])
60 })
61 })
62
63 describe("GET /admin/gift-cards/:id", () => {
64 it("should retrieve a gift card by id", async () => {
65 const {
66 data: { gift_card: createdGiftCard },
67 } = await api.post(`/admin/gift-cards`, giftCardPayload, adminHeaders)
68
69 const {
70 data: { gift_card },
71 } = await api.get(
72 `/admin/gift-cards/${createdGiftCard.id}`,
73 adminHeaders
74 )
75
76 expect(gift_card).toEqual(expect.objectContaining(giftCardResponse))
77 })
78
79 it("should throw an error if the gift card does not exist", async () => {
80 const { response } = await api
81 .get(`/admin/gift-cards/does-not-exist`, adminHeaders)
82 .catch((e) => e)
83
84 expect(response.data).toEqual({
85 message: "GiftCard id not found: does-not-exist",
86 type: "not_found",
87 })
88 })
89 })
90
91 describe("POST /admin/gift-cards", () => {
92 it("successfully creates a gift card", async () => {
93 const {
94 data: { gift_card },
95 } = await api.post(`/admin/gift-cards`, giftCardPayload, adminHeaders)
96
97 expect(gift_card).toEqual(expect.objectContaining(giftCardResponse))
98 })
99
100 it("should create a gift card and an anonymous store credit account for the card", async () => {
101 const giftCard = (
102 await api.post(
103 `/admin/gift-cards?fields=*store_credit_account,*store_credit_account.transactions`,
104 { ...giftCardPayload },
105 adminHeaders
106 )
107 ).data.gift_card
108
109 expect(giftCard).toEqual(
110 expect.objectContaining({
111 status: "redeemed",
112 value: 1000,
113 currency_code: "USD",
114 code: "TEST1",
115 store_credit_account: expect.objectContaining({
116 currency_code: "USD",
117 balance: 1000,
118 credits: 1000,
119 debits: 0,
120 transactions: expect.arrayContaining([
121 expect.objectContaining({
122 amount: 1000,
123 type: "credit",
124 reference: "gift_card",
125 reference_id: giftCard.id,
126 }),
127 ]),
128 }),
129 })
130 )
131 })
132
133 it("should not allow duplicate codes", async () => {
134 await api.post(`/admin/gift-cards`, giftCardPayload, adminHeaders)
135
136 const { response } = await api
137 .post(`/admin/gift-cards`, giftCardPayload, adminHeaders)
138 .catch((err) => err)
139
140 expect(response.status).toEqual(400)
141 })
142
143 it("should auto-generate a code with the default GIFT prefix and 4 sections", async () => {
144 const {
145 data: { gift_card },
146 } = await api.post(
147 `/admin/gift-cards`,
148 { currency_code: "USD", value: 500 },
149 adminHeaders
150 )
151
152 const parts = gift_card.code.split("-")
153 expect(parts[0]).toBe("GIFT")
154 expect(parts.length - 1).toBe(4)
155 parts.slice(1).forEach((section) => expect(section).toHaveLength(4))
156 })
157 })
158
159 describe("POST /admin/gift-cards/:id", () => {
160 it("should update a gift card by id", async () => {
161 const {
162 data: { gift_card: createdGiftCard },
163 } = await api.post(`/admin/gift-cards`, giftCardPayload, adminHeaders)
164
165 const {
166 data: { gift_card },
167 } = await api.post(
168 `/admin/gift-cards/${createdGiftCard.id}`,
169 {
170 status: "redeemed",
171 note: "This is a test note",
172 },
173 adminHeaders
174 )
175
176 expect(gift_card).toEqual(
177 expect.objectContaining({
178 status: "redeemed",
179 note: "This is a test note",
180 })
181 )
182 })
183
184 it("should throw an error if the gift card does not exist", async () => {
185 const { response } = await api
186 .post(
187 `/admin/gift-cards/does-not-exist`,
188 {
189 status: "redeemed",
190 note: "This is a test note",
191 },
192 adminHeaders
193 )
194 .catch((e) => e)
195
196 expect(response.data).toEqual({
197 message: "GiftCard id not found: does-not-exist",
198 type: "not_found",
199 })
200 })
201 })
202
203 describe("createGiftCardsWorkflow", () => {
204 it("should run the full workflow and produce a gift card with store credit account", async () => {
205 const { result } = await createGiftCardsWorkflow(getContainer()).run({
206 input: [{ currency_code: "USD", value: 300, code: "WF-TEST-001" }],
207 })
208
209 const [giftCard] = result
210 expect(giftCard).toEqual(
211 expect.objectContaining({
212 code: "WF-TEST-001",
213 currency_code: "USD",
214 value: 300,
215 status: "redeemed",
216 })
217 )
218 })
219
220 it("should auto-generate a code in GIFT-XXXX-XXXX-XXXX-XXXX format when no code is provided", async () => {
221 const { result } = await createGiftCardsWorkflow(getContainer()).run({
222 input: [{ currency_code: "USD", value: 300 }],
223 })
224
225 const [giftCard] = result
226 const parts = giftCard.code.split("-")
227
228 expect(parts[0]).toBe("GIFT")
229 expect(parts.length - 1).toBe(4)
230 parts.slice(1).forEach((section) => expect(section).toHaveLength(4))
231 })
232
233 it("should auto-generate a code using configured prefix and sections when plugin options are set", async () => {
234 const loyaltyModule = getContainer().resolve<ILoyaltyModuleService>(
235 PluginModule.LOYALTY
236 )
237 const spy = jest
238 .spyOn(loyaltyModule, "getOptions")
239 .mockResolvedValue({ prefix: "GC", sections: 3 })
240
241 try {
242 const { result } = await createGiftCardsWorkflow(getContainer()).run({
243 input: [{ currency_code: "USD", value: 300 }],
244 })
245
246 const [giftCard] = result
247 const parts = giftCard.code.split("-")
248
249 expect(parts[0]).toBe("GC")
250 expect(parts.length - 1).toBe(3)
251 parts.slice(1).forEach((section) => expect(section).toHaveLength(4))
252 } finally {
253 spy.mockRestore()
254 }
255 })
256 })
257 },
258 })