add variant ranking on creation of a product

This commit is contained in:
pKorsholm
2021-09-02 11:42:02 +02:00
parent 1bf2b9c2ac
commit f73841c384
2 changed files with 63 additions and 3 deletions

View File

@@ -11,9 +11,24 @@ const eventBusService = {
describe("ProductService", () => {
describe("retrieve", () => {
const productRepo = MockRepository({
findOneWithRelations: () =>
Promise.resolve({ id: IdMap.getId("ironman") }),
findOneWithRelations: (rels, query) => {
if (query.where.id === "test id with variants")
return {
id: "test id with variants",
variants: [
{ id: "test_321", title: "Green", rank: 1 },
{ id: "test_123", title: "Blue", rank: 0 },
],
}
if (query.where.id === "test id one variant")
return {
id: "test id one variant",
variants: [{ id: "test_123", title: "Blue", rank: 0 }],
}
return Promise.resolve({ id: IdMap.getId("ironman") })
},
})
const productService = new ProductService({
manager: MockManager,
productRepository: productRepo,
@@ -23,6 +38,30 @@ describe("ProductService", () => {
jest.clearAllMocks()
})
it("Orders variants according to rank when retrieving a product", async () => {
const result = await productService.retrieve("test id with variants", {
relations: ["Variants"],
})
expect(productRepo.findOneWithRelations).toHaveBeenCalledTimes(1)
expect(productRepo.findOneWithRelations).toHaveBeenCalledWith(
["Variants"],
{
where: { id: "test id with variants" },
}
)
const expected = {
id: "test id with variants",
variants: [
{ id: "test_123", title: "Blue", rank: 0 },
{ id: "test_321", title: "Green", rank: 1 },
],
}
expect(result).toEqual(expected)
})
it("successfully retrieves a product", async () => {
const result = await productService.retrieve(IdMap.getId("ironman"))
@@ -37,11 +76,12 @@ describe("ProductService", () => {
describe("create", () => {
const productRepository = MockRepository({
create: () => ({
create: product => ({
id: IdMap.getId("ironman"),
title: "Suit",
options: [],
collection: { id: IdMap.getId("cat"), title: "Suits" },
variants: product.variants,
}),
findOneWithRelations: () => ({
id: IdMap.getId("ironman"),
@@ -97,6 +137,10 @@ describe("ProductService", () => {
options: [],
tags: [{ value: "title" }, { value: "title2" }],
type: "type-1",
variants: [
{ id: "test1", title: "green", rank: 0 },
{ id: "test2", title: "blue", rank: 0 },
],
})
expect(eventBusService.emit).toHaveBeenCalledTimes(1)
@@ -108,6 +152,10 @@ describe("ProductService", () => {
expect(productRepository.create).toHaveBeenCalledTimes(1)
expect(productRepository.create).toHaveBeenCalledWith({
title: "Suit",
variants: [
{ id: "test1", title: "green", rank: 0 },
{ id: "test2", title: "blue", rank: 1 },
],
})
expect(productTagRepository.findOne).toHaveBeenCalledTimes(2)
@@ -132,6 +180,10 @@ describe("ProductService", () => {
id: IdMap.getId("cat"),
title: "Suits",
},
variants: [
{ id: "test1", title: "green", rank: 0 },
{ id: "test2", title: "blue", rank: 1 },
],
})
})
})

View File

@@ -185,6 +185,11 @@ class ProductService extends BaseService {
)
}
if (product.variants)
product.variants.sort(
(variant1, variant2) => variant1.rank - variant2.rank
)
return product
}
@@ -293,6 +298,9 @@ class ProductService extends BaseService {
rest.discountable = false
}
if (rest.variants)
for (const [i, variant] of rest.variants.entries()) variant.rank = i
let product = productRepo.create(rest)
if (images && images.length) {