Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

feat(client): filters handler for store-api #740

Merged
merged 5 commits into from
May 14, 2020
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
4 changes: 4 additions & 0 deletions api/shopware-6-client.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,14 @@ export interface ShopwareParams {
// (undocumented)
limit?: number;
// (undocumented)
manufacturer?: string;
// (undocumented)
p?: number;
// (undocumented)
page?: number;
// (undocumented)
properties?: string;
// (undocumented)
sort?: string;
// (undocumented)
term?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
EqualsFilter,
RangeFilter,
MultiFilter,
EqualsAnyFilter,
} from "@shopware-pwa/commons/interfaces/search/SearchFilter";
import { PaginationLimit } from "@shopware-pwa/commons/interfaces/search/Pagination";
import { config, setup, update } from "@shopware-pwa/shopware-6-client";
Expand Down Expand Up @@ -229,6 +230,25 @@ describe("SearchConverter - convertSearchCriteria", () => {
]);
});
});
describe("store-api filters", () => {
it("should have properties property", () => {
const nameFilter: EqualsAnyFilter = {
type: SearchFilterType.EQUALS_ANY,
field: "manufacturerId",
value: ["shopware"],
};

const result = convertSearchCriteria(
{
filters: [nameFilter],
},
ApiType.store
);
expect(result).toStrictEqual({
manufacturer: "shopware",
});
});
});
});
describe("term", () => {
it("should add a term key and proper value", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { convertToStoreApiFilters } from "../../src/helpers/convertToStoreApiFilters";

import { setup } from "@shopware-pwa/shopware-6-client";

describe("SearchConverter - convertToStoreApiFilters", () => {
beforeEach(() => {
setup();
});
it("should return empty object if no params provided", () => {
const result = convertToStoreApiFilters(undefined as any);
expect(result).toEqual({});
});

it("should return empty object if any filter does not match", () => {
const result = convertToStoreApiFilters([
{
field: "test",
} as any,
]);
expect(result).toEqual({});
});
it("should return object with manufacturer property if manufacturerId filter exists", () => {
const result = convertToStoreApiFilters([
{
field: "manufacturerId",
value: ["divante", "shopware"],
} as any,
]);
expect(result).toEqual({
manufacturer: "divante|shopware",
});
});
it("should return object with properties property if multi filter containing propertyIds exists", () => {
const result = convertToStoreApiFilters([
{
type: "multi",
queries: [
{
field: "propertyIds",
value: ["white", "black", "xs", "l"],
},
],
} as any,
]);
expect(result).toEqual({
properties: "white|black|xs|l",
});
});
});
48 changes: 48 additions & 0 deletions packages/shopware-6-client/src/helpers/convertToStoreApiFilters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
SearchFilter,
SearchFilterType,
} from "@shopware-pwa/commons/interfaces/search/SearchFilter";

const concatIds = (ids: string[]) => ids.join("|");
const isFilterForProperty = (property: string, filter: any) =>
filter.hasOwnProperty("field") && filter.field === property;

interface StoreApiListingFilters {
manufacturer?: string;
properties?: string;
rating?: number; // to be handled later on
"shipping-free"?: boolean; // to be handled later on
"min-price"?: number; // to be handled later on
"max-price"?: number; // to be handled later on
}

interface GenericFilter extends SearchFilter {
value?: string[];
queries?: GenericFilter[];
}

export function convertToStoreApiFilters(
filters: Array<GenericFilter>
): StoreApiListingFilters {
let params: StoreApiListingFilters = {};

if (!filters || !filters.length) {
return {};
}

for (const filter of filters) {
if (isFilterForProperty("manufacturerId", filter) && filter.value) {
params.manufacturer = concatIds(filter.value);
}
if (filter.type === SearchFilterType.MULTI && filter.queries) {
for (const subFilter of filter.queries) {
/* istanbul ignore else */
if (isFilterForProperty("propertyIds", subFilter)) {
params.properties = concatIds(subFilter.value as any);
}
}
}
}

return params;
}
16 changes: 14 additions & 2 deletions packages/shopware-6-client/src/helpers/searchConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { PaginationLimit } from "@shopware-pwa/commons/interfaces/search/Paginat
import { config } from "@shopware-pwa/shopware-6-client";
import { ShopwareAssociation } from "@shopware-pwa/commons/interfaces/search/Association";
import { Grouping } from "@shopware-pwa/commons/interfaces/search/Grouping";
import { convertToStoreApiFilters } from "../helpers/convertToStoreApiFilters";

export enum ApiType {
store = "store-api",
Expand All @@ -22,7 +23,7 @@ export enum ApiType {
*/
export interface ShopwareParams {
p?: number; // p for page in store-api
page?: number; // for sales-channel-api
page?: number;
limit?: number;
sort?: string;
term?: string;
Expand All @@ -35,6 +36,8 @@ export interface ShopwareParams {
)[];
associations?: ShopwareAssociation;
grouping?: Grouping;
properties?: string; // store-api filters
manufacturer?: string; // store-api filters
}

export const convertSearchCriteria = (
Expand Down Expand Up @@ -74,7 +77,16 @@ export const convertSearchCriteria = (
}

if (filters && filters.length) {
params.filter = filters;
// append filters in store-api style using convertToStoreApiFilters
if (apiType && apiType === ApiType.store) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be just

Suggested change
if (apiType && apiType === ApiType.store) {
if (apiType === ApiType.store) {

params = Object.assign(
{},
params,
convertToStoreApiFilters(filters as any)
);
} else {
params.filter = filters;
}
}

if (configuration && configuration.associations) {
Expand Down