Subversion Repositories SmartDukaan

Rev

Rev 32403 | Rev 32415 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 32403 Rev 32405
Line 1... Line 1...
1
package com.spice.profitmandi.service;
1
package com.spice.profitmandi.service;
2
 
2
 
3
import java.time.LocalDateTime;
-
 
4
import java.util.ArrayList;
-
 
5
import java.util.Arrays;
-
 
6
import java.util.HashMap;
-
 
7
import java.util.HashSet;
-
 
8
import java.util.List;
-
 
9
import java.util.Map;
-
 
10
import java.util.Set;
-
 
11
import java.util.stream.Collectors;
-
 
12
 
-
 
13
import org.apache.logging.log4j.LogManager;
-
 
14
import org.apache.logging.log4j.Logger;
-
 
15
import org.json.JSONObject;
-
 
16
import org.springframework.beans.factory.annotation.Autowired;
-
 
17
import org.springframework.beans.factory.annotation.Value;
-
 
18
import org.springframework.stereotype.Component;
-
 
19
 
-
 
20
import com.google.gson.Gson;
3
import com.google.gson.Gson;
21
import com.spice.profitmandi.common.enumuration.MessageType;
4
import com.spice.profitmandi.common.enumuration.MessageType;
22
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
5
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
23
import com.spice.profitmandi.common.model.ProfitMandiConstants;
6
import com.spice.profitmandi.common.model.ProfitMandiConstants;
24
import com.spice.profitmandi.common.model.SendNotificationModel;
7
import com.spice.profitmandi.common.model.SendNotificationModel;
25
import com.spice.profitmandi.common.web.client.RestClient;
8
import com.spice.profitmandi.common.web.client.RestClient;
26
import com.spice.profitmandi.dao.entity.auth.AuthUser;
9
import com.spice.profitmandi.dao.entity.auth.AuthUser;
27
import com.spice.profitmandi.dao.entity.dtr.NotificationCampaign;
-
 
28
import com.spice.profitmandi.dao.entity.dtr.Optin;
-
 
29
import com.spice.profitmandi.dao.entity.dtr.PushNotifications;
-
 
30
import com.spice.profitmandi.dao.entity.dtr.User;
10
import com.spice.profitmandi.dao.entity.dtr.*;
31
import com.spice.profitmandi.dao.entity.dtr.UserCampaign;
-
 
32
import com.spice.profitmandi.dao.entity.user.Device;
11
import com.spice.profitmandi.dao.entity.user.Device;
-
 
12
import com.spice.profitmandi.dao.entity.whatsapp.WhatsappMessage;
33
import com.spice.profitmandi.dao.model.SimpleCampaign;
13
import com.spice.profitmandi.dao.model.SimpleCampaign;
34
import com.spice.profitmandi.dao.model.SimpleCampaignParams;
14
import com.spice.profitmandi.dao.model.SimpleCampaignParams;
35
import com.spice.profitmandi.dao.repository.catalog.DeviceRepository;
15
import com.spice.profitmandi.dao.repository.catalog.DeviceRepository;
36
import com.spice.profitmandi.dao.repository.cs.CsService;
16
import com.spice.profitmandi.dao.repository.cs.CsService;
37
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
-
 
38
import com.spice.profitmandi.dao.repository.dtr.NotificationCampaignRepository;
-
 
39
import com.spice.profitmandi.dao.repository.dtr.OptinRepository;
17
import com.spice.profitmandi.dao.repository.dtr.*;
40
import com.spice.profitmandi.dao.repository.dtr.PushNotificationRepository;
-
 
41
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
-
 
42
import com.spice.profitmandi.dao.repository.dtr.UserCampaignRepository;
18
import com.spice.profitmandi.dao.repository.whatsapp.WhatsappMessageRepository;
43
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
-
 
44
import com.spice.profitmandi.service.user.RetailerService;
19
import com.spice.profitmandi.service.user.RetailerService;
-
 
20
import com.spice.profitmandi.service.whatsapp.WhatsappMessageService;
-
 
21
import org.apache.logging.log4j.LogManager;
-
 
22
import org.apache.logging.log4j.Logger;
-
 
23
import org.json.JSONObject;
-
 
24
import org.springframework.beans.factory.annotation.Autowired;
-
 
25
import org.springframework.beans.factory.annotation.Value;
-
 
26
import org.springframework.stereotype.Component;
-
 
27
 
-
 
28
import java.time.LocalDateTime;
-
 
29
import java.util.*;
-
 
30
import java.util.stream.Collectors;
45
 
31
 
46
@Component
32
@Component
47
public class NotificationServiceImpl implements NotificationService {
33
public class NotificationServiceImpl implements NotificationService {
48
 
34
 
49
    private static final Logger LOGGER = LogManager.getLogger(NotificationService.class);
35
    private static final Logger LOGGER = LogManager.getLogger(NotificationService.class);
Line 72... Line 58...
72
    @Autowired
58
    @Autowired
73
    private RestClient restClient;
59
    private RestClient restClient;
74
    @Value("${prod}")
60
    @Value("${prod}")
75
    private boolean isProd;
61
    private boolean isProd;
76
 
62
 
-
 
63
    @Autowired
-
 
64
    private WhatsappMessageRepository messageRepository;
-
 
65
 
-
 
66
    @Autowired
-
 
67
    private WhatsappMessageService whatsappMessageService;
-
 
68
 
77
    @Override
69
    @Override
78
    public void sendNotification(SendNotificationModel sendNotificationModel) throws ProfitMandiBusinessException {
70
    public void sendNotification(SendNotificationModel sendNotificationModel) throws ProfitMandiBusinessException {
79
 
71
 
80
        SimpleCampaignParams scp = new SimpleCampaignParams();
72
        SimpleCampaignParams scp = new SimpleCampaignParams();
81
        scp.setMessage(sendNotificationModel.getMessage());
73
        scp.setMessage(sendNotificationModel.getMessage());
Line 103... Line 95...
103
        if (sendNotificationModel.getUserIds() != null && sendNotificationModel.getUserIds().size() > 0) {
95
        if (sendNotificationModel.getUserIds() != null && sendNotificationModel.getUserIds().size() > 0) {
104
            userIds.addAll(sendNotificationModel.getUserIds());
96
            userIds.addAll(sendNotificationModel.getUserIds());
105
        }
97
        }
106
 
98
 
107
        if (sendNotificationModel.getStateIds() != null && sendNotificationModel.getStateIds().size() > 0) {
99
        if (sendNotificationModel.getStateIds() != null && sendNotificationModel.getStateIds().size() > 0) {
108
            userIds.addAll(fofoStoreRepository.selectByWarehouseIds(sendNotificationModel.getStateIds()).stream().map(x -> x.getId()).collect(Collectors.toList()));
100
            userIds.addAll(fofoStoreRepository.selectByWarehouseIds(sendNotificationModel.getStateIds()).stream()
-
 
101
                    .map(x -> x.getId()).collect(Collectors.toList()));
109
        }
102
        }
110
 
103
 
111
        LOGGER.info("userIds" + userIds);
104
        LOGGER.info("userIds" + userIds);
112
 
105
 
113
        for (Integer userId : userIds) {
106
        for (Integer userId : userIds) {
Line 116... Line 109...
116
            uc.setUserId(userId);
109
            uc.setUserId(userId);
117
            uc.setPushTimestamp(LocalDateTime.now());
110
            uc.setPushTimestamp(LocalDateTime.now());
118
            userCampaignRepository.persist(uc);
111
            userCampaignRepository.persist(uc);
119
        }
112
        }
120
 
113
 
121
        List<Device> devices = deviceRepository.selectByUserIdAndModifiedTimestamp(new ArrayList<>(userIds), LocalDateTime.now().minusMonths(3), LocalDateTime.now());
114
        List<Device> devices = deviceRepository.selectByUserIdAndModifiedTimestamp(new ArrayList<>(userIds),
-
 
115
                LocalDateTime.now().minusMonths(3), LocalDateTime.now());
122
 
116
 
123
        LOGGER.info("devices" + devices);
117
        LOGGER.info("devices" + devices);
124
        pushNotification(nc.getId(), devices);
118
        pushNotification(nc.getId(), devices);
125
 
119
 
126
    }
120
    }
Line 133... Line 127...
133
        authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_CATEGORY));
127
        authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_CATEGORY));
134
        authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES));
128
        authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES));
135
        List<String> emailIds = authUsers.stream().map(x -> x.getEmailId()).collect(Collectors.toList());
129
        List<String> emailIds = authUsers.stream().map(x -> x.getEmailId()).collect(Collectors.toList());
136
        emailIds.add("devkinandan.lal@smartdukaan.com");
130
        emailIds.add("devkinandan.lal@smartdukaan.com");
137
        List<User> systemUsers = dtrUserRepository.selectAllByEmailIds(emailIds);
131
        List<User> systemUsers = dtrUserRepository.selectAllByEmailIds(emailIds);
-
 
132
        sendNotificationModel.getUserIds()
138
        sendNotificationModel.getUserIds().addAll(systemUsers.stream().map(x -> x.getId()).collect(Collectors.toList()));
133
                .addAll(systemUsers.stream().map(x -> x.getId()).collect(Collectors.toList()));
139
        this.sendNotification(sendNotificationModel);
134
        this.sendNotification(sendNotificationModel);
140
    }
135
    }
141
 
136
 
142
    @Override
137
    @Override
143
    public void sendNotification(int fofoId, String campaignName, MessageType messageType, String title, String message) throws ProfitMandiBusinessException {
138
    public void sendNotification(int fofoId, String campaignName, MessageType messageType, String title, String message)
-
 
139
            throws ProfitMandiBusinessException {
144
        SendNotificationModel sendNotificationModel = this.getDefaultNotificationModel();
140
        SendNotificationModel sendNotificationModel = this.getDefaultNotificationModel();
145
        sendNotificationModel.setCampaignName(campaignName);
141
        sendNotificationModel.setCampaignName(campaignName);
146
        sendNotificationModel.setMessageType(messageType);
142
        sendNotificationModel.setMessageType(messageType);
147
        sendNotificationModel.setTitle(title);
143
        sendNotificationModel.setTitle(title);
148
        sendNotificationModel.setMessage(message);
144
        sendNotificationModel.setMessage(message);
Line 172... Line 168...
172
        }
168
        }
173
 
169
 
174
    }
170
    }
175
 
171
 
176
    @Override
172
    @Override
177
    public void sendNotification(int fofoId, String campaignName, MessageType messageType, String title, String message, String url) throws ProfitMandiBusinessException {
173
    public void sendNotification(int fofoId, String campaignName, MessageType messageType, String title, String message,
-
 
174
                                 String url) throws ProfitMandiBusinessException {
178
        SendNotificationModel sendNotificationModel = this.getDefaultNotificationModel();
175
        SendNotificationModel sendNotificationModel = this.getDefaultNotificationModel();
179
        sendNotificationModel.setCampaignName(campaignName);
176
        sendNotificationModel.setCampaignName(campaignName);
180
        sendNotificationModel.setMessageType(messageType);
177
        sendNotificationModel.setMessageType(messageType);
181
        sendNotificationModel.setTitle(title);
178
        sendNotificationModel.setTitle(title);
182
        sendNotificationModel.setMessage(message);
179
        sendNotificationModel.setMessage(message);
Line 186... Line 183...
186
        sendNotificationModel.setUrl(url);
183
        sendNotificationModel.setUrl(url);
187
        this.sendNotification(sendNotificationModel);
184
        this.sendNotification(sendNotificationModel);
188
    }
185
    }
189
 
186
 
190
    @Override
187
    @Override
191
    public void sendWhatsappMessage(String message, String title, String mobile) throws Exception {
188
    public void sendWhatsappMessage(String message, String title, String mobile)
-
 
189
            throws Exception {
-
 
190
        boolean sentMessage = this.isWhatMessageSend(mobile);
-
 
191
        if (sentMessage) {
-
 
192
 
192
        this.sendWhatsappMessage(message, title, mobile, null, null);
193
            this.sendWhatsappMessage(message, title, mobile, null, null);
-
 
194
        }
193
    }
195
    }
194
 
196
 
195
    @Override
197
    @Override
196
    public void sendWhatsappMediaMessage(String message, String mobile, String mediaUrl, String fileName) throws Exception {
198
    public void sendWhatsappMediaMessage(String message, String mobile, String mediaUrl, String fileName)
-
 
199
            throws Exception {
-
 
200
        boolean sentMessage = this.isWhatMessageSend(mobile);
-
 
201
        if (sentMessage) {
197
        this.sendWhatsappMessage(message, null, mobile, mediaUrl, fileName);
202
            this.sendWhatsappMessage(message, null, mobile, mediaUrl, fileName);
-
 
203
        }
198
    }
204
    }
199
 
205
 
200
    @Override
206
    @Override
201
    public void optIn(String phoneNumber) throws Exception {
207
    public void optIn(String phoneNumber) throws Exception {
202
        Map<String, String> requestheaders = new HashMap<>();
208
        Map<String, String> requestheaders = new HashMap<>();
Line 214... Line 220...
214
        requestParams.put("channel", "WHATSAPP");
220
        requestParams.put("channel", "WHATSAPP");
215
        String response = restClient.get("https://media.smsgupshup.com/GatewayAPI/rest", requestParams, requestheaders);
221
        String response = restClient.get("https://media.smsgupshup.com/GatewayAPI/rest", requestParams, requestheaders);
216
        LOGGER.info("response" + response);
222
        LOGGER.info("response" + response);
217
    }
223
    }
218
 
224
 
219
    private void sendWhatsappMessage(String message, String title, String mobile, String mediaUrl, String fileName) throws Exception {
225
    private void sendWhatsappMessage(String message, String title, String mobile, String mediaUrl, String fileName)
-
 
226
            throws Exception {
220
        String sendTo = null;
227
        String sendTo = null;
221
        if (mobile.length() != 10) {
228
        if (mobile.length() != 10) {
222
            return;
229
            return;
223
        } else {
230
        } else {
224
            sendTo = 91 + mobile;
231
            sendTo = 91 + mobile;
Line 231... Line 238...
231
        requestParams.put("send_to", sendTo);
238
        requestParams.put("send_to", sendTo);
232
        requestParams.put("v", "1.1");
239
        requestParams.put("v", "1.1");
233
        requestParams.put("format", "json");
240
        requestParams.put("format", "json");
234
        requestParams.put("auth_scheme", "plain");
241
        requestParams.put("auth_scheme", "plain");
235
        if (mediaUrl == null) {
242
        if (mediaUrl == null) {
236
            requestParams.put("method", "SENDMESSAGE");
243
        requestParams.put("method", "SENDMESSAGE");
237
            /*requestParams.put("msg_type", "HSM");
244
            /*requestParams.put("msg_type", "HSM");
238
            requestParams.put("msg", message);
245
            requestParams.put("msg", message);
239
            requestParams.put("isTemplate", "true");
246
            requestParams.put("isTemplate", "true");
240
            requestParams.put("header", title);*/
247
            requestParams.put("header", title);*/
241
            //} else if(isProd && mediaUrl !=null) {
248
            //} else if(isProd && mediaUrl !=null) {
Line 255... Line 262...
255
            requestParams.put("msg_type", "DOCUMENT");
262
            requestParams.put("msg_type", "DOCUMENT");
256
            requestParams.put("caption", message);
263
            requestParams.put("caption", message);
257
            //requestParams.put("msg", message);
264
            //requestParams.put("msg", message);
258
            requestParams.put("media_url", mediaUrl);
265
            requestParams.put("media_url", mediaUrl);
259
            requestParams.put("filename", fileName);
266
            requestParams.put("filename", fileName);
-
 
267
            String response =
260
            String response = restClient.post("https://media.smsgupshup.com/GatewayAPI/rest", requestParams, requestheaders);
268
                    restClient.post("https://media.smsgupshup.com/GatewayAPI/rest",
-
 
269
                            requestParams,
-
 
270
                            requestheaders);
261
            LOGGER.info("response" + response);
271
            LOGGER.info("response" + response);
262
 
272
 
263
            JSONObject jsonObject = new JSONObject(response);
273
            JSONObject jsonObject = new JSONObject(response);
264
            LOGGER.info("jsonObject" + jsonObject);
-
 
265
 
-
 
266
            JSONObject jo = (JSONObject) jsonObject.get("response");
-
 
267
            LOGGER.info("jo" + jo);
-
 
268
 
274
 
269
            String externalId = jo.getString("id");
275
            JSONObject whatsappResponse = (JSONObject) jsonObject.get("response");
270
            LOGGER.info("externalId" + externalId);
-
 
271
 
276
 
-
 
277
            String externalId = whatsappResponse.getString("id");
-
 
278
            String phone = whatsappResponse.getString("phone");
272
 
279
 
-
 
280
            WhatsappMessage whatsappMessage = new WhatsappMessage();
-
 
281
            whatsappMessage.setCreatedTimestamp(LocalDateTime.now());
-
 
282
            whatsappMessage.setExternalId(externalId);
-
 
283
            whatsappMessage.setDestAddr(phone);
-
 
284
            messageRepository.persist(whatsappMessage);
273
        }
285
        }
274
 
286
 
275
    }
287
    }
276
 
288
 
-
 
289
    @Override
-
 
290
    public boolean isWhatMessageSend(String mobile) {
-
 
291
        String destAddr = "91" + mobile;
-
 
292
        boolean isSent = true;
-
 
293
        List<WhatsappMessage> whatsappMessages = messageRepository.selectByDestAddr(destAddr);
-
 
294
        if (!whatsappMessages.isEmpty()) {
-
 
295
 
-
 
296
            long failedCount = whatsappMessages.stream().filter(x -> x.getFailed().equals("FAILED")).collect(Collectors.counting());
277
 
297
 
-
 
298
 
-
 
299
            if (failedCount >= 2) {
-
 
300
                isSent = false;
-
 
301
            }
-
 
302
        }
-
 
303
        return isSent;
-
 
304
    }
278
}
305
}