diff --git a/app/db_sql.py b/app/db_sql.py index 1dc60bc..b62ded3 100644 --- a/app/db_sql.py +++ b/app/db_sql.py @@ -844,14 +844,33 @@ def sql_delete( def redis_lookup_id_random( record_id_random: int|str, table_name: str, - ): + check_int_id: bool = False, + ) -> str|int|bool|None: log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) if isinstance(record_id_random, str) and len(record_id_random) >= 11 and len(record_id_random) <= 22: pass - elif isinstance(record_id_random, int): return record_id_random + elif isinstance(record_id_random, int): + record_id = record_id_random + if check_int_id: + log.info(f'Checking the int ID if exists. Table Name: {table_name} ID: {record_id}') + if get_id_random_result := get_id_random( + record_id = record_id, + table_name = table_name, + ): + log.info(f'The int ID exists. Returning the int ID. ID Random: {get_id_random_result}') + return record_id + else: + log.info(f'The int ID does not exists. Returning False. Table Name: {table_name} ID: {record_id}') + return False + else: + log.info(f'Not checking if the int ID exists. Returning the int ID. ID: {record_id}') + return record_id + elif record_id_random is None: + log.info(f'No record ID was passed. Returning None') + return None else: - log.info(f'Unexpected data type or string format: {str(type(record_id_random))} Expected type is a string 11 or 22 characters long.') + log.warning(f'Unexpected data type or string format: {str(type(record_id_random))} Expected type is a string 11 or 22 characters long.') return False if record_id_random and table_name: @@ -920,13 +939,12 @@ def redis_lookup_id_random( # ### BEGIN ### API DB SQL ### get_id_random() ### -# Changed name from lookup_id_random() to get_id_random() -# Updated 2021-08-23 +# Updated 2022-01-06 @logger_reset def get_id_random( record_id: int, table_name: str - ): + ) -> str|bool|None: log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) @@ -938,7 +956,6 @@ def get_id_random( """ if select_results := sql_select(sql=sql, data=data): - #log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(select_results) log.debug(type(select_results)) if isinstance(select_results, dict): @@ -946,18 +963,24 @@ def get_id_random( if record_id_random := select_results.get('id_random'): return str(record_id_random) else: - log.setLevel(logging.ERROR) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.error('The SQL result was not what was expected.') return False - else: - log.setLevel(logging.ERROR) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL - log.error('More than one record may have been found. There may be a duplicate id.') + elif isinstance(select_results, list): + log.exception('More than one record may have been found. There may be a duplicate id. This should never happen.') log.error(select_results) return False - else: - #log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL - log.info('Record ID random was not found') + else: + log.exception(f'Got an unexpected result while trying to look up the ID. Is the table name correct? {table_name} Is the record ID valid? {record_id}') + log.error(select_results) + return False + elif select_results is None: + log.warning(f'No results with: Table Name: {table_name} ID: {record_id}') + log.debug(select_results) return None + else: # False or something else not True + log.error(f'Something went wrong while trying to look up the ID. Is the table name correct? {table_name} Is the record ID valid? {record_id}') + log.error(select_results) + return False # ### END ### API DB SQL ### get_id_random() ### diff --git a/app/methods/person_methods.py b/app/methods/person_methods.py index deb1d73..b5634f5 100644 --- a/app/methods/person_methods.py +++ b/app/methods/person_methods.py @@ -424,6 +424,29 @@ def get_person_rec_list( # ### END ### API Person Methods ### get_person_rec_list() ### +# def quick_dict_obj_return( +# data_dict_obj, +# Obj_Type, +# dict_exclude: list, +# ) -> dict: + +# log.debug(type(data_dict_obj)) +# if isinstance(data_dict_obj, dict): +# data_dict = data_dict_obj +# try: +# data_obj = Person_Base(**data_dict) +# data_obj = Obj_Type(**data_dict) +# log.debug(data_obj) +# except ValidationError as e: +# log.error(e.json()) +# return False +# else: +# data_obj = data_dict_obj +# data_obj.account_id = account_id + +# data_dict = data_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude=['contact', 'contact_id', 'contact_id_random', 'email', 'cc_email', 'membership_person_id', 'membership_person_id_random', 'organization', 'user', 'created_on', 'updated_on']) + + # ### BEGIN ### API Person Methods ### create_person_kiss() ### # Updated 2022-01-05 def create_person_kiss( @@ -436,13 +459,13 @@ def create_person_kiss( log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) + # ### SECTION ### Secondary data validation log.info('Create dictionary or Pydantic object') log.debug(type(person_dict_obj)) if isinstance(person_dict_obj, dict): person_dict = person_dict_obj try: person_obj = Person_Base(**person_dict) - log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(person_obj) except ValidationError as e: log.error(e.json()) @@ -453,6 +476,7 @@ def create_person_kiss( person_dict = person_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'contact', 'contact_id', 'contact_id_random', 'email', 'cc_email', 'membership_person_id', 'membership_person_id_random', 'organization', 'user', 'created_on', 'updated_on'}) + # ### SECTION ### Process data if user_id: # Link to an existing user log.info(f'Adding user_id to person_dict. User ID: {user_id}') @@ -469,7 +493,11 @@ def create_person_kiss( if contact_id and person.contact: log.info('Updating Contact object') - if contact_update_result := update_contact(): pass + if contact_update_result := update_contact( + contact_dict_obj = person_obj.contact, + for_type = 'person', + for_id = person_id, + ): pass else: return False elif person.contact: log.info('Creating Contact object') @@ -484,7 +512,10 @@ def create_person_kiss( if user_id and person.user: log.info('Updating User object') - if user_update_result := update_user(): pass + if user_update_result := update_user( + user_dict_obj = person_obj.user, + person_id = person_id, + ): pass # NOTE: There is a trigger that will update the person record with the new user ID. else: return False elif person.user: log.info('Creating User object') diff --git a/app/routers/person.py b/app/routers/person.py index 4cd5296..8888bfa 100644 --- a/app/routers/person.py +++ b/app/routers/person.py @@ -20,7 +20,7 @@ router = APIRouter() # ### BEGIN ### API Person Routers ### post_person_obj() ### -# Updated 2022-01-05 +# Updated 2022-01-06 @router.post('/person', response_model=Resp_Body_Base) async def post_person_obj( person_obj: Person_Base, @@ -33,6 +33,20 @@ async def post_person_obj( log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) + # ### SECTION ### Secondary data validation + if contact_id := redis_lookup_id_random(record_id_random=contact_id, table_name='contact'): pass + elif contact_id is None: pass + else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The contact ID was invalid or not found.') + + if organization_id := redis_lookup_id_random(record_id_random=organization_id, table_name='organization'): pass + elif organization_id is None: pass + else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The organization ID was invalid or not found.') + + if user_id := redis_lookup_id_random(record_id_random=user_id, table_name='user'): pass + elif user_id is None: pass + else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The user ID was invalid or not found.') + + # ### SECTION ### Process data if person_id := create_person_kiss( account_id = commons.x_account_id, person_dict_obj = person_obj, @@ -42,8 +56,9 @@ async def post_person_obj( ): pass else: log.warning('Likely bad request') - return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request + return mk_resp(data=False, status_code=400, response=commons.response, status_message='Something failed while processing the data.') # Bad Request + # ### SECTION ### Return successful results if return_obj: person_obj = load_person_obj( person_id = person_id, @@ -60,7 +75,7 @@ async def post_person_obj( # ### BEGIN ### API Person Routers ### patch_person_obj() ### -# Updated 2022-01-05 +# Updated 2022-01-06 @router.patch('/person/{person_id}', response_model=Resp_Body_Base) async def patch_person_obj( person_obj: Person_Base, @@ -71,10 +86,28 @@ async def patch_person_obj( return_obj: Optional[bool] = True, commons: Common_Route_Params = Depends(common_route_params), ): - log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) + # ### SECTION ### Secondary data validation + if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass + else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The person ID was invalid or not found.') + + if contact_id := redis_lookup_id_random(record_id_random=contact_id, table_name='contact'): pass + elif contact_id is None: pass + else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The contact ID was invalid or not found.') + + if organization_id := redis_lookup_id_random(record_id_random=organization_id, table_name='organization'): pass + elif organization_id is None: pass + else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The organization ID was invalid or not found.') + + if user_id := redis_lookup_id_random(record_id_random=user_id, table_name='user'): pass + elif user_id is None: pass + else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The user ID was invalid or not found.') + + # ### SECTION ### Process data if person_update_result := update_person_kiss( + person_id = person_id, person_dict_obj = person_obj, contact_id = contact_id, organization_id = organization_id, @@ -82,8 +115,9 @@ async def patch_person_obj( ): pass else: log.warning('Likely bad request') - return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request + return mk_resp(data=False, status_code=400, response=commons.response, status_message='Something failed while processing the data.') # Bad Request + # ### SECTION ### Return successful results if return_obj: person_obj = load_person_obj( person_id = person_id,