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