Subversion Repositories SmartDukaan

Rev

Rev 32721 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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.service.OtpProcessor;
import com.spice.profitmandi.dao.service.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 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("mobile") String mobile) throws Exception {
                OTPResponse otpResponse;
                if (!StringUtils.isValidMobile(mobile)) {
                        otpResponse = new OTPResponse();
                        otpResponse.setMessage("Illegal mobile number");
                        return responseSender.badRequest(otpResponse);
                }

                otpResponse = otpProcessor.generateOtp(mobile, 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 String mobile, @RequestParam int referenceId, @RequestParam String otpCode)
                        throws Exception {
                logger.info("mobile - {}", mobile);
                logger.info("referenceId - {}", referenceId);
                logger.info("otpCode - {}", otpCode);
                // TODO validate email & phone from utility method
                OTPResponse otpResponse = otpProcessor.validateOtp(referenceId, mobile, otpCode);
                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);
        }

}