diff --git a/integration-tests/api/__tests__/admin/product.js b/integration-tests/api/__tests__/admin/product.js index e179af156d..681fb7a010 100644 --- a/integration-tests/api/__tests__/admin/product.js +++ b/integration-tests/api/__tests__/admin/product.js @@ -74,7 +74,6 @@ describe("/admin/products", () => { }) expect(response.status).toEqual(200) - expect(response.data.product).toEqual( expect.objectContaining({ title: "Test product", @@ -523,8 +522,6 @@ describe("/admin/products", () => { it("successfully creates soft-deleted product variant", async () => { const api = useApi() - console.log("test") - const product = await api .get("/admin/products/test-product", { headers: { @@ -534,7 +531,6 @@ describe("/admin/products", () => { .catch((err) => { console.log(err) }) - console.log(product.data.product) const response = await api .delete("/admin/products/test-product/variants/test-variant", { diff --git a/integration-tests/api/helpers/product-seeder.js b/integration-tests/api/helpers/product-seeder.js index ae7109b4e6..ea3de2e4b7 100644 --- a/integration-tests/api/helpers/product-seeder.js +++ b/integration-tests/api/helpers/product-seeder.js @@ -69,19 +69,16 @@ module.exports = async (connection, data = {}) => { ], }) - const productOption = manager.create(ProductOption, { - id: "test-option", - title: "test-option", - }) - - await manager.save(productOption) - - p.options = [productOption] - p.images = [image] await manager.save(p) + await manager.save(ProductOption, { + id: "test-option", + title: "test-option", + product_id: "test-product", + }) + const variant1 = await manager.create(ProductVariant, { id: "test-variant", inventory_quantity: 10, @@ -93,21 +90,27 @@ module.exports = async (connection, data = {}) => { barcode: "test-barcode", product_id: "test-product", prices: [{ id: "test-price", currency_code: "usd", amount: 100 }], - // options: [{ id: "test-variant-option", value: "Default variant" }], + options: [ + { + id: "test-variant-option", + value: "Default variant", + option_id: "test-option", + }, + ], }) - 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", - }) + // 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) + // await manager.save(variantOption_1) const variant2 = await manager.create(ProductVariant, { id: "test-variant_1", @@ -120,19 +123,25 @@ module.exports = async (connection, data = {}) => { barcode: "test-barcode 1", product_id: "test-product", prices: [{ id: "test-price1", currency_code: "usd", amount: 100 }], - // options: [{ id: "test-variant-option-1", value: "Default variant 1" }], + options: [ + { + id: "test-variant-option-1", + value: "Default variant 1", + option_id: "test-option", + }, + ], }) - 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", - }) + // 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) + // await manager.save(variantOption_2) // variant2.options = [variantOption_2] @@ -146,28 +155,34 @@ module.exports = async (connection, data = {}) => { upc: "test-upc2", product_id: "test-product", prices: [{ id: "test-price2", currency_code: "usd", amount: 100 }], - // options: [{ id: "test-variant-option-2", value: "Default variant 2" }], + options: [ + { + id: "test-variant-option-2", + value: "Default variant 2", + option_id: "test-option", + }, + ], }) - 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", - }) + // 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) + // 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", - }) + // const moneyAmount = await manager.create(MoneyAmount, { + // id: "money_amount", + // amount: 100, + // currency_code: "usd", + // variant_id: "test-variant", + // }) - await manager.save(moneyAmount) + // 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 eb6c8d3655..f076ffb4e6 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 @@ -96,7 +96,6 @@ describe("POST /admin/products", () => { }) it("returns created product draft", () => { - console.log(subject.body) expect(subject.body.product.id).toEqual(IdMap.getId("product1")) }) 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 779e800109..0f90634ac3 100644 --- a/packages/medusa/src/api/routes/admin/products/create-product.js +++ b/packages/medusa/src/api/routes/admin/products/create-product.js @@ -327,10 +327,6 @@ export default async (req, res) => { .withTransaction(manager) .create({ ...value, profile_id: shippingProfile.id }) - // console.log(variants) - // console.log(value) - // console.log(newProduct) - if (variants) { for (const [i, variant] of variants.entries()) variant.rank = i @@ -356,18 +352,13 @@ export default async (req, res) => { } }) - // console.log("got to after variants") - const product = await productService.retrieve(newProduct.id, { select: defaultFields, relations: defaultRelations, }) - // console.log(product) - res.json({ product }) } catch (err) { - // console.log(err) throw err } } diff --git a/packages/medusa/src/migrations/1630868939283-product_variant_rank.ts b/packages/medusa/src/migrations/1630868939283-product_variant_rank.ts new file mode 100644 index 0000000000..9f76a6596e --- /dev/null +++ b/packages/medusa/src/migrations/1630868939283-product_variant_rank.ts @@ -0,0 +1,14 @@ +import {MigrationInterface, QueryRunner} from "typeorm"; + +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`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "product_variant" DROP COLUMN "rank"`); + } + +} diff --git a/packages/medusa/src/migrations/1630870102483-product_variant_option_cascade.ts b/packages/medusa/src/migrations/1630870102483-product_variant_option_cascade.ts new file mode 100644 index 0000000000..6ab95c0060 --- /dev/null +++ b/packages/medusa/src/migrations/1630870102483-product_variant_option_cascade.ts @@ -0,0 +1,17 @@ +import {MigrationInterface, QueryRunner} from "typeorm"; + +export class productVariantOptionCascade1630870102483 implements MigrationInterface { + name = 'productVariantOptionCascade1630870102483' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "product_option_value" DROP CONSTRAINT "FK_7234ed737ff4eb1b6ae6e6d7b01"`); + await queryRunner.query(`ALTER TABLE "product_option_value" ADD CONSTRAINT "FK_7234ed737ff4eb1b6ae6e6d7b01" FOREIGN KEY ("variant_id") REFERENCES "product_variant"("id") ON DELETE cascade ON UPDATE NO ACTION`); + + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "product_option_value" DROP CONSTRAINT "FK_7234ed737ff4eb1b6ae6e6d7b01"`); + await queryRunner.query(`ALTER TABLE "product_option_value" ADD CONSTRAINT "FK_7234ed737ff4eb1b6ae6e6d7b01" FOREIGN KEY ("variant_id") REFERENCES "product_variant"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + +} diff --git a/packages/medusa/src/models/product-option-value.ts b/packages/medusa/src/models/product-option-value.ts index 64a390bafe..379ad87591 100644 --- a/packages/medusa/src/models/product-option-value.ts +++ b/packages/medusa/src/models/product-option-value.ts @@ -41,7 +41,8 @@ export class ProductOptionValue { @ManyToOne( () => ProductVariant, - variant => variant.options + variant => variant.options, + { onDelete: "cascade" } ) @JoinColumn({ name: "variant_id" }) variant: ProductVariant diff --git a/packages/medusa/src/models/product-variant.ts b/packages/medusa/src/models/product-variant.ts index d3f70ae6ac..a1fd09968a 100644 --- a/packages/medusa/src/models/product-variant.ts +++ b/packages/medusa/src/models/product-variant.ts @@ -43,7 +43,7 @@ export class ProductVariant { @OneToMany( () => MoneyAmount, ma => ma.variant, - { cascade: true, onDelete: "CASCADE" } + { cascade: ["remove"] } ) prices: MoneyAmount[] diff --git a/packages/medusa/src/services/product.js b/packages/medusa/src/services/product.js index 779ef8804a..b439fd12b2 100644 --- a/packages/medusa/src/services/product.js +++ b/packages/medusa/src/services/product.js @@ -185,10 +185,11 @@ class ProductService extends BaseService { ) } - if (product.variants) + if (product.variants) { product.variants.sort( (variant1, variant2) => variant1.rank - variant2.rank ) + } return product }