Subversion Repositories SmartDukaan

Rev

Rev 23204 | Rev 26783 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.util.StringUtils;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.dao.enumuration.dtr.OtpType;
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
import com.spice.profitmandi.web.processor.OtpProcessor;
import com.spice.profitmandi.web.res.OTPResponse;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Controller
@Transactional(rollbackFor=Throwable.class)
public class OTPController {

        private static final Logger logger=LogManager.getLogger(OTPController.class);
        
        @Autowired
        private ResponseSender<?> responseSender;
        
        @Autowired
        private UserRepository userRepositoty;
        
        @Autowired
        private OtpProcessor otpProcessor;

        @RequestMapping(value = ProfitMandiConstants.URL_GENERATE_OTP, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
                                required = true, dataType = "string", paramType = "header")
        })
        @ApiOperation(value = "Generate OTP")
        public ResponseEntity<?> generateOtp(@RequestParam("email") String email, @RequestParam("phone") String phone)
                throws Exception{
                logger.info("Email : "+email+" Phone : "+phone);
                OTPResponse otpResponse;
                
                if (!StringUtils.isValidEmailAddress(email)){
                        otpResponse = new OTPResponse();
                        otpResponse.setMessage("Illegal email address");
                        return responseSender.badRequest(otpResponse);
                }
                
                if (!StringUtils.isValidMobile(phone)){
                        otpResponse = new OTPResponse();
                        otpResponse.setMessage("Illegal mobile number");
                        return responseSender.badRequest(otpResponse);
                }
                
                if (userRepositoty.isExistByEmailId(email) || userRepositoty.isExistByMobileNumber(phone) || userRepositoty.isExistBySecondryEmailId(email)){
                        otpResponse = new OTPResponse();
                        otpResponse.setMessage("User with email or mobile already exists");
                        return responseSender.badRequest(otpResponse);
                }
                
                otpResponse = otpProcessor.generateOtp(email, phone, OtpType.REGISTRATION);
                return responseSender.ok(otpResponse);
                
        }

        @RequestMapping(value = ProfitMandiConstants.URL_VERIFY_OTP, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
                                required = true, dataType = "string", paramType = "header")
        })
        @ApiOperation(value = "Validate OTP")
        public ResponseEntity<?> validateOtp(@RequestParam("email") String email, @RequestParam("reference_id") int reference_id, @RequestParam("otp_number") String otp_number)
                throws Exception{
                logger.info("Email : "+email+" Refference_id : "+reference_id);
                //TODO validate email & phone from utility method
                OTPResponse otpResponse;
                otpResponse = otpProcessor.validateOtp(email, reference_id, otp_number);
                return responseSender.ok(otpResponse);
        }
        @ApiOperation(value = "Parse OTP")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
                                required = true, dataType = "string", paramType = "header")
        })
        @RequestMapping(value = ProfitMandiConstants.URL_PARSE_OTP, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
        public ResponseEntity<?> parseOTP (@RequestParam(name="message") String message) {
                logger.info("message {}", message       );
                String numberOnly= message.replaceAll("[^0-9]", "");
                if(numberOnly.length() !=5) {
                        return responseSender.badRequest("");
                }
                return responseSender.ok(numberOnly);
        }


}