feat(medusa): Separate money amount and variant (#4906)

* initial changes

* working test

* final changes to product tests

* update integration tests

* update price list integration tests

* update integration tests

* update unit tests

* update plugin integration tests

* remove catch from integration test

* undo change

* add andWhere

* update upsertCurrencyMoneyAmount method

* undo line item changes

* undo changes

* update deprecated method

* Update packages/medusa/src/migrations/1692953518123-drop_money_amount_constraints_for_pricing_module.ts

Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>

* rename joinTable

* update with joinTable entity

* update load methods

* remove await create

* re-add context test

* update price list behavior for prices

* update price list snapshots

* re-add admin seeder

* pr feedback

* fix unit tests

* fix plugin integration tests

* initial review changes

* redo changes to variant creation

---------

Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Philip Korsholm
2023-09-13 13:26:20 +02:00
committed by GitHub
co-authored by Carlos R. L. Rodrigues Oli Juhl
parent 3d68be2b6b
commit 5d10c46bb1
52 changed files with 2465 additions and 910 deletions
+1
View File
@@ -54,6 +54,7 @@ export * from "./product-type"
export * from "./product-type-tax-rate"
export * from "./product-variant"
export * from "./product-variant-inventory-item"
export * from "./product-variant-money-amount"
export * from "./publishable-api-key"
export * from "./publishable-api-key-sales-channel"
export * from "./refund"
+42 -7
View File
@@ -1,9 +1,13 @@
import {
AfterLoad,
BeforeInsert,
BeforeUpdate,
Column,
Entity,
Index,
JoinColumn,
JoinTable,
ManyToMany,
ManyToOne,
} from "typeorm"
@@ -43,17 +47,27 @@ export class MoneyAmount extends SoftDeletableEntity {
@JoinColumn({ name: "price_list_id" })
price_list: PriceList | null
@Index('idx_money_amount_variant_id')
@Column({ nullable: true })
variant_id: string
@ManyToOne(() => ProductVariant, (variant) => variant.prices, {
@ManyToMany(() => ProductVariant, {
onDelete: "CASCADE",
})
@JoinColumn({ name: "variant_id" })
@JoinTable({
name: "product_variant_money_amount",
joinColumn: {
name: "money_amount_id",
referencedColumnName: "id",
},
inverseJoinColumn: {
name: "variant_id",
referencedColumnName: "id",
},
})
variants: ProductVariant[]
variant: ProductVariant
@Index('idx_money_amount_region_id')
variant_id: string
@Index("idx_money_amount_region_id")
@Column({ nullable: true })
region_id: string
@@ -64,6 +78,27 @@ export class MoneyAmount extends SoftDeletableEntity {
@BeforeInsert()
private beforeInsert(): undefined | void {
this.id = generateEntityId(this.id, "ma")
if (this.variant || this.variant_id) {
this.variants = [
{ id: this.variant?.id || this.variant_id },
] as ProductVariant[]
}
}
@BeforeUpdate()
private beforeUpdate(): void {
if (this.variant || this.variant_id) {
this.variants = [
{ id: this.variant?.id || this.variant_id },
] as ProductVariant[]
}
}
@AfterLoad()
private afterLoad() {
this.variant = this.variants?.[0]
this.variant_id = this.variant?.id
}
}
@@ -25,7 +25,7 @@ export class ProductCategory extends BaseEntity {
@Column()
name: string
@Column({ nullable: false, default: '' })
@Column({ nullable: false, default: "" })
description: string
@Index({ unique: true })
@@ -0,0 +1,21 @@
import { BeforeInsert, Column, Entity, Index } from "typeorm"
import { generateEntityId } from "../utils"
import { SoftDeletableEntity } from "../interfaces"
@Entity()
export class ProductVariantMoneyAmount extends SoftDeletableEntity {
@Index("idx_product_variant_money_amount_money_amount_id_unique", {
unique: true,
})
@Column()
money_amount_id: string
@Index("idx_product_variant_money_amount_variant_id")
@Column()
variant_id: string
@BeforeInsert()
private beforeInsert(): void {
this.id = generateEntityId(this.id, "pvma_")
}
}
+15 -3
View File
@@ -4,6 +4,8 @@ import {
Entity,
Index,
JoinColumn,
JoinTable,
ManyToMany,
ManyToOne,
OneToMany,
} from "typeorm"
@@ -28,9 +30,19 @@ export class ProductVariant extends SoftDeletableEntity {
@JoinColumn({ name: "product_id" })
product: Product
@OneToMany(() => MoneyAmount, (ma) => ma.variant, {
cascade: true,
onDelete: "CASCADE",
@ManyToMany(() => MoneyAmount, {
cascade: ["remove", "soft-remove", "recover"],
})
@JoinTable({
name: "product_variant_money_amount",
joinColumn: {
name: "variant_id",
referencedColumnName: "id",
},
inverseJoinColumn: {
name: "money_amount_id",
referencedColumnName: "id",
},
})
prices: MoneyAmount[]