Fix phone formatting
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.petshop.backend.entity;
|
||||
|
||||
import com.petshop.backend.util.PhoneUtils;
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
import org.hibernate.annotations.UpdateTimestamp;
|
||||
@@ -77,7 +78,7 @@ public class StoreLocation {
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
this.phone = PhoneUtils.normalize(phone);
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.petshop.backend.entity;
|
||||
|
||||
import com.petshop.backend.util.PhoneUtils;
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
import org.hibernate.annotations.UpdateTimestamp;
|
||||
@@ -97,7 +98,7 @@ public class Supplier {
|
||||
}
|
||||
|
||||
public void setSupPhone(String supPhone) {
|
||||
this.supPhone = supPhone;
|
||||
this.supPhone = PhoneUtils.normalize(supPhone);
|
||||
}
|
||||
|
||||
public LocalDateTime getCreatedAt() {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.petshop.backend.entity;
|
||||
|
||||
import com.petshop.backend.util.PhoneUtils;
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
import org.hibernate.annotations.UpdateTimestamp;
|
||||
@@ -118,7 +119,7 @@ public class User {
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
this.phone = PhoneUtils.normalize(phone);
|
||||
}
|
||||
|
||||
public String getAvatarUrl() {
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.petshop.backend.util;
|
||||
|
||||
public class PhoneUtils {
|
||||
public static String normalize(String phone) {
|
||||
if (phone == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String digits = phone.replaceAll("\\D", "");
|
||||
|
||||
if (digits.length() >= 10) {
|
||||
if (digits.length() == 11 && digits.startsWith("1")) {
|
||||
digits = digits.substring(1);
|
||||
}
|
||||
digits = digits.substring(0, 10);
|
||||
return String.format("(%s) %s-%s",
|
||||
digits.substring(0, 3),
|
||||
digits.substring(3, 6),
|
||||
digits.substring(6));
|
||||
}
|
||||
|
||||
return phone;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user