Rev 23568 | Blame | Last modification | View Log | RSS feed
package com.spice.profitmandi.web.processor;import java.io.IOException;import java.net.URISyntaxException;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 com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.util.Utils;import com.spice.profitmandi.dao.entity.dtr.Otp;import com.spice.profitmandi.dao.enumuration.dtr.OtpType;import com.spice.profitmandi.dao.repository.dtr.OtpRepository;import com.spice.profitmandi.web.res.OTPResponse;@Componentpublic class OtpProcessor {private static final int len = 5;private static final String numbers = "0123456789";private static final String text = "Dear Customer, {0} is the OTP that you have requested to login into SmartDukaan. Don't share your OTP with anyone.";@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.selectAllByEmailWithTime(email);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();}sendOtp(otp, phone);Otp otp_bean = new Otp();otp_bean.setEmail(email);otp_bean.setMobile(phone);otp_bean.setOtp(otp);otp_bean.setOtpType(otpType);otp_bean.setCreatedOn(LocalDateTime.now());otp_bean.setExpiryTimestamp(LocalDateTime.now().plusMinutes(10));otpRepository.persist(otp_bean);otpResponse.setReference_id(otp_bean.getId());otpResponse.setMessage("OTP generated successfully");otpResponse.setResult(true);otpResponse.setOtp(otp_bean.getOtp());return otpResponse;}public OTPResponse validateOtp(String email, int reference_id, String otp_number)throws Exception, ProfitMandiBusinessException {OTPResponse otpResponse = new OTPResponse();Otp otp = otpRepository.selectById(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.persist(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.persist(otp);otpResponse.setMessage("OTP validated successfully");otpResponse.setResult(true);return otpResponse;}private void sendOtp(String otp_text, String phone) {String msg = java.text.MessageFormat.format(text, otp_text);try {Utils.sendSms(msg, phone);} catch (Exception e) {e.printStackTrace();}}}