1 import { HttpTypes } from "@medusajs/types"
2 import { Client } from "../client.js"
3 import { ClientHeaders } from "../types.js"
4
5 export class Campaign {
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 campaign by its ID. It sends a request to the
19 * [Get Campaign](https://docs.medusajs.com/api/admin/campaigns/get-a-campaign) API route.
20 *
21 * @param id - The campaign's ID.
22 * @param query - Configure the fields to retrieve in the campaign.
23 * @param headers - Headers to pass in the request
24 * @returns The campaign's details.
25 *
26 * @example
27 * To retrieve a campaign by its ID:
28 *
29 * ```ts
30 * sdk.admin.campaign.retrieve("procamp_123")
31 * .then(({ campaign }) => {
32 * console.log(campaign)
33 * })
34 * ```
35 *
36 * To specify the fields and relations to retrieve:
37 *
38 * ```ts
39 * sdk.admin.campaign.retrieve("procamp_123", {
40 * fields: "id,*budget"
41 * })
42 * .then(({ campaign }) => {
43 * console.log(campaign)
44 * })
45 * ```
46 *
47 * Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/store/select-fields-and-relations).
48 */
49 async retrieve(
50 id: string,
51 query?: HttpTypes.AdminGetCampaignParams,
52 headers?: ClientHeaders
53 ) {
54 return await this.client.fetch<HttpTypes.AdminCampaignResponse>(
55 `/admin/campaigns/${id}`,
56 {
57 headers,
58 query,
59 }
60 )
61 }
62
63 /**
64 * This method retrieves a paginated list of campaigns. It sends a request to the
65 * [List Campaigns](https://docs.medusajs.com/api/admin/campaigns/list-campaigns) API route.
66 *
67 * @param query - Filters and pagination configurations.
68 * @param headers - Headers to pass in the request.
69 * @returns The paginated list of campaigns.
70 *
71 * @example
72 * To retrieve the list of campaigns:
73 *
74 * ```ts
75 * sdk.admin.campaign.list()
76 * .then(({ campaigns, count, limit, offset }) => {
77 * console.log(campaigns)
78 * })
79 * ```
80 *
81 * To configure the pagination, pass the `limit` and `offset` query parameters.
82 *
83 * For example, to retrieve only 10 items and skip 10 items:
84 *
85 * ```ts
86 * sdk.admin.campaign.list({
87 * limit: 10,
88 * offset: 10
89 * })
90 * .then(({ campaigns, count, limit, offset }) => {
91 * console.log(campaigns)
92 * })
93 * ```
94 *
95 * Using the `fields` query parameter, you can specify the fields and relations to retrieve
96 * in each campaign:
97 *
98 * ```ts
99 * sdk.admin.campaign.list({
100 * fields: "id,*budget"
101 * })
102 * .then(({ campaigns, count, limit, offset }) => {
103 * console.log(campaigns)
104 * })
105 * ```
106 *
107 * Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/store/select-fields-and-relations).
108 */
109 async list(
110 query?: HttpTypes.AdminGetCampaignsParams,
111 headers?: ClientHeaders
112 ) {
113 return await this.client.fetch<HttpTypes.AdminCampaignListResponse>(
114 `/admin/campaigns`,
115 {
116 headers,
117 query,
118 }
119 )
120 }
121
122 /**
123 * This method creates a campaign. It sends a request to the
124 * [Create Campaign](https://docs.medusajs.com/api/admin/campaigns/create-campaign) API route.
125 *
126 * @param payload - The details of the campaign to create.
127 * @param headers - Headers to pass in the request
128 * @returns The campaign's details.
129 *
130 * @example
131 * sdk.admin.campaign.create({
132 * name: "Summer Campaign"
133 * })
134 * .then(({ campaign }) => {
135 * console.log(campaign)
136 * })
137 */
138 async create(
139 payload: HttpTypes.AdminCreateCampaign,
140 headers?: ClientHeaders
141 ) {
142 return await this.client.fetch<HttpTypes.AdminCampaignResponse>(
143 `/admin/campaigns`,
144 {
145 method: "POST",
146 headers,
147 body: payload,
148 }
149 )
150 }
151
152 /**
153 * This method updates a campaign. It sends a request to the
154 * [Update Campaign](https://docs.medusajs.com/api/admin/campaigns/update-a-campaign) API route.
155 *
156 * @param id - The campaign's ID.
157 * @param payload - The data to update in the campaign.
158 * @param headers - Headers to pass in the request
159 * @returns The campaign's details.
160 *
161 * @example
162 * sdk.admin.campaign.update("procamp_123", {
163 * name: "Summer Campaign"
164 * })
165 * .then(({ campaign }) => {
166 * console.log(campaign)
167 * })
168 */
169 async update(
170 id: string,
171 payload: HttpTypes.AdminUpdateCampaign,
172 headers?: ClientHeaders
173 ) {
174 return await this.client.fetch<HttpTypes.AdminCampaignResponse>(
175 `/admin/campaigns/${id}`,
176 {
177 method: "POST",
178 headers,
179 body: payload,
180 }
181 )
182 }
183
184 /**
185 * This method deletes a campaign by its ID. It sends a request to the
186 * [Delete Campaign](https://docs.medusajs.com/api/admin/campaigns/delete-a-campaign) API route.
187 *
188 * @param id - The campaign's ID.
189 * @param headers - Headers to pass in the request
190 * @returns The deletion's details.
191 *
192 * @example
193 * sdk.admin.campaign.delete("procamp_123")
194 * .then(({ deleted }) => {
195 * console.log(deleted)
196 * })
197 */
198 async delete(id: string, headers?: ClientHeaders) {
199 return await this.client.fetch<HttpTypes.DeleteResponse<"campaign">>(
200 `/admin/campaigns/${id}`,
201 {
202 method: "DELETE",
203 headers,
204 }
205 )
206 }
207
208 /**
209 * This method manages the promotions of a campaign to either add or remove the association between them.
210 * It sends a request to the [Manage Promotions](https://docs.medusajs.com/api/admin/campaigns/manage-promotions)
211 * API route.
212 *
213 * @param id - The campaign's ID.
214 * @param payload - The promotions to add or remove associations to them.
215 * @param headers - Headers to pass in the request
216 * @returns The campaign's details.
217 *
218 * @example
219 * sdk.admin.campaign.batchPromotions("procamp_123", {
220 * add: ["prom_123", "prom_456"],
221 * remove: ["prom_789"]
222 * })
223 * .then(({ campaign }) => {
224 * console.log(campaign)
225 * })
226 */
227 async batchPromotions(
228 id: string,
229 payload: HttpTypes.AdminBatchLink,
230 headers?: ClientHeaders
231 ) {
232 return await this.client.fetch<HttpTypes.AdminCampaignResponse>(
233 `/admin/campaigns/${id}/promotions`,
234 {
235 method: "POST",
236 headers,
237 body: payload,
238 }
239 )
240 }
241 }