change variant column 'rank' to 'variant_rank'

This commit is contained in:
pKorsholm
2021-09-06 13:29:24 +02:00
parent 1a87a463b4
commit 0c47c78c08
11 changed files with 72 additions and 92 deletions
@@ -180,11 +180,11 @@ describe("/admin/products", () => {
variants: [
expect.objectContaining({
title: "Test variant 1",
rank: 0,
variant_rank: 0,
}),
expect.objectContaining({
title: "Test variant 2",
rank: 1,
variant_rank: 1,
}),
],
})
@@ -332,17 +332,17 @@ describe("/admin/products", () => {
variants: [
expect.objectContaining({
id: "test-variant",
rank: 0,
variant_rank: 0,
title: "Test variant",
}),
expect.objectContaining({
id: "test-variant_1",
rank: 1,
variant_rank: 1,
title: "Test variant rank (1)",
}),
expect.objectContaining({
id: "test-variant_2",
rank: 2,
variant_rank: 2,
title: "Test variant rank (2)",
}),
],
@@ -83,7 +83,7 @@ module.exports = async (connection, data = {}) => {
id: "test-variant",
inventory_quantity: 10,
title: "Test variant",
rank: 0,
variant_rank: 0,
sku: "test-sku",
ean: "test-ean",
upc: "test-upc",
@@ -99,24 +99,13 @@ module.exports = async (connection, data = {}) => {
],
})
// const variantOption_1 = manager.create(ProductOptionValue, {
// id: "test-option-variant1",
// option_id: "test-option",
// value: "test-option1",
// variant_id: "test-variant",
// // product_id: "test-product",
// })
// variant1.options = [variantOption_1]
await manager.save(variant1)
// await manager.save(variantOption_1)
const variant2 = await manager.create(ProductVariant, {
id: "test-variant_1",
inventory_quantity: 10,
title: "Test variant rank (1)",
rank: 2,
variant_rank: 2,
sku: "test-sku1",
ean: "test-ean1",
upc: "test-upc1",
@@ -132,24 +121,13 @@ module.exports = async (connection, data = {}) => {
],
})
// const variantOption_2 = manager.create(ProductOptionValue, {
// id: "test-option-variant2",
// option_id: "test-option",
// value: "test-option2",
// variant_id: "test-variant_1",
// // product_id: "test-product",
// })
await manager.save(variant2)
// await manager.save(variantOption_2)
// variant2.options = [variantOption_2]
const variant3 = await manager.create(ProductVariant, {
id: "test-variant_2",
inventory_quantity: 10,
title: "Test variant rank (2)",
rank: 1,
variant_rank: 1,
sku: "test-sku2",
ean: "test-ean2",
upc: "test-upc2",
@@ -164,25 +142,5 @@ module.exports = async (connection, data = {}) => {
],
})
// const variantOption_3 = manager.create(ProductOptionValue, {
// id: "test-option-variant3",
// option_id: "test-option",
// value: "test-option3",
// variant_id: "test-variant_2",
// // product_id: "test-product",
// })
await manager.save(variant3)
// await manager.save(variantOption_3)
// variant3.options = [variantOption_3]
// const moneyAmount = await manager.create(MoneyAmount, {
// id: "money_amount",
// amount: 100,
// currency_code: "usd",
// variant_id: "test-variant",
// })
// await manager.save(moneyAmount)
}
@@ -41,6 +41,10 @@ describe("POST /admin/products", () => {
})
})
afterAll(async () => {
jest.clearAllMocks()
})
it("returns 200", () => {
expect(subject.status).toEqual(200)
})
@@ -51,7 +55,7 @@ describe("POST /admin/products", () => {
IdMap.getId("productWithOptions"),
{
title: "Test",
rank: 0,
variant_rank: 0,
prices: [
{
currency_code: "USD",
@@ -67,7 +71,6 @@ describe("POST /admin/products", () => {
inventory_quantity: 0,
}
)
expect(true).toEqual(true)
})
})
@@ -113,19 +116,6 @@ describe("POST /admin/products", () => {
})
})
// it("calls productvariantservice create", () => {
// expect(ProductServiceMock.create).toHaveBeenCalledTimes(1)
// expect(ProductServiceMock.create).toHaveBeenCalledWith({
// title: "Test Product",
// discountable: true,
// description: "Test Description",
// tags: [{ id: "test", value: "test" }],
// handle: "test-product",
// is_giftcard: false,
// profile_id: IdMap.getId("default_shipping_profile"),
// })
// })
it("calls shipping profile default", () => {
expect(ShippingProfileServiceMock.retrieveDefault).toHaveBeenCalledTimes(
1
@@ -328,7 +328,7 @@ export default async (req, res) => {
.create({ ...value, profile_id: shippingProfile.id })
if (variants) {
for (const [i, variant] of variants.entries()) variant.rank = i
for (const [i, variant] of variants.entries()) variant.variant_rank = i
const optionIds = value.options.map(
o => newProduct.options.find(newO => newO.title === o.title).id
@@ -4,11 +4,11 @@ export class productVariantRank1630868939283 implements MigrationInterface {
name = 'productVariantRank1630868939283'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "product_variant" ADD "rank" integer NOT NULL`);
await queryRunner.query(`ALTER TABLE "product_variant" ADD "variant_rank" integer NOT NULL`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "product_variant" DROP COLUMN "rank"`);
await queryRunner.query(`ALTER TABLE "product_variant" DROP COLUMN "variant_rank"`);
}
}
@@ -64,7 +64,7 @@ export class ProductVariant {
upc: string
@Column()
rank: number
variant_rank: number
@Column({ type: "int" })
inventory_quantity: number
@@ -36,6 +36,8 @@ export const ProductServiceMock = {
if (data.title === "Test Product") {
return Promise.resolve(products.product1)
}
if (data.title === "Test Product with variants")
return Promise.resolve(products.productWithOptions)
return Promise.resolve({ ...data })
}),
@@ -148,6 +148,7 @@ describe("ProductVariantService", () => {
expect(productVariantRepository.create).toHaveBeenCalledWith({
id: IdMap.getId("v2"),
product_id: IdMap.getId("ironman"),
variant_rank: 0,
options: [
{
id: IdMap.getId("test"),
@@ -16,14 +16,14 @@ describe("ProductService", () => {
return {
id: "test id with variants",
variants: [
{ id: "test_321", title: "Green", rank: 1 },
{ id: "test_123", title: "Blue", rank: 0 },
{ id: "test_321", title: "Green", variant_rank: 1 },
{ id: "test_123", title: "Blue", variant_rank: 0 },
],
}
if (query.where.id === "test id one variant")
return {
id: "test id one variant",
variants: [{ id: "test_123", title: "Blue", rank: 0 }],
variants: [{ id: "test_123", title: "Blue", variant_rank: 0 }],
}
return Promise.resolve({ id: IdMap.getId("ironman") })
},
@@ -54,8 +54,8 @@ describe("ProductService", () => {
const expected = {
id: "test id with variants",
variants: [
{ id: "test_123", title: "Blue", rank: 0 },
{ id: "test_321", title: "Green", rank: 1 },
{ id: "test_123", title: "Blue", variant_rank: 0 },
{ id: "test_321", title: "Green", variant_rank: 1 },
],
}
@@ -138,8 +138,14 @@ describe("ProductService", () => {
tags: [{ value: "title" }, { value: "title2" }],
type: "type-1",
variants: [
{ id: "test1", title: "green", rank: 0 },
{ id: "test2", title: "blue", rank: 0 },
{
id: "test1",
title: "green",
},
{
id: "test2",
title: "blue",
},
],
})
@@ -153,8 +159,16 @@ describe("ProductService", () => {
expect(productRepository.create).toHaveBeenCalledWith({
title: "Suit",
variants: [
{ id: "test1", title: "green", rank: 0 },
{ id: "test2", title: "blue", rank: 1 },
{
id: "test1",
title: "green",
variant_rank: 0,
},
{
id: "test2",
title: "blue",
variant_rank: 1,
},
],
})
@@ -172,8 +186,14 @@ describe("ProductService", () => {
title: "Suit",
options: [],
tags: [
{ id: "tag-1", value: "title" },
{ id: "tag-2", value: "title2" },
{
id: "tag-1",
value: "title",
},
{
id: "tag-2",
value: "title2",
},
],
type_id: "type",
collection: {
@@ -181,8 +201,16 @@ describe("ProductService", () => {
title: "Suits",
},
variants: [
{ id: "test1", title: "green", rank: 0 },
{ id: "test2", title: "blue", rank: 1 },
{
id: "test1",
title: "green",
variant_rank: 0,
},
{
id: "test2",
title: "blue",
variant_rank: 1,
},
],
})
})
@@ -204,8 +232,8 @@ describe("ProductService", () => {
return Promise.resolve({
id: "ranking test",
variants: [
{ id: "test_321", title: "Greener", rank: 1 },
{ id: "test_123", title: "Blueer", rank: 0 },
{ id: "test_321", title: "Greener", variant_rank: 1 },
{ id: "test_123", title: "Blueer", variant_rank: 0 },
],
})
}
@@ -317,8 +345,8 @@ describe("ProductService", () => {
it("successfully updates variant ranking", async () => {
await productService.update("ranking test", {
variants: [
{ id: "test_321", title: "Greener", rank: 1 },
{ id: "test_123", title: "Blueer", rank: 0 },
{ id: "test_321", title: "Greener", variant_rank: 1 },
{ id: "test_123", title: "Blueer", variant_rank: 0 },
],
})
@@ -332,8 +360,8 @@ describe("ProductService", () => {
expect(productRepository.save).toHaveBeenCalledWith({
id: "ranking test",
variants: [
{ id: "test_321", title: "Greener", rank: 0 },
{ id: "test_123", title: "Blueer", rank: 1 },
{ id: "test_321", title: "Greener", variant_rank: 0 },
{ id: "test_123", title: "Blueer", variant_rank: 1 },
],
})
})
@@ -174,7 +174,7 @@ class ProductVariantService extends BaseService {
)
}
if (!rest.rank) rest.rank = 0
if (!rest.variant_rank) rest.variant_rank = 0
const toCreate = {
...rest,
+4 -3
View File
@@ -187,7 +187,7 @@ class ProductService extends BaseService {
if (product.variants) {
product.variants.sort(
(variant1, variant2) => variant1.rank - variant2.rank
(variant1, variant2) => variant1.variant_rank - variant2.variant_rank
)
}
@@ -300,7 +300,8 @@ class ProductService extends BaseService {
}
if (rest.variants)
for (const [i, variant] of rest.variants.entries()) variant.rank = i
for (const [i, variant] of rest.variants.entries())
variant.variant_rank = i
let product = productRepo.create(rest)
@@ -420,7 +421,7 @@ class ProductService extends BaseService {
const newVariants = []
for (const [i, newVariant] of variants.entries()) {
newVariant.rank = i
newVariant.variant_rank = i
if (newVariant.id) {
const variant = product.variants.find(v => v.id === newVariant.id)