CASH-2934: Normalize phone number

This commit is contained in:
credelinghuys 2026-07-08 13:51:33 -04:00
parent 4864784828
commit cf621394ad
3 changed files with 27 additions and 3 deletions

View file

@ -210,7 +210,7 @@ describe("save-progress-popup-question ", () => {
expect(dispatchStoreAction).toHaveBeenCalledWith(
storeActions.SAVE_PHONE_NUMBER,
"555-123-4567",
"5551234567",
false
);
expect(dispatchStoreAction).not.toHaveBeenCalledWith(

View file

@ -10,12 +10,24 @@ export function isPhoneContactMethod(contactMethod) {
return contactMethod === saveProgressPopupContactMethods.PHONE;
}
export function normalizePhoneNumberForStore(phoneNumber) {
if (!phoneNumber) {
return phoneNumber;
}
return String(phoneNumber).replace(/\D/g, "");
}
export async function saveProgressPopupContactToStore(
dispatchStoreAction,
{ contactMethod, userInput, smsConsent, pageName }
) {
if (isPhoneContactMethod(contactMethod)) {
await dispatchStoreAction(storeActions.SAVE_PHONE_NUMBER, userInput, false);
await dispatchStoreAction(
storeActions.SAVE_PHONE_NUMBER,
normalizePhoneNumberForStore(userInput),
false
);
await dispatchStoreAction(storeActions.SAVE_IS_SMS_OPT_IN, smsConsent.transactional, false);
const existingPageData = store.getters.pageData(pageName) ?? {};

View file

@ -2,6 +2,7 @@ import { storeActions } from "@/constants/store-actions";
import store from "@/store";
import {
isPhoneContactMethod,
normalizePhoneNumberForStore,
saveProgressPopupContactMethods,
saveProgressPopupContactToStore,
} from "./save-progress-popup-contact-helper";
@ -28,6 +29,17 @@ describe("save-progress-popup-contact-helper", () => {
});
});
describe("normalizePhoneNumberForStore", () => {
it("should strip non-digit characters from a formatted phone number", () => {
expect(normalizePhoneNumberForStore("555-123-4567")).toBe("5551234567");
});
it("should return empty values unchanged", () => {
expect(normalizePhoneNumberForStore("")).toBe("");
expect(normalizePhoneNumberForStore(null)).toBe(null);
});
});
describe("saveProgressPopupContactToStore", () => {
it("should save phone number and persist sms consent on phone tab", async () => {
const dispatchStoreAction = jest.fn().mockResolvedValue(undefined);
@ -42,7 +54,7 @@ describe("save-progress-popup-contact-helper", () => {
expect(dispatchStoreAction).toHaveBeenCalledWith(
storeActions.SAVE_PHONE_NUMBER,
"555-123-4567",
"5551234567",
false
);
expect(dispatchStoreAction).not.toHaveBeenCalledWith(