forked from verdnatura/salix-front
fix: refs #6818 get default prefix on err
This commit is contained in:
parent
fcee9a4351
commit
07b2be14ae
|
@ -4,13 +4,15 @@ export default async function parsePhone(phone, country) {
|
||||||
if (!phone) return;
|
if (!phone) return;
|
||||||
if (phone.startsWith('+')) return `${phone.slice(1)}`;
|
if (phone.startsWith('+')) return `${phone.slice(1)}`;
|
||||||
if (phone.startsWith('00')) return `${phone.slice(2)}`;
|
if (phone.startsWith('00')) return `${phone.slice(2)}`;
|
||||||
|
|
||||||
|
let prefix;
|
||||||
try {
|
try {
|
||||||
const prefix = (
|
prefix = (await axios.get(`Prefixes/${country.toLowerCase()}`)).data?.prefix;
|
||||||
await axios.get(`Prefixes/${country.toLowerCase()}`)
|
|
||||||
).data?.prefix.replace(/^0+/, '');
|
|
||||||
if (phone.startsWith(prefix)) return phone;
|
|
||||||
return `${prefix}${phone}`;
|
|
||||||
} catch (e) {
|
} 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}`;
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,4 +36,15 @@ describe('parsePhone filter', () => {
|
||||||
const phone = await parsePhone('+44123456789', '34');
|
const phone = await parsePhone('+44123456789', '34');
|
||||||
expect(phone).toBe('44123456789');
|
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');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue