11from pydantic import ValidationError
22from ..models .utils import Promise
33from ..models .database import GeneralBlob , UserProfile
4- from ..models .response import CODE , IdData , IdsData , UserProfilesData
4+ from ..models .response import CODE , IdData , IdsData , UserProfilesData , ProfileAttributes
55from ..connectors import Session , get_redis_client
66from ..utils import get_encoded_tokens
77from ..env import LOG , CONFIG
@@ -122,6 +122,13 @@ async def add_user_profiles(
122122 assert len (profiles ) == len (
123123 attributes
124124 ), "Length of profiles, attributes must be equal"
125+ for attr in attributes :
126+ try :
127+ ProfileAttributes .model_validate (attr )
128+ except ValidationError as e :
129+ return Promise .reject (
130+ CODE .SERVER_PARSE_ERROR , f"Invalid profile attributes: { e } "
131+ )
125132 with Session () as session :
126133 db_profiles = [
127134 UserProfile (
@@ -132,8 +139,7 @@ async def add_user_profiles(
132139 session .add_all (db_profiles )
133140 session .commit ()
134141 profile_ids = [profile .id for profile in db_profiles ]
135- async with get_redis_client () as redis_client :
136- await redis_client .delete (f"user_profiles::{ project_id } ::{ user_id } " )
142+ await refresh_user_profile_cache (user_id , project_id )
137143 return Promise .resolve (IdsData (ids = profile_ids ))
138144
139145
@@ -166,8 +172,7 @@ async def update_user_profiles(
166172 db_profile .attributes = attribute
167173 db_profiles .append (profile_id )
168174 session .commit ()
169- async with get_redis_client () as redis_client :
170- await redis_client .delete (f"user_profiles::{ project_id } ::{ user_id } " )
175+ await refresh_user_profile_cache (user_id , project_id )
171176 return Promise .resolve (IdsData (ids = db_profiles ))
172177
173178
@@ -186,8 +191,7 @@ async def delete_user_profile(
186191 )
187192 session .delete (db_profile )
188193 session .commit ()
189- async with get_redis_client () as redis_client :
190- await redis_client .delete (f"user_profiles::{ project_id } ::{ user_id } " )
194+ await refresh_user_profile_cache (user_id , project_id )
191195 return Promise .resolve (None )
192196
193197
@@ -201,6 +205,11 @@ async def delete_user_profiles(
201205 UserProfile .project_id == project_id ,
202206 ).delete (synchronize_session = False )
203207 session .commit ()
208+ await refresh_user_profile_cache (user_id , project_id )
209+ return Promise .resolve (IdsData (ids = profile_ids ))
210+
211+
212+ async def refresh_user_profile_cache (user_id : str , project_id : str ) -> Promise [None ]:
204213 async with get_redis_client () as redis_client :
205214 await redis_client .delete (f"user_profiles::{ project_id } ::{ user_id } " )
206- return Promise .resolve (IdsData ( ids = profile_ids ) )
215+ return Promise .resolve (None )
0 commit comments