Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(medusa): Query SalesChannel Products in storefront #2272

Merged
merged 4 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 95 additions & 2 deletions integration-tests/api/__tests__/store/sales-channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,11 @@ describe("sales channels", () => {
})

describe("POST /store/cart/:id", () => {
let salesChannel1, salesChannel2, disabledSalesChannel
let product1, product2
let salesChannel1
let salesChannel2
let disabledSalesChannel
let product1
let product2
let cart

beforeEach(async () => {
Expand Down Expand Up @@ -289,4 +292,94 @@ describe("sales channels", () => {
})
})
})

describe("GET /store/products", () => {
let salesChannel1
let salesChannel2
let product1
let product2
beforeEach(async () => {
salesChannel1 = await simpleSalesChannelFactory(dbConnection, {
name: "salesChannel1",
description: "salesChannel1",
})

salesChannel2 = await simpleSalesChannelFactory(dbConnection, {
name: "salesChannel2",
description: "salesChannel2",
})

product1 = await simpleProductFactory(dbConnection, {
title: "prod 1",
status: "published",
sales_channels: [salesChannel1],
})

product2 = await simpleProductFactory(dbConnection, {
title: "prod 2",
status: "published",
sales_channels: [salesChannel2],
})
})

afterEach(async () => {
const db = useDb()
await db.teardown()
})

it("returns products from a specific sales channel", async () => {
const api = useApi()

const response = await api.get(
`/store/products?sales_channel_id[]=${salesChannel1.id}`
)

expect(response.data.products.length).toBe(1)
expect(response.data.products).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
}),
])
)
})

it("returns products from multiples sales channels", async () => {
const api = useApi()

const response = await api.get(
`/store/products?sales_channel_id[]=${salesChannel1.id}&sales_channel_id[]=${salesChannel2.id}`
)

expect(response.data.products.length).toBe(2)
expect(response.data.products).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
}),
expect.objectContaining({
id: expect.any(String),
}),
])
)
})

it("returns all products by default", async () => {
const api = useApi()

const response = await api.get(`/store/products`)

expect(response.data.products.length).toBe(2)
expect(response.data.products).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
}),
expect.objectContaining({
id: expect.any(String),
}),
])
)
})
})
})
6 changes: 3 additions & 3 deletions integration-tests/api/factories/simple-product-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import {
ProductTag,
ProductType,
ShippingProfile,
ShippingProfileType,
ShippingProfileType
} from "@medusajs/medusa"
import faker from "faker"
import { Connection } from "typeorm"
import {
ProductVariantFactoryData,
simpleProductVariantFactory,
simpleProductVariantFactory
} from "./simple-product-variant-factory"
import {
SalesChannelFactoryData,
simpleSalesChannelFactory,
simpleSalesChannelFactory
} from "./simple-sales-channel-factory"

export type ProductFactoryData = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { IsNumber, IsOptional, IsString } from "class-validator"
import { PricingService, ProductService } from "../../../../services"

import { FilterableProductProps } from "../../../../types/product"
import { PricedProduct } from "../../../../types/pricing"
import { Product } from "../../../../models"
import { Type } from "class-transformer"
import { Product } from "../../../../models"
import { PricedProduct } from "../../../../types/pricing"
import { FilterableProductProps } from "../../../../types/product"

/**
* @oas [get] /products
Expand Down
34 changes: 21 additions & 13 deletions packages/medusa/src/api/routes/store/products/list-products.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import {
adrien2p marked this conversation as resolved.
Show resolved Hide resolved
CartService,
ProductService,
RegionService,
} from "../../../../services"
import { Transform, Type } from "class-transformer"
import {
IsArray,
IsBoolean,
IsNumber,
IsOptional,
IsString,
ValidateNested,
ValidateNested
} from "class-validator"
import { Transform, Type } from "class-transformer"
import { omit, pickBy } from "lodash"
import {
CartService,
ProductService,
RegionService
} from "../../../../services"

import { defaultStoreProductsRelations } from "."
import SalesChannelFeatureFlag from "../../../../loaders/feature-flags/sales-channels"
import { Product } from "../../../../models"
import PricingService from "../../../../services/pricing"
import { DateComparisonOperator } from "../../../../types/common"
import { IsType } from "../../../../utils/validators/is-type"
import { PriceSelectionParams } from "../../../../types/price-selection"
import PricingService from "../../../../services/pricing"
import { Product } from "../../../../models"
import { defaultStoreProductsRelations } from "."
import { optionalBooleanMapper } from "../../../../utils/validators/is-boolean"
import { validator } from "../../../../utils/validator"
import { isDefined } from "../../../../utils"
import { FeatureFlagDecorators } from "../../../../utils/feature-flag-decorators"
import { validator } from "../../../../utils/validator"
import { optionalBooleanMapper } from "../../../../utils/validators/is-boolean"
import { IsType } from "../../../../utils/validators/is-type"

/**
* @oas [get] /products
Expand Down Expand Up @@ -294,6 +296,12 @@ export class StoreGetProductsParams extends StoreGetProductsPaginationParams {
@IsOptional()
type?: string

@FeatureFlagDecorators(SalesChannelFeatureFlag.key, [
IsOptional(),
IsArray(),
])
sales_channel_id?: string[]

@IsOptional()
@ValidateNested()
@Type(() => DateComparisonOperator)
Expand Down
12 changes: 8 additions & 4 deletions packages/medusa/src/types/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import {
IsEnum,
IsOptional,
IsString,
ValidateNested,
ValidateNested
} from "class-validator"
import SalesChannelFeatureFlag from "../loaders/feature-flags/sales-channels"
import { Product, ProductOptionValue, ProductStatus } from "../models"
import { FeatureFlagDecorators } from "../utils/feature-flag-decorators"
import { optionalBooleanMapper } from "../utils/validators/is-boolean"
import { IsType } from "../utils/validators/is-type"
import {
DateComparisonOperator,
FindConfig,
StringComparisonOperator,
StringComparisonOperator
} from "./common"
import { PriceListLoadConfig } from "./price-list"

Expand Down Expand Up @@ -66,8 +68,10 @@ export class FilterableProductProps {
@IsOptional()
type?: string

@IsArray()
@IsOptional()
@FeatureFlagDecorators(SalesChannelFeatureFlag.key, [
IsOptional(),
IsArray(),
])
sales_channel_id?: string[]

@IsOptional()
Expand Down