-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #490 from wazo-platform/WAZO-3942-change-callerids…
…-outgoing-source WAZO 3942 change callerids outgoing source Reviewed-by: rbienvenu0 <[email protected]> Reviewed-by: Charles <[email protected]>
- Loading branch information
Showing
7 changed files
with
111 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,68 @@ | ||
# Copyright 2024 The Wazo Authors (see the AUTHORS file) | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
import phonenumbers | ||
from dataclasses import dataclass | ||
|
||
from xivo_dao.resources.user import dao as user_dao | ||
from xivo_dao.resources.incall import dao as incall_dao | ||
from xivo_dao.resources.phone_number import dao as phone_number_dao | ||
|
||
from .types import CallerIDType | ||
|
||
|
||
class CallerIDAnonymous: | ||
type = 'anonymous' | ||
|
||
|
||
@dataclass() | ||
class CallerID: | ||
type: CallerIDType | ||
number: str | ||
|
||
|
||
def same_phone_number(number1: str, number2: str) -> bool: | ||
''' | ||
compare two strings semantically as phone numbers | ||
''' | ||
result = phonenumbers.is_number_match(number1, number2) | ||
return result in ( | ||
phonenumbers.MatchType.EXACT_MATCH, | ||
phonenumbers.MatchType.NSN_MATCH, | ||
) | ||
|
||
|
||
class UserCallerIDService: | ||
def __init__(self, user_dao, incall_dao): | ||
def __init__(self, user_dao, incall_dao, phone_number_dao): | ||
self.user_dao = user_dao | ||
self.incall_dao = incall_dao | ||
self.phone_number_dao = phone_number_dao | ||
|
||
def search(self, user_id, tenant_uuid, parameters): | ||
callerids = [] | ||
if main_callerid := self.incall_dao.find_main_callerid(tenant_uuid): | ||
callerids.append(main_callerid) | ||
callerids.extend(self.user_dao.list_outgoing_callerid_associated(user_id)) | ||
if main_callerid := self.phone_number_dao.find_by( | ||
main=True, tenant_uuids=[tenant_uuid] | ||
): | ||
callerids.append(CallerID(type='main', number=main_callerid.number)) | ||
|
||
# consider "associated" caller ids from incalls | ||
# as having precedence over shared phone numbers | ||
callerids.extend( | ||
callerid | ||
for callerid in self.user_dao.list_outgoing_callerid_associated(user_id) | ||
if not any(same_phone_number(callerid.number, c.number) for c in callerids) | ||
) | ||
shared_callerids = self.phone_number_dao.find_all_by( | ||
shared=True, main=False, tenant_uuids=[tenant_uuid] | ||
) | ||
callerids.extend( | ||
CallerID(type='shared', number=callerid.number) | ||
for callerid in shared_callerids | ||
if not any(same_phone_number(callerid.number, c.number) for c in callerids) | ||
) | ||
callerids.append(CallerIDAnonymous) | ||
return len(callerids), callerids | ||
|
||
|
||
def build_service(): | ||
return UserCallerIDService(user_dao, incall_dao) | ||
return UserCallerIDService(user_dao, incall_dao, phone_number_dao) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import unittest | ||
|
||
from ..service import same_phone_number | ||
|
||
|
||
class TestSamePhoneNumber(unittest.TestCase): | ||
def test_exact(self): | ||
numbers = ['1234567890', '11234567890', '+11234567890' '4567890', '911'] | ||
for number in numbers: | ||
self.assertTrue(same_phone_number(number, number), number) | ||
|
||
def test_actually_different(self): | ||
number1 = '11234567890' | ||
number2 = '11234567891' | ||
self.assertFalse(same_phone_number(number1, number2), (number1, number2)) | ||
|
||
def test_different_country(self): | ||
number1 = '11234567890' | ||
number2 = '21234567890' | ||
self.assertFalse(same_phone_number(number1, number2), (number1, number2)) | ||
|
||
def test_different_country_one_e164(self): | ||
number1 = '+11234567890' | ||
number2 = '21234567890' | ||
self.assertFalse(same_phone_number(number1, number2), (number1, number2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from typing import Literal | ||
|
||
CallerIDType = Literal['main', 'associated', 'anonymous', 'shared'] |