fix(customer): Unique constraint on customer email (#7439)

**What**
Prevent creating multiple customers with the same email
This commit is contained in:
Adrien de Peretti
2024-05-24 14:20:54 +00:00
committed by GitHub
parent 066fd3c3d2
commit 77d72c5791
11 changed files with 186 additions and 35 deletions
@@ -1,5 +1,10 @@
import { DAL } from "@medusajs/types"
import { DALUtils, Searchable, generateEntityId } from "@medusajs/utils"
import {
createPsqlIndexStatementHelper,
DALUtils,
generateEntityId,
Searchable,
} from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
@@ -7,8 +12,8 @@ import {
Entity,
Filter,
ManyToMany,
OnInit,
OneToMany,
OnInit,
OptionalProps,
PrimaryKey,
Property,
@@ -22,8 +27,16 @@ type OptionalCustomerProps =
| "addresses"
| DAL.SoftDeletableEntityDateColumns
const CustomerUniqueEmail = createPsqlIndexStatementHelper({
tableName: "customer",
columns: ["email", "has_account"],
unique: true,
where: "deleted_at IS NULL",
})
@Entity({ tableName: "customer" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
@CustomerUniqueEmail.MikroORMIndex()
export default class Customer {
[OptionalProps]?: OptionalCustomerProps