Subversion Repositories SmartDukaan

Rev

Rev 21740 | Rev 22906 | 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;
21702 ashik.ali 12
import org.springframework.transaction.annotation.Transactional;
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;
21740 ashik.ali 19
import com.spice.profitmandi.common.model.ProfitMandiResponse;
20
import com.spice.profitmandi.common.model.ResponseStatus;
21285 kshitij.so 21
import com.spice.profitmandi.common.util.StringUtils;
21740 ashik.ali 22
import com.spice.profitmandi.common.web.util.ResponseSender;
21735 ashik.ali 23
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;
21285 kshitij.so 26
import com.spice.profitmandi.web.processor.OtpProcessor;
27
import com.spice.profitmandi.web.res.OTPResponse;
28
 
29
import io.swagger.annotations.ApiImplicitParam;
30
import io.swagger.annotations.ApiImplicitParams;
31
import io.swagger.annotations.ApiOperation;
32
 
33
@Controller
22037 amit.gupta 34
@Transactional(rollbackFor=Throwable.class)
21285 kshitij.so 35
public class OTPController {
36
 
37
	private static final Logger logger=LoggerFactory.getLogger(OTPController.class);
21440 ashik.ali 38
 
21285 kshitij.so 39
	@Autowired
21440 ashik.ali 40
	ResponseSender<?> responseSender;
41
 
42
	@Autowired
21285 kshitij.so 43
	OtpRepository otpRepositoty;
44
	@Autowired
45
	UserRepository userRepositoty;
46
	@Autowired
47
	OtpProcessor otpProcessor;
48
 
49
	@RequestMapping(value = ProfitMandiConstants.URL_GENERATE_OTP, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
50
	@ApiImplicitParams({
51
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
52
				required = true, dataType = "string", paramType = "header")
53
	})
54
	@ApiOperation(value = "Generate OTP")
55
	public ResponseEntity<?> generateOtp(@RequestParam("email") String email, @RequestParam("phone") String phone){
56
		logger.info("Email : "+email+" Phone : "+phone);
57
		OTPResponse otpResponse;
58
 
59
		if (!StringUtils.isValidEmailAddress(email)){
60
			otpResponse = new OTPResponse();
61
			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);
64
		}
65
 
66
		if (!StringUtils.isValidMobile(phone)){
67
			otpResponse = new OTPResponse();
68
			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);
71
		}
72
 
73
		if (userRepositoty.isExistByEmailId(email) || userRepositoty.isExistByMobileNumber(phone)){
74
			otpResponse = new OTPResponse();
75
			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);
78
		}
79
 
80
		try {
81
			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 | ProfitMandiBusinessException e) {
85
			logger.error("Error while generating otp ",e);
86
			otpResponse = new OTPResponse();
21298 kshitij.so 87
			otpResponse.setMessage("Unable to generate OTP, Please try again");
21285 kshitij.so 88
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_GENERATE_OTP , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, otpResponse);
89
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
90
		}
91
	}
92
 
93
	@RequestMapping(value = ProfitMandiConstants.URL_VERIFY_OTP, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
21287 kshitij.so 94
	@ApiImplicitParams({
95
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
96
				required = true, dataType = "string", paramType = "header")
97
	})
21394 kshitij.so 98
	@ApiOperation(value = "Validate OTP")
21472 amit.gupta 99
	public ResponseEntity<?> validateOtp(@RequestParam("email") String email, @RequestParam("reference_id") int reference_id, @RequestParam("otp_number") String otp_number){
21285 kshitij.so 100
		logger.info("Email : "+email+" Refference_id : "+reference_id);
101
		//TODO validate email & phone from utility method
102
		OTPResponse otpResponse;
103
		try {
104
			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 | ProfitMandiBusinessException e) {
108
			logger.error("Error while generating otp ",e);
109
			otpResponse = new OTPResponse();
21298 kshitij.so 110
			otpResponse.setMessage("Unable to verify OTP, please try again");
21285 kshitij.so 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
	}
21382 amit.gupta 115
	@ApiOperation(value = "Parse OTP")
21475 amit.gupta 116
	@ApiImplicitParams({
117
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
118
				required = true, dataType = "string", paramType = "header")
119
	})
120
	@RequestMapping(value = ProfitMandiConstants.URL_PARSE_OTP, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
21495 amit.gupta 121
	public ResponseEntity<?> parseOTP (@RequestParam(name="message") String message) {
21538 amit.gupta 122
		logger.info("message {}", message	);
21476 amit.gupta 123
		String numberOnly= message.replaceAll("[^0-9]", "");
124
		if(numberOnly.length() !=5) {
21483 amit.gupta 125
			return responseSender.badRequest(new ProfitMandiBusinessException(null, null, ""));
21476 amit.gupta 126
		}
127
		return responseSender.ok(numberOnly);
21382 amit.gupta 128
	}
21285 kshitij.so 129
 
130
 
131
}