Subversion Repositories SmartDukaan

Rev

Rev 31828 | Rev 33568 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
33510 tejus.loha 1
package com.spice.profitmandi.dao.service;
21285 kshitij.so 2
 
33510 tejus.loha 3
import com.fasterxml.jackson.annotation.JsonProperty;
4
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
5
import com.spice.profitmandi.common.web.client.RestClient;
6
import com.spice.profitmandi.dao.entity.dtr.Otp;
7
import com.spice.profitmandi.dao.entity.onBoarding.LoiForm;
8
import com.spice.profitmandi.dao.enumuration.dtr.OtpType;
9
import com.spice.profitmandi.dao.repository.dtr.OtpRepository;
10
import com.spice.profitmandi.dao.repository.user.LoiFormRepository;
26712 amit.gupta 11
import org.apache.logging.log4j.LogManager;
12
import org.apache.logging.log4j.Logger;
21285 kshitij.so 13
import org.springframework.beans.factory.annotation.Autowired;
26649 amit.gupta 14
import org.springframework.beans.factory.annotation.Value;
26657 amit.gupta 15
import org.springframework.mail.javamail.JavaMailSender;
21285 kshitij.so 16
import org.springframework.stereotype.Component;
17
 
33510 tejus.loha 18
import java.time.LocalDateTime;
19
import java.util.HashMap;
20
import java.util.List;
21
import java.util.Map;
22
import java.util.Random;
21285 kshitij.so 23
 
24
@Component
26087 tejbeer 25
public class OtpProcessor {
21285 kshitij.so 26
 
33510 tejus.loha 27
    public static final String SELF_CANCELLED_TEMPLATE = "Order Cancelled PO%s: Dear Customer, your order with SmartDukaan of %s has been cancelled on %s as per your request. Team SmartDukaan";
28
    public static final String SELF_CANCELLED_TEMPLATE_ID = "1507161943392646481";
29
    public static final String TEMPLATE_ORDER_CREATED = "Order Placed PO%d: Dear Customer, your order with SmartDukaan of %s worth Rs.%.0f has been placed successfully. Our team will get in touch with you soon. Team SmartDukaan";
30
    public static final String TEMPLATE_ORDER_CREATED_ID = "1507162021138742118";
31
    public static final String LOI_ACCEPTANCE_OTP_TEMPLATE_ID = "1707171687739961361";
32
    public static final String DLT_PRINCIPLE_ENTITY_ID = "1501589330000013091";
33
    public static final String LOI_ACCEPTANCE_OTP_TEMPLATE = "Dear %s,\nThank you for showing your interest in the SmartDukaan franchise.\n%s is the OTP for your LOI acceptance.\nPlease read the LOI and provide your consent by sharing OTP with the SmartDukaan team.\nBest Regards,\nSmartDukaan Team.";
34
    private static final Logger LOGGER = LogManager.getLogger(OtpProcessor.class);
31828 amit.gupta 35
    private static final String SMS_GATEWAY = "http://sms.speqtrainnov.com/api/v4/?api_key=A7897cd4627a8781e176fd31710b057a9";
36
    private static final int len = 5;
37
    private static final String SENDER = "SMTDKN";
38
    private static final String numbers = "0123456789";
39
    private static final String OTP_TEMPLATE = "Dear Customer, %s is the OTP that you have requested to login into SmartDukaan. Don't share your OTP with anyone";
40
    private static final String OTP_TEMPLATE_ID = "1507161889822750240";
33510 tejus.loha 41
    //TODO LOI OTP
42
    private static final String USER = "smartdukaanOTP";
43
    private static final String PASS1 = "Amit@Smart123";
44
    private static String NEXG_API_ENDPOINT = "https://api2.nexgplatforms.com/sms/1/text/query";
26711 amit.gupta 45
 
31828 amit.gupta 46
    @Autowired
47
    OtpRepository otpRepository;
26711 amit.gupta 48
 
31828 amit.gupta 49
    @Autowired
50
    JavaMailSender mailSender;
33510 tejus.loha 51
    @Autowired
52
    RestClient restClient;
53
    @Autowired
54
    LoiFormRepository partnerOnBoardingDataRepository;
31828 amit.gupta 55
    @Value("${prod}")
56
    private boolean prodEnv;
26649 amit.gupta 57
 
31828 amit.gupta 58
    private String getOtp() {
59
        Random rndm_method = new Random();
60
        char[] otp = new char[len];
21285 kshitij.so 61
 
31828 amit.gupta 62
        for (int i = 0; i < len; i++) {
63
            otp[i] = numbers.charAt(rndm_method.nextInt(numbers.length()));
64
        }
65
        return String.valueOf(otp);
66
    }
26087 tejbeer 67
 
31828 amit.gupta 68
    public OTPResponse generateOtp(String mobile, OtpType otpType) throws Exception, ProfitMandiBusinessException {
69
        LOGGER.info("Try generating otp for mobile -- {}", mobile);
70
        OTPResponse otpResponse = new OTPResponse();
71
        List<Otp> otps = otpRepository.selectAllByMobileWithTime(mobile);
72
        String otpCode = null;
33510 tejus.loha 73
        LOGGER.info("Try to size of otps -- {}", otps.size());
31828 amit.gupta 74
        if (otps.size() >= 5) {
75
            otpResponse.setReference_id(0);
76
            otpResponse.setResult(false);
77
            otpResponse.setMessage("Maximum limit reached for the day");
78
            return otpResponse;
79
        }
33510 tejus.loha 80
        LOGGER.info("Try to check otps isEmpty", otps.isEmpty());
31828 amit.gupta 81
        if (!otps.isEmpty()) {
82
            if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(2))) {
83
                otpResponse.setMessage("OTP generated less than 2 minutes ago");
84
                otpResponse.setReference_id(otps.get(0).getId());
85
                otpResponse.setResult(true);
86
                otpResponse.setOtp(otps.get(0).getOtp());
87
                return otpResponse;
88
            } else if (otps.get(0).getCreatedOn().isAfter(LocalDateTime.now().minusMinutes(10))) {
89
                otpCode = otps.get(0).getOtp();
90
            } else {
91
                ;
92
            }
93
        }
33510 tejus.loha 94
        LOGGER.info("Try to check otp null-- {}", otpCode == null);
31828 amit.gupta 95
        if (otpCode == null || otpCode.isEmpty()) {
96
            otpCode = getOtp();
97
        }
26087 tejbeer 98
 
31828 amit.gupta 99
        Otp otp = new Otp();
100
        otp.setMobile(mobile);
101
        otp.setOtp(otpCode);
102
        otp.setOtpType(otpType);
103
        otp.setCreatedOn(LocalDateTime.now());
104
        otp.setExpiryTimestamp(LocalDateTime.now().plusMinutes(10));
105
        otpRepository.persist(otp);
106
        //sendOtp(otp);
33510 tejus.loha 107
        if (otpType.equals(OtpType.REGISTRATION)) {
31828 amit.gupta 108
            sendOtp(otp);
33510 tejus.loha 109
        } else if (otpType.equals(OtpType.LOI_ACCEPTANCE)) {
110
            sendOtpForLoiAcceptance(otp);
31828 amit.gupta 111
        } else {
112
            otpResponse.setOtp(otp.getOtp());
33510 tejus.loha 113
 
31828 amit.gupta 114
        }
26087 tejbeer 115
 
33510 tejus.loha 116
        if (otpType.equals(OtpType.LOI_ACCEPTANCE)) {
117
            LOGGER.info("otp_type-" + otpType);
118
            sendOtpForLoiAcceptance(otp);
119
        }
120
 
31828 amit.gupta 121
        otpResponse.setReference_id(otp.getId());
122
        otpResponse.setMessage("OTP generated successfully");
123
        otpResponse.setResult(true);
124
        return otpResponse;
125
    }
26087 tejbeer 126
 
31828 amit.gupta 127
    public OTPResponse validateOtp(int referenceId, String mobile, String otpCode)
128
            throws Exception, ProfitMandiBusinessException {
33510 tejus.loha 129
        LOGGER.info("validate_call");
31828 amit.gupta 130
        OTPResponse otpResponse = new OTPResponse();
131
        Otp otp = otpRepository.selectById(referenceId);
132
        otp.setTryCount(otp.getTryCount() + 1);
133
        otpResponse.setReference_id(referenceId);
134
        if (!otp.getMobile().equals(mobile) || !otp.getOtp().equalsIgnoreCase(otpCode)) {
135
            otpResponse.setMessage("Invalid otp");
136
            otpResponse.setResult(false);
33510 tejus.loha 137
            LOGGER.info("Invalid otp");
31828 amit.gupta 138
            return otpResponse;
139
        }
140
        if (otp.isExpired() || otp.isVerified() || otp.getExpiryTimestamp().isBefore(LocalDateTime.now())) {
141
            otpResponse.setMessage("OTP expired");
142
            otpResponse.setResult(false);
33510 tejus.loha 143
            LOGGER.info("OTP expired");
31828 amit.gupta 144
            return otpResponse;
145
        }
146
        if (otp.getTryCount() > 5) {
147
            otpResponse.setMessage("Maximum try count reached");
148
            otpResponse.setResult(false);
33510 tejus.loha 149
            LOGGER.info("Maximum try count reached");
31828 amit.gupta 150
            return otpResponse;
151
        }
152
        otp.setExpired(true);
153
        otp.setVerified(true);
154
        otpRepository.persist(otp);
155
        otpResponse.setMessage("OTP validated successfully");
156
        otpResponse.setResult(true);
33510 tejus.loha 157
        LOGGER.info("OTP validated successfully");
31828 amit.gupta 158
        return otpResponse;
159
    }
26711 amit.gupta 160
 
31828 amit.gupta 161
    public void sendOtp(Otp otp) throws Exception {
162
        // In case of Cant receive SMS?
163
        // String mailMessage = java.text.MessageFormat.format(text, otp.getOtp());
164
        String otpString = otp.getOtp();
165
        try {
166
            this.sendSms(OTP_TEMPLATE_ID, String.format(OTP_TEMPLATE, otpString), otp.getMobile());
167
        } catch (Exception e) {
168
            e.printStackTrace();
169
        }
28305 amit.gupta 170
 
31828 amit.gupta 171
    }
26711 amit.gupta 172
 
33510 tejus.loha 173
    //todo
174
    public void sendOtpForLoiAcceptance(Otp otp1) throws Exception {
175
        String otp = otp1.getOtp();
176
        LoiForm pod = partnerOnBoardingDataRepository.selectByEmailOrMobile(otp1.getMobile());
177
        String ownerName = pod.getFirstName();
178
        String message = String.format(LOI_ACCEPTANCE_OTP_TEMPLATE, ownerName, otp);
179
        this.sendSms(LOI_ACCEPTANCE_OTP_TEMPLATE_ID, message, otp1.getMobile());
180
    }
181
 
182
    public String sendSms(String dltTemplateId, String message, String mobileNumber) throws Exception {
31828 amit.gupta 183
        Map<String, String> queryParams = new HashMap<>();
33510 tejus.loha 184
        queryParams.put("from", SENDER);
185
        queryParams.put("indiaDltContentTemplateId", dltTemplateId);
186
        queryParams.put("indiaDltPrincipalEntityId", DLT_PRINCIPLE_ENTITY_ID);
31828 amit.gupta 187
        queryParams.put("to", "91" + mobileNumber);
33510 tejus.loha 188
        queryParams.put("text", message);
189
        queryParams.put("password", PASS1);
190
        queryParams.put("username", USER);
31828 amit.gupta 191
        //OTP Message Template
26783 amit.gupta 192
 
33510 tejus.loha 193
        String response = restClient.post(NEXG_API_ENDPOINT, queryParams, new HashMap<>(), new HashMap<>());
31828 amit.gupta 194
        LOGGER.info(response);
33510 tejus.loha 195
        return response;
26711 amit.gupta 196
 
31828 amit.gupta 197
    }
26783 amit.gupta 198
 
31828 amit.gupta 199
    class SmsMessage {
200
        private String sender = "SMTDKN";
201
        @JsonProperty(value = "templateid")
202
        private String templateId = "54";
26711 amit.gupta 203
 
31828 amit.gupta 204
        private List<Message> message;
26711 amit.gupta 205
 
31828 amit.gupta 206
        @JsonProperty(value = "messagetype")
207
        private String messageType = "TXT";
26711 amit.gupta 208
 
31828 amit.gupta 209
        @Override
210
        public String toString() {
211
            return "SmsMessage [sender=" + sender + ", templateId=" + templateId + ", message=" + message
212
                    + ", messageType=" + messageType + "]";
213
        }
26711 amit.gupta 214
 
31828 amit.gupta 215
        public String getMessageType() {
216
            return messageType;
217
        }
26711 amit.gupta 218
 
31828 amit.gupta 219
        public void setMessageType(String messageType) {
220
            this.messageType = messageType;
221
        }
26711 amit.gupta 222
 
31828 amit.gupta 223
        public String getSender() {
224
            return sender;
225
        }
26711 amit.gupta 226
 
31828 amit.gupta 227
        public void setSender(String sender) {
228
            this.sender = sender;
229
        }
26711 amit.gupta 230
 
31828 amit.gupta 231
        public String getTemplateId() {
232
            return templateId;
233
        }
26711 amit.gupta 234
 
31828 amit.gupta 235
        public void setTemplateId(String templateId) {
236
            this.templateId = templateId;
237
        }
26711 amit.gupta 238
 
31828 amit.gupta 239
        public List<Message> getMessage() {
240
            return message;
241
        }
26711 amit.gupta 242
 
31828 amit.gupta 243
        public void setMessage(List<Message> message) {
244
            this.message = message;
245
        }
26711 amit.gupta 246
 
31828 amit.gupta 247
    }
26711 amit.gupta 248
 
31828 amit.gupta 249
    class Message {
250
        private String number;
33510 tejus.loha 251
        private Map<String, String> text;
26711 amit.gupta 252
 
31828 amit.gupta 253
        public String getNumber() {
254
            return number;
255
        }
26711 amit.gupta 256
 
31828 amit.gupta 257
        public void setNumber(String number) {
258
            this.number = number;
259
        }
26711 amit.gupta 260
 
31828 amit.gupta 261
        @Override
262
        public String toString() {
263
            return "Message [number=" + number + ", text=" + text + "]";
264
        }
26711 amit.gupta 265
 
31828 amit.gupta 266
        public Map<String, String> getText() {
267
            return text;
268
        }
26711 amit.gupta 269
 
31828 amit.gupta 270
        public void setText(Map<String, String> text) {
271
            this.text = text;
272
        }
21285 kshitij.so 273
 
31828 amit.gupta 274
    }
275
 
21285 kshitij.so 276
}