diff --git a/packages/admin-next/dashboard/src/components/forms/transfer-ownership-form/transfer-ownership-form.tsx b/packages/admin-next/dashboard/src/components/forms/transfer-ownership-form/transfer-ownership-form.tsx index 3a5da6c06e..da2a5aba4b 100644 --- a/packages/admin-next/dashboard/src/components/forms/transfer-ownership-form/transfer-ownership-form.tsx +++ b/packages/admin-next/dashboard/src/components/forms/transfer-ownership-form/transfer-ownership-form.tsx @@ -9,7 +9,7 @@ import { useTranslation } from "react-i18next" import { z } from "zod" import { useCustomer } from "../../../hooks/api/customers" -import { client } from "../../../lib/client" +import { sdk } from "../../../lib/client" import { getStylizedAmount } from "../../../lib/money-amount-helpers" import { getOrderFulfillmentStatus, @@ -74,7 +74,7 @@ export const TransferOwnerShipForm = ({ const { data, fetchNextPage, isFetchingNextPage } = useInfiniteQuery({ queryKey: ["customers", debouncedQuery], queryFn: async ({ pageParam = 0 }) => { - const res = await client.customers.list({ + const res = await sdk.admin.customer.list({ q: debouncedQuery, limit: 10, offset: pageParam, diff --git a/packages/admin-next/dashboard/src/hooks/api/campaigns.tsx b/packages/admin-next/dashboard/src/hooks/api/campaigns.tsx index 6bfb79b645..e34e0bff91 100644 --- a/packages/admin-next/dashboard/src/hooks/api/campaigns.tsx +++ b/packages/admin-next/dashboard/src/hooks/api/campaigns.tsx @@ -1,10 +1,4 @@ -import { - AdminCampaignListResponse, - AdminCampaignResponse, - AdminCreateCampaign, - AdminUpdateCampaign, - LinkMethodRequest, -} from "@medusajs/types" +import { HttpTypes, LinkMethodRequest } from "@medusajs/types" import { QueryKey, UseMutationOptions, @@ -12,23 +6,23 @@ import { useMutation, useQuery, } from "@tanstack/react-query" -import { client } from "../../lib/client" +import { sdk } from "../../lib/client" import { queryClient } from "../../lib/query-client" import { queryKeysFactory } from "../../lib/query-key-factory" -import { CampaignDeleteRes } from "../../types/api-responses" import { promotionsQueryKeys } from "./promotions" +import { FetchError } from "@medusajs/js-sdk" const REGIONS_QUERY_KEY = "campaigns" as const export const campaignsQueryKeys = queryKeysFactory(REGIONS_QUERY_KEY) export const useCampaign = ( id: string, - query?: Record, + query?: HttpTypes.AdminGetCampaignParams, options?: Omit< UseQueryOptions< - AdminCampaignResponse, - Error, - AdminCampaignResponse, + HttpTypes.AdminCampaignResponse, + FetchError, + HttpTypes.AdminCampaignResponse, QueryKey >, "queryFn" | "queryKey" @@ -36,7 +30,7 @@ export const useCampaign = ( ) => { const { data, ...rest } = useQuery({ queryKey: campaignsQueryKeys.detail(id), - queryFn: async () => client.campaigns.retrieve(id, query), + queryFn: async () => sdk.admin.campaign.retrieve(id, query), ...options, }) @@ -44,19 +38,19 @@ export const useCampaign = ( } export const useCampaigns = ( - query?: Record, + query?: HttpTypes.AdminGetCampaignsParams, options?: Omit< UseQueryOptions< - AdminCampaignListResponse, - Error, - AdminCampaignListResponse, + HttpTypes.AdminCampaignListResponse, + FetchError, + HttpTypes.AdminCampaignListResponse, QueryKey >, "queryFn" | "queryKey" > ) => { const { data, ...rest } = useQuery({ - queryFn: () => client.campaigns.list(query), + queryFn: () => sdk.admin.campaign.list(query), queryKey: campaignsQueryKeys.list(query), ...options, }) @@ -66,13 +60,13 @@ export const useCampaigns = ( export const useCreateCampaign = ( options?: UseMutationOptions< - AdminCampaignResponse, - Error, - AdminCreateCampaign + HttpTypes.AdminCampaignResponse, + FetchError, + HttpTypes.AdminCreateCampaign > ) => { return useMutation({ - mutationFn: (payload) => client.campaigns.create(payload), + mutationFn: (payload) => sdk.admin.campaign.create(payload), onSuccess: (data, variables, context) => { queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.lists() }) options?.onSuccess?.(data, variables, context) @@ -84,13 +78,13 @@ export const useCreateCampaign = ( export const useUpdateCampaign = ( id: string, options?: UseMutationOptions< - AdminCampaignResponse, - Error, - AdminUpdateCampaign + HttpTypes.AdminCampaignResponse, + FetchError, + HttpTypes.AdminUpdateCampaign > ) => { return useMutation({ - mutationFn: (payload) => client.campaigns.update(id, payload), + mutationFn: (payload) => sdk.admin.campaign.update(id, payload), onSuccess: (data, variables, context) => { queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.lists() }) queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.details() }) @@ -103,10 +97,14 @@ export const useUpdateCampaign = ( export const useDeleteCampaign = ( id: string, - options?: UseMutationOptions + options?: UseMutationOptions< + HttpTypes.DeleteResponse<"campaign">, + FetchError, + void + > ) => { return useMutation({ - mutationFn: () => client.campaigns.delete(id), + mutationFn: () => sdk.admin.campaign.delete(id), onSuccess: (data, variables, context) => { queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.lists() }) queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.details() }) @@ -119,11 +117,14 @@ export const useDeleteCampaign = ( export const useAddOrRemoveCampaignPromotions = ( id: string, - options?: UseMutationOptions + options?: UseMutationOptions< + HttpTypes.AdminCampaignResponse, + FetchError, + LinkMethodRequest + > ) => { return useMutation({ - mutationFn: (payload) => - client.campaigns.addOrRemovePromotions(id, payload), + mutationFn: (payload) => sdk.admin.campaign.batchPromotions(id, payload), onSuccess: (data, variables, context) => { queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.details() }) queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.lists() }) diff --git a/packages/admin-next/dashboard/src/hooks/api/promotions.tsx b/packages/admin-next/dashboard/src/hooks/api/promotions.tsx index e234d0083b..8d7d1e11f7 100644 --- a/packages/admin-next/dashboard/src/hooks/api/promotions.tsx +++ b/packages/admin-next/dashboard/src/hooks/api/promotions.tsx @@ -1,9 +1,5 @@ -import { AdminGetPromotionsParams } from "@medusajs/medusa" -import { - AdminPromotionRuleListResponse, - AdminRuleAttributeOptionsListResponse, - AdminRuleValueOptionsListResponse, -} from "@medusajs/types" +import { HttpTypes } from "@medusajs/types" +import { FetchError } from "@medusajs/js-sdk" import { QueryKey, useMutation, @@ -11,21 +7,9 @@ import { useQuery, UseQueryOptions, } from "@tanstack/react-query" -import { client } from "../../lib/client" +import { sdk } from "../../lib/client" import { queryClient } from "../../lib/query-client" import { queryKeysFactory } from "../../lib/query-key-factory" -import { - BatchAddPromotionRulesReq, - BatchRemovePromotionRulesReq, - BatchUpdatePromotionRulesReq, - CreatePromotionReq, - UpdatePromotionReq, -} from "../../types/api-payloads" -import { - PromotionDeleteRes, - PromotionListRes, - PromotionRes, -} from "../../types/api-responses" import { campaignsQueryKeys } from "./campaigns" const PROMOTIONS_QUERY_KEY = "promotions" as const @@ -35,31 +19,35 @@ export const promotionsQueryKeys = { listRules: ( id: string | null, ruleType: string, - query?: Record + query?: HttpTypes.AdminGetPromotionRuleParams ) => [PROMOTIONS_QUERY_KEY, id, ruleType, query], listRuleAttributes: (ruleType: string, promotionType?: string) => [ PROMOTIONS_QUERY_KEY, ruleType, promotionType, ], - listRuleValues: (ruleType: string, ruleValue: string, query: object) => [ - PROMOTIONS_QUERY_KEY, - ruleType, - ruleValue, - query, - ], + listRuleValues: ( + ruleType: string, + ruleValue: string, + query: HttpTypes.AdminGetPromotionsRuleValueParams + ) => [PROMOTIONS_QUERY_KEY, ruleType, ruleValue, query], } export const usePromotion = ( id: string, options?: Omit< - UseQueryOptions, + UseQueryOptions< + HttpTypes.AdminPromotionResponse, + FetchError, + HttpTypes.AdminPromotionResponse, + QueryKey + >, "queryFn" | "queryKey" > ) => { const { data, ...rest } = useQuery({ queryKey: promotionsQueryKeys.detail(id), - queryFn: async () => client.promotions.retrieve(id), + queryFn: async () => sdk.admin.promotion.retrieve(id), ...options, }) @@ -69,12 +57,12 @@ export const usePromotion = ( export const usePromotionRules = ( id: string | null, ruleType: string, - query?: Record, + query?: HttpTypes.AdminGetPromotionRuleParams, options?: Omit< UseQueryOptions< - AdminPromotionRuleListResponse, - Error, - AdminPromotionRuleListResponse, + HttpTypes.AdminGetPromotionRuleParams, + FetchError, + HttpTypes.AdminPromotionRuleListResponse, QueryKey >, "queryFn" | "queryKey" @@ -82,7 +70,7 @@ export const usePromotionRules = ( ) => { const { data, ...rest } = useQuery({ queryKey: promotionsQueryKeys.listRules(id, ruleType, query), - queryFn: async () => client.promotions.listRules(id, ruleType, query), + queryFn: async () => sdk.admin.promotion.listRules(id, ruleType, query), ...options, }) @@ -90,15 +78,20 @@ export const usePromotionRules = ( } export const usePromotions = ( - query?: AdminGetPromotionsParams, + query?: HttpTypes.AdminGetPromotionsParams, options?: Omit< - UseQueryOptions, + UseQueryOptions< + HttpTypes.AdminPromotionListResponse, + FetchError, + HttpTypes.AdminPromotionListResponse, + QueryKey + >, "queryFn" | "queryKey" > ) => { const { data, ...rest } = useQuery({ queryKey: promotionsQueryKeys.list(query), - queryFn: async () => client.promotions.list(query), + queryFn: async () => sdk.admin.promotion.list(query), ...options, }) @@ -110,9 +103,9 @@ export const usePromotionRuleAttributes = ( promotionType?: string, options?: Omit< UseQueryOptions< - AdminRuleAttributeOptionsListResponse, - Error, - AdminRuleAttributeOptionsListResponse, + HttpTypes.AdminRuleAttributeOptionsListResponse, + FetchError, + HttpTypes.AdminRuleAttributeOptionsListResponse, QueryKey >, "queryFn" | "queryKey" @@ -121,7 +114,7 @@ export const usePromotionRuleAttributes = ( const { data, ...rest } = useQuery({ queryKey: promotionsQueryKeys.listRuleAttributes(ruleType, promotionType), queryFn: async () => - client.promotions.listRuleAttributes(ruleType, promotionType), + sdk.admin.promotion.listRuleAttributes(ruleType, promotionType), ...options, }) @@ -131,12 +124,12 @@ export const usePromotionRuleAttributes = ( export const usePromotionRuleValues = ( ruleType: string, ruleValue: string, - query?: Record, + query?: HttpTypes.AdminGetPromotionsRuleValueParams, options?: Omit< UseQueryOptions< - AdminRuleValueOptionsListResponse, - Error, - AdminRuleValueOptionsListResponse, + HttpTypes.AdminRuleValueOptionsListResponse, + FetchError, + HttpTypes.AdminRuleValueOptionsListResponse, QueryKey >, "queryFn" | "queryKey" @@ -149,7 +142,7 @@ export const usePromotionRuleValues = ( query || {} ), queryFn: async () => - client.promotions.listRuleValues(ruleType, ruleValue, query), + sdk.admin.promotion.listRuleValues(ruleType, ruleValue, query), ...options, }) @@ -158,10 +151,14 @@ export const usePromotionRuleValues = ( export const useDeletePromotion = ( id: string, - options?: UseMutationOptions + options?: UseMutationOptions< + HttpTypes.DeleteResponse<"promotion">, + FetchError, + void + > ) => { return useMutation({ - mutationFn: () => client.promotions.delete(id), + mutationFn: () => sdk.admin.promotion.delete(id), onSuccess: (data, variables, context) => { queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.lists() }) queryClient.invalidateQueries({ @@ -175,10 +172,14 @@ export const useDeletePromotion = ( } export const useCreatePromotion = ( - options?: UseMutationOptions + options?: UseMutationOptions< + HttpTypes.AdminPromotionResponse, + FetchError, + HttpTypes.AdminCreatePromotion + > ) => { return useMutation({ - mutationFn: (payload) => client.promotions.create(payload), + mutationFn: (payload) => sdk.admin.promotion.create(payload), onSuccess: (data, variables, context) => { queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.lists() }) queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.lists() }) @@ -190,10 +191,14 @@ export const useCreatePromotion = ( export const useUpdatePromotion = ( id: string, - options?: UseMutationOptions + options?: UseMutationOptions< + HttpTypes.AdminPromotionResponse, + FetchError, + HttpTypes.AdminUpdatePromotion + > ) => { return useMutation({ - mutationFn: (payload) => client.promotions.update(id, payload), + mutationFn: (payload) => sdk.admin.promotion.update(id, payload), onSuccess: (data, variables, context) => { queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.all }) @@ -206,10 +211,15 @@ export const useUpdatePromotion = ( export const usePromotionAddRules = ( id: string, ruleType: string, - options?: UseMutationOptions + options?: UseMutationOptions< + HttpTypes.AdminPromotionResponse, + FetchError, + HttpTypes.BatchAddPromotionRulesReq + > ) => { return useMutation({ - mutationFn: (payload) => client.promotions.addRules(id, ruleType, payload), + mutationFn: (payload) => + sdk.admin.promotion.addRules(id, ruleType, payload), onSuccess: (data, variables, context) => { queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.all }) @@ -223,14 +233,14 @@ export const usePromotionRemoveRules = ( id: string, ruleType: string, options?: UseMutationOptions< - PromotionRes, - Error, - BatchRemovePromotionRulesReq + HttpTypes.AdminPromotionResponse, + FetchError, + HttpTypes.BatchRemovePromotionRulesReq > ) => { return useMutation({ mutationFn: (payload) => - client.promotions.removeRules(id, ruleType, payload), + sdk.admin.promotion.removeRules(id, ruleType, payload), onSuccess: (data, variables, context) => { queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.all }) @@ -244,14 +254,14 @@ export const usePromotionUpdateRules = ( id: string, ruleType: string, options?: UseMutationOptions< - PromotionRes, - Error, - BatchUpdatePromotionRulesReq + HttpTypes.AdminPromotionResponse, + FetchError, + HttpTypes.BatchUpdatePromotionRulesReq > ) => { return useMutation({ mutationFn: (payload) => - client.promotions.updateRules(id, ruleType, payload), + sdk.admin.promotion.updateRules(id, ruleType, payload), onSuccess: (data, variables, context) => { queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.all }) diff --git a/packages/admin-next/dashboard/src/lib/client/campaigns.ts b/packages/admin-next/dashboard/src/lib/client/campaigns.ts deleted file mode 100644 index 08860d78fa..0000000000 --- a/packages/admin-next/dashboard/src/lib/client/campaigns.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { - AdminCampaignListResponse, - AdminCampaignResponse, - AdminCreateCampaign, - AdminUpdateCampaign, - LinkMethodRequest, -} from "@medusajs/types" -import { CampaignDeleteRes } from "../../types/api-responses" -import { deleteRequest, getRequest, postRequest } from "./common" - -async function retrieveCampaign(id: string, query?: Record) { - return getRequest(`/admin/campaigns/${id}`, query) -} - -async function listCampaigns(query?: Record) { - return getRequest(`/admin/campaigns`, query) -} - -async function createCampaign(payload: AdminCreateCampaign) { - return postRequest(`/admin/campaigns`, payload) -} - -async function updateCampaign(id: string, payload: AdminUpdateCampaign) { - return postRequest(`/admin/campaigns/${id}`, payload) -} - -async function deleteCampaign(id: string) { - return deleteRequest(`/admin/campaigns/${id}`) -} - -async function addOrRemoveCampaignPromotions( - id: string, - payload: LinkMethodRequest -) { - return postRequest( - `/admin/campaigns/${id}/promotions`, - payload - ) -} - -export const campaigns = { - retrieve: retrieveCampaign, - list: listCampaigns, - create: createCampaign, - update: updateCampaign, - delete: deleteCampaign, - addOrRemovePromotions: addOrRemoveCampaignPromotions, -} diff --git a/packages/admin-next/dashboard/src/lib/client/categories.ts b/packages/admin-next/dashboard/src/lib/client/categories.ts deleted file mode 100644 index c6cbd611e0..0000000000 --- a/packages/admin-next/dashboard/src/lib/client/categories.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { - AdminProductCategoryListResponse, - AdminProductCategoryResponse, -} from "@medusajs/types" -import { getRequest } from "./common" - -async function listProductCategories(query?: Record) { - return getRequest( - `/admin/product-categories`, - query - ) -} - -async function retrieveProductCategory( - id: string, - query?: Record -) { - return getRequest( - `/admin/product-categories/${id}`, - query - ) -} - -export const categories = { - list: listProductCategories, - retrieve: retrieveProductCategory, -} diff --git a/packages/admin-next/dashboard/src/lib/client/client.ts b/packages/admin-next/dashboard/src/lib/client/client.ts index aeab240e15..d587805978 100644 --- a/packages/admin-next/dashboard/src/lib/client/client.ts +++ b/packages/admin-next/dashboard/src/lib/client/client.ts @@ -1,16 +1,7 @@ import Medusa from "@medusajs/js-sdk" -import { campaigns } from "./campaigns" -import { categories } from "./categories" -import { promotions } from "./promotions" export const backendUrl = __BACKEND_URL__ ?? "http://localhost:9000" -export const client = { - campaigns: campaigns, - categories: categories, - promotions: promotions, -} - export const sdk = new Medusa({ baseUrl: backendUrl, auth: { diff --git a/packages/admin-next/dashboard/src/lib/client/common.ts b/packages/admin-next/dashboard/src/lib/client/common.ts deleted file mode 100644 index 53c84cf790..0000000000 --- a/packages/admin-next/dashboard/src/lib/client/common.ts +++ /dev/null @@ -1,108 +0,0 @@ -import { stringify } from "qs" - -const baseUrl = __BACKEND_URL__ ?? "http://localhost:9000" - -const commonHeaders: HeadersInit = { - Accept: "application/json", - "Content-Type": "application/json", -} - -function getUrl(path: string, query?: Record) { - const params = query ? stringify(query) : null - - return `${baseUrl}${path}${params ? `?${params}` : ""}` -} - -function getBody(payload?: Record) { - return payload ? JSON.stringify(payload) : undefined -} - -function getOptions( - options?: Omit, - payload?: Record -): RequestInit { - const body = getBody(payload) - - return { - ...options, - headers: { - ...commonHeaders, - ...options?.headers, - }, - body, - credentials: "include", - } -} - -async function makeRequest< - TRes, - TPayload extends Record | undefined, - TQuery extends Record | undefined = undefined, ->( - path: string, - payload?: TPayload, - query?: TQuery, - options?: Omit -): Promise { - const url = getUrl(path, query) - const requestOptions = getOptions(options, payload) - - const response = await fetch(url, requestOptions) - - if (!response.ok) { - const errorData = await response.json() - - // Temp: Add a better error type - throw new Error(`API error ${response.status}: ${errorData.message}`) - } - - return response.json() -} - -export async function getRequest< - TRes, - TQuery extends Record | undefined = {}, ->( - path: string, - query?: TQuery, - options?: Omit -): Promise { - return makeRequest>( - path, - undefined, - query, - { - ...options, - method: "GET", - } - ) -} - -export async function postRequest< - TRes, - TPayload extends Record | undefined = {}, ->( - path: string, - payload?: TPayload, - options?: Omit -): Promise { - return makeRequest, undefined>( - path, - payload, - undefined, - { - ...options, - method: "POST", - } - ) -} - -export async function deleteRequest( - path: string, - options?: Omit -): Promise { - return makeRequest(path, undefined, undefined, { - ...options, - method: "DELETE", - }) -} diff --git a/packages/admin-next/dashboard/src/lib/client/promotions.ts b/packages/admin-next/dashboard/src/lib/client/promotions.ts deleted file mode 100644 index e422b46eea..0000000000 --- a/packages/admin-next/dashboard/src/lib/client/promotions.ts +++ /dev/null @@ -1,126 +0,0 @@ -import { - AdminGetPromotionsParams, - AdminGetPromotionsRuleValueParams, -} from "@medusajs/medusa" -import { AdminRuleValueOptionsListResponse } from "@medusajs/types" -import { - BatchAddPromotionRulesReq, - BatchRemovePromotionRulesReq, - BatchUpdatePromotionRulesReq, - CreatePromotionReq, - UpdatePromotionReq, -} from "../../types/api-payloads" -import { - PromotionDeleteRes, - PromotionListRes, - PromotionRes, - PromotionRuleAttributesListRes, -} from "../../types/api-responses" -import { deleteRequest, getRequest, postRequest } from "./common" - -async function retrievePromotion(id: string, query?: AdminGetPromotionsParams) { - return getRequest( - `/admin/promotions/${id}`, - query - ) -} - -async function listPromotions(query?: AdminGetPromotionsParams) { - return getRequest(`/admin/promotions`, query) -} - -async function deletePromotion(id: string) { - return deleteRequest(`/admin/promotions/${id}`) -} - -async function updatePromotion(id: string, payload: UpdatePromotionReq) { - return postRequest(`/admin/promotions/${id}`, payload) -} - -async function createPromotion(payload: CreatePromotionReq) { - return postRequest(`/admin/promotions`, payload) -} - -async function addPromotionRules( - id: string, - ruleType: string, - payload: BatchAddPromotionRulesReq -) { - return postRequest( - `/admin/promotions/${id}/${ruleType}/batch`, - { - create: payload.rules, - } - ) -} - -async function updatePromotionRules( - id: string, - ruleType: string, - payload: BatchUpdatePromotionRulesReq -) { - return postRequest( - `/admin/promotions/${id}/${ruleType}/batch`, - { - update: payload.rules, - } - ) -} - -async function removePromotionRules( - id: string, - ruleType: string, - payload: BatchRemovePromotionRulesReq -) { - return postRequest( - `/admin/promotions/${id}/${ruleType}/batch`, - { - delete: payload.rule_ids, - } - ) -} - -async function listPromotionRules( - id: string | null, - ruleType: string, - query?: Record -) { - return getRequest( - `/admin/promotions/${id}/${ruleType}`, - query - ) -} - -async function listPromotionRuleAttributes( - ruleType: string, - promotionType?: string -) { - return getRequest( - `/admin/promotions/rule-attribute-options/${ruleType}?promotion_type=${promotionType}` - ) -} - -async function listPromotionRuleValues( - ruleType: string, - ruleValue: string, - query?: AdminGetPromotionsRuleValueParams -) { - return getRequest( - `/admin/promotions/rule-value-options/${ruleType}/${ruleValue}`, - query - ) -} - -export const promotions = { - retrieve: retrievePromotion, - list: listPromotions, - delete: deletePromotion, - update: updatePromotion, - create: createPromotion, - addRules: addPromotionRules, - removeRules: removePromotionRules, - updateRules: updatePromotionRules, - listRules: listPromotionRules, - listRuleAttributes: listPromotionRuleAttributes, - listRuleValues: listPromotionRuleValues, -} diff --git a/packages/admin-next/dashboard/src/routes/campaigns/campaign-detail/loader.ts b/packages/admin-next/dashboard/src/routes/campaigns/campaign-detail/loader.ts index cb114ed37f..0361e3bc63 100644 --- a/packages/admin-next/dashboard/src/routes/campaigns/campaign-detail/loader.ts +++ b/packages/admin-next/dashboard/src/routes/campaigns/campaign-detail/loader.ts @@ -2,13 +2,13 @@ import { AdminCampaignResponse } from "@medusajs/types" import { LoaderFunctionArgs } from "react-router-dom" import { campaignsQueryKeys } from "../../../hooks/api/campaigns" -import { client } from "../../../lib/client" +import { sdk } from "../../../lib/client" import { queryClient } from "../../../lib/query-client" const campaignDetailQuery = (id: string) => ({ queryKey: campaignsQueryKeys.detail(id), queryFn: async () => - client.campaigns.retrieve(id, { + sdk.admin.campaign.retrieve(id, { fields: "+promotions.id", }), }) diff --git a/packages/admin-next/dashboard/src/routes/categories/category-detail/loader.ts b/packages/admin-next/dashboard/src/routes/categories/category-detail/loader.ts index 4ae9c210b5..979cb76d8a 100644 --- a/packages/admin-next/dashboard/src/routes/categories/category-detail/loader.ts +++ b/packages/admin-next/dashboard/src/routes/categories/category-detail/loader.ts @@ -2,12 +2,12 @@ import { AdminProductCategoryResponse } from "@medusajs/types" import { LoaderFunctionArgs } from "react-router-dom" import { categoriesQueryKeys } from "../../../hooks/api/categories" -import { client } from "../../../lib/client" +import { sdk } from "../../../lib/client" import { queryClient } from "../../../lib/query-client" const categoryDetailQuery = (id: string) => ({ queryKey: categoriesQueryKeys.detail(id), - queryFn: async () => client.categories.retrieve(id), + queryFn: async () => sdk.admin.productCategory.retrieve(id), }) export const categoryLoader = async ({ params }: LoaderFunctionArgs) => { diff --git a/packages/admin-next/dashboard/src/routes/customers/customer-detail/components/customer-group-section/customer-group-section.tsx b/packages/admin-next/dashboard/src/routes/customers/customer-detail/components/customer-group-section/customer-group-section.tsx index 3b65d7a4c1..dcb7dd5c4e 100644 --- a/packages/admin-next/dashboard/src/routes/customers/customer-detail/components/customer-group-section/customer-group-section.tsx +++ b/packages/admin-next/dashboard/src/routes/customers/customer-detail/components/customer-group-section/customer-group-section.tsx @@ -26,7 +26,7 @@ import { useCustomerGroupTableColumns } from "../../../../../hooks/table/columns import { useCustomerGroupTableFilters } from "../../../../../hooks/table/filters/use-customer-group-table-filters.tsx" import { useCustomerGroupTableQuery } from "../../../../../hooks/table/query/use-customer-group-table-query.tsx" import { useDataTable } from "../../../../../hooks/use-data-table.tsx" -import { client, sdk } from "../../../../../lib/client/index.ts" +import { sdk } from "../../../../../lib/client/index.ts" import { queryClient } from "../../../../../lib/query-client.ts" type CustomerGroupSectionProps = { diff --git a/packages/admin-next/dashboard/src/routes/promotions/promotion-detail/loader.ts b/packages/admin-next/dashboard/src/routes/promotions/promotion-detail/loader.ts index 3d747f92af..b3bde557c3 100644 --- a/packages/admin-next/dashboard/src/routes/promotions/promotion-detail/loader.ts +++ b/packages/admin-next/dashboard/src/routes/promotions/promotion-detail/loader.ts @@ -1,12 +1,12 @@ import { LoaderFunctionArgs } from "react-router-dom" import { promotionsQueryKeys } from "../../../hooks/api/promotions" -import { client } from "../../../lib/client" +import { sdk } from "../../../lib/client" import { queryClient } from "../../../lib/query-client" import { PromotionRes } from "../../../types/api-responses" const promotionDetailQuery = (id: string) => ({ queryKey: promotionsQueryKeys.detail(id), - queryFn: async () => client.promotions.retrieve(id), + queryFn: async () => sdk.admin.promotion.retrieve(id), }) export const promotionLoader = async ({ params }: LoaderFunctionArgs) => { diff --git a/packages/admin-next/dashboard/src/routes/promotions/promotion-list/loader.ts b/packages/admin-next/dashboard/src/routes/promotions/promotion-list/loader.ts index 3139265c55..9b4e252243 100644 --- a/packages/admin-next/dashboard/src/routes/promotions/promotion-list/loader.ts +++ b/packages/admin-next/dashboard/src/routes/promotions/promotion-list/loader.ts @@ -1,6 +1,6 @@ import { QueryClient } from "@tanstack/react-query" import { promotionsQueryKeys } from "../../../hooks/api/promotions" -import { client } from "../../../lib/client" +import { sdk } from "../../../lib/client" import { queryClient } from "../../../lib/query-client" import { PromotionListRes } from "../../../types/api-responses" @@ -11,7 +11,7 @@ const params = { const promotionsListQuery = () => ({ queryKey: promotionsQueryKeys.list(params), - queryFn: async () => client.promotions.list(params), + queryFn: async () => sdk.admin.promotion.list(params), }) export const promotionsLoader = (client: QueryClient) => { diff --git a/packages/core/js-sdk/src/admin/campaign.ts b/packages/core/js-sdk/src/admin/campaign.ts new file mode 100644 index 0000000000..e7efbcc1f3 --- /dev/null +++ b/packages/core/js-sdk/src/admin/campaign.ts @@ -0,0 +1,91 @@ +import { HttpTypes } from "@medusajs/types" +import { Client } from "../client" +import { ClientHeaders } from "../types" + +export class Campaign { + private client: Client + constructor(client: Client) { + this.client = client + } + + async retrieve( + id: string, + query?: HttpTypes.AdminGetCampaignParams, + headers?: ClientHeaders + ) { + return await this.client.fetch( + `/admin/campaigns/${id}`, + { + headers, + query, + } + ) + } + + async list( + query?: HttpTypes.AdminGetCampaignsParams, + headers?: ClientHeaders + ) { + return await this.client.fetch( + `/admin/campaigns`, + { + headers, + query, + } + ) + } + + async create( + payload: HttpTypes.AdminCreateCampaign, + headers?: ClientHeaders + ) { + return await this.client.fetch( + `/admin/campaigns`, + { + method: "POST", + headers, + body: payload, + } + ) + } + + async update( + id: string, + payload: HttpTypes.AdminUpdateCampaign, + headers?: ClientHeaders + ) { + return await this.client.fetch( + `/admin/campaigns/${id}`, + { + method: "POST", + headers, + body: payload, + } + ) + } + + async delete(id: string, headers?: ClientHeaders) { + return await this.client.fetch>( + `/admin/campaigns/${id}`, + { + method: "DELETE", + headers, + } + ) + } + + async batchPromotions( + id: string, + payload: HttpTypes.AdminBatchLink, + headers?: ClientHeaders + ) { + return await this.client.fetch( + `/admin/campaigns/${id}/promotions`, + { + method: "POST", + headers, + body: payload, + } + ) + } +} diff --git a/packages/core/js-sdk/src/admin/index.ts b/packages/core/js-sdk/src/admin/index.ts index d675bb734f..e89b7ac46c 100644 --- a/packages/core/js-sdk/src/admin/index.ts +++ b/packages/core/js-sdk/src/admin/index.ts @@ -1,5 +1,6 @@ import { Client } from "../client" import { ApiKey } from "./api-key" +import { Campaign } from "./campaign" import { Claim } from "./claim" import { Currency } from "./currency" import { Customer } from "./customer" @@ -23,6 +24,7 @@ import { ProductCollection } from "./product-collection" import { ProductTag } from "./product-tag" import { ProductType } from "./product-type" import { ProductVariant } from "./product-variant" +import { Promotion } from "./promotion" import { RefundReason } from "./refund-reasons" import { Region } from "./region" import Reservation from "./reservation" @@ -79,6 +81,8 @@ export class Admin { public workflowExecution: WorkflowExecution public reservation: Reservation public customerGroup: CustomerGroup + public promotion: Promotion + public campaign: Campaign constructor(client: Client) { this.invite = new Invite(client) @@ -120,5 +124,7 @@ export class Admin { this.workflowExecution = new WorkflowExecution(client) this.reservation = new Reservation(client) this.customerGroup = new CustomerGroup(client) + this.promotion = new Promotion(client) + this.campaign = new Campaign(client) } } diff --git a/packages/core/js-sdk/src/admin/promotion.ts b/packages/core/js-sdk/src/admin/promotion.ts new file mode 100644 index 0000000000..b1a08752c3 --- /dev/null +++ b/packages/core/js-sdk/src/admin/promotion.ts @@ -0,0 +1,170 @@ +import { HttpTypes } from "@medusajs/types" +import { Client } from "../client" +import { ClientHeaders } from "../types" + +export class Promotion { + private client: Client + constructor(client: Client) { + this.client = client + } + + async retrieve( + id: string, + query?: HttpTypes.AdminGetPromotionParams, + headers?: ClientHeaders + ) { + return await this.client.fetch( + `/admin/promotions/${id}`, + { + headers, + query, + } + ) + } + + async list( + query?: HttpTypes.AdminGetPromotionsParams, + headers?: ClientHeaders + ) { + return await this.client.fetch( + `/admin/promotions`, + { + headers, + query, + } + ) + } + + async create( + payload: HttpTypes.AdminCreatePromotion, + headers?: ClientHeaders + ) { + return await this.client.fetch( + `/admin/promotions`, + { + method: "POST", + headers, + body: payload, + } + ) + } + + async update( + id: string, + payload: HttpTypes.AdminUpdatePromotion, + headers?: ClientHeaders + ) { + return await this.client.fetch( + `/admin/promotions/${id}`, + { + method: "POST", + headers, + body: payload, + } + ) + } + + async delete(id: string, headers?: ClientHeaders) { + return await this.client.fetch>( + `/admin/promotions/${id}`, + { + method: "DELETE", + headers, + } + ) + } + + async addRules( + id: string, + ruleType: string, + payload: HttpTypes.BatchAddPromotionRulesReq, + headers?: ClientHeaders + ) { + return await this.client.fetch( + `/admin/promotions/${id}/${ruleType}/batch`, + { + method: "POST", + headers, + body: { create: payload.rules }, + } + ) + } + + async updateRules( + id: string, + ruleType: string, + payload: HttpTypes.BatchUpdatePromotionRulesReq, + headers?: ClientHeaders + ) { + return await this.client.fetch( + `/admin/promotions/${id}/${ruleType}/batch`, + { + method: "POST", + headers, + body: { update: payload.rules }, + } + ) + } + + async removeRules( + id: string, + ruleType: string, + payload: HttpTypes.BatchRemovePromotionRulesReq, + headers?: ClientHeaders + ) { + return await this.client.fetch( + `/admin/promotions/${id}/${ruleType}/batch`, + { + method: "POST", + headers, + body: { delete: payload.rule_ids }, + } + ) + } + + async listRules( + id: string | null, + ruleType: string, + query?: HttpTypes.AdminGetPromotionRuleParams, + headers?: ClientHeaders + ) { + // eslint-disable-next-line max-len + return await this.client.fetch( + `/admin/promotions/${id}/${ruleType}`, + { + headers, + query, + } + ) + } + + async listRuleAttributes( + ruleType: string, + promotionType?: string, + headers?: ClientHeaders + ) { + // eslint-disable-next-line max-len + return await this.client.fetch( + `/admin/promotions/rule-attribute-options/${ruleType}`, + { + headers, + query: { promotion_type: promotionType }, + } + ) + } + + async listRuleValues( + ruleType: string, + ruleValue: string, + query?: HttpTypes.AdminGetPromotionsRuleValueParams, + headers?: ClientHeaders + ) { + return await this.client.fetch( + `/admin/promotions/rule-value-options/${ruleType}/${ruleValue}`, + { + headers, + query, + } + ) + } +} diff --git a/packages/core/types/src/http/campaign/admin/index.ts b/packages/core/types/src/http/campaign/admin/index.ts index 13269a5f52..29d37e7aac 100644 --- a/packages/core/types/src/http/campaign/admin/index.ts +++ b/packages/core/types/src/http/campaign/admin/index.ts @@ -1,2 +1,4 @@ export * from "./payloads" +export * from "./queries" export * from "./responses" + diff --git a/packages/core/types/src/http/campaign/admin/queries.ts b/packages/core/types/src/http/campaign/admin/queries.ts new file mode 100644 index 0000000000..766179b022 --- /dev/null +++ b/packages/core/types/src/http/campaign/admin/queries.ts @@ -0,0 +1,16 @@ +import { BaseFilterable } from "../../../dal" +import { FindParams, SelectParams } from "../../common" + +export interface AdminGetCampaignsParams + extends FindParams, + BaseFilterable { + q?: string + campaign_identifier?: string + budget?: { + currency_code?: string + } + $and?: AdminGetCampaignsParams[] + $or?: AdminGetCampaignsParams[] +} + +export interface AdminGetCampaignParams extends SelectParams {} diff --git a/packages/core/types/src/http/customer/admin/queries.ts b/packages/core/types/src/http/customer/admin/queries.ts index 62d4001d3b..a05d44e735 100644 --- a/packages/core/types/src/http/customer/admin/queries.ts +++ b/packages/core/types/src/http/customer/admin/queries.ts @@ -5,7 +5,8 @@ import { } from "../common" export interface AdminCustomerFilters extends BaseCustomerFilters { - groups: CustomerGroupInCustomerFilters | string[] | string + groups?: CustomerGroupInCustomerFilters | string[] | string + has_account?: boolean } export interface AdminCustomerAddressFilters extends BaseCustomerAddressFilters {} diff --git a/packages/core/types/src/http/promotion/admin/index.ts b/packages/core/types/src/http/promotion/admin/index.ts index 57fa23181b..1f82a2ead5 100644 --- a/packages/core/types/src/http/promotion/admin/index.ts +++ b/packages/core/types/src/http/promotion/admin/index.ts @@ -1,2 +1,4 @@ export * from "./entities" -export * from "./responses" \ No newline at end of file +export * from "./payloads" +export * from "./queries" +export * from "./responses" diff --git a/packages/core/types/src/http/promotion/admin/payloads.ts b/packages/core/types/src/http/promotion/admin/payloads.ts new file mode 100644 index 0000000000..65b3d73d51 --- /dev/null +++ b/packages/core/types/src/http/promotion/admin/payloads.ts @@ -0,0 +1,83 @@ +import { + ApplicationMethodAllocationValues, + ApplicationMethodTargetTypeValues, + ApplicationMethodTypeValues, + PromotionRuleOperatorValues, + PromotionTypeValues, +} from "../../../promotion" +import { AdminCreateCampaign } from "../../campaign" + +export interface AdminCreatePromotionRule { + operator: PromotionRuleOperatorValues + description?: string | null + attribute: string + values: string | string[] +} + +export interface AdminUpdatePromotionRule { + id: string + operator?: PromotionRuleOperatorValues + description?: string | null + attribute?: string + values: string | string[] +} + +export interface AdminCreateApplicationMethod { + description?: string | null + value: number + currency_code?: string | null + max_quantity?: number | null + type: ApplicationMethodTypeValues + target_type: ApplicationMethodTargetTypeValues + allocation?: ApplicationMethodAllocationValues + target_rules?: AdminCreatePromotionRule[] + buy_rules?: AdminCreatePromotionRule[] + apply_to_quantity?: number | null + buy_rules_min_quantity?: number | null +} + +export interface AdminUpdateApplicationMethod { + description?: string | null + value?: number + max_quantity?: number | null + currency_code?: string | null + type?: ApplicationMethodTypeValues + target_type?: ApplicationMethodTargetTypeValues + allocation?: ApplicationMethodAllocationValues + target_rules?: AdminCreatePromotionRule[] + buy_rules?: AdminCreatePromotionRule[] + apply_to_quantity?: number | null + buy_rules_min_quantity?: number | null +} + +export interface AdminCreatePromotion { + code: string + is_automatic?: boolean + type: PromotionTypeValues + campaign_id?: string | null + campaign?: AdminCreateCampaign + application_method: AdminCreateApplicationMethod + rules?: AdminCreatePromotionRule[] +} + +export interface AdminUpdatePromotion { + code?: string + is_automatic?: boolean + type?: PromotionTypeValues + campaign_id?: string | null + campaign?: AdminCreateCampaign + application_method?: AdminUpdateApplicationMethod + rules?: AdminCreatePromotionRule[] +} + +export interface BatchAddPromotionRulesReq { + rules: AdminCreatePromotionRule[] +} + +export interface BatchRemovePromotionRulesReq { + rule_ids: string[] +} + +export interface BatchUpdatePromotionRulesReq { + rules: AdminUpdatePromotionRule[] +} diff --git a/packages/core/types/src/http/promotion/admin/queries.ts b/packages/core/types/src/http/promotion/admin/queries.ts new file mode 100644 index 0000000000..9b75d37095 --- /dev/null +++ b/packages/core/types/src/http/promotion/admin/queries.ts @@ -0,0 +1,35 @@ +import { BaseFilterable, OperatorMap } from "../../../dal" +import { FindParams, SelectParams } from "../../common" + +export interface AdminGetPromotionParams extends SelectParams {} + +export interface AdminGetPromotionsParams + extends FindParams, + BaseFilterable { + q?: string + code?: string | string[] + campaign_id?: string | string[] + application_method?: { + currency_code?: string | string[] + } + created_at?: OperatorMap + updated_at?: OperatorMap + deleted_at?: OperatorMap + $and?: AdminGetPromotionsParams[] + $or?: AdminGetPromotionsParams[] +} + +export interface AdminGetPromotionRuleParams { + promotion_type?: string + application_method_type?: string +} + +export interface AdminGetPromotionRuleTypeParams extends SelectParams { + promotion_type?: string + application_method_type?: string +} + +export interface AdminGetPromotionsRuleValueParams extends FindParams { + q?: string + value?: string | string[] +} diff --git a/packages/core/types/src/http/workflow-execution/admin/index.ts b/packages/core/types/src/http/workflow-execution/admin/index.ts index 020c34f02c..3b6853ce1c 100644 --- a/packages/core/types/src/http/workflow-execution/admin/index.ts +++ b/packages/core/types/src/http/workflow-execution/admin/index.ts @@ -1,3 +1,4 @@ export * from "./entities" export * from "./queries" export * from "./responses" +