152 lines
3.9 KiB
TypeScript
152 lines
3.9 KiB
TypeScript
import {
|
|
BeforeInsert,
|
|
Column,
|
|
Entity,
|
|
Index,
|
|
JoinColumn,
|
|
JoinTable,
|
|
ManyToMany,
|
|
OneToMany,
|
|
OneToOne,
|
|
} from "typeorm"
|
|
|
|
import { Address } from "./address"
|
|
import { CustomerGroup } from "./customer-group"
|
|
import { DbAwareColumn } from "../utils/db-aware-column"
|
|
import { Order } from "./order"
|
|
import { SoftDeletableEntity } from "../interfaces/models/soft-deletable-entity"
|
|
import { generateEntityId } from "../utils/generate-entity-id"
|
|
|
|
@Entity()
|
|
export class Customer extends SoftDeletableEntity {
|
|
@Index({ unique: true })
|
|
@Column()
|
|
email: string
|
|
|
|
@Column({ nullable: true })
|
|
first_name: string
|
|
|
|
@Column({ nullable: true })
|
|
last_name: string
|
|
|
|
@Index()
|
|
@Column({ nullable: true })
|
|
billing_address_id: string | null
|
|
|
|
@OneToOne(() => Address)
|
|
@JoinColumn({ name: "billing_address_id" })
|
|
billing_address: Address
|
|
|
|
@OneToMany(() => Address, (address) => address.customer)
|
|
shipping_addresses: Address[]
|
|
|
|
@Column({ nullable: true, select: false })
|
|
password_hash: string
|
|
|
|
@Column({ nullable: true })
|
|
phone: string
|
|
|
|
@Column({ default: false })
|
|
has_account: boolean
|
|
|
|
@OneToMany(() => Order, (order) => order.customer)
|
|
orders: Order[]
|
|
|
|
@JoinTable({
|
|
name: "customer_group_customers",
|
|
inverseJoinColumn: {
|
|
name: "customer_group_id",
|
|
referencedColumnName: "id",
|
|
},
|
|
joinColumn: {
|
|
name: "customer_id",
|
|
referencedColumnName: "id",
|
|
},
|
|
})
|
|
@ManyToMany(() => CustomerGroup, (cg) => cg.customers, {
|
|
onDelete: "CASCADE",
|
|
})
|
|
groups: CustomerGroup[]
|
|
|
|
@DbAwareColumn({ type: "jsonb", nullable: true })
|
|
metadata: Record<string, unknown>
|
|
|
|
@BeforeInsert()
|
|
private beforeInsert(): void {
|
|
this.id = generateEntityId(this.id, "cus")
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @schema customer
|
|
* title: "Customer"
|
|
* description: "Represents a customer"
|
|
* x-resourceId: customer
|
|
* required:
|
|
* - email
|
|
* properties:
|
|
* id:
|
|
* type: string
|
|
* description: The customer's ID
|
|
* example: cus_01G2SG30J8C85S4A5CHM2S1NS2
|
|
* email:
|
|
* type: string
|
|
* description: The customer's email
|
|
* format: email
|
|
* first_name:
|
|
* type: string
|
|
* description: The customer's first name
|
|
* example: Arno
|
|
* last_name:
|
|
* type: string
|
|
* description: The customer's first name
|
|
* example: Willms
|
|
* billing_address_id:
|
|
* type: string
|
|
* description: The customer's billing address ID
|
|
* example: addr_01G8ZH853YPY9B94857DY91YGW
|
|
* billing_address:
|
|
* description: Available if the relation `billing_address` is expanded.
|
|
* $ref: "#/components/schemas/address"
|
|
* shipping_addresses:
|
|
* description: Available if the relation `shipping_addresses` is expanded.
|
|
* type: array
|
|
* items:
|
|
* $ref: "#/components/schemas/address"
|
|
* phone:
|
|
* type: string
|
|
* description: The customer's phone number
|
|
* example: 16128234334802
|
|
* has_account:
|
|
* type: boolean
|
|
* description: Whether the customer has an account or not
|
|
* default: false
|
|
* orders:
|
|
* description: Available if the relation `orders` is expanded.
|
|
* type: array
|
|
* items:
|
|
* type: object
|
|
* description: An order object.
|
|
* groups:
|
|
* description: The customer groups the customer belongs to. Available if the relation `groups` is expanded.
|
|
* type: array
|
|
* items:
|
|
* $ref: "#/components/schemas/customer_group"
|
|
* created_at:
|
|
* type: string
|
|
* description: "The date with timezone at which the resource was created."
|
|
* format: date-time
|
|
* updated_at:
|
|
* type: string
|
|
* description: "The date with timezone at which the resource was updated."
|
|
* format: date-time
|
|
* deleted_at:
|
|
* type: string
|
|
* description: "The date with timezone at which the resource was deleted."
|
|
* format: date-time
|
|
* metadata:
|
|
* type: object
|
|
* description: An optional key-value map with additional details
|
|
* example: {car: "white"}
|
|
*/
|