Subversion Repositories SmartDukaan

Rev

Rev 26672 | Blame | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.processor;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.util.Utils;
import com.spice.profitmandi.common.web.client.RestClient;
import com.spice.profitmandi.dao.entity.dtr.Otp;
import com.spice.profitmandi.dao.enumuration.dtr.OtpType;
import com.spice.profitmandi.dao.repository.dtr.OtpRepository;
import com.spice.profitmandi.web.res.OTPResponse;

@Component
public class OtpProcessor {

        private static final String SMS_GATEWAY = "https://bmg.spicedigital.in/SMSGateway/messagePush";
        private static final int len = 5;
        private static final String numbers = "0123456789";
        private static final String text = "Dear Customer, {0} is the OTP that you have requested to login into SmartDukaan. Don't share your OTP with anyone.";

        @Autowired
        OtpRepository otpRepository;
        
        @Autowired JavaMailSender mailSender;
        
        @Value("${prod}")
        private boolean prodEnv;
        
        @Autowired RestClient restClient;


        private String getOtp() {
                Random rndm_method = new Random();
                char[] otp = new char[len];

                for (int i = 0; i < len; i++) {
                        otp[i] = numbers.charAt(rndm_method.nextInt(numbers.length()));
                }
                return String.valueOf(otp);
        }

        public OTPResponse generateOtp(String email, String phone, OtpType otpType)
                        throws Exception, ProfitMandiBusinessException {
                OTPResponse otpResponse = new OTPResponse();
                List<Otp> otps = otpRepository.selectAllByEmailWithTime(email);
                String otp = null;
                if (otps.size() >= 5) {
                        otpResponse.setReference_id(0);
                        otpResponse.setResult(false);
                        otpResponse.setMessage("Maximum limit reached for the day");
                        return otpResponse;
                }
                if (!otps.isEmpty()) {
                        if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(2))) {
                                otpResponse.setMessage("OTP generated less than 2 minutes ago");
                                otpResponse.setReference_id(otps.get(0).getId());
                                otpResponse.setResult(true);
                                otpResponse.setOtp(otps.get(0).getOtp());
                                return otpResponse;
                        } else if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(10))) {
                                otp = otps.get(0).getOtp();
                        } else {
                                ;
                        }
                }
                if (otp == null || otp.isEmpty()) {
                        otp = getOtp();
                }

                Otp otp_bean = new Otp();
                otp_bean.setEmail(email);
                otp_bean.setMobile(phone);
                otp_bean.setOtp(otp);
                otp_bean.setOtpType(otpType);
                otp_bean.setCreatedOn(LocalDateTime.now());
                otp_bean.setExpiryTimestamp(LocalDateTime.now().plusMinutes(10));
                otpRepository.persist(otp_bean);
                if(prodEnv) {
                        sendOtp(otp_bean);
                }

                otpResponse.setReference_id(otp_bean.getId());
                otpResponse.setMessage("OTP generated successfully");
                otpResponse.setResult(true);
                otpResponse.setOtp(otp_bean.getOtp());
                return otpResponse;
        }

        public OTPResponse validateOtp(String email, int reference_id, String otp_number)
                        throws Exception, ProfitMandiBusinessException {
                OTPResponse otpResponse = new OTPResponse();
                Otp otp = otpRepository.selectById(reference_id);
                otp.setTryCount(otp.getTryCount() + 1);
                otpResponse.setReference_id(reference_id);
                if (!otp.getEmail().equalsIgnoreCase(email) || !otp.getOtp().equalsIgnoreCase(otp_number)) {
                        otpResponse.setMessage("Invalid otp");
                        otpResponse.setResult(false);
                        otpRepository.persist(otp);
                        return otpResponse;
                }
                if (otp.isExpired() || otp.isVerified() || otp.getExpiryTimestamp().isBefore(LocalDateTime.now())) {
                        otpResponse.setMessage("OTP expired");
                        otpResponse.setResult(false);
                        return otpResponse;
                }
                if (otp.getTryCount() > 5) {
                        otpResponse.setMessage("Maximum try count reached");
                        otpResponse.setResult(false);
                        return otpResponse;
                }
                otp.setExpired(true);
                otp.setVerified(true);
                otpRepository.persist(otp);
                otpResponse.setMessage("OTP validated successfully");
                otpResponse.setResult(true);
                return otpResponse;
        }

        private void sendOtp(Otp otp) throws Exception {
                String msg = java.text.MessageFormat.format(text, otp.getOtp());
                Utils.sendMailWithAttachments(mailSender, otp.getEmail(), null, "One Time Password - Smartdukaan", msg, null);
                /*try {
                        this.sendSms(msg, otp.getMobile());
                } catch (Exception e) {
                        e.printStackTrace();
                }*/
                

        }
        
        public void sendSms(String text, String mobileNumber) throws Exception {
                Map<String, String> paramsMap = new HashMap<>();
                paramsMap.put("username", "SmartDukaanT");
                paramsMap.put("password", "Smart@91");
                paramsMap.put("senderID", "SMTDKN");
                paramsMap.put("message", text);
                paramsMap.put("mobile", mobileNumber);
                paramsMap.put("messageType", "Text");
                restClient.getResponse(SMS_GATEWAY, paramsMap, null);
        }

}