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