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