56 lines
1.3 KiB
TypeScript
56 lines
1.3 KiB
TypeScript
import { Entity, Property, OneToMany, Collection, Cascade } from '@mikro-orm/core';
|
|
import { BaseEntity } from '../../../common/entities/base.entity';
|
|
import { UserAddress } from './user-address.entity';
|
|
import { Order } from 'src/modules/orders/entities/order.entity';
|
|
import { normalizePhone } from '../../utils/phone.util';
|
|
|
|
@Entity({ tableName: 'users' })
|
|
export class User extends BaseEntity {
|
|
@OneToMany(() => Order, order => order.user)
|
|
orders = new Collection<Order>(this);
|
|
|
|
@Property()
|
|
firstName!: string;
|
|
|
|
@Property({ nullable: true })
|
|
lastName?: string;
|
|
|
|
private _phone!: string;
|
|
|
|
@Property({ unique: true })
|
|
get phone(): string {
|
|
return this._phone;
|
|
}
|
|
|
|
set phone(value: string) {
|
|
this._phone = normalizePhone(value);
|
|
}
|
|
|
|
@Property({ default: null, type: 'date' })
|
|
birthDate!: Date;
|
|
|
|
@Property({ default: null, type: 'date' })
|
|
marriageDate!: Date;
|
|
|
|
@Property({ nullable: true })
|
|
referrer?: string;
|
|
|
|
@Property({ default: true })
|
|
isActive?: boolean = true;
|
|
|
|
@Property({ default: true })
|
|
gender?: boolean;
|
|
|
|
@Property({ default: 0, type: 'int' })
|
|
wallet: number = 0;
|
|
|
|
@Property({ default: 0, type: 'int' })
|
|
points: number = 0;
|
|
|
|
@OneToMany(() => UserAddress, address => address.user, {
|
|
cascade: [Cascade.ALL],
|
|
orphanRemoval: true,
|
|
})
|
|
addresses = new Collection<UserAddress>(this);
|
|
}
|