add variant ranking on creation of a product
This commit is contained in:
1 parent
1bf2b9c2ac
commit
f73841c384
2 files changed
+63
-3
No files matched your search
@@ -11,9 +11,24 @@ const eventBusService = {
|
|||||||
describe("ProductService", () => {
|
describe("ProductService", () => {
|
||||||
describe("retrieve", () => {
|
describe("retrieve", () => {
|
||||||
const productRepo = MockRepository({
|
const productRepo = MockRepository({
|
||||||
findOneWithRelations: () =>
|
findOneWithRelations: (rels, query) => {
|
||||||
Promise.resolve({ id: IdMap.getId("ironman") }),
|
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({
|
const productService = new ProductService({
|
||||||
manager: MockManager,
|
manager: MockManager,
|
||||||
productRepository: productRepo,
|
productRepository: productRepo,
|
||||||
@@ -23,6 +38,30 @@ describe("ProductService", () => {
|
|||||||
jest.clearAllMocks()
|
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 () => {
|
it("successfully retrieves a product", async () => {
|
||||||
const result = await productService.retrieve(IdMap.getId("ironman"))
|
const result = await productService.retrieve(IdMap.getId("ironman"))
|
||||||
|
|
||||||
@@ -37,11 +76,12 @@ describe("ProductService", () => {
|
|||||||
|
|
||||||
describe("create", () => {
|
describe("create", () => {
|
||||||
const productRepository = MockRepository({
|
const productRepository = MockRepository({
|
||||||
create: () => ({
|
create: product => ({
|
||||||
id: IdMap.getId("ironman"),
|
id: IdMap.getId("ironman"),
|
||||||
title: "Suit",
|
title: "Suit",
|
||||||
options: [],
|
options: [],
|
||||||
collection: { id: IdMap.getId("cat"), title: "Suits" },
|
collection: { id: IdMap.getId("cat"), title: "Suits" },
|
||||||
|
variants: product.variants,
|
||||||
}),
|
}),
|
||||||
findOneWithRelations: () => ({
|
findOneWithRelations: () => ({
|
||||||
id: IdMap.getId("ironman"),
|
id: IdMap.getId("ironman"),
|
||||||
@@ -97,6 +137,10 @@ describe("ProductService", () => {
|
|||||||
options: [],
|
options: [],
|
||||||
tags: [{ value: "title" }, { value: "title2" }],
|
tags: [{ value: "title" }, { value: "title2" }],
|
||||||
type: "type-1",
|
type: "type-1",
|
||||||
|
variants: [
|
||||||
|
{ id: "test1", title: "green", rank: 0 },
|
||||||
|
{ id: "test2", title: "blue", rank: 0 },
|
||||||
|
],
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(eventBusService.emit).toHaveBeenCalledTimes(1)
|
expect(eventBusService.emit).toHaveBeenCalledTimes(1)
|
||||||
@@ -108,6 +152,10 @@ describe("ProductService", () => {
|
|||||||
expect(productRepository.create).toHaveBeenCalledTimes(1)
|
expect(productRepository.create).toHaveBeenCalledTimes(1)
|
||||||
expect(productRepository.create).toHaveBeenCalledWith({
|
expect(productRepository.create).toHaveBeenCalledWith({
|
||||||
title: "Suit",
|
title: "Suit",
|
||||||
|
variants: [
|
||||||
|
{ id: "test1", title: "green", rank: 0 },
|
||||||
|
{ id: "test2", title: "blue", rank: 1 },
|
||||||
|
],
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(productTagRepository.findOne).toHaveBeenCalledTimes(2)
|
expect(productTagRepository.findOne).toHaveBeenCalledTimes(2)
|
||||||
@@ -132,6 +180,10 @@ describe("ProductService", () => {
|
|||||||
id: IdMap.getId("cat"),
|
id: IdMap.getId("cat"),
|
||||||
title: "Suits",
|
title: "Suits",
|
||||||
},
|
},
|
||||||
|
variants: [
|
||||||
|
{ id: "test1", title: "green", rank: 0 },
|
||||||
|
{ id: "test2", title: "blue", rank: 1 },
|
||||||
|
],
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -185,6 +185,11 @@ class ProductService extends BaseService {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (product.variants)
|
||||||
|
product.variants.sort(
|
||||||
|
(variant1, variant2) => variant1.rank - variant2.rank
|
||||||
|
)
|
||||||
|
|
||||||
return product
|
return product
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -293,6 +298,9 @@ class ProductService extends BaseService {
|
|||||||
rest.discountable = false
|
rest.discountable = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (rest.variants)
|
||||||
|
for (const [i, variant] of rest.variants.entries()) variant.rank = i
|
||||||
|
|
||||||
let product = productRepo.create(rest)
|
let product = productRepo.create(rest)
|
||||||
|
|
||||||
if (images && images.length) {
|
if (images && images.length) {
|
||||||
|
|||||||
Reference in new issue
Block a user