Subversion Repositories SmartDukaan

Rev

Rev 21379 | Rev 21394 | 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;
26
 
27
import io.swagger.annotations.ApiImplicitParam;
28
import io.swagger.annotations.ApiImplicitParams;
29
import io.swagger.annotations.ApiOperation;
30
 
31
@Controller
32
public class OTPController {
33
 
34
	private static final Logger logger=LoggerFactory.getLogger(OTPController.class);
35
	@Autowired
36
	OtpRepository otpRepositoty;
37
	@Autowired
38
	UserRepository userRepositoty;
39
	@Autowired
40
	OtpProcessor otpProcessor;
41
 
42
	@RequestMapping(value = ProfitMandiConstants.URL_GENERATE_OTP, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
43
	@ApiImplicitParams({
44
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
45
				required = true, dataType = "string", paramType = "header")
46
	})
47
	@ApiOperation(value = "Generate OTP")
48
	public ResponseEntity<?> generateOtp(@RequestParam("email") String email, @RequestParam("phone") String phone){
49
		logger.info("Email : "+email+" Phone : "+phone);
50
		OTPResponse otpResponse;
51
 
52
		if (!StringUtils.isValidEmailAddress(email)){
53
			otpResponse = new OTPResponse();
54
			otpResponse.setMessage("Illegal email address");
55
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_GENERATE_OTP , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, otpResponse);
56
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
57
		}
58
 
59
		if (!StringUtils.isValidMobile(phone)){
60
			otpResponse = new OTPResponse();
61
			otpResponse.setMessage("Illegal mobile number");
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);
64
		}
65
 
66
		if (userRepositoty.isExistByEmailId(email) || userRepositoty.isExistByMobileNumber(phone)){
67
			otpResponse = new OTPResponse();
68
			otpResponse.setMessage("User with email or mobile already exists");
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);
71
		}
72
 
73
		try {
74
			otpResponse = otpProcessor.generateOtp(email, phone, OtpType.REGISTRATION);
75
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_GENERATE_OTP , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, otpResponse);
76
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
77
		} catch (Exception | ProfitMandiBusinessException e) {
78
			logger.error("Error while generating otp ",e);
79
			otpResponse = new OTPResponse();
21298 kshitij.so 80
			otpResponse.setMessage("Unable to generate OTP, Please try again");
21285 kshitij.so 81
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_GENERATE_OTP , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, otpResponse);
82
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
83
		}
84
	}
85
 
86
	@RequestMapping(value = ProfitMandiConstants.URL_VERIFY_OTP, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
21287 kshitij.so 87
	@ApiImplicitParams({
88
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
89
				required = true, dataType = "string", paramType = "header")
90
	})
21285 kshitij.so 91
	@ApiOperation(value = "Generate OTP")
92
	public ResponseEntity<?> validateOtp(@RequestParam("email") String email, @RequestParam("reference_id") long reference_id, @RequestParam("otp_number") String otp_number){
93
		logger.info("Email : "+email+" Refference_id : "+reference_id);
94
		//TODO validate email & phone from utility method
95
		OTPResponse otpResponse;
96
		try {
97
			otpResponse = otpProcessor.validateOtp(email, reference_id, otp_number);
98
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_VERIFY_OTP , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, otpResponse);
99
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
100
		} catch (Exception | ProfitMandiBusinessException e) {
101
			logger.error("Error while generating otp ",e);
102
			otpResponse = new OTPResponse();
21298 kshitij.so 103
			otpResponse.setMessage("Unable to verify OTP, please try again");
21285 kshitij.so 104
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_VERIFY_OTP , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, otpResponse);
105
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
106
		}
107
	}
21382 amit.gupta 108
	@ApiOperation(value = "Parse OTP")
109
	@RequestMapping(value = ProfitMandiConstants.URL_PARSE_OTP, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
110
	public ResponseEntity<?> parseOTP (@RequestParam("message") String message) {
111
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(ProfitMandiConstants.URL_PARSE_OTP, "322332");
112
		return new ResponseEntity<>(profitMandiResponse, HttpStatus.OK);
113
	}
21285 kshitij.so 114
 
115
 
116
}