Browse Source

reformat staff_profile get and save interface

luojunhui 1 month ago
parent
commit
7bc873813b
2 changed files with 34 additions and 37 deletions
  1. 27 10
      pqai_agent_server/api_server.py
  2. 7 27
      pqai_agent_server/models/mysql_staff_manager.py

+ 27 - 10
pqai_agent_server/api_server.py

@@ -40,26 +40,44 @@ def get_staff_profile():
     staff_id = request.args['staff_id']
     if not staff_id:
         return wrap_response(400, msg='staff_id is required')
-    profile = app.staff_manager.get_staff_profile(staff_id)
-    if not profile:
+    agent_profile = app.staff_manager.get_staff_profile(staff_id)
+    if not agent_profile:
         return wrap_response(404, msg='staff not found')
     else:
-        return wrap_response(200, data=profile)
+        field_map_list = apollo_config.get_json_value("field_map_list", [])
+        field_map = {
+            item["field_name"]: item["display_name"] for item in field_map_list
+        }
+        profile_info = [
+            {
+                "field_name": key,
+                "display_name": field_map[key],
+                "field_value": value,
+            }
+            for key, value in agent_profile.items()
+            if agent_profile.get(key)
+        ]
+        return wrap_response(200, data=profile_info)
 
 
 @app.route('/api/saveStaffProfile', methods=['POST'])
 def save_staff_profile():
     staff_id = request.json.get('staff_id')
-    profile = request.json.get('profile')
+    staff_profile = request.json.get('staff_profile')
     if not staff_id:
         return wrap_response(400, msg='staff id is required')
 
-    if not profile:
+    if not staff_profile:
         return wrap_response(400, msg='profile is required')
     else:
         try:
-            profile_json = json.loads(profile)
-            affected_rows = app.staff_manager.save_staff_profile(staff_id, profile_json)
+            profile_info_list = json.loads(staff_profile)
+            profile_dict = {}
+            for profile_info in profile_info_list:
+                field_name = profile_info['field_name']
+                profile_dict[field_name] = profile_info['field_value']
+
+            affected_rows = app.staff_manager.save_staff_profile(staff_id, profile_dict)
             if not affected_rows:
                 return wrap_response(500, msg='save staff profile failed')
             else:
@@ -70,7 +88,7 @@ def save_staff_profile():
 
 @app.route('/api/getProfileFields', methods=['GET'])
 def get_profile_fields():
-    return wrap_response(200, data=app.staff_manager.list_profile_fields())
+    return wrap_response(200, data=apollo_config.get_json_value("field_map_list", []))
 
 
 @app.route('/api/getUserProfile', methods=['GET'])
@@ -380,8 +398,7 @@ if __name__ == '__main__':
     staff_manager = MySQLStaffManager(
         db_config=user_db_config['mysql'],
         staff_table=staff_db_config['table'],
-        user_table=user_db_config['table'],
-        config=apollo_config
+        user_table=user_db_config['table']
     )
     app.staff_manager = staff_manager
 

+ 7 - 27
pqai_agent_server/models/mysql_staff_manager.py

@@ -23,11 +23,10 @@ class StaffManager(abc.ABC):
 
 class MySQLStaffManager(StaffManager):
 
-    def __init__(self, db_config, staff_table, user_table, config):
+    def __init__(self, db_config, staff_table, user_table):
         self.db = MySQLManager(db_config)
         self.staff_table = staff_table
         self.user_table = user_table
-        self.config = config
 
     def save_staff_profile(self, staff_id: str, staff_profile: Dict):
         update_query = f"""
@@ -39,7 +38,7 @@ class MySQLStaffManager(StaffManager):
         return affected_rows
 
     def get_staff_profile(self, staff_id) -> Dict:
-        profile_obj = {"staff_id": staff_id}
+        empty_profile = {}
         sql = f"""select agent_profile from {self.staff_table} where third_party_user_id = %s;"""
         response = self.db.select(
             sql=sql,
@@ -47,28 +46,13 @@ class MySQLStaffManager(StaffManager):
             args=(staff_id,),
         )
         if not response:
-            profile_obj["data"] = []
-            return profile_obj
+            return empty_profile
 
         agent_profile = response[0]["agent_profile"]
-        if not agent_profile:
-            profile_obj["data"] = []
-        else:
-            field_map_list = self.list_profile_fields()
-            field_map = {
-                item["field_name"]: item["display_name"] for item in field_map_list
-            }
-            agent_profile = json.loads(agent_profile)
-            profile_obj["data"] = [
-                {
-                    "field_name": key,
-                    "display_name": field_map[key],
-                    "field_value": value,
-                }
-                for key, value in agent_profile.items()
-                if agent_profile.get(key)
-            ]
-        return profile_obj
+        profile = json.loads(agent_profile)
+        if not profile:
+            return empty_profile
+        return profile
 
     def list_all_staffs(self, page_id: int, page_size: int) -> Dict:
         """
@@ -99,7 +83,3 @@ class MySQLStaffManager(StaffManager):
             "next_page": next_page_id,
             "data": staff_list,
         }
-
-    def list_profile_fields(self) -> List[Dict]:
-        response = self.config.get_json_value("field_map_list", [])
-        return response