feat: customer module skeleton (#6126)

This commit is contained in:
Sebastian Rindom
2024-01-19 10:18:54 +00:00
committed by GitHub
parent a12c28b7d5
commit 12aa1737d5
47 changed files with 1157 additions and 0 deletions
+89
View File
@@ -0,0 +1,89 @@
import { DAL } from "@medusajs/types"
import { generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Entity,
OnInit,
OptionalProps,
PrimaryKey,
Property,
ManyToOne,
} from "@mikro-orm/core"
import Customer from "./customer"
type OptionalAddressProps = DAL.EntityDateColumns // TODO: To be revisited when more clear
@Entity({ tableName: "customer_address" })
export default class Address {
[OptionalProps]: OptionalAddressProps
@PrimaryKey({ columnType: "text" })
id!: string
@Property({ columnType: "text" })
customer_id: string
@ManyToOne(() => Customer, {
fieldName: "customer_id",
nullable: true,
})
customer?: Customer
@Property({ columnType: "text", nullable: true })
company: string | null = null
@Property({ columnType: "text", nullable: true })
first_name: string | null = null
@Property({ columnType: "text", nullable: true })
last_name: string | null = null
@Property({ columnType: "text", nullable: true })
address_1: string | null = null
@Property({ columnType: "text", nullable: true })
address_2: string | null = null
@Property({ columnType: "text", nullable: true })
city: string | null = null
@Property({ columnType: "text", nullable: true })
country_code: string | null = null
@Property({ columnType: "text", nullable: true })
province: string | null = null
@Property({ columnType: "text", nullable: true })
postal_code: string | null = null
@Property({ columnType: "text", nullable: true })
phone: string | null = null
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null = null
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "cuaddr")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "cuaddr")
}
}
@@ -0,0 +1,65 @@
import { DAL } from "@medusajs/types"
import { generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
ManyToOne,
Entity,
OnInit,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import Customer from "./customer"
import CustomerGroup from "./customer-group"
type OptionalGroupProps = DAL.EntityDateColumns // TODO: To be revisited when more clear
@Entity({ tableName: "customer_group_customer" })
export default class CustomerGroupCustomer {
[OptionalProps]: OptionalGroupProps
@PrimaryKey({ columnType: "text" })
id!: string
@ManyToOne({
entity: () => Customer,
fieldName: "customer__id",
index: "IDX_customer_group_customer_customer_id",
})
customer: Customer
@ManyToOne({
entity: () => CustomerGroup,
fieldName: "customer_group_id",
index: "IDX_customer_group_customer_group_id",
})
customer_group: CustomerGroup
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null = null
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "cusgc")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "cusgc")
}
}
@@ -0,0 +1,61 @@
import { DAL } from "@medusajs/types"
import { generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Entity,
OnInit,
OptionalProps,
PrimaryKey,
Property,
ManyToMany,
Collection,
} from "@mikro-orm/core"
import Customer from "./customer"
import CustomerGroupCustomer from "./customer-group-customer"
type OptionalGroupProps = DAL.EntityDateColumns // TODO: To be revisited when more clear
@Entity({ tableName: "customer_group" })
export default class CustomerGroup {
[OptionalProps]: OptionalGroupProps
@PrimaryKey({ columnType: "text" })
id!: string
@Property({ columnType: "text", nullable: true })
name: string | null = null
@ManyToMany({
entity: () => Customer,
pivotEntity: () => CustomerGroupCustomer,
})
customers = new Collection<Customer>(this)
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null = null
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "cusgroup")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "cusgroup")
}
}
+115
View File
@@ -0,0 +1,115 @@
import { DAL } from "@medusajs/types"
import { generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
Index,
ManyToMany,
ManyToOne,
OnInit,
OneToMany,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import CustomerGroup from "./customer-group"
import CustomerGroupCustomer from "./customer-group-customer"
import Address from "./address"
type OptionalCustomerProps =
| "groups"
| "addresses"
| "default_shipping_address"
| "default_billing_address"
| DAL.EntityDateColumns
@Entity({ tableName: "customer" })
export default class Customer {
[OptionalProps]?: OptionalCustomerProps
@PrimaryKey({ columnType: "text" })
id: string
@Property({ columnType: "text", nullable: true })
company_name: string | null = null
@Property({ columnType: "text", nullable: true })
first_name: string | null = null
@Property({ columnType: "text", nullable: true })
last_name: string | null = null
@Property({ columnType: "text", nullable: true })
email: string | null = null
@Property({ columnType: "text", nullable: true })
phone: string | null = null
@Index({ name: "IDX_customer_default_shipping_address_id" })
@Property({ columnType: "text", nullable: true })
default_shipping_address_id: string | null = null
@ManyToOne(() => Address, {
fieldName: "default_shipping_address_id",
nullable: true,
})
default_shipping_address: Address | null
@Index({ name: "IDX_customer_default_billing_address_id" })
@Property({ columnType: "text", nullable: true })
default_billing_address_id: string | null = null
@ManyToOne(() => Address, {
fieldName: "default_billing_address_id",
nullable: true,
})
default_billing_address: Address | null
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null = null
@ManyToMany({
inversedBy: (group) => group.customers,
entity: () => CustomerGroup,
pivotEntity: () => CustomerGroupCustomer,
})
groups = new Collection<CustomerGroup>(this)
@OneToMany(() => Address, (address) => address.customer, {
cascade: [Cascade.REMOVE],
})
addresses = new Collection<Address>(this)
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@Property({ columnType: "text", nullable: true })
created_by: string | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "cus")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "cus")
}
}
+4
View File
@@ -0,0 +1,4 @@
export { default as Address } from "./address"
export { default as Customer } from "./customer"
export { default as CustomerGroup } from "./customer-group"
export { default as CustomerGroupCustomer } from "./customer-group-customer"