1 import { HttpTypes } from "@medusajs/types"
2 import { Client } from "../client.js"
3 import { ClientHeaders } from "../types.js"
4
5 export class Promotion {
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 retrieves a promotion by its ID. It sends a request to the
19 * [Retrieve Promotion](https://docs.medusajs.com/api/admin/promotions/get-a-promotion)
20 * API route.
21 *
22 * @param id - The promotion's ID.
23 * @param query - Configure the fields to retrieve in the promotion.
24 * @param headers - Headers to pass in the request.
25 * @returns The promotion's details.
26 *
27 * @example
28 * To retrieve a promotion by its ID:
29 *
30 * ```ts
31 * sdk.admin.promotion.retrieve("promo_123")
32 * .then(({ promotion }) => {
33 * console.log(promotion)
34 * })
35 * ```
36 *
37 * To specify the fields and relations to retrieve:
38 *
39 * ```ts
40 * sdk.admin.promotion.retrieve("promo_123", {
41 * fields: "id,*application_method"
42 * })
43 * .then(({ promotion }) => {
44 * console.log(promotion)
45 * })
46 * ```
47 *
48 * Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/admin/select-fields-and-relations).
49 */
50 async retrieve(
51 id: string,
52 query?: HttpTypes.AdminGetPromotionParams,
53 headers?: ClientHeaders
54 ) {
55 return await this.client.fetch<HttpTypes.AdminPromotionResponse>(
56 `/admin/promotions/${id}`,
57 {
58 headers,
59 query,
60 }
61 )
62 }
63
64 /**
65 * This method retrieves a list of promotions. It sends a request to the
66 * [List Promotions](https://docs.medusajs.com/api/admin/promotions/list-promotions)
67 * API route.
68 *
69 * @param query - Filters and pagination configurations.
70 * @param headers - Headers to pass in the request.
71 * @returns The list of promotions.
72 *
73 * @example
74 * To retrieve the list of promotions:
75 *
76 * ```ts
77 * sdk.admin.promotion.list()
78 * .then(({ promotions, count, limit, offset }) => {
79 * console.log(promotions)
80 * })
81 * ```
82 *
83 * To configure the pagination, pass the `limit` and `offset` query parameters.
84 *
85 * For example, to retrieve only 10 items and skip 10 items:
86 *
87 * ```ts
88 * sdk.admin.promotion.list({
89 * limit: 10,
90 * offset: 10
91 * })
92 * .then(({ promotions, count, limit, offset }) => {
93 * console.log(promotions)
94 * })
95 * ```
96 *
97 * Using the `fields` query parameter, you can specify the fields and relations to retrieve
98 * in each promotion:
99 *
100 * ```ts
101 * sdk.admin.promotion.list({
102 * fields: "id,*application_method"
103 * })
104 * .then(({ promotions, count, limit, offset }) => {
105 * console.log(promotions)
106 * })
107 * ```
108 *
109 * Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/admin/select-fields-and-relations).
110 */
111 async list(
112 query?: HttpTypes.AdminGetPromotionsParams,
113 headers?: ClientHeaders
114 ) {
115 return await this.client.fetch<HttpTypes.AdminPromotionListResponse>(
116 `/admin/promotions`,
117 {
118 headers,
119 query,
120 }
121 )
122 }
123
124 /**
125 * This method creates a new promotion. It sends a request to the
126 * [Create Promotion](https://docs.medusajs.com/api/admin/promotions/create-promotion)
127 * API route.
128 *
129 * @param payload - The promotion to create.
130 * @param headers - Headers to pass in the request.
131 * @returns The promotion's details.
132 *
133 * @example
134 * sdk.admin.promotion.create({
135 * name: "My Promotion",
136 * description: "This is a test promotion",
137 * code: "PROMO123",
138 * starts_at: "2021-01-01",
139 * ends_at: "2021-01-01",
140 * })
141 * .then(({ promotion }) => {
142 * console.log(promotion)
143 * })
144 */
145 async create(
146 payload: HttpTypes.AdminCreatePromotion,
147 headers?: ClientHeaders
148 ) {
149 return await this.client.fetch<HttpTypes.AdminPromotionResponse>(
150 `/admin/promotions`,
151 {
152 method: "POST",
153 headers,
154 body: payload,
155 }
156 )
157 }
158
159 /**
160 * This method updates a promotion. It sends a request to the
161 * [Update Promotion](https://docs.medusajs.com/api/admin/promotions/update-a-promotion)
162 * API route.
163 *
164 * @param id - The promotion's ID.
165 * @param payload - The details to update in the promotion.
166 * @param headers - Headers to pass in the request.
167 * @returns The promotion's details.
168 *
169 * @example
170 * sdk.admin.promotion.update("promo_123", {
171 * code: "PROMO123",
172 * })
173 * .then(({ promotion }) => {
174 * console.log(promotion)
175 * })
176 */
177 async update(
178 id: string,
179 payload: HttpTypes.AdminUpdatePromotion,
180 headers?: ClientHeaders
181 ) {
182 return await this.client.fetch<HttpTypes.AdminPromotionResponse>(
183 `/admin/promotions/${id}`,
184 {
185 method: "POST",
186 headers,
187 body: payload,
188 }
189 )
190 }
191
192 /**
193 * This method deletes a promotion. It sends a request to the
194 * [Delete Promotion](https://docs.medusajs.com/api/admin/promotions/delete-a-promotion)
195 * API route.
196 *
197 * @param id - The promotion's ID.
198 * @param headers - Headers to pass in the request.
199 * @returns The deleted promotion's details.
200 *
201 * @example
202 * sdk.admin.promotion.delete("promo_123")
203 * .then(({ promotion }) => {
204 * console.log(promotion)
205 * })
206 */
207 async delete(id: string, headers?: ClientHeaders) {
208 return await this.client.fetch<HttpTypes.DeleteResponse<"promotion">>(
209 `/admin/promotions/${id}`,
210 {
211 method: "DELETE",
212 headers,
213 }
214 )
215 }
216
217 /**
218 * This method creates and adds rules to a promotion. It can be the promotion's rules,
219 * or its application method's buy or target rules. That depends on the rule type
220 * you specify as a parameter.
221 *
222 * - If you set the `ruleType` to `rules`, the method sends a request to the
223 * [Manage Promotion's Rules API Route](https://docs.medusajs.com/api/admin/promotions/manage-rules).
224 * - If you set the `ruleType` to `buy-rules`, the method sends a request to the
225 * [Manage Promotion's Buy Rules API Route](https://docs.medusajs.com/api/admin/promotions/manage-buy-rules).
226 * - If you set the `ruleType` to `target-rules`, the method sends a request to the
227 * [Manage Promotion's Target Rules API Route](https://docs.medusajs.com/api/admin/promotions/manage-target-rules).
228 *
229 * @param id - The promotion's ID.
230 * @param ruleType - The type of rules to create.
231 * @param payload - The rules to create.
232 * @param headers - Headers to pass in the request.
233 * @returns The promotion's details.
234 *
235 * @example
236 * sdk.admin.promotion.addRules("promo_123", "rules", {
237 * rules: [
238 * {
239 * operator: "eq",
240 * attribute: "product_id",
241 * values: ["prod_123"]
242 * }
243 * ]
244 * })
245 * .then(({ promotion }) => {
246 * console.log(promotion)
247 * })
248 */
249 async addRules(
250 id: string,
251 ruleType: string,
252 payload: HttpTypes.BatchAddPromotionRulesReq,
253 headers?: ClientHeaders
254 ) {
255 return await this.client.fetch<HttpTypes.AdminPromotionResponse>(
256 `/admin/promotions/${id}/${ruleType}/batch`,
257 {
258 method: "POST",
259 headers,
260 body: { create: payload.rules },
261 }
262 )
263 }
264
265 /**
266 * This method updates the rules of a promotion. It can be the promotion's rules,
267 * or its application method's buy or target rules. That depends on the rule type
268 * you specify as a parameter.
269 *
270 * - If you set the `ruleType` to `rules`, the method sends a request to the
271 * [Manage Promotion's Rules API Route](https://docs.medusajs.com/api/admin/promotions/manage-rules).
272 * - If you set the `ruleType` to `buy-rules`, the method sends a request to the
273 * [Manage Promotion's Buy Rules API Route](https://docs.medusajs.com/api/admin/promotions/manage-buy-rules).
274 * - If you set the `ruleType` to `target-rules`, the method sends a request to the
275 * [Manage Promotion's Target Rules API Route](https://docs.medusajs.com/api/admin/promotions/manage-target-rules).
276 *
277 * @param id - The promotion's ID.
278 * @param ruleType - The type of rules to update.
279 * @param payload - The rules to update.
280 * @param headers - Headers to pass in the request.
281 * @returns The promotion's details.
282 *
283 * @example
284 * sdk.admin.promotion.updateRules("promo_123", "rules", {
285 * rules: [
286 * {
287 * id: "rule_123",
288 * operator: "ne",
289 * }
290 * ]
291 * })
292 * .then(({ promotion }) => {
293 * console.log(promotion)
294 * })
295 */
296 async updateRules(
297 id: string,
298 ruleType: string,
299 payload: HttpTypes.BatchUpdatePromotionRulesReq,
300 headers?: ClientHeaders
301 ) {
302 return await this.client.fetch<HttpTypes.AdminPromotionResponse>(
303 `/admin/promotions/${id}/${ruleType}/batch`,
304 {
305 method: "POST",
306 headers,
307 body: { update: payload.rules },
308 }
309 )
310 }
311
312 /**
313 * This method removes rules from a promotion. It can be the promotion's rules,
314 * or its application method's buy or target rules. That depends on the rule type
315 * you specify as a parameter.
316 *
317 * - If you set the `ruleType` to `rules`, the method sends a request to the
318 * [Manage Promotion's Rules API Route](https://docs.medusajs.com/api/admin/promotions/manage-rules).
319 * - If you set the `ruleType` to `buy-rules`, the method sends a request to the
320 * [Manage Promotion's Buy Rules API Route](https://docs.medusajs.com/api/admin/promotions/manage-buy-rules).
321 * - If you set the `ruleType` to `target-rules`, the method sends a request to the
322 * [Manage Promotion's Target Rules API Route](https://docs.medusajs.com/api/admin/promotions/manage-target-rules).
323 *
324 * @param id - The promotion's ID.
325 * @param ruleType - The type of rules to remove.
326 * @param payload - The rules to remove.
327 * @param headers - Headers to pass in the request.
328 * @returns The promotion's details.
329 *
330 * @example
331 * sdk.admin.promotion.removeRules("promo_123", "rules", {
332 * rule_ids: ["rule_123"]
333 * })
334 * .then(({ promotion }) => {
335 * console.log(promotion)
336 * })
337 */
338 async removeRules(
339 id: string,
340 ruleType: string,
341 payload: HttpTypes.BatchRemovePromotionRulesReq,
342 headers?: ClientHeaders
343 ) {
344 return await this.client.fetch<HttpTypes.AdminPromotionResponse>(
345 `/admin/promotions/${id}/${ruleType}/batch`,
346 {
347 method: "POST",
348 headers,
349 body: { delete: payload.rule_ids },
350 }
351 )
352 }
353
354 /**
355 * This method retrieves the rules of a promotion. It can be the promotion's rules,
356 * or its application method's buy or target rules. That depends on the rule type
357 * you specify as a parameter.
358 *
359 * This method sends a request to the
360 * [List Rules of a Promotion API Route](https://docs.medusajs.com/api/admin/promotions/list-rules)
361 *
362 * @param id - The promotion's ID.
363 * @param ruleType - The type of rules to retrieve. Can be `rules`, `buy-rules`, or `target-rules`.
364 * @param query - Configure the fields to retrieve in the rules.
365 * @param headers - Headers to pass in the request.
366 * @returns The promotion's rules.
367 *
368 * @example
369 * sdk.admin.promotion.listRules("promo_123", "rules")
370 * .then(({ rules }) => {
371 * console.log(rules)
372 * })
373 */
374 async listRules(
375 id: string | null,
376 ruleType: string,
377 query?: HttpTypes.AdminGetPromotionRuleParams,
378 headers?: ClientHeaders
379 ) {
380 // eslint-disable-next-line max-len
381 return await this.client.fetch<HttpTypes.AdminPromotionRuleListResponse>(
382 `/admin/promotions/${id}/${ruleType}`,
383 {
384 headers,
385 query,
386 }
387 )
388 }
389
390 /**
391 * Retrieve a list of potential rule attributes for the promotion and application method types specified in the query parameters. Only the attributes of the rule type specified in the path parameter are retrieved:
392 *
393 * - If `rule_type` is `rules`, the attributes of the promotion's type are retrieved.
394 * - If `rule_type` is `target-rules`, the target rules' attributes of the application method's type are retrieved.
395 * - If `rule_type` is `buy-rules`, the buy rules' attributes of the application method's type are retrieved.
396 *
397 * This method sends a request to the
398 * [List Rule Attribute Options API Route](https://docs.medusajs.com/api/admin/promotions/list-potential-rule-attributes)
399 *
400 * @param ruleType - The type of rules to retrieve the attributes for. Can be `rules`, `buy-rules`, or `target-rules`.
401 * @param promotionType - The type of promotion to retrieve the attributes for. It can be `standard` or `buyget`.
402 * @param applicationMethodTargetType - The type of application method to retrieve the attributes for. It can be `order`, `items` (default) or `shipping_methods`.
403 * @param headers - Headers to pass in the request.
404 * @returns The list of rule attributes.
405 *
406 * @example
407 * sdk.admin.promotion.listRuleAttributes("rules", "standard")
408 * .then(({ attributes }) => {
409 * console.log(attributes)
410 * })
411 */
412 async listRuleAttributes(
413 ruleType: string,
414 promotionType?: string,
415 applicationMethodTargetType?: string,
416 headers?: ClientHeaders
417 ) {
418 // eslint-disable-next-line max-len
419 return await this.client.fetch<HttpTypes.AdminRuleAttributeOptionsListResponse>(
420 `/admin/promotions/rule-attribute-options/${ruleType}`,
421 {
422 headers,
423 query: {
424 promotion_type: promotionType,
425 application_method_target_type: applicationMethodTargetType,
426 },
427 }
428 )
429 }
430
431 /**
432 * Retrieve all potential values for promotion rules and target and buy rules based on the specified rule attribute and type.
433 * For example, if you provide the ID of the `currency_code` rule attribute, and set `rule_type` to rules,
434 * a list of currencies are retrieved in label-value pairs.
435 *
436 * This method sends a request to the
437 * [List Rule Values API Route](https://docs.medusajs.com/api/admin/promotions/list-rule-values)
438 *
439 * @param ruleType - The type of rules to retrieve the values for. Can be `rules`, `buy-rules`, or `target-rules`.
440 * @param ruleValue - The ID of the rule attribute to retrieve the values for.
441 * @param query - Configure the fields to retrieve in the rule values.
442 * @param headers - Headers to pass in the request.
443 * @returns The list of rule values.
444 *
445 * @example
446 * sdk.admin.promotion.listRuleValues("rules", "attr_123")
447 * .then(({ values }) => {
448 * console.log(values)
449 * })
450 */
451 async listRuleValues(
452 ruleType: string,
453 ruleValue: string,
454 query?: HttpTypes.AdminGetPromotionsRuleValueParams,
455 headers?: ClientHeaders
456 ) {
457 return await this.client.fetch<HttpTypes.AdminRuleValueOptionsListResponse>(
458 `/admin/promotions/rule-value-options/${ruleType}/${ruleValue}`,
459 {
460 headers,
461 query,
462 }
463 )
464 }
465 }