1 import { HttpTypes } from "@medusajs/types"
2 import { Client } from "../client.js"
3 import { ClientHeaders } from "../types.js"
4
5 export class OrderEdit {
6 /**
7 * @ignore
8 */
9 private client: Client
10 /**
11 * @ignore
12 */
13 constructor(client: Client) {
14 this.client = client
15 }
16
17 /**
18 * This method creates an order edit request. It sends a HTTP request to the
19 * [Create Order Edit](https://docs.medusajs.com/api/admin/order-edits/create-order-edit)
20 * API route.
21 *
22 * @param body - The order edit's details.
23 * @param query - Configure the fields to retrieve in the order edit.
24 * @param headers - Headers to pass in the request.
25 * @returns The order edit's details.
26 *
27 * @example
28 * sdk.admin.orderEdit.initiateRequest({
29 * order_id: "order_123"
30 * })
31 * .then(({ order_change }) => {
32 * console.log(order_change)
33 * })
34 */
35 async initiateRequest(
36 body: HttpTypes.AdminInitiateOrderEditRequest,
37 query?: HttpTypes.SelectParams,
38 headers?: ClientHeaders
39 ) {
40 return await this.client.fetch<HttpTypes.AdminOrderEditResponse>(
41 `/admin/order-edits`,
42 {
43 method: "POST",
44 headers,
45 body,
46 query,
47 }
48 )
49 }
50
51 /**
52 * This method changes an order edit to requested. It sends a request to the
53 * [Request Order Edit](https://docs.medusajs.com/api/admin/order-edits/request-order-edit)
54 * API route.
55 *
56 * @param id - The ID of the order that is being edited.
57 * @param body - The order edit request's details.
58 * @param query - Configure the fields to retrieve in the order preview.
59 * @param headers - Headers to pass in the request.
60 * @returns The order preview's details.
61 *
62 * @example
63 * sdk.admin.orderEdit.request("order_123", {
64 * no_notification: false
65 * })
66 * .then(({ order_preview }) => {
67 * console.log(order_preview)
68 * })
69 */
70 async request(
71 id: string,
72 body?: HttpTypes.AdminRequestOrderEdit,
73 query?: HttpTypes.SelectParams,
74 headers?: ClientHeaders
75 ) {
76 return await this.client.fetch<HttpTypes.AdminOrderEditPreviewResponse>(
77 `/admin/order-edits/${id}/request`,
78 {
79 method: "POST",
80 headers,
81 body,
82 query,
83 }
84 )
85 }
86
87 /**
88 * This method confirms an order edit and applies it on the order. It sends a request
89 * to the [Confirm Order Edit](https://docs.medusajs.com/api/admin/order-edits/confirm-order-edit)
90 * API route.
91 *
92 * @param id - The ID of the order that is being edited.
93 * @param query - Configure the fields to retrieve in the order preview.
94 * @param headers - Headers to pass in the request.
95 * @returns The order preview's details.
96 *
97 * @example
98 * sdk.admin.orderEdit.confirm("order_123")
99 * .then(({ order_preview }) => {
100 * console.log(order_preview)
101 * })
102 */
103 async confirm(
104 id: string,
105 query?: HttpTypes.SelectParams,
106 headers?: ClientHeaders
107 ) {
108 return await this.client.fetch<HttpTypes.AdminOrderEditPreviewResponse>(
109 `/admin/order-edits/${id}/confirm`,
110 {
111 method: "POST",
112 headers,
113 query,
114 }
115 )
116 }
117
118 /**
119 * This method cancels a requested order edit. It sends a request to the
120 * [Cancel Order Edit](https://docs.medusajs.com/api/admin/order-edits/cancel-order-edit)
121 * API route.
122 *
123 * @param id - The ID of the order that is being edited.
124 * @param query - Query parameters
125 * @param headers - Headers to pass in the request.
126 * @returns The deletion's details.
127 *
128 * @example
129 * sdk.admin.orderEdit.cancelRequest("order_123")
130 * .then(({ deleted }) => {
131 * console.log(deleted)
132 * })
133 */
134 async cancelRequest(
135 id: string,
136 query?: HttpTypes.SelectParams,
137 headers?: ClientHeaders
138 ) {
139 return await this.client.fetch<HttpTypes.AdminOrderEditDeleteResponse>(
140 `/admin/order-edits/${id}`,
141 {
142 method: "DELETE",
143 headers,
144 query,
145 }
146 )
147 }
148
149 /**
150 * This method adds items to an order edit. These items will have the action `ITEM_ADD`.
151 *
152 * The method sends a request to the [Add Items](https://docs.medusajs.com/api/admin/order-edits/add-items)
153 * API route.
154 *
155 * @param id - The ID of the order that is being edited.
156 * @param body - The items to add.
157 * @param query - Configure the fields to retrieve in the order preview.
158 * @param headers - Headers to pass in the request.
159 * @returns The order preview's details.
160 *
161 * @example
162 * sdk.admin.orderEdit.addItems("order_123", {
163 * items: [
164 * {
165 * variant_id: "variant_123",
166 * quantity: 1
167 * }
168 * ]
169 * })
170 * .then(({ order_preview }) => {
171 * console.log(order_preview)
172 * })
173 */
174 async addItems(
175 id: string,
176 body: HttpTypes.AdminAddOrderEditItems,
177 query?: HttpTypes.SelectParams,
178 headers?: ClientHeaders
179 ) {
180 return await this.client.fetch<HttpTypes.AdminOrderEditPreviewResponse>(
181 `/admin/order-edits/${id}/items`,
182 {
183 method: "POST",
184 headers,
185 body,
186 query,
187 }
188 )
189 }
190
191 /**
192 * This method updates the quantity and other details of an item in an order. It sends a request to the
193 * [Update Item Quantity](https://docs.medusajs.com/api/admin/order-edits/update-item-quantity)
194 * API route.
195 *
196 * You can also use this method to remove an item from an order by setting the `quantity` to `0`.
197 *
198 * @param id - The ID of the order that is being edited.
199 * @param itemId - The item's ID in the order.
200 * @param body - The data to edit in the item.
201 * @param query - Configure the fields to retrieve in the order preview.
202 * @param headers - Headers to pass in the request.
203 * @returns The order preview's details.
204 *
205 * @example
206 * sdk.admin.orderEdit.updateOriginalItem(
207 * "order_123",
208 * "orli_123",
209 * {
210 * quantity: 1
211 * }
212 * )
213 * .then(({ order_preview }) => {
214 * console.log(order_preview)
215 * })
216 */
217 async updateOriginalItem(
218 id: string,
219 itemId: string,
220 body: HttpTypes.AdminUpdateOrderEditItem,
221 query?: HttpTypes.SelectParams,
222 headers?: ClientHeaders
223 ) {
224 return await this.client.fetch<HttpTypes.AdminOrderEditPreviewResponse>(
225 `/admin/order-edits/${id}/items/item/${itemId}`,
226 {
227 method: "POST",
228 headers,
229 body,
230 query,
231 }
232 )
233 }
234
235 /**
236 * This method updates an added item in the order edit by the ID of the item's `ITEM_ADD` action.
237 *
238 * Every item has an `actions` property, whose value is an array of actions.
239 * You can check the action's name using its `action` property, and use the value of the `id` property.
240 *
241 * It sends a request
242 * to the [Update Item](https://docs.medusajs.com/api/admin/order-edits/update-item)
243 * API route.
244 *
245 * @param id - The ID of the order that is being edited.
246 * @param actionId - The id of the new item's `ITEM_ADD` action.
247 * @param body - The data to update.
248 * @param query - Configure the fields to retrieve in the order preview.
249 * @param headers - Headers to pass in the request.
250 * @returns The order preview's details.
251 *
252 * @example
253 * sdk.admin.orderEdit.updateAddedItem(
254 * "order_123",
255 * "orli_123",
256 * {
257 * quantity: 1
258 * }
259 * )
260 * .then(({ order_preview }) => {
261 * console.log(order_preview)
262 * })
263 */
264 async updateAddedItem(
265 id: string,
266 actionId: string,
267 body: HttpTypes.AdminUpdateOrderEditItem,
268 query?: HttpTypes.SelectParams,
269 headers?: ClientHeaders
270 ) {
271 return await this.client.fetch<HttpTypes.AdminOrderEditPreviewResponse>(
272 `/admin/order-edits/${id}/items/${actionId}`,
273 {
274 method: "POST",
275 headers,
276 body,
277 query,
278 }
279 )
280 }
281
282 /**
283 * This method removes an added item in the order edit by the ID of the item's `ITEM_ADD` action.
284 *
285 * Every item has an `actions` property, whose value is an array of actions.
286 * You can check the action's name using its `action` property, and use the value of the `id` property.
287 *
288 * @param id - The ID of the order that is being edited.
289 * @param actionId - The id of the new item's `ITEM_ADD` action.
290 * @param query - Configure the fields to retrieve in the order preview.
291 * @param headers - Headers to pass in the request.
292 * @returns The order preview's details.
293 *
294 * @example
295 * sdk.admin.orderEdit.removeAddedItem(
296 * "order_123",
297 * "orli_123",
298 * )
299 * .then(({ order_preview }) => {
300 * console.log(order_preview)
301 * })
302 */
303 async removeAddedItem(
304 id: string,
305 actionId: string,
306 query?: HttpTypes.SelectParams,
307 headers?: ClientHeaders
308 ) {
309 return await this.client.fetch<HttpTypes.AdminOrderEditPreviewResponse>(
310 `/admin/order-edits/${id}/items/${actionId}`,
311 {
312 method: "DELETE",
313 headers,
314 query,
315 }
316 )
317 }
318 }