0
0
Fork 0

refactor: refs #6818 check prefix

This commit is contained in:
Jorge Penadés 2024-11-27 12:54:11 +01:00
parent bfb0915da0
commit 25b8535f19
4 changed files with 37 additions and 25 deletions

View File

@ -1,10 +1,10 @@
<script setup> <script setup>
import { reactive, useAttrs, onBeforeMount, capitalize } from 'vue'; import { 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 },
}); });
const config = reactive({ const config = reactive({
@ -25,11 +25,19 @@ onBeforeMount(async () => {
.data; .data;
if (!channel) channel = defaultChannel; if (!channel) channel = defaultChannel;
config[type].href = `${url}?customerIdentity=%2B${parsePhone( const phone = await parsePhone(props.phoneNumber, props.country.toLowerCase());
props.phoneNumber config[type].href = `${url}?customerIdentity=%2B${phone}&channelId=${channel}`;
)}&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

View File

@ -1,12 +1,10 @@
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.startsWith('+')) return `${phone.slice(1)}`;
if (phone.startsWith('00')) { if (phone.startsWith('00')) return `${phone.slice(2)}`;
return `${phone.slice(2)}`;
} const prefix = (await axios.get(`Prefixes/${country.toLowerCase()}`)).data?.prefix;
if (phone.startsWith(prefix) && phone.length === prefix.length + 9) { if (phone.startsWith(prefix)) return phone;
return `${prefix}${phone.slice(prefix.length)}`;
}
return `${prefix}${phone}`; return `${prefix}${phone}`;
} }

View File

@ -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>

View File

@ -1,29 +1,34 @@
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' } });
});
it("adds prefix +34 if it doesn't have one", async () => {
const resultado = await parsePhone('123456789', '34');
expect(resultado).toBe('34123456789'); expect(resultado).toBe('34123456789');
}); });
it('maintains prefix +34 if it is already correct', () => { it('maintains prefix +34 if it is already correct', async () => {
const resultado = parsePhone('+34123456789', '34'); const resultado = await parsePhone('+34123456789', '34');
expect(resultado).toBe('34123456789'); expect(resultado).toBe('34123456789');
}); });
it('converts prefix 0034 to +34', () => { it('converts prefix 0034 to +34', async () => {
const resultado = parsePhone('0034123456789', '34'); const resultado = await parsePhone('0034123456789', '34');
expect(resultado).toBe('34123456789'); expect(resultado).toBe('34123456789');
}); });
it('converts prefix 34 without symbol to +34', () => { it('converts prefix 34 without symbol to +34', async () => {
const resultado = parsePhone('34123456789', '34'); const resultado = await parsePhone('34123456789', '34');
expect(resultado).toBe('34123456789'); expect(resultado).toBe('34123456789');
}); });
it('replaces incorrect prefix with the correct one', () => { it('replaces incorrect prefix with the correct one', async () => {
const resultado = parsePhone('+44123456789', '34'); const resultado = await parsePhone('+44123456789', '34');
expect(resultado).toBe('44123456789'); expect(resultado).toBe('44123456789');
}); });
}); });