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

Commit

Permalink
feat(product): add sorting and pagination draft, fix(product): fixes …
Browse files Browse the repository at this point in the history
…after CR
  • Loading branch information
Maciej Kucmus committed Oct 15, 2019
1 parent d86bad2 commit 5c4e7c0
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { ProductService } from "../../src/index";

describe("ProductService", () => {
describe("getProductsIds", () => {
it("should return array of products' ids (default amount of 10)", async () => {
try {
const result = await ProductService.getProductsIds();
expect(result.total).toEqual(60);
expect(result.data).toHaveLength(10);
} catch (e) {
console.error("Connection problem", e);
}
});
});
describe("getProducts", () => {
it("should return array of products (default amount of 10)", async () => {
try {
const result = await ProductService.getProducts();
expect(result.total).toEqual(60);
expect(result.data).toHaveLength(10);
} catch (e) {
console.error("Connection problem", e);
}
});
it("should return array of products limited to 5", async () => {
try {
const pagination = {
page: 1,
limit: 5
};
const result = await ProductService.getProducts(pagination);
expect(result.total).toEqual(60);
expect(result.data).toHaveLength(5);
} catch (e) {
console.error("Connection problem", e);
}
});
it("should return a different array of products sorted by name", async () => {
try {
const pagination = {
page: 1,
limit: 1
};

/** get the products with descending order to compare */
const sort = {
sort: `-name`
};
const result = await ProductService.getProducts(pagination, sort);
expect(result.data).toHaveLength(1);
const nameDesc = result.data[0].name;

/** get the products with ascending order to compare */
const sortAsc = {
sort: `name`
};
const resultAsc = await ProductService.getProducts(pagination, sortAsc);
expect(resultAsc.data).toHaveLength(1);
const nameAsc = resultAsc.data[0].name;

/** compare first results's name from different sorting order */
expect(nameAsc).not.toBe(nameDesc);
} catch (e) {
console.error("Connection problem", e);
}
});
});
describe("getProduct", () => {
it("should return chosen product", async () => {
try {
const productId = "044a190a54ab4f06803909c3ee8063ef";
const result = await ProductService.getProduct(productId);
expect(result).toHaveProperty("id");
expect(result.id).toEqual(productId);
} catch (e) {
console.error("Connection problem", e);
}
});
});
});
38 changes: 38 additions & 0 deletions packages/shopware-6-client/src/helpers/paramsConverter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export interface Params {
page?: number;
limit?: number;
sort?: string;
filter?: any;
}

export interface ParamsConverter {
getParams: (pagination?: any, sort?: any, filter?: any) => Params | null;
}
/**
* @description Combines parameters into one object
*/
const getParams = (
pagination?: any,
sort?: any,
filter?: any
): Params | null => {
let params = {};

if (pagination) {
params = Object.assign(params, pagination);
}

if (sort) {
params = Object.assign(params, sort);
}

if (filter) {
params = Object.assign(params, filter);
}

return pagination || sort || filter ? params : null;
};

export const ParamsConverter: ParamsConverter = {
getParams
};
41 changes: 22 additions & 19 deletions packages/shopware-6-client/src/services/productService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { config } from "../settings";
import { getProductEndpoint } from "../endpoints";
import { SearchResult } from "../interfaces/response/SearchResult";
import { Product } from "../interfaces/models/content/product/Product";
import { ParamsConverter } from "../helpers/paramsConverter";

/**
* Usage example:
Expand All @@ -12,14 +13,16 @@ import { Product } from "../interfaces/models/content/product/Product";
*/
export interface ProductService {
getProductsIds: () => Promise<SearchResult<string[]>>;
getProducts: () => Promise<SearchResult<Product[]>>;
getProduct: (productId: string) => Promise<SearchResult<Partial<Product>>>;
getProducts: (
pagination?: any,
sort?: any,
filter?: any
) => Promise<SearchResult<Product[]>>;
getProduct: (productId: string) => Promise<Product>;
}

/**
* Get default amount of products
*
* @returns Promise<SearchResult<Product[]>>
* @description Get default amount of products
*/
const getProductsIds = async function(): Promise<SearchResult<string[]>> {
const resp = await axios.post(
Expand All @@ -29,32 +32,32 @@ const getProductsIds = async function(): Promise<SearchResult<string[]>> {
};

/**
* Get default amount of products' ids
*
* @returns Promise<SearchResult<string[]>>
* @description Get default amount of products' ids
*/
const getProducts = async function(): Promise<SearchResult<Product[]>> {
const resp = await axios.get(`${config.endpoint}${getProductEndpoint()}`);

const getProducts = async function(
pagination?: any,
sort?: any,
filters?: any
): Promise<SearchResult<Product[]>> {
const resp = await axios.get(`${config.endpoint}${getProductEndpoint()}`, {
params: ParamsConverter.getParams(pagination, sort, filters)
});
return resp.data;
};

/**
* Get the product with passed productId
*
* @param productId
* @returns Promise<SearchResult<Partial<Product>>>
* @description Get the product with passed productId
*/
const getProduct = async function(
productId: string
): Promise<SearchResult<Partial<Product>>> {
const getProduct = async function(productId: string): Promise<Product> {
const resp = await axios.get(
`${config.endpoint}${getProductEndpoint()}/${productId}`
);
return resp.data;
return resp.data.data;
};

/**
* Expose public methods of the service
* @description Expose public methods of the service
*/
export const ProductService: ProductService = {
getProductsIds,
Expand Down

0 comments on commit 5c4e7c0

Please sign in to comment.