Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
25822 amit.gupta 1
package com.spice.profitmandi.service;
2
 
3
import com.google.gson.Gson;
30025 amit.gupta 4
import com.spice.profitmandi.common.enumuration.MessageType;
29198 manish 5
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
29927 amit.gupta 6
import com.spice.profitmandi.common.model.ProfitMandiConstants;
25822 amit.gupta 7
import com.spice.profitmandi.common.model.SendNotificationModel;
30989 tejbeer 8
import com.spice.profitmandi.common.web.client.RestClient;
29927 amit.gupta 9
import com.spice.profitmandi.dao.entity.auth.AuthUser;
32253 amit.gupta 10
import com.spice.profitmandi.dao.entity.dtr.*;
25822 amit.gupta 11
import com.spice.profitmandi.dao.entity.user.Device;
12
import com.spice.profitmandi.dao.model.SimpleCampaign;
13
import com.spice.profitmandi.dao.model.SimpleCampaignParams;
14
import com.spice.profitmandi.dao.repository.catalog.DeviceRepository;
29927 amit.gupta 15
import com.spice.profitmandi.dao.repository.cs.CsService;
16
import com.spice.profitmandi.dao.repository.dtr.*;
29198 manish 17
import com.spice.profitmandi.service.user.RetailerService;
29927 amit.gupta 18
import org.apache.logging.log4j.LogManager;
19
import org.apache.logging.log4j.Logger;
20
import org.springframework.beans.factory.annotation.Autowired;
32253 amit.gupta 21
import org.springframework.beans.factory.annotation.Value;
29927 amit.gupta 22
import org.springframework.stereotype.Component;
25822 amit.gupta 23
 
29927 amit.gupta 24
import java.time.LocalDateTime;
30025 amit.gupta 25
import java.util.*;
29927 amit.gupta 26
import java.util.stream.Collectors;
27
 
25835 amit.gupta 28
@Component
25822 amit.gupta 29
public class NotificationServiceImpl implements NotificationService {
30
 
32253 amit.gupta 31
    private static final Logger LOGGER = LogManager.getLogger(NotificationService.class);
32
    @Autowired
33
    UserCampaignRepository userCampaignRepository;
34
    @Autowired
35
    UserRepository dtrUserRepository;
36
    @Autowired
37
    UserAccountRepository userAccountRepository;
38
    @Autowired
39
    NotificationCampaignRepository notificationCampaignRepository;
40
    @Autowired
41
    DeviceRepository deviceRepository;
42
    @Autowired
43
    FofoStoreRepository fofoStoreRepository;
44
    @Autowired
45
    CsService csService;
46
    @Autowired
47
    OptinRepository optinRepository;
48
    @Autowired
49
    RetailerService retailerService;
50
    @Autowired
51
    PushNotificationRepository pushNotificationRepository;
52
    @Autowired
53
    private Gson gson;
54
    @Autowired
55
    private RestClient restClient;
56
    @Value("${prod}")
57
    private boolean isProd;
25822 amit.gupta 58
 
32253 amit.gupta 59
    @Override
60
    public void sendNotification(SendNotificationModel sendNotificationModel) throws ProfitMandiBusinessException {
29927 amit.gupta 61
 
32253 amit.gupta 62
        SimpleCampaignParams scp = new SimpleCampaignParams();
63
        scp.setMessage(sendNotificationModel.getMessage());
64
        scp.setTitle(sendNotificationModel.getTitle());
65
        scp.setImageUrl(sendNotificationModel.getImageUrl());
66
        scp.setType(sendNotificationModel.getType());
67
        scp.setUrl(sendNotificationModel.getUrl());
68
        scp.setShowImage(sendNotificationModel.getShowImage());
69
        scp.setExpireTimestamp(sendNotificationModel.getExpiresat());
70
        SimpleCampaign sc = new SimpleCampaign(scp);
71
        sc.setSimpleCampaignParams(scp);
30025 amit.gupta 72
 
32253 amit.gupta 73
        NotificationCampaign nc = new NotificationCampaign();
74
        nc.setName(sendNotificationModel.getCampaignName());
75
        nc.setImplementationType("SimpleCampaign");
76
        nc.setImplementationParams(gson.toJson(scp));
77
        nc.setMessageType(sendNotificationModel.getMessageType());
78
        nc.setDocumentId(sendNotificationModel.getDocumentId());
79
        nc.setCreatedTimestamp(LocalDateTime.now());
80
        notificationCampaignRepository.persist(nc);
25822 amit.gupta 81
 
32253 amit.gupta 82
        UserCampaign uc = null;
25822 amit.gupta 83
 
32253 amit.gupta 84
        Set<Integer> userIds = new HashSet<>();
85
        if (sendNotificationModel.getUserIds() != null && sendNotificationModel.getUserIds().size() > 0) {
86
            userIds.addAll(sendNotificationModel.getUserIds());
87
        }
29198 manish 88
 
32253 amit.gupta 89
        if (sendNotificationModel.getStateIds() != null && sendNotificationModel.getStateIds().size() > 0) {
90
            userIds.addAll(fofoStoreRepository.selectByWarehouseIds(sendNotificationModel.getStateIds()).stream()
91
                    .map(x -> x.getId()).collect(Collectors.toList()));
92
        }
29927 amit.gupta 93
 
32253 amit.gupta 94
        LOGGER.info("userIds" + userIds);
29198 manish 95
 
32253 amit.gupta 96
        for (Integer userId : userIds) {
97
            uc = new UserCampaign();
98
            uc.setCampaignId(nc.getId());
99
            uc.setUserId(userId);
100
            uc.setPushTimestamp(LocalDateTime.now());
101
            userCampaignRepository.persist(uc);
102
        }
25822 amit.gupta 103
 
32253 amit.gupta 104
        List<Device> devices = deviceRepository.selectByUserIdAndModifiedTimestamp(new ArrayList<>(userIds),
105
                LocalDateTime.now().minusMonths(3), LocalDateTime.now());
25822 amit.gupta 106
 
32253 amit.gupta 107
        LOGGER.info("devices" + devices);
108
        pushNotification(nc.getId(), devices);
30989 tejbeer 109
 
32253 amit.gupta 110
    }
25822 amit.gupta 111
 
32253 amit.gupta 112
    @Override
113
    public void sendNotificationToAll(SendNotificationModel sendNotificationModel) throws ProfitMandiBusinessException {
114
        sendNotificationModel.setUserIds(fofoStoreRepository.selectAllDtrUserIds());
115
        Set<AuthUser> authUsers = new HashSet<>(
116
                csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_RBM));
117
        authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_CATEGORY));
118
        authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES));
119
        List<String> emailIds = authUsers.stream().map(x -> x.getEmailId()).collect(Collectors.toList());
120
        emailIds.add("devkinandan.lal@smartdukaan.com");
121
        List<User> systemUsers = dtrUserRepository.selectAllByEmailIds(emailIds);
122
        sendNotificationModel.getUserIds()
123
                .addAll(systemUsers.stream().map(x -> x.getId()).collect(Collectors.toList()));
124
        this.sendNotification(sendNotificationModel);
125
    }
25822 amit.gupta 126
 
32253 amit.gupta 127
    @Override
128
    public void sendNotification(int fofoId, String campaignName, MessageType messageType, String title, String message)
129
            throws ProfitMandiBusinessException {
130
        SendNotificationModel sendNotificationModel = this.getDefaultNotificationModel();
131
        sendNotificationModel.setCampaignName(campaignName);
132
        sendNotificationModel.setMessageType(messageType);
133
        sendNotificationModel.setTitle(title);
134
        sendNotificationModel.setMessage(message);
135
        int userId = userAccountRepository.selectUserIdByRetailerId(fofoId);
136
        sendNotificationModel.setUserIds(Arrays.asList(userId));
137
        sendNotificationModel.setMessageType(MessageType.wallet);
138
        this.sendNotification(sendNotificationModel);
139
    }
25822 amit.gupta 140
 
32253 amit.gupta 141
    public SendNotificationModel getDefaultNotificationModel() {
142
        SendNotificationModel sendNotificationModel = new SendNotificationModel();
143
        sendNotificationModel.setType("url");
144
        sendNotificationModel.setUrl("https://app.smartdukaan.com/pages/home/notifications");
145
        sendNotificationModel.setExpiresat(LocalDateTime.now().plusDays(1));
146
        sendNotificationModel.setMessageType(MessageType.notification);
147
        return sendNotificationModel;
148
    }
25822 amit.gupta 149
 
32253 amit.gupta 150
    public void pushNotification(int cid, List<Device> devices) {
29198 manish 151
 
32253 amit.gupta 152
        for (Device device : devices) {
153
            PushNotifications pn = new PushNotifications();
154
            pn.setNotificationCampaignid(cid);
155
            pn.setDeviceId(device.getId());
156
            pn.setUserId(device.getUser_id());
157
            pushNotificationRepository.persist(pn);
158
        }
29198 manish 159
 
32253 amit.gupta 160
    }
29198 manish 161
 
32253 amit.gupta 162
    @Override
163
    public void sendNotification(int fofoId, String campaignName, MessageType messageType, String title, String message,
164
                                 String url) throws ProfitMandiBusinessException {
165
        SendNotificationModel sendNotificationModel = this.getDefaultNotificationModel();
166
        sendNotificationModel.setCampaignName(campaignName);
167
        sendNotificationModel.setMessageType(messageType);
168
        sendNotificationModel.setTitle(title);
169
        sendNotificationModel.setMessage(message);
170
        int userId = userAccountRepository.selectUserIdByRetailerId(fofoId);
171
        sendNotificationModel.setUserIds(Arrays.asList(userId));
172
        sendNotificationModel.setMessageType(messageType);
173
        sendNotificationModel.setUrl(url);
174
        this.sendNotification(sendNotificationModel);
175
    }
29198 manish 176
 
32253 amit.gupta 177
    @Override
178
    public void sendWhatsappMessage(String message, String title, String mobile)
179
            throws Exception {
180
        this.sendWhatsappMessage(message, title, mobile, null, null);
181
    }
25822 amit.gupta 182
 
32253 amit.gupta 183
    @Override
184
    public void sendWhatsappMediaMessage(String message, String mobile, String mediaUrl, String fileName)
185
            throws Exception {
186
        this.sendWhatsappMessage(message, null, mobile, mediaUrl, fileName);
187
    }
25822 amit.gupta 188
 
32253 amit.gupta 189
    @Override
190
    public void optIn(String phoneNumber) throws Exception {
191
        Map<String, String> requestheaders = new HashMap<>();
192
        requestheaders.put("Content-Type", "application/x-www-form-urlencoded");
193
        Map<String, String> requestParams = new HashMap<>();
194
        requestParams.put("userid", String.valueOf(2000215976));
195
        requestParams.put("password", "MFRd!BBL");
196
        requestParams.put("phone_number", phoneNumber);
197
        requestParams.put("auth_scheme", "plain");
198
        requestParams.put("v", "1.1");
199
        requestParams.put("format", "json");
28400 tejbeer 200
 
32253 amit.gupta 201
        requestParams.put("method", "OPT_IN");
25822 amit.gupta 202
 
32253 amit.gupta 203
        requestParams.put("channel", "WHATSAPP");
204
        String response = restClient.get("https://media.smsgupshup.com/GatewayAPI/rest", requestParams, requestheaders);
205
        LOGGER.info("response" + response);
206
    }
29927 amit.gupta 207
 
32253 amit.gupta 208
    private void sendWhatsappMessage(String message, String title, String mobile, String mediaUrl, String fileName)
209
            throws Exception {
32260 amit.gupta 210
        String sendTo = null;
32259 amit.gupta 211
        if (mobile.length() != 10) {
212
            return;
213
        } else {
32260 amit.gupta 214
            sendTo = 91 + mobile;
32259 amit.gupta 215
        }
32253 amit.gupta 216
        Map<String, String> requestheaders = new HashMap<>();
217
        requestheaders.put("Content-Type", "application/x-www-form-urlencoded");
218
        Map<String, String> requestParams = new HashMap<>();
219
        requestParams.put("userid", String.valueOf(2000215976));
220
        requestParams.put("password", "MFRd!BBL");
32285 amit.gupta 221
        requestParams.put("send_to", sendTo);
32253 amit.gupta 222
        requestParams.put("v", "1.1");
223
        requestParams.put("format", "json");
224
        requestParams.put("auth_scheme", "plain");
225
        if (mediaUrl == null) {
32268 amit.gupta 226
        requestParams.put("method", "SENDMESSAGE");
32253 amit.gupta 227
            /*requestParams.put("msg_type", "HSM");
228
            requestParams.put("msg", message);
229
            requestParams.put("isTemplate", "true");
230
            requestParams.put("header", title);*/
231
            //} else if(isProd && mediaUrl !=null) {
232
        } else if (mediaUrl != null) {
233
            //requestParams.put("isHSM", "true");
32286 amit.gupta 234
 
235
            Optin optin = optinRepository.selectByMobile(mobile);
236
            if (optin == null) {
237
                this.optIn(sendTo);
238
                optin = new Optin();
239
                optin.setCreated(LocalDateTime.now());
240
                optin.setMobile(mobile);
241
                optinRepository.persist(optin);
242
            }
243
 
32268 amit.gupta 244
            requestParams.put("method", "SENDMEDIAMESSAGE");
32253 amit.gupta 245
            requestParams.put("msg_type", "DOCUMENT");
246
            requestParams.put("caption", message);
32268 amit.gupta 247
            //requestParams.put("msg", message);
32253 amit.gupta 248
            requestParams.put("media_url", mediaUrl);
249
            requestParams.put("filename", fileName);
32255 amit.gupta 250
            String response =
251
                    restClient.post("https://media.smsgupshup.com/GatewayAPI/rest",
252
                            requestParams,
253
                            requestheaders);
254
            LOGGER.info("response" + response);
32253 amit.gupta 255
        }
30025 amit.gupta 256
 
32253 amit.gupta 257
    }
30025 amit.gupta 258
 
28397 tejbeer 259
 
25822 amit.gupta 260
}