MukeshKapoor25 commited on
Commit
26d5826
·
1 Parent(s): d87c960

feat(service-professional): Migrate from staff_id/staff_code to partner_id identifier

Browse files

- Replace staff_id and staff_code references with partner_id throughout authentication flow
- Update OTP data storage in Redis to use partner_id instead of dual identifiers
- Simplify JWT token payload to use partner_id as standard identifier
- Remove redundant staff_code logging statements for cleaner logs
- Consolidate professional identifier to single partner_id field for consistency
- Maintain backward compatibility in token payload structure

app/auth/services/service_professional_auth_service.py CHANGED
@@ -95,7 +95,7 @@ class ServiceProfessionalAuthService:
95
  professional_status = professional.get("status", "").lower()
96
  if professional_status != "active":
97
  logger.warning(
98
- f"Inactive service professional attempted OTP: {professional.get('staff_code')}, "
99
  f"status: {professional_status}"
100
  )
101
  return False, f"Service professional account is {professional_status}", 0
@@ -111,8 +111,7 @@ class ServiceProfessionalAuthService:
111
  # Store OTP in Redis FIRST (before attempting to send)
112
  otp_data = {
113
  "phone": normalized_phone,
114
- "staff_id": professional.get("staff_id"),
115
- "staff_code": professional.get("staff_code"),
116
  "otp": otp,
117
  "created_at": datetime.utcnow().isoformat(),
118
  "expires_at": expires_at.isoformat(),
@@ -128,7 +127,6 @@ class ServiceProfessionalAuthService:
128
 
129
  logger.info(
130
  f"Service professional OTP generated and stored in Redis for {normalized_phone} "
131
- f"(staff: {professional.get('staff_code')}), expires in {expires_in}s"
132
  )
133
 
134
  # Attempt to send OTP via WATI WhatsApp API
@@ -147,7 +145,6 @@ class ServiceProfessionalAuthService:
147
 
148
  logger.info(
149
  f"Service professional OTP sent successfully via WATI to {normalized_phone} "
150
- f"for staff {professional.get('staff_code')}. Message ID: {message_id}"
151
  )
152
  return True, "OTP sent successfully via WhatsApp", expires_in
153
  else:
@@ -158,7 +155,6 @@ class ServiceProfessionalAuthService:
158
 
159
  logger.warning(
160
  f"WhatsApp delivery failed for service professional {normalized_phone} "
161
- f"(staff: {professional.get('staff_code')}), but OTP is cached in Redis. Error: {wati_message}"
162
  )
163
  return True, "OTP generated (WhatsApp delivery pending). Please use the OTP if received.", expires_in
164
 
@@ -234,13 +230,12 @@ class ServiceProfessionalAuthService:
234
  # Verify professional is still active
235
  professional_status = professional.get("status", "").lower()
236
  if professional_status != "active":
237
- logger.warning(f"Inactive service professional verified OTP: {professional.get('staff_code')}")
238
  return None, f"Service professional account is {professional_status}"
239
 
240
  # Prepare professional data for token generation
241
  professional_data = {
242
- "staff_id": professional.get("staff_id"),
243
- "staff_code": professional.get("staff_code"),
244
  "name": professional.get("name"),
245
  "phone": professional.get("phone"),
246
  "email": professional.get("email"),
@@ -251,7 +246,6 @@ class ServiceProfessionalAuthService:
251
 
252
  logger.info(
253
  f"Service professional OTP verified successfully for {normalized_phone}, "
254
- f"staff: {professional.get('staff_code')}"
255
  )
256
  return professional_data, "OTP verified successfully"
257
 
@@ -277,11 +271,11 @@ class ServiceProfessionalAuthService:
277
  expires_delta = timedelta(hours=settings.TOKEN_EXPIRATION_HOURS)
278
  expire = datetime.utcnow() + expires_delta
279
 
280
- # Token payload
281
  token_data = {
282
- "sub": professional_data["staff_id"],
283
- "staff_id": professional_data["staff_id"], # Required by SPA-MS auth dependency
284
- "staff_code": professional_data["staff_code"],
285
  "user_type": "service_professional",
286
  "exp": expire,
287
  "iat": datetime.utcnow()
 
95
  professional_status = professional.get("status", "").lower()
96
  if professional_status != "active":
97
  logger.warning(
98
+ f"Inactive service professional attempted OTP: {professional.get('partner_id')}, "
99
  f"status: {professional_status}"
100
  )
101
  return False, f"Service professional account is {professional_status}", 0
 
111
  # Store OTP in Redis FIRST (before attempting to send)
112
  otp_data = {
113
  "phone": normalized_phone,
114
+ "partner_id": professional.get("partner_id"),
 
115
  "otp": otp,
116
  "created_at": datetime.utcnow().isoformat(),
117
  "expires_at": expires_at.isoformat(),
 
127
 
128
  logger.info(
129
  f"Service professional OTP generated and stored in Redis for {normalized_phone} "
 
130
  )
131
 
132
  # Attempt to send OTP via WATI WhatsApp API
 
145
 
146
  logger.info(
147
  f"Service professional OTP sent successfully via WATI to {normalized_phone} "
 
148
  )
149
  return True, "OTP sent successfully via WhatsApp", expires_in
150
  else:
 
155
 
156
  logger.warning(
157
  f"WhatsApp delivery failed for service professional {normalized_phone} "
 
158
  )
159
  return True, "OTP generated (WhatsApp delivery pending). Please use the OTP if received.", expires_in
160
 
 
230
  # Verify professional is still active
231
  professional_status = professional.get("status", "").lower()
232
  if professional_status != "active":
233
+ logger.warning(f"Inactive service professional verified OTP: {professional.get('partner_id')}")
234
  return None, f"Service professional account is {professional_status}"
235
 
236
  # Prepare professional data for token generation
237
  professional_data = {
238
+ "partner_id": professional.get("partner_id"),
 
239
  "name": professional.get("name"),
240
  "phone": professional.get("phone"),
241
  "email": professional.get("email"),
 
246
 
247
  logger.info(
248
  f"Service professional OTP verified successfully for {normalized_phone}, "
 
249
  )
250
  return professional_data, "OTP verified successfully"
251
 
 
271
  expires_delta = timedelta(hours=settings.TOKEN_EXPIRATION_HOURS)
272
  expire = datetime.utcnow() + expires_delta
273
 
274
+ # Token payload - use partner_id as the standard identifier
275
  token_data = {
276
+ "sub": professional_data["partner_id"],
277
+ "partner_id": professional_data["partner_id"], # Standard identifier for SPA-MS
278
+ "partner_id": professional_data["partner_id"], # Keep for backward compatibility
279
  "user_type": "service_professional",
280
  "exp": expire,
281
  "iat": datetime.utcnow()