Subversion Repositories SmartDukaan

Rev

Rev 22906 | Rev 23204 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 22906 Rev 22931
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
3
import java.time.LocalDateTime;
-
 
4
 
-
 
5
import org.slf4j.Logger;
3
import org.slf4j.Logger;
6
import org.slf4j.LoggerFactory;
4
import org.slf4j.LoggerFactory;
7
import org.springframework.beans.factory.annotation.Autowired;
5
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.http.HttpStatus;
-
 
9
import org.springframework.http.MediaType;
6
import org.springframework.http.MediaType;
10
import org.springframework.http.ResponseEntity;
7
import org.springframework.http.ResponseEntity;
11
import org.springframework.stereotype.Controller;
8
import org.springframework.stereotype.Controller;
12
import org.springframework.transaction.annotation.Transactional;
9
import org.springframework.transaction.annotation.Transactional;
13
import org.springframework.web.bind.annotation.RequestMapping;
10
import org.springframework.web.bind.annotation.RequestMapping;
14
import org.springframework.web.bind.annotation.RequestMethod;
11
import org.springframework.web.bind.annotation.RequestMethod;
15
import org.springframework.web.bind.annotation.RequestParam;
12
import org.springframework.web.bind.annotation.RequestParam;
16
 
13
 
17
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
-
 
18
import com.spice.profitmandi.common.model.ProfitMandiConstants;
14
import com.spice.profitmandi.common.model.ProfitMandiConstants;
19
import com.spice.profitmandi.common.model.ProfitMandiResponse;
-
 
20
import com.spice.profitmandi.common.model.ResponseStatus;
-
 
21
import com.spice.profitmandi.common.util.StringUtils;
15
import com.spice.profitmandi.common.util.StringUtils;
22
import com.spice.profitmandi.common.web.util.ResponseSender;
16
import com.spice.profitmandi.common.web.util.ResponseSender;
23
import com.spice.profitmandi.dao.enumuration.dtr.OtpType;
17
import com.spice.profitmandi.dao.enumuration.dtr.OtpType;
24
import com.spice.profitmandi.dao.repository.dtr.OtpRepository;
-
 
25
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
18
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
26
import com.spice.profitmandi.web.processor.OtpProcessor;
19
import com.spice.profitmandi.web.processor.OtpProcessor;
27
import com.spice.profitmandi.web.res.OTPResponse;
20
import com.spice.profitmandi.web.res.OTPResponse;
28
 
21
 
29
import io.swagger.annotations.ApiImplicitParam;
22
import io.swagger.annotations.ApiImplicitParam;
Line 35... Line 28...
35
public class OTPController {
28
public class OTPController {
36
 
29
 
37
	private static final Logger logger=LoggerFactory.getLogger(OTPController.class);
30
	private static final Logger logger=LoggerFactory.getLogger(OTPController.class);
38
	
31
	
39
	@Autowired
32
	@Autowired
40
	ResponseSender<?> responseSender;
33
	private ResponseSender<?> responseSender;
41
	
34
	
42
	@Autowired
35
	@Autowired
43
	OtpRepository otpRepositoty;
36
	private UserRepository userRepositoty;
44
	@Autowired
37
	
45
	UserRepository userRepositoty;
-
 
46
	@Autowired
38
	@Autowired
47
	OtpProcessor otpProcessor;
39
	private OtpProcessor otpProcessor;
48
 
40
 
49
	@RequestMapping(value = ProfitMandiConstants.URL_GENERATE_OTP, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
41
	@RequestMapping(value = ProfitMandiConstants.URL_GENERATE_OTP, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
50
	@ApiImplicitParams({
42
	@ApiImplicitParams({
51
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
43
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
52
				required = true, dataType = "string", paramType = "header")
44
				required = true, dataType = "string", paramType = "header")
53
	})
45
	})
54
	@ApiOperation(value = "Generate OTP")
46
	@ApiOperation(value = "Generate OTP")
55
	public ResponseEntity<?> generateOtp(@RequestParam("email") String email, @RequestParam("phone") String phone){
47
	public ResponseEntity<?> generateOtp(@RequestParam("email") String email, @RequestParam("phone") String phone)
-
 
48
		throws Exception{
56
		logger.info("Email : "+email+" Phone : "+phone);
49
		logger.info("Email : "+email+" Phone : "+phone);
57
		OTPResponse otpResponse;
50
		OTPResponse otpResponse;
58
		
51
		
59
		if (!StringUtils.isValidEmailAddress(email)){
52
		if (!StringUtils.isValidEmailAddress(email)){
60
			otpResponse = new OTPResponse();
53
			otpResponse = new OTPResponse();
61
			otpResponse.setMessage("Illegal email address");
54
			otpResponse.setMessage("Illegal email address");
62
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_GENERATE_OTP , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, otpResponse);
-
 
63
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
55
			return responseSender.badRequest(otpResponse);
64
		}
56
		}
65
		
57
		
66
		if (!StringUtils.isValidMobile(phone)){
58
		if (!StringUtils.isValidMobile(phone)){
67
			otpResponse = new OTPResponse();
59
			otpResponse = new OTPResponse();
68
			otpResponse.setMessage("Illegal mobile number");
60
			otpResponse.setMessage("Illegal mobile number");
69
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_GENERATE_OTP , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, otpResponse);
-
 
70
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
61
			return responseSender.badRequest(otpResponse);
71
		}
62
		}
72
		
63
		
73
		if (userRepositoty.isExistByEmailId(email) || userRepositoty.isExistByMobileNumber(phone)){
64
		if (userRepositoty.isExistByEmailId(email) || userRepositoty.isExistByMobileNumber(phone)){
74
			otpResponse = new OTPResponse();
65
			otpResponse = new OTPResponse();
75
			otpResponse.setMessage("User with email or mobile already exists");
66
			otpResponse.setMessage("User with email or mobile already exists");
76
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_GENERATE_OTP , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, otpResponse);
-
 
77
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
67
			return responseSender.badRequest(otpResponse);
78
		}
68
		}
79
		
69
		
80
		try {
-
 
81
			otpResponse = otpProcessor.generateOtp(email, phone, OtpType.REGISTRATION);
70
		otpResponse = otpProcessor.generateOtp(email, phone, OtpType.REGISTRATION);
82
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_GENERATE_OTP , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, otpResponse);
-
 
83
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
-
 
84
		} catch (Exception e) {
-
 
85
			logger.error("Error while generating otp ",e);
-
 
86
			otpResponse = new OTPResponse();
-
 
87
			otpResponse.setMessage("Unable to generate OTP, Please try again");
-
 
88
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_GENERATE_OTP , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, otpResponse);
71
		return responseSender.ok(otpResponse);
89
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
-
 
90
		}
72
		
91
	}
73
	}
92
 
74
 
93
	@RequestMapping(value = ProfitMandiConstants.URL_VERIFY_OTP, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
75
	@RequestMapping(value = ProfitMandiConstants.URL_VERIFY_OTP, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
94
	@ApiImplicitParams({
76
	@ApiImplicitParams({
95
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
77
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
96
				required = true, dataType = "string", paramType = "header")
78
				required = true, dataType = "string", paramType = "header")
97
	})
79
	})
98
	@ApiOperation(value = "Validate OTP")
80
	@ApiOperation(value = "Validate OTP")
99
	public ResponseEntity<?> validateOtp(@RequestParam("email") String email, @RequestParam("reference_id") int reference_id, @RequestParam("otp_number") String otp_number){
81
	public ResponseEntity<?> validateOtp(@RequestParam("email") String email, @RequestParam("reference_id") int reference_id, @RequestParam("otp_number") String otp_number)
-
 
82
		throws Exception{
100
		logger.info("Email : "+email+" Refference_id : "+reference_id);
83
		logger.info("Email : "+email+" Refference_id : "+reference_id);
101
		//TODO validate email & phone from utility method
84
		//TODO validate email & phone from utility method
102
		OTPResponse otpResponse;
85
		OTPResponse otpResponse;
103
		try {
-
 
104
			otpResponse = otpProcessor.validateOtp(email, reference_id, otp_number);
86
		otpResponse = otpProcessor.validateOtp(email, reference_id, otp_number);
105
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_VERIFY_OTP , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, otpResponse);
-
 
106
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
-
 
107
		} catch (Exception e) {
-
 
108
			logger.error("Error while generating otp ",e);
-
 
109
			otpResponse = new OTPResponse();
87
		return responseSender.ok(otpResponse);
110
			otpResponse.setMessage("Unable to verify OTP, please try again");
-
 
111
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_VERIFY_OTP , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, otpResponse);
-
 
112
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
-
 
113
		}
-
 
114
	}
88
	}
115
	@ApiOperation(value = "Parse OTP")
89
	@ApiOperation(value = "Parse OTP")
116
	@ApiImplicitParams({
90
	@ApiImplicitParams({
117
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
91
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
118
				required = true, dataType = "string", paramType = "header")
92
				required = true, dataType = "string", paramType = "header")
Line 120... Line 94...
120
	@RequestMapping(value = ProfitMandiConstants.URL_PARSE_OTP, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
94
	@RequestMapping(value = ProfitMandiConstants.URL_PARSE_OTP, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
121
	public ResponseEntity<?> parseOTP (@RequestParam(name="message") String message) {
95
	public ResponseEntity<?> parseOTP (@RequestParam(name="message") String message) {
122
		logger.info("message {}", message	);
96
		logger.info("message {}", message	);
123
		String numberOnly= message.replaceAll("[^0-9]", "");
97
		String numberOnly= message.replaceAll("[^0-9]", "");
124
		if(numberOnly.length() !=5) {
98
		if(numberOnly.length() !=5) {
125
			return responseSender.badRequest(new ProfitMandiBusinessException(null, null, ""));
99
			return responseSender.badRequest("");
126
		}
100
		}
127
		return responseSender.ok(numberOnly);
101
		return responseSender.ok(numberOnly);
128
	}
102
	}
129
 
103
 
130
 
104