2024-11-27 11:54:11 +00:00
|
|
|
import axios from 'axios';
|
|
|
|
|
|
|
|
export default async function parsePhone(phone, country) {
|
2024-11-28 09:28:06 +00:00
|
|
|
if (!phone) return;
|
2024-11-27 11:54:11 +00:00
|
|
|
if (phone.startsWith('+')) return `${phone.slice(1)}`;
|
|
|
|
if (phone.startsWith('00')) return `${phone.slice(2)}`;
|
2024-12-02 13:39:55 +00:00
|
|
|
|
|
|
|
let prefix;
|
2024-11-28 09:25:50 +00:00
|
|
|
try {
|
2024-12-02 13:39:55 +00:00
|
|
|
prefix = (await axios.get(`Prefixes/${country.toLowerCase()}`)).data?.prefix;
|
2024-11-28 09:25:50 +00:00
|
|
|
} catch (e) {
|
2024-12-02 13:39:55 +00:00
|
|
|
prefix = (await axios.get('PbxConfigs/findOne')).data?.defaultPrefix;
|
2024-11-28 09:25:50 +00:00
|
|
|
}
|
2024-12-02 13:39:55 +00:00
|
|
|
prefix = prefix.replace(/^0+/, '');
|
|
|
|
|
|
|
|
if (phone.startsWith(prefix)) return phone;
|
|
|
|
return `${prefix}${phone}`;
|
2024-11-26 21:57:47 +00:00
|
|
|
}
|