1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import { createCustomersWorkflow } from "@medusajs/medusa/core-flows"
3 import {
4 claimStoreCreditAccountWorkflow,
5 createStoreCreditAccountsWorkflow,
6 } from "@medusajs/loyalty-plugin/workflows"
7 import {
8 adminHeaders,
9 createAdminUser,
10 generatePublishableKey,
11 generateStoreHeaders,
12 } from "../../../../helpers/create-admin-user"
13 import { createAuthenticatedCustomer } from "../../../helpers/create-authenticated-customer"
14
15 async function createAccount(input, container) {
16 const {
17 result: [storeCreditAccount],
18 } = await createStoreCreditAccountsWorkflow.run({
19 input: [input],
20 container,
21 })
22
23 return storeCreditAccount
24 }
25
26 jest.setTimeout(60 * 1000)
27
28 medusaIntegrationTestRunner({
29 testSuite: ({ dbConnection, api, getContainer, dbUtils }) => {
30 let customer, storeHeaders
31
32 beforeAll(async () => {
33 await createAdminUser(dbConnection, adminHeaders, getContainer())
34 const publishableKey = await generatePublishableKey(getContainer())
35 storeHeaders = generateStoreHeaders({ publishableKey })
36
37 const user = await createAuthenticatedCustomer(api, storeHeaders, {
38 email: "initial@customer.com",
39 })
40 storeHeaders.headers["Authorization"] = `Bearer ${user.jwt}`
41 customer = user.customer
42
43 await dbUtils.snapshot()
44 })
45
46 describe("claimStoreCreditAccountWorkflow", () => {
47 it("should fail to claim a store credit account if code is not provided", async () => {
48 const { errors } = await claimStoreCreditAccountWorkflow.run({
49 input: {
50 customer_id: customer.id,
51 },
52 container: getContainer(),
53 throwOnError: false,
54 })
55
56 const error = errors[0]
57
58 expect(error.error.message).toEqual(
59 "Code is required to claim a store credit account"
60 )
61 })
62
63 it("should fail to claim a store credit account if customer id is not provided", async () => {
64 const storeCreditAccount = await createAccount(
65 {
66 currency_code: "usd",
67 },
68 getContainer()
69 )
70
71 const { errors } = await claimStoreCreditAccountWorkflow.run({
72 input: {
73 code: storeCreditAccount.code,
74 },
75 container: getContainer(),
76 throwOnError: false,
77 })
78
79 const error = errors[0]
80
81 expect(error.error.message).toEqual(
82 "Customer Id is required to claim a store credit account"
83 )
84 })
85
86 it("should fail to claim a store credit account if customer does not have an account", async () => {
87 const {
88 result: [newCustomer],
89 } = await createCustomersWorkflow.run({
90 input: {
91 customersData: [
92 {
93 first_name: "John",
94 last_name: "Doe",
95 email: "john.doe@example.com",
96 },
97 ],
98 },
99 container: getContainer(),
100 })
101
102 const storeCreditAccount = await createAccount(
103 {
104 currency_code: "usd",
105 },
106 getContainer()
107 )
108
109 const { errors } = await claimStoreCreditAccountWorkflow.run({
110 input: {
111 code: storeCreditAccount.code,
112 customer_id: newCustomer.id,
113 },
114 container: getContainer(),
115 throwOnError: false,
116 })
117
118 const error = errors[0]
119
120 expect(error.error.message).toEqual(
121 "Only customers with an account can claim a store credit account"
122 )
123 })
124
125 it("should fail to claim a store credit account if source account belongs to a customer", async () => {
126 const sourceCreditAccount = await createAccount(
127 {
128 currency_code: "usd",
129 customer_id: customer.id,
130 },
131 getContainer()
132 )
133
134 const { errors } = await claimStoreCreditAccountWorkflow.run({
135 input: {
136 code: sourceCreditAccount.code,
137 customer_id: customer.id,
138 },
139 container: getContainer(),
140 throwOnError: false,
141 })
142
143 const error = errors[0]
144
145 expect(error.error.message).toEqual(
146 "Customer already owns the store credit account"
147 )
148 })
149
150 it("should fail to claim a store credit account if source account has no balance", async () => {
151 const sourceCreditAccount = await createAccount(
152 {
153 currency_code: "usd",
154 },
155 getContainer()
156 )
157
158 const { errors } = await claimStoreCreditAccountWorkflow.run({
159 input: {
160 code: sourceCreditAccount.code,
161 customer_id: customer.id,
162 },
163 container: getContainer(),
164 throwOnError: false,
165 })
166
167 const error = errors[0]
168
169 expect(error.error.message).toEqual(
170 "Cannot claim a store credit account with no balance"
171 )
172 })
173 })
174 },
175 })