refactor: refs #6818 check prefix
gitea/salix-front/pipeline/pr-master This commit looks good Details

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

View File

@ -1,12 +1,10 @@
export default function (phone, prefix = 34) {
if (phone.startsWith('+')) {
return `${phone.slice(1)}`;
}
if (phone.startsWith('00')) {
return `${phone.slice(2)}`;
}
if (phone.startsWith(prefix) && phone.length === prefix.length + 9) {
return `${prefix}${phone.slice(prefix.length)}`;
}
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;
if (phone.startsWith(prefix)) return phone;
return `${prefix}${phone}`;
}

View File

@ -95,6 +95,7 @@ const sumRisk = ({ clientRisks }) => {
:phone-number="entity.mobile"
:channel="entity.country?.saySimpleCountry?.channel"
class="q-ml-xs"
:country="entity.country?.code"
/>
</template>
</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';
describe('parsePhone filter', () => {
it("adds prefix +34 if it doesn't have one", () => {
const resultado = parsePhone('123456789', '34');
beforeAll(async () => {
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');
});
it('maintains prefix +34 if it is already correct', () => {
const resultado = parsePhone('+34123456789', '34');
it('maintains prefix +34 if it is already correct', async () => {
const resultado = await parsePhone('+34123456789', '34');
expect(resultado).toBe('34123456789');
});
it('converts prefix 0034 to +34', () => {
const resultado = parsePhone('0034123456789', '34');
it('converts prefix 0034 to +34', async () => {
const resultado = await parsePhone('0034123456789', '34');
expect(resultado).toBe('34123456789');
});
it('converts prefix 34 without symbol to +34', () => {
const resultado = parsePhone('34123456789', '34');
it('converts prefix 34 without symbol to +34', async () => {
const resultado = await parsePhone('34123456789', '34');
expect(resultado).toBe('34123456789');
});
it('replaces incorrect prefix with the correct one', () => {
const resultado = parsePhone('+44123456789', '34');
it('replaces incorrect prefix with the correct one', async () => {
const resultado = await parsePhone('+44123456789', '34');
expect(resultado).toBe('44123456789');
});
});