Rev 21285 | Blame | Last modification | View Log | RSS feed
package com.spice.profitmandi.web.processor;import java.time.LocalDateTime;import java.util.List;import java.util.Random;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.transaction.annotation.Transactional;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.dao.entity.Otp;import com.spice.profitmandi.dao.enumuration.OtpType;import com.spice.profitmandi.dao.repository.OtpRepository;import com.spice.profitmandi.web.res.OTPResponse;@Componentpublic class OtpProcessor{private static final int len = 5;private static final String numbers ="0123456789";@AutowiredOtpRepository otpRepository;private String getOtp(){Random rndm_method = new Random();char[] otp = new char[len];for (int i = 0; i < len; i++){otp[i] = numbers.charAt(rndm_method.nextInt(numbers.length()));}return String.valueOf(otp);}public OTPResponse generateOtp(String email, String phone, OtpType otpType) throws Exception, ProfitMandiBusinessException{OTPResponse otpResponse = new OTPResponse();List<Otp> otps = otpRepository.getGeneratedOtpForCredentials(email, otpType);String otp = null;if (otps.size() >=5){otpResponse.setReference_id(0);otpResponse.setResult(false);otpResponse.setMessage("Maximum limit reached for the day");return otpResponse;}if (!otps.isEmpty()){if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(2))){otpResponse.setMessage("OTP generated less than 2 minutes ago");otpResponse.setReference_id(otps.get(0).getId());otpResponse.setResult(true);otpResponse.setOtp(otps.get(0).getOtp());return otpResponse;}else if(otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(10))){otp = otps.get(0).getOtp();}else{;}}if (otp == null || otp.isEmpty()){otp = getOtp();}Otp otp_d = otpRepository.generateOtp(email, phone, otpType, otp);otpResponse.setReference_id(otp_d.getId());otpResponse.setMessage("OTP generated successfully");otpResponse.setResult(true);otpResponse.setOtp(otp_d.getOtp());return otpResponse;}public OTPResponse validateOtp(String email, long reference_id, String otp_number) throws Exception, ProfitMandiBusinessException{OTPResponse otpResponse = new OTPResponse();Otp otp = otpRepository.getById(reference_id);otp.setTryCount(otp.getTryCount()+1);otpResponse.setReference_id(reference_id);if (!otp.getEmail().equalsIgnoreCase(email) || !otp.getOtp().equalsIgnoreCase(otp_number)){otpResponse.setMessage("Invalid otp");otpResponse.setResult(false);otpRepository.updateById(otp);return otpResponse;}if (otp.isExpired() || otp.isVerified() || otp.getExpiryTimestamp().isBefore(LocalDateTime.now())){otpResponse.setMessage("OTP expired");otpResponse.setResult(false);return otpResponse;}if (otp.getTryCount() >5){otpResponse.setMessage("Maximum try count reached");otpResponse.setResult(false);return otpResponse;}otp.setExpired(true);otp.setVerified(true);otpRepository.updateById(otp);otpResponse.setMessage("OTP validated successfully");otpResponse.setResult(true);return otpResponse;}}