Merge pull request 'fix: refs #6818 get default prefix on err' (!1034) from 6818-hotfix-getDefaultPrefix into master
gitea/salix-front/pipeline/head This commit looks good Details

Reviewed-on: #1034
Reviewed-by: Javi Gallego <jgallego@verdnatura.es>
This commit is contained in:
Jorge Penadés 2024-12-02 15:08:04 +00:00
commit 4d6ee25aeb
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+/, '');
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}`;
}

View File

@ -36,4 +36,15 @@ describe('parsePhone filter', () => {
const phone = await parsePhone('+44123456789', '34');
expect(phone).toBe('44123456789');
});
it('adds default prefix on error', 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');
});
});