Implement API to Send OTP for Email-Based Login #85

Merged
vikas.nale merged 2 commits from Ashutosh_Task#480_Send_OTP into Issue_Jun_1W_2 2025-06-06 12:13:45 +00:00
Collaborator

1. Validate Input Email

  • Checks if the generateOTP.Email is null or whitespace.
  • If invalid, logs a warning and returns a 400 Bad Request with an appropriate error message.

2. Fetch User

  • Uses _userManager.FindByEmailAsync() to find the user based on the provided email.
  • Verifies that the user exists and is active.

3. Fetch Employee Details

  • Retrieves the associated Employee entity using the ApplicationUserId of the found user.

4. Generate OTP

  • Creates a random 4-digit OTP using Random().Next(1000, 9999).
  • The OTP is valid for 10 minutes (600 seconds).

5. Store OTP in Database

  • Creates a new OTPDetails object:
    • Assigns the UserId, generated OTP, expiration time, current UTC timestamp, and TenantId.
  • Adds the record to the database and calls SaveChangesAsync() to persist it.

6. Prepare Email

  • Constructs a recipient list using the provided email.
  • Combines the employee's first and last name for personalization.
  • Fetches the email template from the MailingList using the title "Send OTP" (case-insensitive).
  • Extracts the subject and body from the template (or defaults to empty strings).

7. Send Email

  • Calls _emailSender.SendOTP() with the recipient list, template body, employee name, OTP, and subject.

8. Log and Respond

  • Logs a success message after sending the OTP.
  • Returns a 200 OK response with a success message and status.

9. Handle Invalid or Inactive User

  • If the user is not found or is inactive:
    • Logs a warning and returns a 400 Bad Request.

10. Exception Handling

  • Catches any unexpected exceptions.
  • Logs the exception with the email for debugging.
  • Returns a 500 Internal Server Error with a generic error message and the exception details.
**1. Validate Input Email** - Checks if the generateOTP.Email is null or whitespace. - If invalid, logs a warning and returns a 400 Bad Request with an appropriate error message. **2. Fetch User** - Uses _userManager.FindByEmailAsync() to find the user based on the provided email. - Verifies that the user exists and is active. **3. Fetch Employee Details** - Retrieves the associated Employee entity using the ApplicationUserId of the found user. **4. Generate OTP** - Creates a random 4-digit OTP using Random().Next(1000, 9999). - The OTP is valid for 10 minutes (600 seconds). **5. Store OTP in Database** - Creates a new OTPDetails object: - Assigns the UserId, generated OTP, expiration time, current UTC timestamp, and TenantId. - Adds the record to the database and calls SaveChangesAsync() to persist it. **6. Prepare Email** - Constructs a recipient list using the provided email. - Combines the employee's first and last name for personalization. - Fetches the email template from the MailingList using the title "Send OTP" (case-insensitive). - Extracts the subject and body from the template (or defaults to empty strings). **7. Send Email** - Calls _emailSender.SendOTP() with the recipient list, template body, employee name, OTP, and subject. **8. Log and Respond** - Logs a success message after sending the OTP. - Returns a 200 OK response with a success message and status. **9. Handle Invalid or Inactive User** - If the user is not found or is inactive: - Logs a warning and returns a 400 Bad Request. **10. Exception Handling** - Catches any unexpected exceptions. - Logs the exception with the email for debugging. - Returns a 500 Internal Server Error with a generic error message and the exception details.
ashutosh.nehete added 1 commit 2025-06-06 11:05:19 +00:00
ashutosh.nehete added 1 commit 2025-06-06 12:10:35 +00:00
vikas.nale merged commit 47ad6231dd into Issue_Jun_1W_2 2025-06-06 12:13:45 +00:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: admin/marco.pms.api#85
No description provided.