feat: customer groups client endpoints (#1147)
* feat: WIP add customer group endpoints, fix generated type exports from api * fix: export api types, add `list`method * fix: export customer groups types from the `medusa-js` package * fix: cleanup type exports from customer groups api * feat: add customer group batch methods * fix: refactor comments * fix: address PR comments Co-authored-by: fPolic <frane@medusajs.com>
This commit is contained in:
127
packages/medusa-js/src/resources/admin/customer-groups.ts
Normal file
127
packages/medusa-js/src/resources/admin/customer-groups.ts
Normal file
@@ -0,0 +1,127 @@
|
||||
import {
|
||||
AdminPostCustomerGroupsReq,
|
||||
AdminCustomerGroupsRes,
|
||||
AdminGetCustomerGroupsParams,
|
||||
AdminCustomerGroupsListRes,
|
||||
AdminPostCustomerGroupsGroupReq,
|
||||
AdminCustomerGroupsDeleteRes,
|
||||
AdminPostCustomerGroupsGroupCustomersBatchReq,
|
||||
AdminDeleteCustomerGroupsGroupCustomerBatchReq,
|
||||
} from "@medusajs/medusa"
|
||||
import qs from "qs"
|
||||
|
||||
import BaseResource from "../base"
|
||||
import { ResponsePromise } from "../.."
|
||||
|
||||
class AdminCustomerGroupsResource extends BaseResource {
|
||||
/**
|
||||
* Create a customer group.
|
||||
*
|
||||
* @param payload - Customer group info.
|
||||
* @param customHeaders
|
||||
*/
|
||||
create(
|
||||
payload: AdminPostCustomerGroupsReq,
|
||||
customHeaders: Record<string, any> = {}
|
||||
): ResponsePromise<AdminCustomerGroupsRes> {
|
||||
const path = `/admin/customer-groups`
|
||||
return this.client.request("POST", path, payload, {}, customHeaders)
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a customer group.
|
||||
*
|
||||
* @param id - customer group id
|
||||
* @param customHeaders
|
||||
*/
|
||||
retrieve(
|
||||
id: string,
|
||||
customHeaders: Record<string, any> = {}
|
||||
): ResponsePromise<AdminCustomerGroupsRes> {
|
||||
const path = `/admin/customer-groups/${id}`
|
||||
return this.client.request("GET", path, {}, {}, customHeaders)
|
||||
}
|
||||
/**
|
||||
* Updates a customer group
|
||||
*
|
||||
* @param id customer group id
|
||||
* @param payload data to update customer group with
|
||||
* @param customHeaders
|
||||
*/
|
||||
update(
|
||||
id: string,
|
||||
payload: AdminPostCustomerGroupsGroupReq,
|
||||
customHeaders: Record<string, any> = {}
|
||||
): ResponsePromise<AdminCustomerGroupsRes> {
|
||||
const path = `/admin/customer-groups/${id}`
|
||||
return this.client.request("POST", path, payload, {}, customHeaders)
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a cusotmer group.
|
||||
*
|
||||
* @param id - id of the customer gorup
|
||||
* @param customHeaders
|
||||
*/
|
||||
delete(
|
||||
id: string,
|
||||
customHeaders: Record<string, any> = {}
|
||||
): ResponsePromise<AdminCustomerGroupsDeleteRes> {
|
||||
const path = `/admin/customer-groups/${id}`
|
||||
return this.client.request("DELETE", path, {}, {}, customHeaders)
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists customer groups.
|
||||
*
|
||||
* @param query optional
|
||||
* @param customHeaders
|
||||
*/
|
||||
list(
|
||||
query?: AdminGetCustomerGroupsParams,
|
||||
customHeaders: Record<string, any> = {}
|
||||
): ResponsePromise<AdminCustomerGroupsListRes> {
|
||||
let path = `/admin/customer-groups`
|
||||
|
||||
if (query) {
|
||||
const queryString = qs.stringify(query)
|
||||
path = `/admin/customer-groups?${queryString}`
|
||||
}
|
||||
|
||||
return this.client.request("GET", path, {}, {}, customHeaders)
|
||||
}
|
||||
|
||||
/**
|
||||
* Add multiple customers to a customer group.
|
||||
*
|
||||
* @param id - customer group id
|
||||
* @param payload - an object which contains an array of customer ids which will be added to the group
|
||||
* @param customHeaders
|
||||
*/
|
||||
addCustomers(
|
||||
id: string,
|
||||
payload: AdminPostCustomerGroupsGroupCustomersBatchReq,
|
||||
customHeaders: Record<string, any> = {}
|
||||
): Promise<AdminCustomerGroupsRes> {
|
||||
const path = `/admin/customer-groups/${id}/customers/batch`
|
||||
return this.client.request("POST", path, payload, {}, customHeaders)
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove multiple customers from a customer group.
|
||||
*
|
||||
* @param id - customer group id
|
||||
* @param payload - an object which contains an array of customers ids which will be removed from the group
|
||||
* @param customHeaders
|
||||
*/
|
||||
removeCustomers(
|
||||
id: string,
|
||||
payload: AdminDeleteCustomerGroupsGroupCustomerBatchReq,
|
||||
customHeaders: Record<string, any> = {}
|
||||
): Promise<AdminCustomerGroupsRes> {
|
||||
const path = `/admin/customer-groups/${id}/customers/batch`
|
||||
return this.client.request("DELETE", path, payload, {}, customHeaders)
|
||||
}
|
||||
}
|
||||
|
||||
export default AdminCustomerGroupsResource
|
||||
@@ -15,7 +15,10 @@ class AdminCustomersResource extends BaseResource {
|
||||
* @param payload information of customer
|
||||
* @param customHeaders
|
||||
*/
|
||||
create(payload: AdminPostCustomersReq, customHeaders: Record<string, any> = {}): ResponsePromise<AdminCustomersRes> {
|
||||
create(
|
||||
payload: AdminPostCustomersReq,
|
||||
customHeaders: Record<string, any> = {}
|
||||
): ResponsePromise<AdminCustomersRes> {
|
||||
const path = `/admin/customers`
|
||||
return this.client.request("POST", path, payload, {}, customHeaders)
|
||||
}
|
||||
@@ -40,7 +43,10 @@ class AdminCustomersResource extends BaseResource {
|
||||
* @param id customer id
|
||||
* @param customHeaders
|
||||
*/
|
||||
retrieve(id: string, customHeaders: Record<string, any> = {}): ResponsePromise<AdminCustomersRes> {
|
||||
retrieve(
|
||||
id: string,
|
||||
customHeaders: Record<string, any> = {}
|
||||
): ResponsePromise<AdminCustomersRes> {
|
||||
const path = `/admin/customers/${id}`
|
||||
return this.client.request("GET", path, {}, {}, customHeaders)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import BaseResource from "../base"
|
||||
import AdminAuthResource from "./auth"
|
||||
import AdminCustomersResource from "./customers"
|
||||
import AdminCustomerGroupsResource from "./customer-groups"
|
||||
import AdminDiscountsResource from "./discounts"
|
||||
import CollectionsResource from "./collections"
|
||||
import AdminDraftOrdersResource from "./draft-orders"
|
||||
@@ -27,6 +28,7 @@ import AdminProductTagsResource from "./product-tags"
|
||||
class Admin extends BaseResource {
|
||||
public auth = new AdminAuthResource(this.client)
|
||||
public customers = new AdminCustomersResource(this.client)
|
||||
public customerGroups = new AdminCustomerGroupsResource(this.client)
|
||||
public discounts = new AdminDiscountsResource(this.client)
|
||||
public collections = new CollectionsResource(this.client)
|
||||
public draftOrders = new AdminDraftOrdersResource(this.client)
|
||||
|
||||
@@ -19,6 +19,7 @@ export default (container, config) => {
|
||||
export * from "./routes/admin/collections"
|
||||
export * from "./routes/admin/auth"
|
||||
export * from "./routes/admin/customers"
|
||||
export * from "./routes/admin/customer-groups"
|
||||
export * from "./routes/admin/discounts"
|
||||
export * from "./routes/admin/draft-orders"
|
||||
export * from "./routes/admin/gift-cards"
|
||||
|
||||
@@ -32,6 +32,10 @@ export default (app) => {
|
||||
return app
|
||||
}
|
||||
|
||||
/* ************************************** */
|
||||
/* ******** EXPORT API CLIENT TYPES ***** */
|
||||
/* ************************************** */
|
||||
|
||||
export type AdminCustomerGroupsRes = {
|
||||
customer_group: CustomerGroup
|
||||
}
|
||||
@@ -44,4 +48,9 @@ export type AdminCustomerGroupsListRes = PaginatedResponse & {
|
||||
|
||||
export const defaultAdminCustomerGroupsRelations = []
|
||||
|
||||
export * from "./add-customers-batch"
|
||||
export * from "./create-customer-group"
|
||||
export * from "./delete-customers-batch"
|
||||
export * from "./get-customer-group"
|
||||
export * from "./list-customer-groups"
|
||||
export * from "./update-customer-group"
|
||||
|
||||
Reference in New Issue
Block a user