feat(customer): add customer addresses (#6224)
**What** - adds methods to create update list customer addresses - removes default_shipping_id and billing id from customer record and moves them to address (better normalization)
This commit is contained in:
@@ -8,24 +8,46 @@ import {
|
||||
PrimaryKey,
|
||||
Property,
|
||||
ManyToOne,
|
||||
Cascade,
|
||||
Index,
|
||||
} from "@mikro-orm/core"
|
||||
import Customer from "./customer"
|
||||
|
||||
type OptionalAddressProps = DAL.EntityDateColumns // TODO: To be revisited when more clear
|
||||
|
||||
@Entity({ tableName: "customer_address" })
|
||||
@Index({
|
||||
name: "IDX_customer_address_unique_customer_shipping",
|
||||
expression:
|
||||
'create unique index "IDX_customer_address_unique_customer_shipping" on "customer_address" ("customer_id") where "is_default_shipping" = true',
|
||||
})
|
||||
@Index({
|
||||
name: "IDX_customer_address_unique_customer_billing",
|
||||
expression:
|
||||
'create unique index "IDX_customer_address_unique_customer_billing" on "customer_address" ("customer_id") where "is_default_billing" = true',
|
||||
})
|
||||
export default class Address {
|
||||
[OptionalProps]: OptionalAddressProps
|
||||
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id!: string
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
address_name: string | null = null
|
||||
|
||||
@Property({ columnType: "boolean", default: false })
|
||||
is_default_shipping: boolean = false
|
||||
|
||||
@Property({ columnType: "boolean", default: false })
|
||||
is_default_billing: boolean = false
|
||||
|
||||
@Property({ columnType: "text" })
|
||||
customer_id: string
|
||||
|
||||
@ManyToOne(() => Customer, {
|
||||
fieldName: "customer_id",
|
||||
index: "IDX_customer_address_customer_id",
|
||||
cascade: [Cascade.REMOVE, Cascade.PERSIST],
|
||||
})
|
||||
customer: Customer
|
||||
|
||||
|
||||
@@ -22,8 +22,6 @@ import Address from "./address"
|
||||
type OptionalCustomerProps =
|
||||
| "groups"
|
||||
| "addresses"
|
||||
| "default_shipping_address"
|
||||
| "default_billing_address"
|
||||
| DAL.SoftDeletableEntityDateColumns
|
||||
|
||||
@Entity({ tableName: "customer" })
|
||||
@@ -52,26 +50,6 @@ export default class Customer {
|
||||
@Property({ columnType: "boolean", default: false })
|
||||
has_account: boolean = false
|
||||
|
||||
@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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user