fix: refs #6818 get default prefix on err
gitea/salix-front/pipeline/pr-master This commit looks good Details

This commit is contained in:
Jorge Penadés 2024-12-02 14:39:55 +01:00
parent fcee9a4351
commit 07b2be14ae
2 changed files with 19 additions and 6 deletions

View File

@ -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+/, '');
prefix = (await axios.get(`Prefixes/${country.toLowerCase()}`)).data?.prefix;
} catch (e) {
prefix = (await axios.get('PbxConfigs/findOne')).data?.defaultPrefix;
}
prefix = prefix.replace(/^0+/, '');
if (phone.startsWith(prefix)) return phone;
return `${prefix}${phone}`;
} catch (e) {
return null;
}
}

View File

@ -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');
});
});