From 145610cd4216d4ba805deb5dc849cd900443aa87 Mon Sep 17 00:00:00 2001 From: Dragomir Todorov Date: Fri, 11 Jan 2019 13:41:05 +0200 Subject: [PATCH] NY-6609: Add countrySelector to ContactInfo --- .../biz/nynja/account/models/ContactInfo.java | 16 +++++++++++++- .../account/phone/PhoneNumberNormalizer.java | 21 +++++++++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/main/java/biz/nynja/account/models/ContactInfo.java b/src/main/java/biz/nynja/account/models/ContactInfo.java index 9fffa48..6acc182 100644 --- a/src/main/java/biz/nynja/account/models/ContactInfo.java +++ b/src/main/java/biz/nynja/account/models/ContactInfo.java @@ -8,6 +8,7 @@ import org.springframework.data.cassandra.core.mapping.UserDefinedType; import biz.nynja.account.grpc.ContactDetails; import biz.nynja.account.grpc.ContactDetails.Builder; import biz.nynja.account.grpc.ContactType; +import biz.nynja.account.phone.PhoneNumberUtils; @UserDefinedType public class ContactInfo { @@ -100,7 +101,20 @@ public class ContactInfo { builder.setType(contactType); } if (getValue() != null) { - builder.setValue(getValue()); + if(ContactType.PHONE_CONTACT.equals(contactType)) { + if(getValue().split(":").length == 2) { + // Here we consider the stored phone number is stored in format BG:359898123456 (new way) + String[] phoneParts = getValue().split(":"); + builder.setCountrySelector(phoneParts[0]); + builder.setValue(phoneParts[1]); + } else { + // Regular phone number stored in just 359898123456 + builder.setValue(getValue()); + builder.setCountrySelector(PhoneNumberUtils.getCountrySelector(getValue())); // TODO remove this after db clean up + } + } else { + builder.setValue(getValue()); + } } if (getLabel() != null) { builder.setLabel(getLabel()); diff --git a/src/main/java/biz/nynja/account/phone/PhoneNumberNormalizer.java b/src/main/java/biz/nynja/account/phone/PhoneNumberNormalizer.java index 7ffc912..d5834b4 100644 --- a/src/main/java/biz/nynja/account/phone/PhoneNumberNormalizer.java +++ b/src/main/java/biz/nynja/account/phone/PhoneNumberNormalizer.java @@ -28,7 +28,7 @@ public class PhoneNumberNormalizer { public AddContactInfoRequest normalizePhoneNumber(AddContactInfoRequest request) { // Get the normalized phone number from libphone ContactDetails newContactDetails = ContactDetails.newBuilder().setType(request.getContactInfo().getType()) - .setValue(getNormalizedPhoneNumber(request.getContactInfo().getValue())) + .setValue(getNormalizedPhoneNumberContactInfo(request.getContactInfo().getValue())) .setLabel(request.getContactInfo().getLabel()).build(); AddContactInfoRequest newRequest = AddContactInfoRequest.newBuilder().setAccountId(request.getAccountId()) .setContactInfo(newContactDetails).build(); @@ -39,7 +39,7 @@ public class PhoneNumberNormalizer { public DeleteContactInfoRequest normalizePhoneNumber(DeleteContactInfoRequest request) { // Get the normalized phone number from libphone ContactDetails newContactDetails = ContactDetails.newBuilder().setType(request.getContactInfo().getType()) - .setValue(getNormalizedPhoneNumber(request.getContactInfo().getValue())) + .setValue(getNormalizedPhoneNumberContactInfo(request.getContactInfo().getValue())) .setLabel(request.getContactInfo().getLabel()).build(); DeleteContactInfoRequest newRequest = DeleteContactInfoRequest.newBuilder().setAccountId(request.getAccountId()) .setContactInfo(newContactDetails).build(); @@ -51,12 +51,12 @@ public class PhoneNumberNormalizer { // Get the normalized phone number from libphone for the old number ContactDetails contactDetailsOldNumber = ContactDetails.newBuilder() .setType(request.getOldContactInfo().getType()) - .setValue(getNormalizedPhoneNumber(request.getOldContactInfo().getValue())) + .setValue(getNormalizedPhoneNumberContactInfo(request.getOldContactInfo().getValue())) .setLabel(request.getOldContactInfo().getLabel()).build(); // Get the normalized phone number from libphone for the edited number ContactDetails contactDetailsEditedNumber = ContactDetails.newBuilder() .setType(request.getEditedContactInfo().getType()) - .setValue(getNormalizedPhoneNumber(request.getEditedContactInfo().getValue())) + .setValue(getNormalizedPhoneNumberContactInfo(request.getEditedContactInfo().getValue())) .setLabel(request.getEditedContactInfo().getLabel()).build(); EditContactInfoRequest newRequest = EditContactInfoRequest.newBuilder().setAccountId(request.getAccountId()) .setOldContactInfo(contactDetailsOldNumber).setEditedContactInfo(contactDetailsEditedNumber).build(); @@ -134,4 +134,17 @@ public class PhoneNumberNormalizer { } return normalizedPhoneNumber; } + + public String getNormalizedPhoneNumberContactInfo(String rawPhoneNumber) throws InvalidPhoneNumberException { + String normalizedPhoneNumber = getNormalizedPhoneNumber(rawPhoneNumber); + + String country; + String[] provider = rawPhoneNumber.split(":"); + if (provider.length == 1) { + country = PhoneNumberUtils.getCountrySelector(provider[0]); + } else { + country = provider[0]; + } + return country + ":" + normalizedPhoneNumber; + } } -- GitLab