Subversion Repositories SmartDukaan

Rev

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