| 21285 |
kshitij.so |
1 |
package com.spice.profitmandi.web.processor;
|
|
|
2 |
|
|
|
3 |
|
|
|
4 |
import java.time.LocalDateTime;
|
|
|
5 |
import java.util.List;
|
|
|
6 |
import java.util.Random;
|
|
|
7 |
|
|
|
8 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
9 |
import org.springframework.stereotype.Component;
|
|
|
10 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
11 |
|
|
|
12 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
13 |
import com.spice.profitmandi.dao.entity.Otp;
|
|
|
14 |
import com.spice.profitmandi.dao.enumuration.OtpType;
|
|
|
15 |
import com.spice.profitmandi.dao.repository.OtpRepository;
|
|
|
16 |
import com.spice.profitmandi.web.res.OTPResponse;
|
|
|
17 |
|
|
|
18 |
@Component
|
|
|
19 |
public class OtpProcessor{
|
|
|
20 |
|
|
|
21 |
private static final int len = 5;
|
|
|
22 |
private static final String numbers ="0123456789";
|
|
|
23 |
|
|
|
24 |
@Autowired
|
|
|
25 |
OtpRepository otpRepository;
|
|
|
26 |
|
|
|
27 |
private String getOtp(){
|
|
|
28 |
Random rndm_method = new Random();
|
|
|
29 |
char[] otp = new char[len];
|
|
|
30 |
|
|
|
31 |
for (int i = 0; i < len; i++)
|
|
|
32 |
{
|
|
|
33 |
otp[i] = numbers.charAt(rndm_method.nextInt(numbers.length()));
|
|
|
34 |
}
|
|
|
35 |
return String.valueOf(otp);
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
public OTPResponse generateOtp(String email, String phone, OtpType otpType) throws Exception, ProfitMandiBusinessException{
|
|
|
39 |
OTPResponse otpResponse = new OTPResponse();
|
|
|
40 |
List<Otp> otps = otpRepository.getGeneratedOtpForCredentials(email, otpType);
|
|
|
41 |
String otp = null;
|
|
|
42 |
if (otps.size() >=5){
|
|
|
43 |
otpResponse.setReference_id(0);
|
|
|
44 |
otpResponse.setResult(false);
|
|
|
45 |
otpResponse.setMessage("Maximum limit reached for the day");
|
|
|
46 |
return otpResponse;
|
|
|
47 |
}
|
|
|
48 |
if (!otps.isEmpty()){
|
|
|
49 |
if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(2))){
|
|
|
50 |
otpResponse.setMessage("OTP generated less than 2 minutes ago");
|
|
|
51 |
otpResponse.setReference_id(otps.get(0).getId());
|
|
|
52 |
otpResponse.setResult(true);
|
| 21299 |
kshitij.so |
53 |
otpResponse.setOtp(otps.get(0).getOtp());
|
| 21285 |
kshitij.so |
54 |
return otpResponse;
|
|
|
55 |
}
|
|
|
56 |
else if(otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(10))){
|
|
|
57 |
otp = otps.get(0).getOtp();
|
|
|
58 |
}
|
|
|
59 |
else{
|
|
|
60 |
;
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
if (otp == null || otp.isEmpty()){
|
|
|
64 |
otp = getOtp();
|
|
|
65 |
}
|
|
|
66 |
Otp otp_d = otpRepository.generateOtp(email, phone, otpType, otp);
|
|
|
67 |
otpResponse.setReference_id(otp_d.getId());
|
|
|
68 |
otpResponse.setMessage("OTP generated successfully");
|
|
|
69 |
otpResponse.setResult(true);
|
| 21299 |
kshitij.so |
70 |
otpResponse.setOtp(otp_d.getOtp());
|
| 21285 |
kshitij.so |
71 |
return otpResponse;
|
|
|
72 |
}
|
|
|
73 |
|
| 21472 |
amit.gupta |
74 |
public OTPResponse validateOtp(String email, int reference_id, String otp_number) throws Exception, ProfitMandiBusinessException{
|
| 21285 |
kshitij.so |
75 |
OTPResponse otpResponse = new OTPResponse();
|
|
|
76 |
Otp otp = otpRepository.getById(reference_id);
|
|
|
77 |
otp.setTryCount(otp.getTryCount()+1);
|
|
|
78 |
otpResponse.setReference_id(reference_id);
|
|
|
79 |
if (!otp.getEmail().equalsIgnoreCase(email) || !otp.getOtp().equalsIgnoreCase(otp_number)){
|
|
|
80 |
otpResponse.setMessage("Invalid otp");
|
|
|
81 |
otpResponse.setResult(false);
|
|
|
82 |
otpRepository.updateById(otp);
|
|
|
83 |
return otpResponse;
|
|
|
84 |
}
|
|
|
85 |
if (otp.isExpired() || otp.isVerified() || otp.getExpiryTimestamp().isBefore(LocalDateTime.now())){
|
|
|
86 |
otpResponse.setMessage("OTP expired");
|
|
|
87 |
otpResponse.setResult(false);
|
|
|
88 |
return otpResponse;
|
|
|
89 |
}
|
|
|
90 |
if (otp.getTryCount() >5){
|
|
|
91 |
otpResponse.setMessage("Maximum try count reached");
|
|
|
92 |
otpResponse.setResult(false);
|
|
|
93 |
return otpResponse;
|
|
|
94 |
}
|
|
|
95 |
otp.setExpired(true);
|
|
|
96 |
otp.setVerified(true);
|
|
|
97 |
otpRepository.updateById(otp);
|
|
|
98 |
otpResponse.setMessage("OTP validated successfully");
|
|
|
99 |
otpResponse.setResult(true);
|
|
|
100 |
return otpResponse;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
}
|