Subversion Repositories SmartDukaan

Rev

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

package com.spice.profitmandi.web.controller;

import java.time.LocalDateTime;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
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.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.model.ProfitMandiResponse;
import com.spice.profitmandi.common.model.ResponseStatus;
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.OtpRepository;
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
public class OTPController {

        private static final Logger logger=LoggerFactory.getLogger(OTPController.class);
        
        @Autowired
        ResponseSender<?> responseSender;
        
        @Autowired
        OtpRepository otpRepositoty;
        @Autowired
        UserRepository userRepositoty;
        @Autowired
        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){
                logger.info("Email : "+email+" Phone : "+phone);
                OTPResponse otpResponse;
                
                if (!StringUtils.isValidEmailAddress(email)){
                        otpResponse = new OTPResponse();
                        otpResponse.setMessage("Illegal email address");
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_GENERATE_OTP , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, otpResponse);
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
                }
                
                if (!StringUtils.isValidMobile(phone)){
                        otpResponse = new OTPResponse();
                        otpResponse.setMessage("Illegal mobile number");
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_GENERATE_OTP , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, otpResponse);
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
                }
                
                if (userRepositoty.isExistByEmailId(email) || userRepositoty.isExistByMobileNumber(phone)){
                        otpResponse = new OTPResponse();
                        otpResponse.setMessage("User with email or mobile already exists");
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_GENERATE_OTP , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, otpResponse);
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
                }
                
                try {
                        otpResponse = otpProcessor.generateOtp(email, phone, OtpType.REGISTRATION);
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_GENERATE_OTP , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, otpResponse);
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
                } catch (Exception | ProfitMandiBusinessException e) {
                        logger.error("Error while generating otp ",e);
                        otpResponse = new OTPResponse();
                        otpResponse.setMessage("Unable to generate OTP, Please try again");
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_GENERATE_OTP , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, otpResponse);
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
                }
        }

        @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){
                logger.info("Email : "+email+" Refference_id : "+reference_id);
                //TODO validate email & phone from utility method
                OTPResponse otpResponse;
                try {
                        otpResponse = otpProcessor.validateOtp(email, reference_id, otp_number);
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_VERIFY_OTP , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, otpResponse);
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
                } catch (Exception | ProfitMandiBusinessException e) {
                        logger.error("Error while generating otp ",e);
                        otpResponse = new OTPResponse();
                        otpResponse.setMessage("Unable to verify OTP, please try again");
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_VERIFY_OTP , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, otpResponse);
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
                }
        }
        @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(new ProfitMandiBusinessException(null, null, ""));
                }
                return responseSender.ok(numberOnly);
        }


}