diff --git a/src/filters/parsePhone.js b/src/filters/parsePhone.js index 27474321f..6cb1bea17 100644 --- a/src/filters/parsePhone.js +++ b/src/filters/parsePhone.js @@ -4,13 +4,15 @@ export default async function parsePhone(phone, country) { if (!phone) return; if (phone.startsWith('+')) return `${phone.slice(1)}`; if (phone.startsWith('00')) return `${phone.slice(2)}`; + + let prefix; try { - const prefix = ( - await axios.get(`Prefixes/${country.toLowerCase()}`) - ).data?.prefix.replace(/^0+/, ''); - if (phone.startsWith(prefix)) return phone; - return `${prefix}${phone}`; + prefix = (await axios.get(`Prefixes/${country.toLowerCase()}`)).data?.prefix; } catch (e) { - return null; + prefix = (await axios.get('PbxConfigs/findOne')).data?.defaultPrefix; } + prefix = prefix.replace(/^0+/, ''); + + if (phone.startsWith(prefix)) return phone; + return `${prefix}${phone}`; } diff --git a/test/vitest/__tests__/components/common/VnLinkPhone.spec.js b/test/vitest/__tests__/components/common/VnLinkPhone.spec.js index 1505e32f7..3e0a532ed 100644 --- a/test/vitest/__tests__/components/common/VnLinkPhone.spec.js +++ b/test/vitest/__tests__/components/common/VnLinkPhone.spec.js @@ -36,4 +36,15 @@ describe('parsePhone filter', () => { const phone = await parsePhone('+44123456789', '34'); expect(phone).toBe('44123456789'); }); + + it('adds default prefix when entering the catch block', async () => { + vi.spyOn(axios, 'get').mockImplementation((url) => { + if (url.includes('Prefixes')) + return Promise.reject(new Error('Network error')); + else if (url.includes('PbxConfigs')) + return Promise.resolve({ data: { defaultPrefix: '39' } }); + }); + const phone = await parsePhone('123456789', '34'); + expect(phone).toBe('39123456789'); + }); });