1919 CustomerView ,
2020)
2121from fastapi_ecom .utils .auth import verify_cust_cred
22+ from fastapi_ecom .utils .logging_setup import failure , general , success , warning
2223
2324router = APIRouter (prefix = "/customer" )
2425
@@ -50,19 +51,23 @@ async def create_customer(customer: CustomerCreate, db: AsyncSession = Depends(g
5051 state = customer .state .strip (),
5152 uuid = uuid4 ().hex [0 :8 ] # Assign UUID manually; One UUID per transaction
5253 )
54+ general (f"Adding account for customer in database: { customer .email } " )
5355 db .add (db_customer )
5456 try :
5557 await db .flush ()
5658 except IntegrityError as expt :
57- raise HTTPException (
59+ failure (f"Customer account creation failed - Uniqueness constraint violation for email: { customer .email } " )
60+ raise HTTPException (
5861 status_code = status .HTTP_409_CONFLICT ,
5962 detail = "Uniqueness constraint failed - Please try again"
6063 ) from expt
6164 except Exception as expt :
65+ failure (f"Customer account creation failed with unexpected error for email: { customer .email } " )
6266 raise HTTPException (
6367 status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
6468 detail = "An unexpected database error occurred."
6569 ) from expt
70+ success (f"Customer account created successfully with email: { customer .email } " )
6671 return {
6772 "action" : "post" ,
6873 "customer" : CustomerView .model_validate (db_customer ).model_dump ()
@@ -77,6 +82,7 @@ async def get_customer_me(customer_auth = Depends(verify_cust_cred)) -> dict[str
7782
7883 :return: Dictionary containing the action type and the authenticated customer's email.
7984 """
85+ general (f"Customer authentication successful for: { customer_auth .email } " )
8086 return {
8187 "action" : "get" ,
8288 "email" : customer_auth .email
@@ -101,14 +107,17 @@ async def get_customers(
101107 :raises HTTPException:
102108 - If no customer exist in the database, it raises 404 Not Found.
103109 """
110+ general (f"Searching customers with skip={ skip } , limit={ limit } " )
104111 query = select (Customer ).options (selectinload ("*" )).offset (skip ).limit (limit )
105112 result = await db .execute (query )
106113 customers = result .scalars ().all ()
107114 if not customers :
115+ warning ("No customers found in database" )
108116 raise HTTPException (
109117 status_code = status .HTTP_404_NOT_FOUND ,
110118 detail = "No customer present in database"
111119 )
120+ success (f"Found { len (customers )} customers" )
112121 return {
113122 "action" : "get" ,
114123 "customers" : [CustomerView .model_validate (customer ).model_dump () for customer in customers ]
@@ -122,12 +131,14 @@ async def delete_customer(db: AsyncSession = Depends(get_db), customer_auth = De
122131 :param db: Active asynchronous database session dependency.
123132 :param customer_auth: Authenticated customer object.
124133
125- :return: Dictionary containing the action type and the deleted customer's data.
134+ :return: Dictionary containing the action type and the deleted customer's data, validated and
135+ serialized using the `CustomerView` schema.
126136
127137 :raises HTTPException:
128138 - If a uniqueness constraint fails, it returns a 409 Conflict status.
129139 - If there are other database errors, it returns a 500 Internal Server Error.
130140 """
141+ general (f"Deleting customer account: { customer_auth .email } " )
131142 query = select (Customer ).where (Customer .uuid == customer_auth .uuid ).options (selectinload ("*" ))
132143 result = await db .execute (query )
133144 customer_to_delete = result .scalar_one_or_none ()
@@ -141,10 +152,12 @@ async def delete_customer(db: AsyncSession = Depends(get_db), customer_auth = De
141152 interactions due to which mocking one part wont produce the desired result. Thus,
142153 we will keep it uncovered until a alternative can be made for testing this exception block.
143154 """
155+ failure (f"Customer account deletion failed for: { customer_auth .email } " )
144156 raise HTTPException (
145157 status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
146158 detail = "An unexpected database error occurred."
147159 ) from expt
160+ success (f"Customer account deleted successfully: { customer_auth .email } " )
148161 return {
149162 "action" : "delete" ,
150163 "customer" : CustomerView .model_validate (customer_to_delete ).model_dump ()
@@ -165,6 +178,8 @@ async def update_customer(customer: CustomerUpdate, db: AsyncSession = Depends(g
165178 - If a uniqueness constraint fails, it returns a 409 Conflict status.
166179 - If there are other database errors, it returns a 500 Internal Server Error.
167180 """
181+ customer_email = customer_auth .email # Capture email before potential database errors
182+ general (f"Updating details of customer: { customer_email } " )
168183 query = select (Customer ).where (Customer .uuid == customer_auth .uuid ).options (selectinload ("*" ))
169184 result = await db .execute (query )
170185 customer_to_update = result .scalar_one_or_none ()
@@ -184,6 +199,7 @@ async def update_customer(customer: CustomerUpdate, db: AsyncSession = Depends(g
184199 try :
185200 await db .flush ()
186201 except IntegrityError as expt :
202+ failure (f"Customer update failed - Uniqueness constraint violation for: { customer_email } " )
187203 raise HTTPException (
188204 status_code = status .HTTP_409_CONFLICT ,
189205 detail = "Uniqueness constraint failed - Please try again"
@@ -194,10 +210,12 @@ async def update_customer(customer: CustomerUpdate, db: AsyncSession = Depends(g
194210 interactions due to which mocking one part wont produce the desired result. Thus,
195211 we will keep it uncovered until a alternative can be made for testing this exception block.
196212 """
213+ failure (f"Customer update failed with unexpected error for: { customer_email } " )
197214 raise HTTPException (
198215 status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
199216 detail = "An unexpected database error occurred."
200217 ) from expt
218+ success (f"Customer details updated successfully: { customer_email } " )
201219 return {
202220 "action" : "put" ,
203221 "customer" : CustomerView .model_validate (customer_to_update ).model_dump ()
0 commit comments