diff --git a/integration-tests/api/__tests__/admin/product.js b/integration-tests/api/__tests__/admin/product.js index 681fb7a010..6d49e86fed 100644 --- a/integration-tests/api/__tests__/admin/product.js +++ b/integration-tests/api/__tests__/admin/product.js @@ -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)", }), ], diff --git a/integration-tests/api/helpers/product-seeder.js b/integration-tests/api/helpers/product-seeder.js index ea3de2e4b7..c92137cfe6 100644 --- a/integration-tests/api/helpers/product-seeder.js +++ b/integration-tests/api/helpers/product-seeder.js @@ -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) } diff --git a/packages/medusa/src/api/routes/admin/products/__tests__/create-product.js b/packages/medusa/src/api/routes/admin/products/__tests__/create-product.js index f076ffb4e6..25a9012395 100644 --- a/packages/medusa/src/api/routes/admin/products/__tests__/create-product.js +++ b/packages/medusa/src/api/routes/admin/products/__tests__/create-product.js @@ -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 diff --git a/packages/medusa/src/api/routes/admin/products/create-product.js b/packages/medusa/src/api/routes/admin/products/create-product.js index 0f90634ac3..95c5f4b3b7 100644 --- a/packages/medusa/src/api/routes/admin/products/create-product.js +++ b/packages/medusa/src/api/routes/admin/products/create-product.js @@ -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 diff --git a/packages/medusa/src/migrations/1630868939283-product_variant_rank.ts b/packages/medusa/src/migrations/1630868939283-product_variant_rank.ts index 9f76a6596e..0461e33c01 100644 --- a/packages/medusa/src/migrations/1630868939283-product_variant_rank.ts +++ b/packages/medusa/src/migrations/1630868939283-product_variant_rank.ts @@ -4,11 +4,11 @@ export class productVariantRank1630868939283 implements MigrationInterface { name = 'productVariantRank1630868939283' public async up(queryRunner: QueryRunner): Promise { - 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 { - await queryRunner.query(`ALTER TABLE "product_variant" DROP COLUMN "rank"`); + await queryRunner.query(`ALTER TABLE "product_variant" DROP COLUMN "variant_rank"`); } } diff --git a/packages/medusa/src/models/product-variant.ts b/packages/medusa/src/models/product-variant.ts index d3f70ae6ac..363ef074a8 100644 --- a/packages/medusa/src/models/product-variant.ts +++ b/packages/medusa/src/models/product-variant.ts @@ -64,7 +64,7 @@ export class ProductVariant { upc: string @Column() - rank: number + variant_rank: number @Column({ type: "int" }) inventory_quantity: number diff --git a/packages/medusa/src/services/__mocks__/product.js b/packages/medusa/src/services/__mocks__/product.js index 82fcff22ae..9880d920f2 100644 --- a/packages/medusa/src/services/__mocks__/product.js +++ b/packages/medusa/src/services/__mocks__/product.js @@ -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 }) }), diff --git a/packages/medusa/src/services/__tests__/product-variant.js b/packages/medusa/src/services/__tests__/product-variant.js index 08950022fc..ca6d00614a 100644 --- a/packages/medusa/src/services/__tests__/product-variant.js +++ b/packages/medusa/src/services/__tests__/product-variant.js @@ -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"), diff --git a/packages/medusa/src/services/__tests__/product.js b/packages/medusa/src/services/__tests__/product.js index 4f94b92494..2cc3a90918 100644 --- a/packages/medusa/src/services/__tests__/product.js +++ b/packages/medusa/src/services/__tests__/product.js @@ -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 }, ], }) }) diff --git a/packages/medusa/src/services/product-variant.js b/packages/medusa/src/services/product-variant.js index adb4af2135..db7373ecc9 100644 --- a/packages/medusa/src/services/product-variant.js +++ b/packages/medusa/src/services/product-variant.js @@ -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, diff --git a/packages/medusa/src/services/product.js b/packages/medusa/src/services/product.js index b439fd12b2..dc020b45c7 100644 --- a/packages/medusa/src/services/product.js +++ b/packages/medusa/src/services/product.js @@ -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)