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