normalize phone

This commit is contained in:
2025-12-06 22:42:26 +03:30
parent 3978065400
commit 01631c147a
9 changed files with 100 additions and 27 deletions
+10 -1
View File
@@ -2,6 +2,7 @@ import { Entity, Property, OneToMany, Collection, Cascade } from '@mikro-orm/cor
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 {
@@ -14,8 +15,16 @@ export class User extends BaseEntity {
@Property({ nullable: true })
lastName?: string;
private _phone!: string;
@Property({ unique: true })
phone!: string;
get phone(): string {
return this._phone;
}
set phone(value: string) {
this._phone = normalizePhone(value);
}
@Property({ default: null, type: 'date' })
birthDate!: Date;
+14 -6
View File
@@ -9,6 +9,7 @@ import { CreateUserAddressDto } from './dto/create-user-address.dto';
import { UpdateUserAddressDto } from './dto/update-user-address.dto';
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
import { UserRepository } from './repositories/user.repository';
import { normalizePhone } from '../utils/phone.util';
@Injectable()
export class UserService {
@@ -18,13 +19,14 @@ export class UserService {
) {}
async findOrCreateByPhone(phone: string): Promise<User> {
let user = await this.userRepository.findOne({ phone });
const normalizedPhone = normalizePhone(phone);
let user = await this.userRepository.findOne({ phone: normalizedPhone });
if (!user) {
// firstName is required on the entity; use phone as a sensible default
const _raw = {
phone,
firstName: phone,
phone: normalizedPhone,
firstName: normalizedPhone,
// keep dates undefined (no value) — cast through unknown to satisfy TS typing
birthDate: undefined,
marriageDate: undefined,
@@ -54,7 +56,8 @@ export class UserService {
// }
async findByPhone(phone: string): Promise<User | null> {
return this.userRepository.findOne({ phone });
const normalizedPhone = normalizePhone(phone);
return this.userRepository.findOne({ phone: normalizedPhone });
}
async findById(id: string): Promise<User | null> {
@@ -62,9 +65,10 @@ export class UserService {
}
async create(phone: string): Promise<User> {
const normalizedPhone = normalizePhone(phone);
const _raw = {
phone,
firstName: phone,
phone: normalizedPhone,
firstName: normalizedPhone,
birthDate: undefined,
marriageDate: undefined,
wallet: 0,
@@ -117,11 +121,15 @@ export class UserService {
// 4. Add 'search' logic (case-insensitive)
if (search) {
const searchPattern = `%${search}%`;
const normalizedSearch = normalizePhone(search);
const normalizedSearchPattern = `%${normalizedSearch}%`;
// $ilike is case-insensitive (PostgreSQL). Use $like for MySQL (often CI by default)
// Search with both original and normalized phone patterns to handle various input formats
where.$or = [
{ firstName: { $ilike: searchPattern } },
{ lastName: { $ilike: searchPattern } },
{ phone: { $ilike: searchPattern } },
...(normalizedSearch !== search ? [{ phone: { $ilike: normalizedSearchPattern } }] : []),
];
}