Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21285 kshitij.so 1
package com.spice.profitmandi.web.processor;
2
 
21499 kshitij.so 3
import java.io.IOException;
4
import java.net.URISyntaxException;
21285 kshitij.so 5
import java.time.LocalDateTime;
6
import java.util.List;
7
import java.util.Random;
8
 
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.stereotype.Component;
11
 
12
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
21499 kshitij.so 13
import com.spice.profitmandi.common.util.Utils;
21735 ashik.ali 14
import com.spice.profitmandi.dao.entity.dtr.Otp;
15
import com.spice.profitmandi.dao.enumuration.dtr.OtpType;
16
import com.spice.profitmandi.dao.repository.dtr.OtpRepository;
21285 kshitij.so 17
import com.spice.profitmandi.web.res.OTPResponse;
18
 
19
@Component
26087 tejbeer 20
public class OtpProcessor {
21285 kshitij.so 21
 
22
	private static final int len = 5;
26087 tejbeer 23
	private static final String numbers = "0123456789";
23309 amit.gupta 24
	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.";
21285 kshitij.so 25
 
26
	@Autowired
27
	OtpRepository otpRepository;
28
 
26087 tejbeer 29
	private String getOtp() {
21285 kshitij.so 30
		Random rndm_method = new Random();
31
		char[] otp = new char[len];
32
 
26087 tejbeer 33
		for (int i = 0; i < len; i++) {
21285 kshitij.so 34
			otp[i] = numbers.charAt(rndm_method.nextInt(numbers.length()));
35
		}
36
		return String.valueOf(otp);
37
	}
38
 
26087 tejbeer 39
	public OTPResponse generateOtp(String email, String phone, OtpType otpType)
40
			throws Exception, ProfitMandiBusinessException {
21285 kshitij.so 41
		OTPResponse otpResponse = new OTPResponse();
23273 ashik.ali 42
		List<Otp> otps = otpRepository.selectAllByEmailWithTime(email);
21285 kshitij.so 43
		String otp = null;
26087 tejbeer 44
		if (otps.size() >= 5) {
21285 kshitij.so 45
			otpResponse.setReference_id(0);
46
			otpResponse.setResult(false);
47
			otpResponse.setMessage("Maximum limit reached for the day");
48
			return otpResponse;
49
		}
26087 tejbeer 50
		if (!otps.isEmpty()) {
51
			if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(2))) {
21285 kshitij.so 52
				otpResponse.setMessage("OTP generated less than 2 minutes ago");
53
				otpResponse.setReference_id(otps.get(0).getId());
54
				otpResponse.setResult(true);
21299 kshitij.so 55
				otpResponse.setOtp(otps.get(0).getOtp());
21285 kshitij.so 56
				return otpResponse;
26087 tejbeer 57
			} else if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(10))) {
21285 kshitij.so 58
				otp = otps.get(0).getOtp();
26087 tejbeer 59
			} else {
21285 kshitij.so 60
				;
61
			}
62
		}
26087 tejbeer 63
		if (otp == null || otp.isEmpty()) {
21285 kshitij.so 64
			otp = getOtp();
65
		}
21499 kshitij.so 66
		sendOtp(otp, phone);
26087 tejbeer 67
 
23273 ashik.ali 68
		Otp otp_bean = new Otp();
69
		otp_bean.setEmail(email);
70
		otp_bean.setMobile(phone);
71
		otp_bean.setOtp(otp);
72
		otp_bean.setOtpType(otpType);
73
		otp_bean.setCreatedOn(LocalDateTime.now());
74
		otp_bean.setExpiryTimestamp(LocalDateTime.now().plusMinutes(10));
75
		otpRepository.persist(otp_bean);
26087 tejbeer 76
 
23273 ashik.ali 77
		otpResponse.setReference_id(otp_bean.getId());
21285 kshitij.so 78
		otpResponse.setMessage("OTP generated successfully");
79
		otpResponse.setResult(true);
23273 ashik.ali 80
		otpResponse.setOtp(otp_bean.getOtp());
21285 kshitij.so 81
		return otpResponse;
82
	}
83
 
26087 tejbeer 84
	public OTPResponse validateOtp(String email, int reference_id, String otp_number)
85
			throws Exception, ProfitMandiBusinessException {
21285 kshitij.so 86
		OTPResponse otpResponse = new OTPResponse();
23420 ashik.ali 87
		Otp otp = otpRepository.selectById(reference_id);
26087 tejbeer 88
		otp.setTryCount(otp.getTryCount() + 1);
21285 kshitij.so 89
		otpResponse.setReference_id(reference_id);
26087 tejbeer 90
		if (!otp.getEmail().equalsIgnoreCase(email) || !otp.getOtp().equalsIgnoreCase(otp_number)) {
21285 kshitij.so 91
			otpResponse.setMessage("Invalid otp");
92
			otpResponse.setResult(false);
23273 ashik.ali 93
			otpRepository.persist(otp);
21285 kshitij.so 94
			return otpResponse;
95
		}
26087 tejbeer 96
		if (otp.isExpired() || otp.isVerified() || otp.getExpiryTimestamp().isBefore(LocalDateTime.now())) {
21285 kshitij.so 97
			otpResponse.setMessage("OTP expired");
98
			otpResponse.setResult(false);
99
			return otpResponse;
100
		}
26087 tejbeer 101
		if (otp.getTryCount() > 5) {
21285 kshitij.so 102
			otpResponse.setMessage("Maximum try count reached");
103
			otpResponse.setResult(false);
104
			return otpResponse;
105
		}
106
		otp.setExpired(true);
107
		otp.setVerified(true);
23273 ashik.ali 108
		otpRepository.persist(otp);
21285 kshitij.so 109
		otpResponse.setMessage("OTP validated successfully");
110
		otpResponse.setResult(true);
111
		return otpResponse;
112
	}
26087 tejbeer 113
 
114
	private void sendOtp(String otp_text, String phone) {
21499 kshitij.so 115
		String msg = java.text.MessageFormat.format(text, otp_text);
116
		try {
117
			Utils.sendSms(msg, phone);
26087 tejbeer 118
		} catch (Exception e) {
21499 kshitij.so 119
			e.printStackTrace();
120
		}
26087 tejbeer 121
 
21499 kshitij.so 122
	}
21285 kshitij.so 123
 
124
}