feature(#134): update API for get contacts by ids in firebase database
Browse files
Brain/logs/activity.log
CHANGED
|
@@ -146,3 +146,5 @@
|
|
| 146 |
2023-07-03 15:34:55,373 WARNING firebase init this app name: firebase_admin_45qrtaer345tret does not exist
|
| 147 |
2023-07-03 16:21:15,252 WARNING firebase init this app name: firebase_admin_45qrtaer345tret does not exist
|
| 148 |
2023-07-03 17:01:09,415 WARNING firebase init this app name: firebase_admin_45qrtaer345tret does not exist
|
|
|
|
|
|
|
|
|
| 146 |
2023-07-03 15:34:55,373 WARNING firebase init this app name: firebase_admin_45qrtaer345tret does not exist
|
| 147 |
2023-07-03 16:21:15,252 WARNING firebase init this app name: firebase_admin_45qrtaer345tret does not exist
|
| 148 |
2023-07-03 17:01:09,415 WARNING firebase init this app name: firebase_admin_45qrtaer345tret does not exist
|
| 149 |
+
2023-07-03 17:45:56,553 WARNING firebase init this app name: firebase_admin_45qrtaer345tret does not exist
|
| 150 |
+
2023-07-04 05:52:58,591 WARNING firebase init this app name: firebase_admin_45qrtaer345tret does not exist
|
Brain/src/model/requests/request_model.py
CHANGED
|
@@ -15,10 +15,10 @@ class ClientInfo:
|
|
| 15 |
|
| 16 |
def is_browser(self) -> bool:
|
| 17 |
if (
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
):
|
| 23 |
return True
|
| 24 |
return False
|
|
@@ -215,3 +215,10 @@ class AutoTaskDelete(BasicReq):
|
|
| 215 |
reference_link: str
|
| 216 |
|
| 217 |
data: Body
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def is_browser(self) -> bool:
|
| 17 |
if (
|
| 18 |
+
self.browser == "Chrome"
|
| 19 |
+
or self.browser == "Firefox"
|
| 20 |
+
or self.browser == "Safari"
|
| 21 |
+
or self.browser == "Edge"
|
| 22 |
):
|
| 23 |
return True
|
| 24 |
return False
|
|
|
|
| 215 |
reference_link: str
|
| 216 |
|
| 217 |
data: Body
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
"""endpoint : /contact/getbyIds"""
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
class GetContactsByIds(BasicReq):
|
| 224 |
+
contactIds: list[str]
|
Brain/src/router/api.py
CHANGED
|
@@ -18,7 +18,7 @@ from Brain.src.model.requests.request_model import (
|
|
| 18 |
BasicReq,
|
| 19 |
ClientInfo,
|
| 20 |
get_client_info,
|
| 21 |
-
AutoTaskDelete,
|
| 22 |
)
|
| 23 |
from Brain.src.rising_plugin.risingplugin import (
|
| 24 |
getCompletion,
|
|
@@ -495,7 +495,10 @@ def construct_blueprint_api() -> APIRouter:
|
|
| 495 |
"""@generator.request_body(
|
| 496 |
{
|
| 497 |
"token": "String",
|
| 498 |
-
"uuid": "String",
|
|
|
|
|
|
|
|
|
|
| 499 |
}
|
| 500 |
)
|
| 501 |
|
|
@@ -505,8 +508,8 @@ def construct_blueprint_api() -> APIRouter:
|
|
| 505 |
|
| 506 |
"""
|
| 507 |
|
| 508 |
-
@router.post("/contacts/
|
| 509 |
-
def
|
| 510 |
try:
|
| 511 |
setting, firebase_app = firebase_admin_with_setting(data)
|
| 512 |
except BrainException as ex:
|
|
@@ -515,7 +518,8 @@ def construct_blueprint_api() -> APIRouter:
|
|
| 515 |
token: str = setting.token
|
| 516 |
uuid: str = setting.uuid
|
| 517 |
|
| 518 |
-
result = ContactsService(firebase_app=firebase_app, setting=setting).
|
|
|
|
| 519 |
|
| 520 |
return assembler.to_response(
|
| 521 |
200, "Success to get contacts by uuid", result
|
|
|
|
| 18 |
BasicReq,
|
| 19 |
ClientInfo,
|
| 20 |
get_client_info,
|
| 21 |
+
AutoTaskDelete, GetContactsByIds,
|
| 22 |
)
|
| 23 |
from Brain.src.rising_plugin.risingplugin import (
|
| 24 |
getCompletion,
|
|
|
|
| 495 |
"""@generator.request_body(
|
| 496 |
{
|
| 497 |
"token": "String",
|
| 498 |
+
"uuid": "String",
|
| 499 |
+
"contactIds": [
|
| 500 |
+
String
|
| 501 |
+
]
|
| 502 |
}
|
| 503 |
)
|
| 504 |
|
|
|
|
| 508 |
|
| 509 |
"""
|
| 510 |
|
| 511 |
+
@router.post("/contacts/getByIds")
|
| 512 |
+
def getContactsByIds(data: GetContactsByIds):
|
| 513 |
try:
|
| 514 |
setting, firebase_app = firebase_admin_with_setting(data)
|
| 515 |
except BrainException as ex:
|
|
|
|
| 518 |
token: str = setting.token
|
| 519 |
uuid: str = setting.uuid
|
| 520 |
|
| 521 |
+
result = ContactsService(firebase_app=firebase_app, setting=setting).getContactsByIds(uuid=uuid,
|
| 522 |
+
contactIds=data.contactIds)
|
| 523 |
|
| 524 |
return assembler.to_response(
|
| 525 |
200, "Success to get contacts by uuid", result
|
Brain/src/service/contact_service.py
CHANGED
|
@@ -103,11 +103,13 @@ class ContactsService:
|
|
| 103 |
return get_pinecone_index_namespace(f"{uuid}-contacts")
|
| 104 |
|
| 105 |
"""init firestore to save user's contacts"""
|
|
|
|
| 106 |
def init_firestore(self):
|
| 107 |
self.db = firestore.client(app=self.firebase_app)
|
| 108 |
self.phones_ref = self.db.collection("phones")
|
| 109 |
|
| 110 |
"""create a contact into document which name is uuid in phone collections in firestore"""
|
|
|
|
| 111 |
def create_one_contact(self, uuid: str, contact: ContactModel):
|
| 112 |
assembler = Assembler()
|
| 113 |
data = assembler.to_contact_result_format(contact)
|
|
@@ -116,6 +118,7 @@ class ContactsService:
|
|
| 116 |
contacts_doc_ref.set(data)
|
| 117 |
|
| 118 |
"""update a contact into document which name is uuid in phone collections in firestore"""
|
|
|
|
| 119 |
def update_one_contact(self, uuid: str, contact: ContactModel):
|
| 120 |
assembler = Assembler()
|
| 121 |
data = assembler.to_contact_result_format(contact)
|
|
@@ -124,19 +127,18 @@ class ContactsService:
|
|
| 124 |
contacts_doc_ref.update(data)
|
| 125 |
|
| 126 |
"""delete a contact into document which name is uuid in phone collections in firestore"""
|
|
|
|
| 127 |
def delete_one_contact(self, uuid: str, contact: ContactModel):
|
| 128 |
phones_doc_ref = self.phones_ref.document(uuid)
|
| 129 |
contacts_doc_ref = phones_doc_ref.collection("contacts").document(contact.contact_id)
|
| 130 |
contacts_doc_ref.delete()
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
def getContactsByUUID(self, uuid: str) -> []:
|
| 135 |
phones_doc_ref = self.phones_ref.document(uuid)
|
| 136 |
contacts_ref = phones_doc_ref.collection("contacts")
|
| 137 |
|
| 138 |
# Retrieve all documents in the 'contacts' sub-collection
|
| 139 |
-
contacts = contacts_ref.stream()
|
| 140 |
|
| 141 |
result = []
|
| 142 |
# Iterate through the documents and print out their data
|
|
|
|
| 103 |
return get_pinecone_index_namespace(f"{uuid}-contacts")
|
| 104 |
|
| 105 |
"""init firestore to save user's contacts"""
|
| 106 |
+
|
| 107 |
def init_firestore(self):
|
| 108 |
self.db = firestore.client(app=self.firebase_app)
|
| 109 |
self.phones_ref = self.db.collection("phones")
|
| 110 |
|
| 111 |
"""create a contact into document which name is uuid in phone collections in firestore"""
|
| 112 |
+
|
| 113 |
def create_one_contact(self, uuid: str, contact: ContactModel):
|
| 114 |
assembler = Assembler()
|
| 115 |
data = assembler.to_contact_result_format(contact)
|
|
|
|
| 118 |
contacts_doc_ref.set(data)
|
| 119 |
|
| 120 |
"""update a contact into document which name is uuid in phone collections in firestore"""
|
| 121 |
+
|
| 122 |
def update_one_contact(self, uuid: str, contact: ContactModel):
|
| 123 |
assembler = Assembler()
|
| 124 |
data = assembler.to_contact_result_format(contact)
|
|
|
|
| 127 |
contacts_doc_ref.update(data)
|
| 128 |
|
| 129 |
"""delete a contact into document which name is uuid in phone collections in firestore"""
|
| 130 |
+
|
| 131 |
def delete_one_contact(self, uuid: str, contact: ContactModel):
|
| 132 |
phones_doc_ref = self.phones_ref.document(uuid)
|
| 133 |
contacts_doc_ref = phones_doc_ref.collection("contacts").document(contact.contact_id)
|
| 134 |
contacts_doc_ref.delete()
|
| 135 |
|
| 136 |
+
def getContactsByIds(self, uuid: str, contactIds: list[str]) -> []:
|
|
|
|
|
|
|
| 137 |
phones_doc_ref = self.phones_ref.document(uuid)
|
| 138 |
contacts_ref = phones_doc_ref.collection("contacts")
|
| 139 |
|
| 140 |
# Retrieve all documents in the 'contacts' sub-collection
|
| 141 |
+
contacts = contacts_ref.where("contactId", "in", contactIds).stream()
|
| 142 |
|
| 143 |
result = []
|
| 144 |
# Iterate through the documents and print out their data
|