| Line 1... |
Line 1... |
| 1 |
package com.spice.profitmandi.web.controller;
|
1 |
package com.spice.profitmandi.web.controller;
|
| 2 |
|
2 |
|
| 3 |
import org.apache.logging.log4j.Logger;
|
- |
|
| 4 |
import org.apache.logging.log4j.LogManager;
|
3 |
import org.apache.logging.log4j.LogManager;
|
| - |
|
4 |
import org.apache.logging.log4j.Logger;
|
| 5 |
import org.springframework.beans.factory.annotation.Autowired;
|
5 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 6 |
import org.springframework.http.MediaType;
|
6 |
import org.springframework.http.MediaType;
|
| 7 |
import org.springframework.http.ResponseEntity;
|
7 |
import org.springframework.http.ResponseEntity;
|
| 8 |
import org.springframework.stereotype.Controller;
|
8 |
import org.springframework.stereotype.Controller;
|
| 9 |
import org.springframework.transaction.annotation.Transactional;
|
9 |
import org.springframework.transaction.annotation.Transactional;
|
| Line 14... |
Line 14... |
| 14 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
14 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 15 |
import com.spice.profitmandi.common.util.StringUtils;
|
15 |
import com.spice.profitmandi.common.util.StringUtils;
|
| 16 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
16 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
| 17 |
import com.spice.profitmandi.dao.enumuration.dtr.OtpType;
|
17 |
import com.spice.profitmandi.dao.enumuration.dtr.OtpType;
|
| 18 |
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
|
18 |
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
|
| - |
|
19 |
import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;
|
| 19 |
import com.spice.profitmandi.web.processor.OtpProcessor;
|
20 |
import com.spice.profitmandi.web.processor.OtpProcessor;
|
| 20 |
import com.spice.profitmandi.web.res.OTPResponse;
|
21 |
import com.spice.profitmandi.web.res.OTPResponse;
|
| 21 |
|
22 |
|
| 22 |
import io.swagger.annotations.ApiImplicitParam;
|
23 |
import io.swagger.annotations.ApiImplicitParam;
|
| 23 |
import io.swagger.annotations.ApiImplicitParams;
|
24 |
import io.swagger.annotations.ApiImplicitParams;
|
| 24 |
import io.swagger.annotations.ApiOperation;
|
25 |
import io.swagger.annotations.ApiOperation;
|
| 25 |
|
26 |
|
| 26 |
@Controller
|
27 |
@Controller
|
| 27 |
@Transactional(rollbackFor=Throwable.class)
|
28 |
@Transactional(rollbackFor = Throwable.class)
|
| 28 |
public class OTPController {
|
29 |
public class OTPController {
|
| 29 |
|
30 |
|
| 30 |
private static final Logger logger=LogManager.getLogger(OTPController.class);
|
31 |
private static final Logger logger = LogManager.getLogger(OTPController.class);
|
| 31 |
|
32 |
|
| 32 |
@Autowired
|
33 |
@Autowired
|
| 33 |
private ResponseSender<?> responseSender;
|
34 |
private ResponseSender<?> responseSender;
|
| 34 |
|
35 |
|
| 35 |
@Autowired
|
- |
|
| 36 |
private UserRepository userRepositoty;
|
- |
|
| 37 |
|
36 |
|
| 38 |
@Autowired
|
37 |
@Autowired
|
| 39 |
private OtpProcessor otpProcessor;
|
38 |
private OtpProcessor otpProcessor;
|
| 40 |
|
39 |
|
| 41 |
@RequestMapping(value = ProfitMandiConstants.URL_GENERATE_OTP, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
|
40 |
@RequestMapping(value = ProfitMandiConstants.URL_GENERATE_OTP, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
| 42 |
@ApiImplicitParams({
|
41 |
@ApiImplicitParams({
|
| 43 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token",
|
42 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
| 44 |
required = true, dataType = "string", paramType = "header")
|
- |
|
| 45 |
})
|
- |
|
| 46 |
@ApiOperation(value = "Generate OTP")
|
43 |
@ApiOperation(value = "Generate OTP")
|
| 47 |
public ResponseEntity<?> generateOtp(@RequestParam("email") String email, @RequestParam("phone") String phone)
|
44 |
public ResponseEntity<?> generateOtp(@RequestParam("mobile") String mobile) throws Exception {
|
| 48 |
throws Exception{
|
- |
|
| 49 |
logger.info("Email : "+email+" Phone : "+phone);
|
- |
|
| 50 |
OTPResponse otpResponse;
|
45 |
OTPResponse otpResponse;
|
| 51 |
|
- |
|
| 52 |
if (!StringUtils.isValidEmailAddress(email)){
|
- |
|
| 53 |
otpResponse = new OTPResponse();
|
- |
|
| 54 |
otpResponse.setMessage("Illegal email address");
|
- |
|
| 55 |
return responseSender.badRequest(otpResponse);
|
- |
|
| 56 |
}
|
- |
|
| 57 |
|
- |
|
| 58 |
if (!StringUtils.isValidMobile(phone)){
|
46 |
if (!StringUtils.isValidMobile(mobile)) {
|
| 59 |
otpResponse = new OTPResponse();
|
47 |
otpResponse = new OTPResponse();
|
| 60 |
otpResponse.setMessage("Illegal mobile number");
|
48 |
otpResponse.setMessage("Illegal mobile number");
|
| 61 |
return responseSender.badRequest(otpResponse);
|
49 |
return responseSender.badRequest(otpResponse);
|
| 62 |
}
|
50 |
}
|
| 63 |
|
- |
|
| 64 |
if (userRepositoty.isExistByEmailId(email) || userRepositoty.isExistByMobileNumber(phone) || userRepositoty.isExistBySecondryEmailId(email)){
|
- |
|
| 65 |
otpResponse = new OTPResponse();
|
- |
|
| 66 |
otpResponse.setMessage("User with email or mobile already exists");
|
- |
|
| 67 |
return responseSender.badRequest(otpResponse);
|
- |
|
| 68 |
}
|
- |
|
| 69 |
|
51 |
|
| 70 |
otpResponse = otpProcessor.generateOtp(email, phone, OtpType.REGISTRATION);
|
52 |
otpResponse = otpProcessor.generateOtp(mobile, OtpType.REGISTRATION);
|
| 71 |
return responseSender.ok(otpResponse);
|
53 |
return responseSender.ok(otpResponse);
|
| 72 |
|
54 |
|
| 73 |
}
|
55 |
}
|
| 74 |
|
56 |
|
| 75 |
@RequestMapping(value = ProfitMandiConstants.URL_VERIFY_OTP, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
|
57 |
@RequestMapping(value = ProfitMandiConstants.URL_VERIFY_OTP, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
| 76 |
@ApiImplicitParams({
|
58 |
@ApiImplicitParams({
|
| 77 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token",
|
59 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
| 78 |
required = true, dataType = "string", paramType = "header")
|
- |
|
| 79 |
})
|
- |
|
| 80 |
@ApiOperation(value = "Validate OTP")
|
60 |
@ApiOperation(value = "Validate OTP")
|
| 81 |
public ResponseEntity<?> validateOtp(@RequestParam("email") String email, @RequestParam("reference_id") int reference_id, @RequestParam("otp_number") String otp_number)
|
61 |
public ResponseEntity<?> validateOtp(String mobile, @RequestParam int referenceId, String otpCode)
|
| 82 |
throws Exception{
|
62 |
throws Exception {
|
| 83 |
logger.info("Email : "+email+" Refference_id : "+reference_id);
|
- |
|
| 84 |
//TODO validate email & phone from utility method
|
63 |
// TODO validate email & phone from utility method
|
| 85 |
OTPResponse otpResponse;
|
64 |
OTPResponse otpResponse;
|
| 86 |
otpResponse = otpProcessor.validateOtp(email, reference_id, otp_number);
|
65 |
otpResponse = otpProcessor.validateOtp(referenceId, mobile, otpCode);
|
| 87 |
return responseSender.ok(otpResponse);
|
66 |
return responseSender.ok(otpResponse);
|
| 88 |
}
|
67 |
}
|
| - |
|
68 |
|
| 89 |
@ApiOperation(value = "Parse OTP")
|
69 |
@ApiOperation(value = "Parse OTP")
|
| 90 |
@ApiImplicitParams({
|
70 |
@ApiImplicitParams({
|
| 91 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token",
|
71 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
| 92 |
required = true, dataType = "string", paramType = "header")
|
- |
|
| 93 |
})
|
- |
|
| 94 |
@RequestMapping(value = ProfitMandiConstants.URL_PARSE_OTP, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
|
72 |
@RequestMapping(value = ProfitMandiConstants.URL_PARSE_OTP, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
|
| 95 |
public ResponseEntity<?> parseOTP (@RequestParam(name="message") String message) {
|
73 |
public ResponseEntity<?> parseOTP(@RequestParam(name = "message") String message) {
|
| 96 |
logger.info("message {}", message );
|
74 |
logger.info("message {}", message);
|
| 97 |
String numberOnly= message.replaceAll("[^0-9]", "");
|
75 |
String numberOnly = message.replaceAll("[^0-9]", "");
|
| 98 |
if(numberOnly.length() !=5) {
|
76 |
if (numberOnly.length() != 5) {
|
| 99 |
return responseSender.badRequest("");
|
77 |
return responseSender.badRequest("");
|
| 100 |
}
|
78 |
}
|
| 101 |
return responseSender.ok(numberOnly);
|
79 |
return responseSender.ok(numberOnly);
|
| 102 |
}
|
80 |
}
|
| 103 |
|
81 |
|
| 104 |
|
- |
|
| 105 |
}
|
82 |
}
|