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