feat(medusa): Sales Channels model (#1782)

**What**
- added `SalesChannel` entity
- added `SalesChannel` repository
- added `SalesChannel` relations to the order, cart and store entities
- added a migrations file

**How**
- introduced entities and relations under a new feature flag "sales-channels"

Fixes CORE-271

Co-authored-by: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com>
This commit is contained in:
Frane Polić
2022-07-05 07:31:11 +00:00
committed by GitHub
co-authored by Philip Korsholm
parent 9a14b84e58
commit f9c3218aac
10 changed files with 184 additions and 13 deletions
-1
View File
@@ -15,7 +15,6 @@ import { DbAwareColumn, resolveDbType } from "../utils/db-aware-column"
import { SoftDeletableEntity } from "../interfaces/models/soft-deletable-entity"
import { generateEntityId } from "../utils/generate-entity-id"
import { User } from "./user"
import { RequestQueryFields, Selector } from "../types/common"
@Entity()
export class BatchJob extends SoftDeletableEntity {
+14
View File
@@ -107,6 +107,11 @@ import { Region } from "./region"
import { ShippingMethod } from "./shipping-method"
import { SoftDeletableEntity } from "../interfaces/models/soft-deletable-entity"
import { generateEntityId } from "../utils/generate-entity-id"
import {
FeatureFlagColumn,
FeatureFlagDecorators,
} from "../utils/feature-flag-decorators"
import { SalesChannel } from "./sales-channel"
export enum CartType {
DEFAULT = "default",
@@ -230,6 +235,15 @@ export class Cart extends SoftDeletableEntity {
@DbAwareColumn({ type: "jsonb", nullable: true })
metadata: Record<string, unknown>
@FeatureFlagColumn("sales_channels", { type: "varchar", nullable: true })
sales_channel_id: string | null
@FeatureFlagDecorators("sales_channels", [
ManyToOne(() => SalesChannel),
JoinColumn({ name: "sales_channel_id" }),
])
sales_channel: SalesChannel
shipping_total?: number
discount_total?: number
tax_total?: number | null
+14
View File
@@ -36,6 +36,11 @@ import { Return } from "./return"
import { ShippingMethod } from "./shipping-method"
import { Swap } from "./swap"
import { generateEntityId } from "../utils/generate-entity-id"
import {
FeatureFlagColumn,
FeatureFlagDecorators,
} from "../utils/feature-flag-decorators"
import { SalesChannel } from "./sales-channel"
export enum OrderStatus {
PENDING = "pending",
@@ -225,6 +230,15 @@ export class Order extends BaseEntity {
@Column({ type: "varchar", nullable: true })
external_id: string | null
@FeatureFlagColumn("sales_channels", { type: "varchar", nullable: true })
sales_channel_id: string | null
@FeatureFlagDecorators("sales_channels", [
ManyToOne(() => SalesChannel),
JoinColumn({ name: "sales_channel_id" }),
])
sales_channel: SalesChannel
// Total fields
shipping_total: number
discount_total: number
+26 -11
View File
@@ -9,6 +9,7 @@ import {
ManyToMany,
ManyToOne,
OneToMany,
OneToOne,
} from "typeorm"
import { DbAwareColumn } from "../utils/db-aware-column"
import { Image } from "./image"
@@ -20,6 +21,11 @@ import { ProductVariant } from "./product-variant"
import { ShippingProfile } from "./shipping-profile"
import { SoftDeletableEntity } from "../interfaces/models/soft-deletable-entity"
import { generateEntityId } from "../utils/generate-entity-id"
import {
FeatureFlagColumn,
FeatureFlagDecorators,
} from "../utils/feature-flag-decorators"
import { SalesChannel } from "./sales-channel"
export enum ProductStatus {
DRAFT = "draft",
@@ -66,19 +72,12 @@ export class Product extends SoftDeletableEntity {
@Column({ type: "text", nullable: true })
thumbnail: string | null
@OneToMany(
() => ProductOption,
(productOption) => productOption.product
)
@OneToMany(() => ProductOption, (productOption) => productOption.product)
options: ProductOption[]
@OneToMany(
() => ProductVariant,
(variant) => variant.product,
{
cascade: true,
}
)
@OneToMany(() => ProductVariant, (variant) => variant.product, {
cascade: true,
})
variants: ProductVariant[]
@Index()
@@ -150,6 +149,22 @@ export class Product extends SoftDeletableEntity {
@DbAwareColumn({ type: "jsonb", nullable: true })
metadata: Record<string, unknown> | null
@FeatureFlagDecorators("sales_channels", [
ManyToMany(() => SalesChannel, { cascade: true }),
JoinTable({
name: "product_sales_channel",
joinColumn: {
name: "product_id",
referencedColumnName: "id",
},
inverseJoinColumn: {
name: "sales_channel_id",
referencedColumnName: "id",
},
}),
])
sales_channels: SalesChannel[]
@BeforeInsert()
private beforeInsert(): void {
if (this.id) return
@@ -0,0 +1,26 @@
import { BeforeInsert, Column } from "typeorm"
import { SoftDeletableEntity } from "../interfaces"
import { FeatureFlagEntity } from "../utils/feature-flag-decorators"
import { resolveDbType } from "../utils/db-aware-column"
import { generateEntityId } from "../utils"
@FeatureFlagEntity("sales_channels")
export class SalesChannel extends SoftDeletableEntity {
@Column()
name: string
@Column({ type: "varchar", nullable: true })
description: string | null
@Column({ default: false })
is_disabled: boolean
// @Column({ type: resolveDbType("timestamptz"), nullable: true })
// disabled_at: Date | null
@BeforeInsert()
private beforeInsert(): void {
this.id = generateEntityId(this.id, "sc")
}
}
+15
View File
@@ -6,12 +6,18 @@ import {
JoinTable,
ManyToMany,
ManyToOne,
OneToOne,
} from "typeorm"
import { BaseEntity } from "../interfaces/models/base-entity"
import { DbAwareColumn } from "../utils/db-aware-column"
import { Currency } from "./currency"
import { generateEntityId } from "../utils/generate-entity-id"
import { SalesChannel } from "./sales-channel"
import {
FeatureFlagColumn,
FeatureFlagDecorators,
} from "../utils/feature-flag-decorators"
@Entity()
export class Store extends BaseEntity {
@@ -51,6 +57,15 @@ export class Store extends BaseEntity {
@DbAwareColumn({ type: "jsonb", nullable: true })
metadata: Record<string, unknown>
@FeatureFlagColumn("sales_channels", { nullable: true })
default_sales_channel_id: string
@FeatureFlagDecorators("sales_channels", [
OneToOne(() => SalesChannel),
JoinColumn({ name: "default_sales_channel_id" }),
])
default_sales_channel: SalesChannel
@BeforeInsert()
private beforeInsert(): void {
this.id = generateEntityId(this.id, "store")