Subversion Repositories SmartDukaan

Rev

Rev 21440 | Rev 21475 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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