Subversion Repositories SmartDukaan

Rev

Rev 23204 | Rev 26783 | 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
 
23568 govind 3
import org.apache.logging.log4j.Logger;
4
import org.apache.logging.log4j.LogManager;
21285 kshitij.so 5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.http.MediaType;
7
import org.springframework.http.ResponseEntity;
8
import org.springframework.stereotype.Controller;
21702 ashik.ali 9
import org.springframework.transaction.annotation.Transactional;
21285 kshitij.so 10
import org.springframework.web.bind.annotation.RequestMapping;
11
import org.springframework.web.bind.annotation.RequestMethod;
12
import org.springframework.web.bind.annotation.RequestParam;
13
 
14
import com.spice.profitmandi.common.model.ProfitMandiConstants;
15
import com.spice.profitmandi.common.util.StringUtils;
21740 ashik.ali 16
import com.spice.profitmandi.common.web.util.ResponseSender;
21735 ashik.ali 17
import com.spice.profitmandi.dao.enumuration.dtr.OtpType;
18
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
21285 kshitij.so 19
import com.spice.profitmandi.web.processor.OtpProcessor;
20
import com.spice.profitmandi.web.res.OTPResponse;
21
 
22
import io.swagger.annotations.ApiImplicitParam;
23
import io.swagger.annotations.ApiImplicitParams;
24
import io.swagger.annotations.ApiOperation;
25
 
26
@Controller
22037 amit.gupta 27
@Transactional(rollbackFor=Throwable.class)
21285 kshitij.so 28
public class OTPController {
29
 
23568 govind 30
	private static final Logger logger=LogManager.getLogger(OTPController.class);
21440 ashik.ali 31
 
21285 kshitij.so 32
	@Autowired
22931 ashik.ali 33
	private ResponseSender<?> responseSender;
21440 ashik.ali 34
 
35
	@Autowired
22931 ashik.ali 36
	private UserRepository userRepositoty;
37
 
21285 kshitij.so 38
	@Autowired
22931 ashik.ali 39
	private OtpProcessor otpProcessor;
21285 kshitij.so 40
 
41
	@RequestMapping(value = ProfitMandiConstants.URL_GENERATE_OTP, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
42
	@ApiImplicitParams({
43
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
44
				required = true, dataType = "string", paramType = "header")
45
	})
46
	@ApiOperation(value = "Generate OTP")
22931 ashik.ali 47
	public ResponseEntity<?> generateOtp(@RequestParam("email") String email, @RequestParam("phone") String phone)
48
		throws Exception{
21285 kshitij.so 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");
22931 ashik.ali 55
			return responseSender.badRequest(otpResponse);
21285 kshitij.so 56
		}
57
 
58
		if (!StringUtils.isValidMobile(phone)){
59
			otpResponse = new OTPResponse();
60
			otpResponse.setMessage("Illegal mobile number");
22931 ashik.ali 61
			return responseSender.badRequest(otpResponse);
21285 kshitij.so 62
		}
63
 
23204 ashik.ali 64
		if (userRepositoty.isExistByEmailId(email) || userRepositoty.isExistByMobileNumber(phone) || userRepositoty.isExistBySecondryEmailId(email)){
21285 kshitij.so 65
			otpResponse = new OTPResponse();
66
			otpResponse.setMessage("User with email or mobile already exists");
22931 ashik.ali 67
			return responseSender.badRequest(otpResponse);
21285 kshitij.so 68
		}
69
 
22931 ashik.ali 70
		otpResponse = otpProcessor.generateOtp(email, phone, OtpType.REGISTRATION);
71
		return responseSender.ok(otpResponse);
72
 
21285 kshitij.so 73
	}
74
 
75
	@RequestMapping(value = ProfitMandiConstants.URL_VERIFY_OTP, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
21287 kshitij.so 76
	@ApiImplicitParams({
77
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
78
				required = true, dataType = "string", paramType = "header")
79
	})
21394 kshitij.so 80
	@ApiOperation(value = "Validate OTP")
22931 ashik.ali 81
	public ResponseEntity<?> validateOtp(@RequestParam("email") String email, @RequestParam("reference_id") int reference_id, @RequestParam("otp_number") String otp_number)
82
		throws Exception{
21285 kshitij.so 83
		logger.info("Email : "+email+" Refference_id : "+reference_id);
84
		//TODO validate email & phone from utility method
85
		OTPResponse otpResponse;
22931 ashik.ali 86
		otpResponse = otpProcessor.validateOtp(email, reference_id, otp_number);
87
		return responseSender.ok(otpResponse);
21285 kshitij.so 88
	}
21382 amit.gupta 89
	@ApiOperation(value = "Parse OTP")
21475 amit.gupta 90
	@ApiImplicitParams({
91
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
92
				required = true, dataType = "string", paramType = "header")
93
	})
94
	@RequestMapping(value = ProfitMandiConstants.URL_PARSE_OTP, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
21495 amit.gupta 95
	public ResponseEntity<?> parseOTP (@RequestParam(name="message") String message) {
21538 amit.gupta 96
		logger.info("message {}", message	);
21476 amit.gupta 97
		String numberOnly= message.replaceAll("[^0-9]", "");
98
		if(numberOnly.length() !=5) {
22931 ashik.ali 99
			return responseSender.badRequest("");
21476 amit.gupta 100
		}
101
		return responseSender.ok(numberOnly);
21382 amit.gupta 102
	}
21285 kshitij.so 103
 
104
 
105
}