Merge pull request 'refactor: refs #6818 check prefix' (!1013) from 6818-hotfix-refactorParsePhone into master
gitea/salix-front/pipeline/head This commit looks good
Details
gitea/salix-front/pipeline/head This commit looks good
Details
Reviewed-on: #1013 Reviewed-by: Javi Gallego <jgallego@verdnatura.es>
This commit is contained in:
commit
1940ea638a
|
@ -1,23 +1,28 @@
|
||||||
<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';
|
import { parsePhone } from 'src/filters';
|
||||||
|
import useOpenURL from 'src/composables/useOpenURL';
|
||||||
|
|
||||||
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 },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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': {
|
||||||
icon: 'vn:saysimple',
|
icon: 'vn:saysimple',
|
||||||
href: null,
|
url: null,
|
||||||
channel: props.channel,
|
channel: props.channel,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const type = Object.keys(config).find((key) => key in useAttrs()) || 'sip';
|
const type = Object.keys(config).find((key) => key in useAttrs()) || 'sip';
|
||||||
|
|
||||||
onBeforeMount(async () => {
|
onBeforeMount(async () => {
|
||||||
|
if (!phone.value) return;
|
||||||
let { channel } = config[type];
|
let { channel } = config[type];
|
||||||
|
|
||||||
if (type === 'say-simple') {
|
if (type === 'say-simple') {
|
||||||
|
@ -25,23 +30,28 @@ onBeforeMount(async () => {
|
||||||
.data;
|
.data;
|
||||||
if (!channel) channel = defaultChannel;
|
if (!channel) channel = defaultChannel;
|
||||||
|
|
||||||
config[type].href = `${url}?customerIdentity=%2B${parsePhone(
|
phone.value = await parsePhone(props.phoneNumber, props.country.toLowerCase());
|
||||||
props.phoneNumber
|
config[
|
||||||
)}&channelId=${channel}`;
|
type
|
||||||
|
].url = `${url}?customerIdentity=%2B${phone.value}&channelId=${channel}`;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function handleClick() {
|
||||||
|
if (config[type].url) useOpenURL(config[type].url);
|
||||||
|
else if (config[type].href) window.location.href = config[type].href;
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
<template>
|
<template>
|
||||||
<QBtn
|
<QBtn
|
||||||
v-if="phoneNumber"
|
v-if="phone"
|
||||||
flat
|
flat
|
||||||
round
|
round
|
||||||
:icon="config[type].icon"
|
:icon="config[type].icon"
|
||||||
size="sm"
|
size="sm"
|
||||||
color="primary"
|
color="primary"
|
||||||
padding="none"
|
padding="none"
|
||||||
:href="config[type].href"
|
@click.stop="handleClick"
|
||||||
@click.stop
|
|
||||||
>
|
>
|
||||||
<QTooltip>
|
<QTooltip>
|
||||||
{{ capitalize(type).replace('-', '') }}
|
{{ capitalize(type).replace('-', '') }}
|
||||||
|
|
|
@ -1,12 +1,16 @@
|
||||||
export default function (phone, prefix = 34) {
|
import axios from 'axios';
|
||||||
if (phone.startsWith('+')) {
|
|
||||||
return `${phone.slice(1)}`;
|
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)}`;
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
if (phone.startsWith('00')) {
|
|
||||||
return `${phone.slice(2)}`;
|
|
||||||
}
|
|
||||||
if (phone.startsWith(prefix) && phone.length === prefix.length + 9) {
|
|
||||||
return `${prefix}${phone.slice(prefix.length)}`;
|
|
||||||
}
|
|
||||||
return `${prefix}${phone}`;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -95,6 +95,7 @@ const sumRisk = ({ clientRisks }) => {
|
||||||
:phone-number="entity.mobile"
|
:phone-number="entity.mobile"
|
||||||
:channel="entity.country?.saySimpleCountry?.channel"
|
:channel="entity.country?.saySimpleCountry?.channel"
|
||||||
class="q-ml-xs"
|
class="q-ml-xs"
|
||||||
|
:country="entity.country?.code"
|
||||||
/>
|
/>
|
||||||
</template>
|
</template>
|
||||||
</VnLv>
|
</VnLv>
|
||||||
|
|
|
@ -1,29 +1,39 @@
|
||||||
import { describe, it, expect } from 'vitest';
|
import { describe, it, expect, beforeAll, vi } from 'vitest';
|
||||||
|
import { axios } from 'app/test/vitest/helper';
|
||||||
import parsePhone from 'src/filters/parsePhone';
|
import parsePhone from 'src/filters/parsePhone';
|
||||||
|
|
||||||
describe('parsePhone filter', () => {
|
describe('parsePhone filter', () => {
|
||||||
it("adds prefix +34 if it doesn't have one", () => {
|
beforeAll(async () => {
|
||||||
const resultado = parsePhone('123456789', '34');
|
vi.spyOn(axios, 'get').mockReturnValue({ data: { prefix: '34' } });
|
||||||
expect(resultado).toBe('34123456789');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('maintains prefix +34 if it is already correct', () => {
|
it('no phone', async () => {
|
||||||
const resultado = parsePhone('+34123456789', '34');
|
const phone = await parsePhone(null, '34');
|
||||||
expect(resultado).toBe('34123456789');
|
expect(phone).toBe(undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('converts prefix 0034 to +34', () => {
|
it("adds prefix +34 if it doesn't have one", async () => {
|
||||||
const resultado = parsePhone('0034123456789', '34');
|
const phone = await parsePhone('123456789', '34');
|
||||||
expect(resultado).toBe('34123456789');
|
expect(phone).toBe('34123456789');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('converts prefix 34 without symbol to +34', () => {
|
it('maintains prefix +34 if it is already correct', async () => {
|
||||||
const resultado = parsePhone('34123456789', '34');
|
const phone = await parsePhone('+34123456789', '34');
|
||||||
expect(resultado).toBe('34123456789');
|
expect(phone).toBe('34123456789');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('replaces incorrect prefix with the correct one', () => {
|
it('converts prefix 0034 to +34', async () => {
|
||||||
const resultado = parsePhone('+44123456789', '34');
|
const phone = await parsePhone('0034123456789', '34');
|
||||||
expect(resultado).toBe('44123456789');
|
expect(phone).toBe('34123456789');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('converts prefix 34 without symbol to +34', async () => {
|
||||||
|
const phone = await parsePhone('34123456789', '34');
|
||||||
|
expect(phone).toBe('34123456789');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('replaces incorrect prefix with the correct one', async () => {
|
||||||
|
const phone = await parsePhone('+44123456789', '34');
|
||||||
|
expect(phone).toBe('44123456789');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue