Skip to content

Commit 2aab286

Browse files
committed
feat(portaswitch): add endpoint to retrieve user contact by ID
1 parent 76fed21 commit 2aab286

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

app/bss/adapters/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ def retrieve_contacts(self, session: SessionInfo, user: UserInfo) -> List[Contac
246246
"""List of other extensions in the PBX"""
247247
raise NotImplementedError("Override this method in your sub-class")
248248

249+
@abstractmethod
250+
def retrieve_contact_by_user_id(self, session: SessionInfo, user: UserInfo, user_id: str) -> ContactInfo:
251+
"""Retrieve extension by User ID in the PBX"""
252+
raise NotImplementedError("Override this method in your sub-class")
253+
249254
@abstractmethod
250255
def retrieve_calls(
251256
self,

app/bss/adapters/portaswitch/adapter.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,14 @@ def retrieve_contacts(self, session: SessionInfo, user: UserInfo) -> list[Contac
540540

541541
raise error
542542

543+
def retrieve_contact_by_user_id(self, session: SessionInfo, user: UserInfo, user_id: str) -> ContactInfo:
544+
"""Retrieve extension by User ID in the PBX"""
545+
account_info = self._admin_api.get_account_info(i_account=int(user_id)).get("account_info")
546+
if not account_info:
547+
raise WebTritErrorException(404, f"There is no an account with such id: {user_id}")
548+
549+
return Serializer.get_contact_info_by_account(account_info, int(user.user_id))
550+
543551
def retrieve_calls(
544552
self,
545553
session: SessionInfo,

app/bss/adapters/portaswitch/api/admin.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,13 @@ def get_account_info(self, **params) -> dict:
146146
dict: The API method execution result that contains an account info.
147147
"""
148148
return self._send_request(
149-
module="Account", method="get_account_info", params={"without_service_features": 1, **params}
149+
module="Account",
150+
method="get_account_info",
151+
params={
152+
"without_service_features": 1,
153+
"detailed_info": 1,
154+
**params
155+
}
150156
)
151157

152158
def get_env_info(self) -> dict:

app/main.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import logging
43
import os
54
import sys
65
from datetime import datetime
@@ -59,6 +58,7 @@
5958
UpdateSessionNotFoundErrorResponse,
6059
UpdateSessionUnprocessableEntityErrorResponse,
6160
Contacts,
61+
ContactInfo,
6262
# signup
6363
UserCreateRequest,
6464
UserCreateResponse,
@@ -193,7 +193,7 @@ def create_session(
193193

194194
if eval_as_bool(config.get_conf_val("Capabilities",
195195
"Password", "Force",
196-
default="False")):
196+
default="False")):
197197
# forcefully allow this method to be called even if the
198198
# adapter declares it as not implemented. This is useful
199199
# in case when app UI should not display this type of login
@@ -205,7 +205,7 @@ def create_session(
205205
is_method_allowed(Capabilities.passwordSignin)
206206

207207
user_ref = safely_extract_scalar_value(body.user_ref) or \
208-
safely_extract_scalar_value(body.login)
208+
safely_extract_scalar_value(body.login)
209209
if not (user_ref and body.password):
210210
# missing parameters
211211
raise_webtrit_error(422, "Missing username & password")
@@ -579,6 +579,49 @@ def get_user_contact_list(
579579
return Contacts(items=[], )
580580

581581

582+
@router.get(
583+
'/user/contacts/{user_id}',
584+
response_model=ContactInfo,
585+
responses={
586+
'401': {'model': GetUserContactListUnauthorizedErrorResponse},
587+
'404': {'model': GetUserContactListNotFoundErrorResponse},
588+
'422': {'model': GetUserContactListUnprocessableEntityErrorResponse},
589+
'500': {'model': GetUserContactListInternalServerErrorErrorResponse},
590+
},
591+
tags=['user'],
592+
)
593+
def get_user_contact_by_id(
594+
user_id: str,
595+
auth_data: HTTPAuthorizationCredentials = Depends(security),
596+
x_webtrit_tenant_id: Optional[str] = Header(None, alias=TENANT_ID_HTTP_HEADER),
597+
) -> (
598+
Union[
599+
ContactInfo,
600+
GetUserContactListUnauthorizedErrorResponse,
601+
GetUserContactListNotFoundErrorResponse,
602+
GetUserContactListUnprocessableEntityErrorResponse,
603+
GetUserContactListInternalServerErrorErrorResponse,
604+
]
605+
):
606+
"""
607+
Get contact information by user ID
608+
"""
609+
global bss
610+
611+
access_token = auth_data.credentials
612+
session = bss.validate_session(access_token)
613+
614+
contact = bss.retrieve_contact_by_user_id(
615+
session,
616+
ExtendedUserInfo(
617+
user_id=safely_extract_scalar_value(session.user_id),
618+
tenant_id=bss.default_id_if_none(x_webtrit_tenant_id)
619+
),
620+
user_id
621+
)
622+
return contact
623+
624+
582625
@router.get(
583626
'/user/history',
584627
response_model=Calls,
@@ -949,7 +992,7 @@ def create_user_event(
949992
) -> Union[
950993
Response,
951994
CreateUserEventUnauthorizedErrorResponse,
952-
CreateUserEventUnprocessableEntityErrorResponse,
995+
CreateUserEventUnprocessableEntityErrorResponse,
953996
CreateUserEventInternalServerErrorErrorResponse,
954997
]:
955998
"""

0 commit comments

Comments
 (0)