feat(medusa): Continue create product workflow changes (#4473)
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import {
|
||||
AfterLoad,
|
||||
BeforeInsert,
|
||||
BeforeUpdate,
|
||||
Column,
|
||||
Entity,
|
||||
Index,
|
||||
@@ -23,7 +25,7 @@ import { SalesChannel } from "./sales-channel"
|
||||
import { ShippingProfile } from "./shipping-profile"
|
||||
import { SoftDeletableEntity } from "../interfaces/models/soft-deletable-entity"
|
||||
import _ from "lodash"
|
||||
import { generateEntityId } from "../utils/generate-entity-id"
|
||||
import { generateEntityId } from "../utils"
|
||||
|
||||
export enum ProductStatus {
|
||||
DRAFT = "draft",
|
||||
@@ -92,14 +94,26 @@ export class Product extends SoftDeletableEntity {
|
||||
})
|
||||
categories: ProductCategory[]
|
||||
|
||||
@Index()
|
||||
@Column()
|
||||
profile_id: string
|
||||
|
||||
@ManyToOne(() => ShippingProfile)
|
||||
@JoinColumn({ name: "profile_id" })
|
||||
profile: ShippingProfile
|
||||
|
||||
@ManyToMany(() => ShippingProfile, {
|
||||
cascade: ["remove", "soft-remove"],
|
||||
})
|
||||
@JoinTable({
|
||||
name: "product_shipping_profile",
|
||||
joinColumn: {
|
||||
name: "product_id",
|
||||
referencedColumnName: "id",
|
||||
},
|
||||
inverseJoinColumn: {
|
||||
name: "profile_id",
|
||||
referencedColumnName: "id",
|
||||
},
|
||||
})
|
||||
profiles: ShippingProfile[]
|
||||
|
||||
@Column({ type: "int", nullable: true })
|
||||
weight: number | null
|
||||
|
||||
@@ -185,6 +199,25 @@ export class Product extends SoftDeletableEntity {
|
||||
if (!this.handle) {
|
||||
this.handle = _.kebabCase(this.title)
|
||||
}
|
||||
|
||||
if (this.profile_id) {
|
||||
this.profiles = [{ id: this.profile_id }] as ShippingProfile[]
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeUpdate()
|
||||
private beforeUpdate(): void {
|
||||
if (this.profile_id) {
|
||||
this.profiles = [{ id: this.profile_id }] as ShippingProfile[]
|
||||
}
|
||||
}
|
||||
|
||||
@AfterLoad()
|
||||
private afterLoad(): void {
|
||||
if (this.profiles) {
|
||||
this.profile = this.profiles[this.profiles.length - 1]!
|
||||
this.profile_id = this.profile?.id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -288,6 +321,12 @@ export class Product extends SoftDeletableEntity {
|
||||
* description: Available if the relation `profile` is expanded.
|
||||
* nullable: true
|
||||
* $ref: "#/components/schemas/ShippingProfile"
|
||||
* profiles:
|
||||
* description: Available if the relation `profiles` is expanded.
|
||||
* nullable: true
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: "#/components/schemas/ShippingProfile"
|
||||
* weight:
|
||||
* description: The weight of the Product Variant. May be used in shipping rate calculations.
|
||||
* nullable: true
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
import { BeforeInsert, Column, Entity, OneToMany } from "typeorm"
|
||||
import {
|
||||
BeforeInsert,
|
||||
Column,
|
||||
Entity,
|
||||
JoinTable,
|
||||
ManyToMany,
|
||||
OneToMany,
|
||||
} from "typeorm"
|
||||
|
||||
import { DbAwareColumn } from "../utils/db-aware-column"
|
||||
import { DbAwareColumn, generateEntityId } from "../utils"
|
||||
import { Product } from "./product"
|
||||
import { ShippingOption } from "./shipping-option"
|
||||
import { SoftDeletableEntity } from "../interfaces/models/soft-deletable-entity"
|
||||
import { generateEntityId } from "../utils/generate-entity-id"
|
||||
import { SoftDeletableEntity } from "../interfaces"
|
||||
|
||||
export enum ShippingProfileType {
|
||||
DEFAULT = "default",
|
||||
@@ -20,7 +26,18 @@ export class ShippingProfile extends SoftDeletableEntity {
|
||||
@DbAwareColumn({ type: "enum", enum: ShippingProfileType })
|
||||
type: ShippingProfileType
|
||||
|
||||
@OneToMany(() => Product, (product) => product.profile)
|
||||
@ManyToMany(() => Product)
|
||||
@JoinTable({
|
||||
name: "product_shipping_profile",
|
||||
joinColumn: {
|
||||
name: "profile_id",
|
||||
referencedColumnName: "id",
|
||||
},
|
||||
inverseJoinColumn: {
|
||||
name: "product_id",
|
||||
referencedColumnName: "id",
|
||||
},
|
||||
})
|
||||
products: Product[]
|
||||
|
||||
@OneToMany(() => ShippingOption, (so) => so.profile)
|
||||
|
||||
Reference in New Issue
Block a user