Skip to content

Commit

Permalink
feat: Add missing endpoints and normalize customer and currency endpo…
Browse files Browse the repository at this point in the history
…ints for storefront (#7160)
  • Loading branch information
sradevski committed Apr 29, 2024
1 parent 08a9297 commit 32c2a9d
Show file tree
Hide file tree
Showing 24 changed files with 368 additions and 423 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ medusaIntegrationTestRunner({
)

expect(response.status).toEqual(200)
expect(response.data.address).toEqual(
expect(response.data.customer.addresses[0]).toEqual(
expect.objectContaining({
id: expect.any(String),
first_name: "John",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createAuthenticatedCustomer } from "../../../helpers/create-authenticated-customer"
import {medusaIntegrationTestRunner} from "medusa-test-utils";
import { medusaIntegrationTestRunner } from "medusa-test-utils"

const env = { MEDUSA_FF_MEDUSA_V2: true }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ medusaIntegrationTestRunner({
)

expect(response.status).toEqual(200)
expect(response.data.address).toEqual(
expect(response.data.customer.addresses[0]).toEqual(
expect.objectContaining({
id: address.id,
first_name: "Jane",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
CreateCustomerAddressDTO,
CustomerAddressDTO,
UpdateCustomerAddressDTO,
FilterableCustomerAddressProps,
ICustomerModuleService,
} from "@medusajs/types"
Expand All @@ -13,7 +13,7 @@ type StepInput = {
create?: CreateCustomerAddressDTO[]
update?: {
selector: FilterableCustomerAddressProps
update: Partial<CustomerAddressDTO>
update: UpdateCustomerAddressDTO
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
CreateCustomerAddressDTO,
CustomerAddressDTO,
UpdateCustomerAddressDTO,
FilterableCustomerAddressProps,
ICustomerModuleService,
} from "@medusajs/types"
Expand All @@ -13,7 +13,7 @@ type StepInput = {
create?: CreateCustomerAddressDTO[]
update?: {
selector: FilterableCustomerAddressProps
update: Partial<CustomerAddressDTO>
update: UpdateCustomerAddressDTO
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core-flows/src/customer/steps/update-addresses.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import {
CustomerAddressDTO,
UpdateCustomerAddressDTO,
FilterableCustomerAddressProps,
ICustomerModuleService,
} from "@medusajs/types"
Expand All @@ -12,7 +12,7 @@ import { createStep, StepResponse } from "@medusajs/workflows-sdk"

type UpdateCustomerAddresseStepInput = {
selector: FilterableCustomerAddressProps
update: Partial<CustomerAddressDTO>
update: UpdateCustomerAddressDTO
}

export const updateCustomerAddresseStepId = "update-customer-addresses"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
CustomerAddressDTO,
UpdateCustomerAddressDTO,
FilterableCustomerAddressProps,
ICustomerModuleService,
} from "@medusajs/types"
Expand All @@ -8,7 +8,7 @@ import { StepResponse } from "@medusajs/workflows-sdk"
export const unsetForUpdate = async (
data: {
selector: FilterableCustomerAddressProps
update: Partial<CustomerAddressDTO>
update: UpdateCustomerAddressDTO
},
customerService: ICustomerModuleService,
field: "is_default_billing" | "is_default_shipping"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
FilterableCustomerAddressProps,
CustomerAddressDTO,
UpdateCustomerAddressDTO,
} from "@medusajs/types"
import {
WorkflowData,
Expand All @@ -16,7 +17,7 @@ import {

type WorkflowInput = {
selector: FilterableCustomerAddressProps
update: Partial<CustomerAddressDTO>
update: UpdateCustomerAddressDTO
}

export const updateCustomerAddressesWorkflowId = "update-customer-addresses"
Expand Down
8 changes: 8 additions & 0 deletions packages/medusa/src/api-v2/admin/currencies/[code]/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ContainerRegistrationKeys,
MedusaError,
remoteQueryObjectFromString,
} from "@medusajs/utils"
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
Expand All @@ -16,5 +17,12 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
})

const [currency] = await remoteQuery(queryObject)
if (!currency) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`Currency with code: ${req.params.code} was not found`
)
}

res.status(200).json({ currency })
}
9 changes: 0 additions & 9 deletions packages/medusa/src/api-v2/admin/customers/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,11 @@ export const AdminCustomerAdressesParams = createFindParams({
}).merge(
z.object({
q: z.string().optional(),
address_name: z.union([z.string(), z.array(z.string())]).optional(),
is_default_shipping: z.boolean().optional(),
is_default_billing: z.boolean().optional(),
company: z.union([z.string(), z.array(z.string())]).optional(),
first_name: z.union([z.string(), z.array(z.string())]).optional(),
last_name: z.union([z.string(), z.array(z.string())]).optional(),
address_1: z.union([z.string(), z.array(z.string())]).optional(),
address_2: z.union([z.string(), z.array(z.string())]).optional(),
city: z.union([z.string(), z.array(z.string())]).optional(),
country_code: z.union([z.string(), z.array(z.string())]).optional(),
province: z.union([z.string(), z.array(z.string())]).optional(),
postal_code: z.union([z.string(), z.array(z.string())]).optional(),
phone: z.union([z.string(), z.array(z.string())]).optional(),
metadata: z.record(z.unknown()).optional(),
})
)

Expand Down
18 changes: 14 additions & 4 deletions packages/medusa/src/api-v2/store/currencies/[code]/route.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import { remoteQueryObjectFromString } from "@medusajs/utils"
import {
ContainerRegistrationKeys,
MedusaError,
remoteQueryObjectFromString,
} from "@medusajs/utils"
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
import { defaultStoreCurrencyFields } from "../query-config"

export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
const remoteQuery = req.scope.resolve("remoteQuery")
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)

const variables = { code: req.params.code }

const queryObject = remoteQueryObjectFromString({
entryPoint: "currency",
variables,
fields: defaultStoreCurrencyFields,
fields: req.remoteQueryConfig.fields,
})

const [currency] = await remoteQuery(queryObject)
if (!currency) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`Currency with code: ${req.params.code} was not found`
)
}

res.status(200).json({ currency })
}
14 changes: 5 additions & 9 deletions packages/medusa/src/api-v2/store/currencies/middlewares.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { transformQuery } from "../../../api/middlewares"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import { authenticate } from "../../../utils/authenticate-middleware"
import { validateAndTransformQuery } from "../../utils/validate-query"
import * as QueryConfig from "./query-config"
import {
StoreGetCurrenciesCurrencyParams,
StoreGetCurrenciesParams,
} from "./validators"
import { StoreGetCurrenciesParams, StoreGetCurrencyParams } from "./validators"

export const storeCurrencyRoutesMiddlewares: MiddlewareRoute[] = [
{
method: ["GET"],
matcher: "/store/currencies",
middlewares: [
transformQuery(
validateAndTransformQuery(
StoreGetCurrenciesParams,
QueryConfig.listTransformQueryConfig
),
Expand All @@ -22,8 +18,8 @@ export const storeCurrencyRoutesMiddlewares: MiddlewareRoute[] = [
method: ["GET"],
matcher: "/store/currencies/:code",
middlewares: [
transformQuery(
StoreGetCurrenciesCurrencyParams,
validateAndTransformQuery(
StoreGetCurrencyParams,
QueryConfig.retrieveTransformQueryConfig
),
],
Expand Down
11 changes: 5 additions & 6 deletions packages/medusa/src/api-v2/store/currencies/query-config.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
export const defaultStoreCurrencyRelations = []
export const allowedStoreCurrencyRelations = []
export const defaultStoreCurrencyFields = [
"code",
"name",
"symbol",
"symbol_native",
"decimal_digits",
"rounding",
]

export const retrieveTransformQueryConfig = {
defaultFields: defaultStoreCurrencyFields,
defaultRelations: defaultStoreCurrencyRelations,
allowedRelations: allowedStoreCurrencyRelations,
defaults: defaultStoreCurrencyFields,
isList: false,
}

export const listTransformQueryConfig = {
defaultLimit: 20,
...retrieveTransformQueryConfig,
defaultLimit: 50,
isList: true,
}
14 changes: 7 additions & 7 deletions packages/medusa/src/api-v2/store/currencies/route.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { remoteQueryObjectFromString } from "@medusajs/utils"
import {
ContainerRegistrationKeys,
remoteQueryObjectFromString,
} from "@medusajs/utils"
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
import { defaultStoreCurrencyFields } from "./query-config"

export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
const remoteQuery = req.scope.resolve("remoteQuery")
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)

const queryObject = remoteQueryObjectFromString({
entryPoint: "currency",
variables: {
filters: req.filterableFields,
order: req.listConfig.order,
skip: req.listConfig.skip,
take: req.listConfig.take,
...req.remoteQueryConfig.pagination,
},
fields: defaultStoreCurrencyFields,
fields: req.remoteQueryConfig.fields,
})

const { rows: currencies, metadata } = await remoteQuery(queryObject)
Expand Down
45 changes: 17 additions & 28 deletions packages/medusa/src/api-v2/store/currencies/validators.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
import { Type } from "class-transformer"
import { IsOptional, IsString, ValidateNested } from "class-validator"
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
import { createFindParams, createSelectParams } from "../../utils/validators"
import { z } from "zod"

export class StoreGetCurrenciesCurrencyParams extends FindParams {}
/**
* Parameters used to filter and configure the pagination of the retrieved currencies.
*/
export class StoreGetCurrenciesParams extends extendedFindParamsMixin({
limit: 50,
offset: 0,
}) {
/**
* Search parameter for currencies.
*/
@IsString({ each: true })
@IsOptional()
code?: string | string[]

// Additional filters from BaseFilterable
@IsOptional()
@ValidateNested({ each: true })
@Type(() => StoreGetCurrenciesParams)
$and?: StoreGetCurrenciesParams[]
export const StoreGetCurrencyParams = createSelectParams()

@IsOptional()
@ValidateNested({ each: true })
@Type(() => StoreGetCurrenciesParams)
$or?: StoreGetCurrenciesParams[]
}
export type StoreGetCurrenciesParamsType = z.infer<
typeof StoreGetCurrenciesParams
>
export const StoreGetCurrenciesParams = createFindParams({
offset: 0,
limit: 50,
}).merge(
z.object({
q: z.string().optional(),
code: z.union([z.string(), z.array(z.string())]).optional(),
$and: z.lazy(() => StoreGetCurrenciesParams.array()).optional(),
$or: z.lazy(() => StoreGetCurrenciesParams.array()).optional(),
})
)
23 changes: 23 additions & 0 deletions packages/medusa/src/api-v2/store/customers/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { MedusaContainer } from "@medusajs/types"
import {
ContainerRegistrationKeys,
remoteQueryObjectFromString,
} from "@medusajs/utils"

export const refetchCustomer = async (
customerId: string,
scope: MedusaContainer,
fields: string[]
) => {
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
const queryObject = remoteQueryObjectFromString({
entryPoint: "customer",
variables: {
filters: { id: customerId },
},
fields: fields,
})

const customers = await remoteQuery(queryObject)
return customers[0]
}

0 comments on commit 32c2a9d

Please sign in to comment.