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