1 import {
2 CreateOrderDTO,
3 IOrderModuleService,
4 OrderDTO,
5 } from "@medusajs/framework/types"
6 import { Modules } from "@medusajs/framework/utils"
7 import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
8
9 jest.setTimeout(100000)
10
11 moduleIntegrationTestRunner<IOrderModuleService>({
12 moduleName: Modules.ORDER,
13 testSuite: ({ service, MikroOrmWrapper }) => {
14 describe("deleteOrders", () => {
15 let order: OrderDTO
16
17 beforeEach(async () => {
18 order = await service.createOrders({
19 email: "foo@bar.com",
20 items: [
21 {
22 title: "Item 1",
23 subtitle: "Subtitle 1",
24 thumbnail: "thumbnail1.jpg",
25 quantity: 1,
26 product_id: "product1",
27 product_title: "Product 1",
28 product_description: "Description 1",
29 product_subtitle: "Product Subtitle 1",
30 product_type: "Type 1",
31 product_type_id: "type_1",
32 product_collection: "Collection 1",
33 product_handle: "handle1",
34 variant_id: "variant1",
35 variant_sku: "SKU1",
36 variant_barcode: "Barcode1",
37 variant_title: "Variant 1",
38 variant_option_values: {
39 color: "Red",
40 size: "Large",
41 },
42 requires_shipping: true,
43 is_discountable: true,
44 is_tax_inclusive: true,
45 compare_at_unit_price: 10,
46 unit_price: 20,
47 tax_lines: [
48 {
49 description: "Tax 1",
50 tax_rate_id: "tax_usa",
51 code: "code",
52 rate: 10,
53 provider_id: "taxify_master",
54 },
55 ],
56 adjustments: [
57 {
58 code: "VIP_10",
59 amount: 10,
60 description: "VIP discount",
61 promotion_id: "prom_123",
62 provider_id: "coupon_kings",
63 },
64 ],
65 },
66 ],
67 shipping_methods: [
68 {
69 name: "Test shipping method",
70 amount: 10,
71 data: {},
72 tax_lines: [
73 {
74 description: "shipping Tax 1",
75 tax_rate_id: "tax_usa_shipping",
76 code: "code",
77 rate: 10,
78 },
79 ],
80 adjustments: [
81 {
82 code: "VIP_10",
83 amount: 1,
84 description: "VIP discount",
85 promotion_id: "prom_123",
86 },
87 ],
88 },
89 ],
90 shipping_address: {
91 first_name: "Test",
92 last_name: "Test",
93 address_1: "Test",
94 city: "Test",
95 country_code: "US",
96 postal_code: "12345",
97 phone: "12345",
98 },
99 billing_address: {
100 first_name: "Test",
101 last_name: "Test",
102 address_1: "Test",
103 city: "Test",
104 country_code: "US",
105 postal_code: "12345",
106 },
107 currency_code: "usd",
108 customer_id: "joe",
109 } as CreateOrderDTO)
110 })
111
112 it("should delete an order, shipping method, taxes, adjustments, and items", async function () {
113 const lineItemsBefore = await service.listOrderLineItems({})
114 const itemAdjustmentsBefore =
115 await service.listOrderLineItemAdjustments({})
116 const itemTaxLinesBefore = await service.listOrderLineItemTaxLines({})
117 const shippingMethodTaxLinesBefore =
118 await service.listOrderShippingMethodTaxLines({})
119 const shippingMethodAdjustmentsBefore =
120 await service.listOrderShippingMethodAdjustments({})
121 const shippingMethodsBefore = await service.listOrderShippingMethods(
122 {},
123 {}
124 )
125
126 expect(lineItemsBefore.length).toEqual(1)
127 expect(itemAdjustmentsBefore.length).toEqual(1)
128 expect(itemTaxLinesBefore.length).toEqual(1)
129 expect(shippingMethodTaxLinesBefore.length).toEqual(1)
130 expect(shippingMethodAdjustmentsBefore.length).toEqual(1)
131 expect(shippingMethodsBefore.length).toEqual(1)
132
133 await service.deleteOrders(order.id)
134
135 const lineItems = await service.listOrderLineItems({})
136 const itemAdjustments = await service.listOrderLineItemAdjustments({})
137 const itemTaxLines = await service.listOrderLineItemTaxLines({})
138 const shippingMethodTaxLines =
139 await service.listOrderShippingMethodTaxLines({})
140 const shippingMethodAdjustments =
141 await service.listOrderShippingMethodAdjustments({})
142 const shippingMethods = await service.listOrderShippingMethods({}, {})
143
144 expect(lineItems).toEqual([])
145 expect(itemAdjustments).toEqual([])
146 expect(itemTaxLines).toEqual([])
147 expect(shippingMethodTaxLines).toEqual([])
148 expect(shippingMethodAdjustments).toEqual([])
149 expect(shippingMethods).toEqual([])
150 })
151
152 // CONTEXT: The following tests checks that deleting an order without an address does not affect other orders through cascade deletes.
153 // See the following advisory for more details: https://github.com/medusajs/medusa/security/advisories/GHSA-hc6q-q5gm-w585
154 it("should delete an order without an address without affecting other orders", async function () {
155 const createdOrder = await service.retrieveOrder(order.id)
156 expect(createdOrder).toEqual(
157 expect.objectContaining({
158 id: order.id,
159 })
160 )
161
162 const orderWithoutAddress = await service.createOrders({
163 email: "foo@bar.com",
164 currency_code: "usd",
165 customer_id: "joe",
166 } as CreateOrderDTO)
167
168 await service.deleteOrders(orderWithoutAddress.id)
169
170 const orders = await service.listOrders({})
171 expect(orders.length).toEqual(1)
172 expect(orders[0].id).toEqual(order.id)
173 })
174
175 it("should delete an order address and set null on the order through the FK", async function () {
176 const createdOrder = await service.retrieveOrder(order.id)
177 expect(createdOrder).toEqual(
178 expect.objectContaining({
179 id: order.id,
180 })
181 )
182
183 const createdOrderWithAddresses = await service.createOrders({
184 email: "foo@bar.com",
185 currency_code: "usd",
186 customer_id: "joe",
187 shipping_address: {
188 first_name: "Test",
189 last_name: "Test",
190 address_1: "Test",
191 city: "Test",
192 country_code: "US",
193 postal_code: "12345",
194 },
195 billing_address: {
196 first_name: "Test",
197 last_name: "Test",
198 address_1: "Test",
199 city: "Test",
200 country_code: "US",
201 postal_code: "12345",
202 },
203 } as CreateOrderDTO)
204
205 const orderWithAddresses = await service.retrieveOrder(
206 createdOrderWithAddresses.id,
207 { relations: ["shipping_address", "billing_address"] }
208 )
209
210 await service.deleteOrderAddresses([
211 orderWithAddresses.shipping_address?.id!,
212 orderWithAddresses.billing_address?.id!,
213 ])
214
215 const retrievedOrder = await service.retrieveOrder(
216 createdOrderWithAddresses.id,
217 { relations: ["shipping_address", "billing_address"] }
218 )
219
220 expect(retrievedOrder.shipping_address).toEqual(null)
221 expect(retrievedOrder.billing_address).toEqual(null)
222 })
223 })
224 },
225 })