fix: refs #6818 refactor phone parsing and improve error handling
gitea/salix-front/pipeline/pr-master This commit looks good Details

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

View File

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