fix: CustomerGroups missing features in the clients (#1159)
* feat: support for query params in customer groups retrieve method * feat: add `useAdminDeleteCustomerGroup`hook * fix: formatting * fix: pass `query` object in `useAdminCustomerGroup` hook Co-authored-by: fPolic <frane@medusajs.com>
This commit is contained in:
@@ -7,6 +7,7 @@ import {
|
|||||||
AdminCustomerGroupsDeleteRes,
|
AdminCustomerGroupsDeleteRes,
|
||||||
AdminPostCustomerGroupsGroupCustomersBatchReq,
|
AdminPostCustomerGroupsGroupCustomersBatchReq,
|
||||||
AdminDeleteCustomerGroupsGroupCustomerBatchReq,
|
AdminDeleteCustomerGroupsGroupCustomerBatchReq,
|
||||||
|
AdminGetCustomerGroupsGroupParams,
|
||||||
} from "@medusajs/medusa"
|
} from "@medusajs/medusa"
|
||||||
import qs from "qs"
|
import qs from "qs"
|
||||||
|
|
||||||
@@ -32,13 +33,21 @@ class AdminCustomerGroupsResource extends BaseResource {
|
|||||||
* Retrieves a customer group.
|
* Retrieves a customer group.
|
||||||
*
|
*
|
||||||
* @param id - customer group id
|
* @param id - customer group id
|
||||||
|
* @param query - pass query options such as "expand", "fields" etc.
|
||||||
* @param customHeaders
|
* @param customHeaders
|
||||||
*/
|
*/
|
||||||
retrieve(
|
retrieve(
|
||||||
id: string,
|
id: string,
|
||||||
|
query?: AdminGetCustomerGroupsGroupParams,
|
||||||
customHeaders: Record<string, any> = {}
|
customHeaders: Record<string, any> = {}
|
||||||
): ResponsePromise<AdminCustomerGroupsRes> {
|
): ResponsePromise<AdminCustomerGroupsRes> {
|
||||||
const path = `/admin/customer-groups/${id}`
|
let path = `/admin/customer-groups/${id}`
|
||||||
|
|
||||||
|
if (query) {
|
||||||
|
const queryString = qs.stringify(query)
|
||||||
|
path += `?${queryString}`
|
||||||
|
}
|
||||||
|
|
||||||
return this.client.request("GET", path, {}, {}, customHeaders)
|
return this.client.request("GET", path, {}, {}, customHeaders)
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@@ -58,9 +67,9 @@ class AdminCustomerGroupsResource extends BaseResource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes a cusotmer group.
|
* Deletes a customer group.
|
||||||
*
|
*
|
||||||
* @param id - id of the customer gorup
|
* @param id - id of the customer group
|
||||||
* @param customHeaders
|
* @param customHeaders
|
||||||
*/
|
*/
|
||||||
delete(
|
delete(
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
|
AdminCustomerGroupsDeleteRes,
|
||||||
AdminCustomerGroupsRes,
|
AdminCustomerGroupsRes,
|
||||||
AdminDeleteCustomerGroupsGroupCustomerBatchReq,
|
AdminDeleteCustomerGroupsGroupCustomerBatchReq,
|
||||||
AdminPostCustomerGroupsGroupCustomersBatchReq,
|
AdminPostCustomerGroupsGroupCustomersBatchReq,
|
||||||
@@ -8,7 +9,7 @@ import {
|
|||||||
import { useMutation, UseMutationOptions, useQueryClient } from "react-query"
|
import { useMutation, UseMutationOptions, useQueryClient } from "react-query"
|
||||||
import { Response } from "@medusajs/medusa-js"
|
import { Response } from "@medusajs/medusa-js"
|
||||||
|
|
||||||
import { useMedusa } from "../../../contexts/medusa"
|
import { useMedusa } from "../../../contexts"
|
||||||
import { buildOptions } from "../../utils/buildOptions"
|
import { buildOptions } from "../../utils/buildOptions"
|
||||||
import { adminCustomerGroupKeys } from "./queries"
|
import { adminCustomerGroupKeys } from "./queries"
|
||||||
|
|
||||||
@@ -50,6 +51,7 @@ export const useAdminUpdateCustomerGroup = (
|
|||||||
) => {
|
) => {
|
||||||
const { client } = useMedusa()
|
const { client } = useMedusa()
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
|
|
||||||
return useMutation(
|
return useMutation(
|
||||||
(payload: AdminPostCustomerGroupsGroupReq) =>
|
(payload: AdminPostCustomerGroupsGroupReq) =>
|
||||||
client.admin.customerGroups.update(id, payload),
|
client.admin.customerGroups.update(id, payload),
|
||||||
@@ -61,6 +63,33 @@ export const useAdminUpdateCustomerGroup = (
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hook return functions for deleting a customer group.
|
||||||
|
*
|
||||||
|
* @param id - id of the customer group that is being deleted
|
||||||
|
* @param options
|
||||||
|
*/
|
||||||
|
export const useAdminDeleteCustomerGroup = (
|
||||||
|
id: string,
|
||||||
|
options?: UseMutationOptions<
|
||||||
|
Response<AdminCustomerGroupsDeleteRes>,
|
||||||
|
Error,
|
||||||
|
void
|
||||||
|
>
|
||||||
|
) => {
|
||||||
|
const { client } = useMedusa()
|
||||||
|
const queryClient = useQueryClient()
|
||||||
|
|
||||||
|
return useMutation(
|
||||||
|
() => client.admin.customerGroups.delete(id),
|
||||||
|
buildOptions(
|
||||||
|
queryClient,
|
||||||
|
[adminCustomerGroupKeys.lists(), adminCustomerGroupKeys.detail(id)],
|
||||||
|
options
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hook returns functions for addition of multiple customers to a customer group.
|
* Hook returns functions for addition of multiple customers to a customer group.
|
||||||
*
|
*
|
||||||
@@ -77,6 +106,7 @@ export const useAdminAddCustomersToCustomerGroup = (
|
|||||||
) => {
|
) => {
|
||||||
const { client } = useMedusa()
|
const { client } = useMedusa()
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
|
|
||||||
return useMutation(
|
return useMutation(
|
||||||
(payload: AdminPostCustomerGroupsGroupCustomersBatchReq) =>
|
(payload: AdminPostCustomerGroupsGroupCustomersBatchReq) =>
|
||||||
client.admin.customerGroups.addCustomers(id, payload),
|
client.admin.customerGroups.addCustomers(id, payload),
|
||||||
@@ -104,6 +134,7 @@ export const useAdminRemoveCustomersFromCustomerGroup = (
|
|||||||
) => {
|
) => {
|
||||||
const { client } = useMedusa()
|
const { client } = useMedusa()
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
|
|
||||||
return useMutation(
|
return useMutation(
|
||||||
(payload: AdminDeleteCustomerGroupsGroupCustomerBatchReq) =>
|
(payload: AdminDeleteCustomerGroupsGroupCustomerBatchReq) =>
|
||||||
client.admin.customerGroups.removeCustomers(id, payload),
|
client.admin.customerGroups.removeCustomers(id, payload),
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import {
|
import {
|
||||||
AdminCustomerGroupsListRes,
|
AdminCustomerGroupsListRes,
|
||||||
AdminCustomerGroupsRes,
|
AdminCustomerGroupsRes,
|
||||||
|
AdminGetCustomerGroupsGroupParams,
|
||||||
AdminGetCustomerGroupsParams,
|
AdminGetCustomerGroupsParams,
|
||||||
} from "@medusajs/medusa"
|
} from "@medusajs/medusa"
|
||||||
import { Response } from "@medusajs/medusa-js"
|
import { Response } from "@medusajs/medusa-js"
|
||||||
@@ -8,7 +9,7 @@ import { useQuery } from "react-query"
|
|||||||
|
|
||||||
import { useMedusa } from "../../../contexts"
|
import { useMedusa } from "../../../contexts"
|
||||||
import { UseQueryOptionsWrapper } from "../../../types"
|
import { UseQueryOptionsWrapper } from "../../../types"
|
||||||
import { queryKeysFactory } from "../../utils/index"
|
import { queryKeysFactory } from "../../utils"
|
||||||
|
|
||||||
const ADMIN_CUSTOMER_GROUPS_QUERY_KEY = `admin_customer_groups` as const
|
const ADMIN_CUSTOMER_GROUPS_QUERY_KEY = `admin_customer_groups` as const
|
||||||
|
|
||||||
@@ -22,10 +23,12 @@ type CustomerGroupQueryKeys = typeof adminCustomerGroupKeys
|
|||||||
* Hook retrieves a customer group by id.
|
* Hook retrieves a customer group by id.
|
||||||
*
|
*
|
||||||
* @param id - customer group id
|
* @param id - customer group id
|
||||||
|
* @param query - query params
|
||||||
* @param options
|
* @param options
|
||||||
*/
|
*/
|
||||||
export const useAdminCustomerGroup = (
|
export const useAdminCustomerGroup = (
|
||||||
id: string,
|
id: string,
|
||||||
|
query?: AdminGetCustomerGroupsGroupParams,
|
||||||
options?: UseQueryOptionsWrapper<
|
options?: UseQueryOptionsWrapper<
|
||||||
Response<AdminCustomerGroupsRes>,
|
Response<AdminCustomerGroupsRes>,
|
||||||
Error,
|
Error,
|
||||||
@@ -35,14 +38,14 @@ export const useAdminCustomerGroup = (
|
|||||||
const { client } = useMedusa()
|
const { client } = useMedusa()
|
||||||
const { data, ...rest } = useQuery(
|
const { data, ...rest } = useQuery(
|
||||||
adminCustomerGroupKeys.detail(id),
|
adminCustomerGroupKeys.detail(id),
|
||||||
() => client.admin.customerGroups.retrieve(id),
|
() => client.admin.customerGroups.retrieve(id, query),
|
||||||
options
|
options
|
||||||
)
|
)
|
||||||
return { ...data, ...rest } as const
|
return { ...data, ...rest } as const
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hook retrieves a list of customer gorups.
|
* Hook retrieves a list of customer groups.
|
||||||
*
|
*
|
||||||
* @param query - pagination/filtering params
|
* @param query - pagination/filtering params
|
||||||
* @param options
|
* @param options
|
||||||
|
|||||||
Reference in New Issue
Block a user