0
0
Fork 0

fix: refs #6818 refactor phone parsing and improve error handling

This commit is contained in:
Jorge Penadés 2024-11-28 10:25:50 +01:00
parent ad45d612ee
commit d4028da92e
2 changed files with 18 additions and 17 deletions

View File

@ -1,12 +1,15 @@
<script setup> <script setup>
import { reactive, useAttrs, onBeforeMount, capitalize } from 'vue'; import { ref, reactive, useAttrs, onBeforeMount, capitalize } from 'vue';
import axios from 'axios'; import axios from 'axios';
import { parsePhone } from 'src/filters';
const props = defineProps({ const props = defineProps({
phoneNumber: { type: [String, Number], default: null }, phoneNumber: { type: [String, Number], default: null },
channel: { type: Number, default: null }, channel: { type: Number, default: null },
country: { type: String, default: null }, country: { type: String, default: null },
}); });
const phone = ref(props.phoneNumber);
const config = reactive({ const config = reactive({
sip: { icon: 'phone', href: `sip:${props.phoneNumber}` }, sip: { icon: 'phone', href: `sip:${props.phoneNumber}` },
'say-simple': { 'say-simple': {
@ -25,23 +28,16 @@ onBeforeMount(async () => {
.data; .data;
if (!channel) channel = defaultChannel; if (!channel) channel = defaultChannel;
const phone = await parsePhone(props.phoneNumber, props.country.toLowerCase()); phone.value = await parsePhone(props.phoneNumber, props.country.toLowerCase());
config[type].href = `${url}?customerIdentity=%2B${phone}&channelId=${channel}`; config[
type
].href = `${url}?customerIdentity=%2B${phone.value}&channelId=${channel}`;
} }
}); });
async function parsePhone(phone, country) {
if (phone.startsWith('+')) return `${phone.slice(1)}`;
if (phone.startsWith('00')) return `${phone.slice(2)}`;
const prefix = (await axios.get(`Prefixes/${country.toLowerCase()}`)).data?.prefix;
if (phone.startsWith(prefix)) return phone;
return `${prefix}${phone}`;
}
</script> </script>
<template> <template>
<QBtn <QBtn
v-if="phoneNumber" v-if="phone"
flat flat
round round
:icon="config[type].icon" :icon="config[type].icon"

View File

@ -3,8 +3,13 @@ import axios from 'axios';
export default async function parsePhone(phone, country) { export default async function parsePhone(phone, country) {
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)}`;
try {
const prefix = (await axios.get(`Prefixes/${country.toLowerCase()}`)).data?.prefix; const prefix = (
await axios.get(`Prefixes/${country.toLowerCase()}`)
).data?.prefix.replace(/^0+/, '');
if (phone.startsWith(prefix)) return phone; if (phone.startsWith(prefix)) return phone;
return `${prefix}${phone}`; return `${prefix}${phone}`;
} catch (e) {
return null;
}
} }