| 21248 |
ashik.ali |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
|
|
3 |
import java.time.LocalDateTime;
|
| 21855 |
amit.gupta |
4 |
import java.util.ArrayList;
|
| 21428 |
amit.gupta |
5 |
import java.util.HashMap;
|
| 21855 |
amit.gupta |
6 |
import java.util.List;
|
| 21277 |
ashik.ali |
7 |
import java.util.Map;
|
| 21248 |
ashik.ali |
8 |
|
|
|
9 |
import javax.servlet.http.HttpServletRequest;
|
|
|
10 |
|
|
|
11 |
import org.slf4j.Logger;
|
|
|
12 |
import org.slf4j.LoggerFactory;
|
| 21278 |
ashik.ali |
13 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 21414 |
kshitij.so |
14 |
import org.springframework.beans.factory.annotation.Value;
|
| 21248 |
ashik.ali |
15 |
import org.springframework.http.HttpStatus;
|
|
|
16 |
import org.springframework.http.ResponseEntity;
|
|
|
17 |
import org.springframework.stereotype.Controller;
|
| 21702 |
ashik.ali |
18 |
import org.springframework.transaction.annotation.Transactional;
|
| 21366 |
kshitij.so |
19 |
import org.springframework.web.bind.annotation.RequestBody;
|
| 21248 |
ashik.ali |
20 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
21 |
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
22 |
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
23 |
|
| 22015 |
ashik.ali |
24 |
import com.eclipsesource.json.Json;
|
| 21855 |
amit.gupta |
25 |
import com.eclipsesource.json.JsonArray;
|
|
|
26 |
import com.eclipsesource.json.JsonObject;
|
|
|
27 |
import com.eclipsesource.json.JsonValue;
|
|
|
28 |
import com.google.gson.Gson;
|
| 21248 |
ashik.ali |
29 |
import com.spice.profitmandi.common.ResponseCodeHolder;
|
| 21855 |
amit.gupta |
30 |
import com.spice.profitmandi.common.enumuration.SchemeType;
|
| 21248 |
ashik.ali |
31 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
32 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 21740 |
ashik.ali |
33 |
import com.spice.profitmandi.common.model.ProfitMandiResponse;
|
| 22492 |
amit.gupta |
34 |
import com.spice.profitmandi.common.model.RegisteredUserInfo;
|
| 21740 |
ashik.ali |
35 |
import com.spice.profitmandi.common.model.ResponseStatus;
|
| 21469 |
amit.gupta |
36 |
import com.spice.profitmandi.common.model.UserInfo;
|
| 21282 |
ashik.ali |
37 |
import com.spice.profitmandi.common.util.JWTUtil;
|
| 21855 |
amit.gupta |
38 |
import com.spice.profitmandi.common.web.client.RestClient;
|
| 21740 |
ashik.ali |
39 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
| 21735 |
ashik.ali |
40 |
import com.spice.profitmandi.dao.entity.dtr.Permission;
|
|
|
41 |
import com.spice.profitmandi.dao.entity.dtr.Retailer;
|
|
|
42 |
import com.spice.profitmandi.dao.entity.dtr.User;
|
|
|
43 |
import com.spice.profitmandi.dao.entity.dtr.UserRole;
|
| 22355 |
ashik.ali |
44 |
import com.spice.profitmandi.dao.entity.user.Address;
|
| 21735 |
ashik.ali |
45 |
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
|
| 21643 |
ashik.ali |
46 |
import com.spice.profitmandi.dao.model.UserCart;
|
| 21735 |
ashik.ali |
47 |
import com.spice.profitmandi.dao.repository.dtr.PermissionRepository;
|
|
|
48 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
|
|
|
49 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
|
|
50 |
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
|
|
|
51 |
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
|
| 22355 |
ashik.ali |
52 |
import com.spice.profitmandi.dao.repository.user.AddressRepository;
|
| 21784 |
amit.gupta |
53 |
import com.spice.profitmandi.service.UserService;
|
| 21469 |
amit.gupta |
54 |
import com.spice.profitmandi.web.enumuration.UserStatus;
|
| 21277 |
ashik.ali |
55 |
import com.spice.profitmandi.web.processor.GoogleLoginProcessor;
|
| 21426 |
ashik.ali |
56 |
import com.spice.profitmandi.web.req.UserAddRoleRequest;
|
| 21366 |
kshitij.so |
57 |
import com.spice.profitmandi.web.req.UserRequest;
|
| 21855 |
amit.gupta |
58 |
import com.spice.profitmandi.web.res.Notification;
|
| 21248 |
ashik.ali |
59 |
|
| 21469 |
amit.gupta |
60 |
import io.swagger.annotations.ApiImplicitParam;
|
|
|
61 |
import io.swagger.annotations.ApiImplicitParams;
|
|
|
62 |
|
| 21248 |
ashik.ali |
63 |
/**
|
|
|
64 |
* @author ashikali
|
|
|
65 |
*
|
|
|
66 |
*/
|
|
|
67 |
@Controller
|
| 22037 |
amit.gupta |
68 |
@Transactional(rollbackFor=Throwable.class)
|
| 21248 |
ashik.ali |
69 |
public class UserController {
|
| 21469 |
amit.gupta |
70 |
|
| 21448 |
ashik.ali |
71 |
@Autowired
|
|
|
72 |
ResponseSender<?> responseSender;
|
| 21469 |
amit.gupta |
73 |
|
|
|
74 |
private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
|
|
|
75 |
|
| 21855 |
amit.gupta |
76 |
@Value("${notifications.api.host}")
|
|
|
77 |
private String nodeHost;
|
|
|
78 |
@Value("${notifications.api.port}")
|
|
|
79 |
private int nodePort;
|
|
|
80 |
|
| 21414 |
kshitij.so |
81 |
@Value("${admin.token}")
|
|
|
82 |
private String validAdminToken;
|
| 21469 |
amit.gupta |
83 |
|
| 21278 |
ashik.ali |
84 |
@Autowired
|
| 22873 |
ashik.ali |
85 |
private UserRepository userRepository;
|
| 22355 |
ashik.ali |
86 |
|
|
|
87 |
@Autowired
|
| 22873 |
ashik.ali |
88 |
private com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
|
| 21469 |
amit.gupta |
89 |
|
| 21278 |
ashik.ali |
90 |
@Autowired
|
| 22873 |
ashik.ali |
91 |
private RetailerRepository retailerRepository;
|
| 21485 |
amit.gupta |
92 |
|
|
|
93 |
@Autowired
|
| 22873 |
ashik.ali |
94 |
private UserRoleRepository userRoleRepository;
|
| 21469 |
amit.gupta |
95 |
|
| 21426 |
ashik.ali |
96 |
@Autowired
|
| 22873 |
ashik.ali |
97 |
private UserAccountRepository userAccountRepository;
|
| 21485 |
amit.gupta |
98 |
|
|
|
99 |
@Autowired
|
| 22873 |
ashik.ali |
100 |
private PermissionRepository permissionRepository;
|
| 22355 |
ashik.ali |
101 |
|
|
|
102 |
@Autowired
|
| 22873 |
ashik.ali |
103 |
private AddressRepository addressRepository;
|
| 21469 |
amit.gupta |
104 |
|
| 21426 |
ashik.ali |
105 |
@Autowired
|
| 22873 |
ashik.ali |
106 |
private GoogleLoginProcessor googleLoginProcessor;
|
| 21855 |
amit.gupta |
107 |
|
| 21784 |
amit.gupta |
108 |
@Autowired
|
| 22873 |
ashik.ali |
109 |
private UserService userService;
|
| 21469 |
amit.gupta |
110 |
|
| 21277 |
ashik.ali |
111 |
@SuppressWarnings("unchecked")
|
| 21469 |
amit.gupta |
112 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_GOOGLE_LOGIN, method = RequestMethod.POST)
|
|
|
113 |
public ResponseEntity<?> googleLogin(HttpServletRequest request) {
|
|
|
114 |
LOGGER.info("requested url : " + request.getRequestURL().toString());
|
|
|
115 |
final Map<String, Object> googleLoginMap = (Map<String, Object>) request
|
|
|
116 |
.getAttribute(ProfitMandiConstants.GOOGLE_LOGIN_MAP);
|
| 21277 |
ashik.ali |
117 |
request.removeAttribute(ProfitMandiConstants.GOOGLE_LOGIN_MAP);
|
|
|
118 |
try {
|
| 21448 |
ashik.ali |
119 |
return responseSender.ok(googleLoginProcessor.process(googleLoginMap));
|
| 21469 |
amit.gupta |
120 |
} catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
121 |
LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
|
| 21448 |
ashik.ali |
122 |
return responseSender.badRequest(profitMandiBusinessException);
|
| 21277 |
ashik.ali |
123 |
}
|
|
|
124 |
}
|
| 21469 |
amit.gupta |
125 |
|
|
|
126 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_TOKEN_IS_EXPIRED, method = RequestMethod.GET)
|
|
|
127 |
public ResponseEntity<?> tokenIsExpired(HttpServletRequest request, @RequestParam(name = "token") String token) {
|
|
|
128 |
LOGGER.info("requested url : " + request.getRequestURL().toString());
|
| 21282 |
ashik.ali |
129 |
try {
|
| 21448 |
ashik.ali |
130 |
return responseSender.ok(JWTUtil.isExpired(token));
|
| 21469 |
amit.gupta |
131 |
|
|
|
132 |
} catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
133 |
LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
|
| 21448 |
ashik.ali |
134 |
return responseSender.badRequest(profitMandiBusinessException);
|
| 21282 |
ashik.ali |
135 |
}
|
|
|
136 |
}
|
| 21469 |
amit.gupta |
137 |
|
| 22355 |
ashik.ali |
138 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_DETAIL_BY_TOKEN, method = RequestMethod.GET)
|
| 21469 |
amit.gupta |
139 |
@ApiImplicitParams({
|
|
|
140 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
|
|
141 |
public ResponseEntity<?> tokenInfo(HttpServletRequest request) throws Throwable {
|
|
|
142 |
Map<String, Object> responseMap = new HashMap<>();
|
|
|
143 |
UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
|
| 21483 |
amit.gupta |
144 |
User user = null;
|
| 21855 |
amit.gupta |
145 |
if (userInfo.getUserId() > -1) {
|
| 21483 |
amit.gupta |
146 |
user = userRepository.selectById(userInfo.getUserId());
|
|
|
147 |
} else {
|
|
|
148 |
try {
|
|
|
149 |
user = userRepository.selectByEmailId(userInfo.getEmail());
|
|
|
150 |
} catch (ProfitMandiBusinessException e1) {
|
| 21526 |
amit.gupta |
151 |
LOGGER.info("Uneregistered user", userInfo.getEmail());
|
| 21483 |
amit.gupta |
152 |
}
|
|
|
153 |
}
|
|
|
154 |
if (user != null) {
|
| 21491 |
amit.gupta |
155 |
responseMap.put(ProfitMandiConstants.EMAIL_ID, user.getEmailId());
|
| 21526 |
amit.gupta |
156 |
responseMap.put(ProfitMandiConstants.USER_ID, user.getId());
|
| 22017 |
amit.gupta |
157 |
responseMap.put(ProfitMandiConstants.USER_NAME, user.getFirstName() + " " + user.getLastName());
|
| 22554 |
amit.gupta |
158 |
List<RoleType> roleTypes = userRoleRepository.selectRoleTypesByUserId(user.getId());
|
|
|
159 |
//LOGGER.info("userRoles {} ", userRoles);
|
| 22032 |
ashik.ali |
160 |
|
|
|
161 |
// generate new token if roles have been updated
|
| 22554 |
amit.gupta |
162 |
if (userInfo.getRoleNames() == null || roleTypes.size() != userInfo.getRoleNames().size()) {
|
| 22603 |
amit.gupta |
163 |
String[] roleType = new String[roleTypes.size()];
|
|
|
164 |
int index = 0;
|
|
|
165 |
for (RoleType userRole : roleTypes) {
|
|
|
166 |
roleType[index++] = userRole.toString();
|
|
|
167 |
}
|
|
|
168 |
String newToken = JWTUtil.create(user.getId(), roleType);
|
| 21483 |
amit.gupta |
169 |
responseMap.put("newAuthToken", newToken);
|
|
|
170 |
}
|
| 22032 |
ashik.ali |
171 |
|
| 21469 |
amit.gupta |
172 |
// if user is retailer
|
| 22554 |
amit.gupta |
173 |
if (roleTypes.contains(RoleType.RETAILER)) {
|
| 22032 |
ashik.ali |
174 |
UserCart uc = userAccountRepository.getUserCart(userInfo.getUserId());
|
|
|
175 |
Retailer retailer = retailerRepository.selectById(uc.getUserId());
|
| 22370 |
amit.gupta |
176 |
com.spice.profitmandi.dao.entity.user.User saholicUser = userUserRepository.selectById(uc.getUserId());
|
|
|
177 |
if(saholicUser.getAddressId() != null){
|
|
|
178 |
Address address = addressRepository.selectById(saholicUser.getAddressId());
|
|
|
179 |
responseMap.put(ProfitMandiConstants.ADDRESS, address);
|
|
|
180 |
}
|
| 21485 |
amit.gupta |
181 |
// if retailer is activated 1 then verified retailer
|
|
|
182 |
// else if migrated is 1 then old retailer
|
| 22497 |
amit.gupta |
183 |
// also lets incoporte old process i.e is user is activated then also retailer is verified retailer
|
| 21485 |
amit.gupta |
184 |
// else retailer is not verifed
|
| 22497 |
amit.gupta |
185 |
if (retailer.isActive() || user.isActivated()) {
|
| 22554 |
amit.gupta |
186 |
if (roleTypes.contains(RoleType.FOFO)) {
|
| 22017 |
amit.gupta |
187 |
responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.FOFO.getValue());
|
|
|
188 |
} else {
|
|
|
189 |
responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.VERIFIED_RETAILER.getValue());
|
|
|
190 |
}
|
| 22032 |
ashik.ali |
191 |
} else if (retailer.isMigrated()) {
|
| 21485 |
amit.gupta |
192 |
responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.RETAILER.getValue());
|
| 21469 |
amit.gupta |
193 |
} else {
|
| 21485 |
amit.gupta |
194 |
responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_VERIFIED_RETAILER.getValue());
|
| 21469 |
amit.gupta |
195 |
}
|
| 22554 |
amit.gupta |
196 |
} else if (roleTypes.contains(RoleType.USER)) {
|
| 22502 |
amit.gupta |
197 |
responseMap.put("userInfo", getRegisteredUserInfo(user));
|
| 21469 |
amit.gupta |
198 |
responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.REGISTERED.getValue());
|
| 22032 |
ashik.ali |
199 |
}
|
| 21491 |
amit.gupta |
200 |
} else {
|
|
|
201 |
responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_REGISTERED.getValue());
|
|
|
202 |
responseMap.put(ProfitMandiConstants.EMAIL_ID, userInfo.getEmail());
|
| 21469 |
amit.gupta |
203 |
}
|
| 22032 |
ashik.ali |
204 |
|
| 21469 |
amit.gupta |
205 |
return responseSender.ok(responseMap);
|
|
|
206 |
}
|
| 22492 |
amit.gupta |
207 |
|
| 22502 |
amit.gupta |
208 |
private RegisteredUserInfo getRegisteredUserInfo(User user) throws Throwable {
|
| 22492 |
amit.gupta |
209 |
RegisteredUserInfo ri = new RegisteredUserInfo();
|
|
|
210 |
ri.setCity(user.getCity());
|
|
|
211 |
ri.setFirstName(user.getFirstName());
|
|
|
212 |
ri.setLastName(user.getLastName());
|
| 22493 |
amit.gupta |
213 |
ri.setPhone(user.getMobileNumber());
|
| 22492 |
amit.gupta |
214 |
ri.setPinCode(user.getPinCode());
|
| 22845 |
amit.gupta |
215 |
ri.setState(user.getState());
|
| 22492 |
amit.gupta |
216 |
return ri;
|
|
|
217 |
}
|
| 21469 |
amit.gupta |
218 |
|
|
|
219 |
@RequestMapping(value = ProfitMandiConstants.URL_USER, method = RequestMethod.POST)
|
| 21501 |
amit.gupta |
220 |
@ApiImplicitParams({
|
| 22032 |
ashik.ali |
221 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
|
|
222 |
public ResponseEntity<?> createUser(HttpServletRequest request, @RequestBody UserRequest userRequest)
|
|
|
223 |
throws Throwable {
|
| 21469 |
amit.gupta |
224 |
LOGGER.info("requested url : " + request.getRequestURL().toString());
|
| 21368 |
kshitij.so |
225 |
User user = new User();
|
|
|
226 |
user.setFirstName(userRequest.getFirstName());
|
|
|
227 |
user.setLastName(userRequest.getLastName());
|
|
|
228 |
user.setCity(userRequest.getCity());
|
|
|
229 |
user.setPinCode(Integer.valueOf(userRequest.getPinCode()));
|
| 22845 |
amit.gupta |
230 |
user.setState(userRequest.getState());
|
| 21708 |
amit.gupta |
231 |
user.setMobileNumber(userRequest.getMobieNumber());
|
| 21368 |
kshitij.so |
232 |
user.setEmailId(userRequest.getEmailId());
|
|
|
233 |
user.setUsername("");
|
|
|
234 |
user.setPassword("");
|
|
|
235 |
user.setMobile_verified(false);
|
|
|
236 |
user.setReferral_url("");
|
|
|
237 |
user.setGroup_id(1);
|
| 22504 |
amit.gupta |
238 |
user.setStatus(1);
|
| 21368 |
kshitij.so |
239 |
user.setActivated(false);
|
| 21501 |
amit.gupta |
240 |
user.setCreateTimestamp(LocalDateTime.now());
|
|
|
241 |
user.setUpdateTimestamp(LocalDateTime.now());
|
| 22032 |
ashik.ali |
242 |
try {
|
| 21503 |
ashik.ali |
243 |
userRepository.persist(user);
|
|
|
244 |
UserRole userRole = new UserRole();
|
| 22011 |
ashik.ali |
245 |
userRole.setRoleType(RoleType.USER);
|
| 21503 |
ashik.ali |
246 |
userRole.setUserId(user.getId());
|
|
|
247 |
userRoleRepository.persist(userRole);
|
|
|
248 |
return responseSender.ok(ResponseCodeHolder.getMessage("USR_OK_1000"));
|
|
|
249 |
} catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
250 |
LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
|
|
|
251 |
return responseSender.badRequest(profitMandiBusinessException);
|
|
|
252 |
}
|
| 21278 |
ashik.ali |
253 |
}
|
| 21469 |
amit.gupta |
254 |
|
|
|
255 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_ALL, method = RequestMethod.GET)
|
| 22032 |
ashik.ali |
256 |
public ResponseEntity<?> getAll(HttpServletRequest request,
|
|
|
257 |
@RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber,
|
|
|
258 |
@RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize) {
|
| 21469 |
amit.gupta |
259 |
LOGGER.info("requested url : " + request.getRequestURL().toString());
|
| 21497 |
ashik.ali |
260 |
return responseSender.ok(userRepository.selectAll(pageNumber, pageSize));
|
| 21248 |
ashik.ali |
261 |
}
|
| 21469 |
amit.gupta |
262 |
|
|
|
263 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_ID, method = RequestMethod.GET)
|
|
|
264 |
public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id) {
|
|
|
265 |
LOGGER.info("requested url : " + request.getRequestURL().toString());
|
| 21248 |
ashik.ali |
266 |
try {
|
| 21448 |
ashik.ali |
267 |
return responseSender.ok(userRepository.selectById(id));
|
| 21469 |
amit.gupta |
268 |
} catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
269 |
LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
|
| 21448 |
ashik.ali |
270 |
return responseSender.badRequest(profitMandiBusinessException);
|
| 21248 |
ashik.ali |
271 |
}
|
|
|
272 |
}
|
| 21469 |
amit.gupta |
273 |
|
|
|
274 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_MOBILE_NUMBER, method = RequestMethod.GET)
|
|
|
275 |
public ResponseEntity<?> getByMobileNumber(HttpServletRequest request,
|
|
|
276 |
@RequestParam(name = "mobileNumber") String mobileNumber) {
|
|
|
277 |
LOGGER.info("requested url : " + request.getRequestURL().toString());
|
| 21248 |
ashik.ali |
278 |
try {
|
| 21448 |
ashik.ali |
279 |
return responseSender.ok(userRepository.selectByMobileNumber(mobileNumber));
|
| 21469 |
amit.gupta |
280 |
} catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
281 |
LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
|
| 21448 |
ashik.ali |
282 |
return responseSender.badRequest(profitMandiBusinessException);
|
| 21248 |
ashik.ali |
283 |
}
|
|
|
284 |
}
|
| 21469 |
amit.gupta |
285 |
|
| 21784 |
amit.gupta |
286 |
@ApiImplicitParams({
|
| 21501 |
amit.gupta |
287 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
|
|
288 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_ACTIVATE, method = RequestMethod.POST)
|
|
|
289 |
public ResponseEntity<?> activateUser(HttpServletRequest request,
|
| 21784 |
amit.gupta |
290 |
@RequestParam(name = "activationCode") String activationCode) throws Throwable {
|
| 21855 |
amit.gupta |
291 |
int userId = (int) request.getAttribute("userId");
|
| 21784 |
amit.gupta |
292 |
UserCart uc = userAccountRepository.getUserCart(userId);
|
|
|
293 |
return responseSender.ok(userService.updateActivation(userId, uc.getUserId(), activationCode));
|
|
|
294 |
}
|
| 21855 |
amit.gupta |
295 |
|
|
|
296 |
@ApiImplicitParams({
|
|
|
297 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
|
|
298 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_NOTIFICATIONS, method = RequestMethod.GET)
|
|
|
299 |
public ResponseEntity<?> getNofitications(HttpServletRequest request,
|
|
|
300 |
@RequestParam(name = "androidId") String androidId, @RequestParam(name = "pageNumber") int pageNumber,
|
|
|
301 |
@RequestParam(name = "pageSize") int pageSize) throws Throwable {
|
|
|
302 |
int userId = (int) request.getAttribute("userId");
|
|
|
303 |
String restResponse = null;
|
|
|
304 |
Map<String, String> params = new HashMap<>();
|
|
|
305 |
|
|
|
306 |
String uri = "/getAllNotifications";
|
|
|
307 |
params.put("user_id", userId + "");
|
|
|
308 |
params.put("android_id", androidId);
|
|
|
309 |
params.put("limit", pageSize + "");
|
|
|
310 |
params.put("offset", "" + ((pageNumber - 1) * pageSize));
|
|
|
311 |
try {
|
|
|
312 |
RestClient rc = new RestClient(SchemeType.HTTP, nodeHost, nodePort);
|
|
|
313 |
restResponse = rc.get(uri, params);
|
|
|
314 |
} catch (Exception | ProfitMandiBusinessException e) {
|
|
|
315 |
LOGGER.error("Unable to data from node server", e);
|
|
|
316 |
return responseSender.internalServerError(e);
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
JsonArray result_json = Json.parse(restResponse).asArray();
|
|
|
320 |
|
|
|
321 |
List<Notification> notifications = new ArrayList<>();
|
|
|
322 |
|
|
|
323 |
for (JsonValue j : result_json) {
|
|
|
324 |
notifications.add(toNotifiaction(j.asObject()));
|
|
|
325 |
}
|
|
|
326 |
|
|
|
327 |
return responseSender.ok(notifications);
|
|
|
328 |
}
|
|
|
329 |
|
|
|
330 |
private Notification toNotifiaction(JsonObject jsonObject) {
|
|
|
331 |
Notification n = (Notification) (new Gson().fromJson(jsonObject.toString(), Notification.class));
|
| 21995 |
amit.gupta |
332 |
if (n.getStatus().equals("opened") || n.getStatus().equals("referrer") || n.getStatus().equals("seen")) {
|
| 21855 |
amit.gupta |
333 |
n.setSeen(true);
|
|
|
334 |
}
|
|
|
335 |
return n;
|
|
|
336 |
}
|
|
|
337 |
|
| 21426 |
ashik.ali |
338 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_IS_EXIST_MOBILE_NUMBER, method = RequestMethod.GET)
|
| 21469 |
amit.gupta |
339 |
public ResponseEntity<?> isMobileNumberExist(HttpServletRequest request,
|
|
|
340 |
@RequestParam(name = "mobileNumber") String mobileNumber) {
|
|
|
341 |
LOGGER.info("requested url : " + request.getRequestURL().toString());
|
| 21448 |
ashik.ali |
342 |
return responseSender.ok(userRepository.isExistByMobileNumber(mobileNumber));
|
| 21426 |
ashik.ali |
343 |
}
|
| 21469 |
amit.gupta |
344 |
|
|
|
345 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_EMAIL_ID, method = RequestMethod.GET)
|
|
|
346 |
public ResponseEntity<?> getByEmailId(HttpServletRequest request, @RequestParam(name = "emailId") String emailId) {
|
|
|
347 |
LOGGER.info("requested url : " + request.getRequestURL().toString());
|
| 21248 |
ashik.ali |
348 |
try {
|
| 21448 |
ashik.ali |
349 |
return responseSender.ok(userRepository.selectByEmailId(emailId));
|
| 21469 |
amit.gupta |
350 |
} catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
351 |
LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
|
| 21448 |
ashik.ali |
352 |
return responseSender.badRequest(profitMandiBusinessException);
|
| 21248 |
ashik.ali |
353 |
}
|
|
|
354 |
}
|
| 21469 |
amit.gupta |
355 |
|
|
|
356 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_ADD, method = RequestMethod.POST)
|
|
|
357 |
public ResponseEntity<?> addRole(HttpServletRequest request, @RequestBody UserAddRoleRequest userAddRoleRequest) {
|
|
|
358 |
LOGGER.info("requested url : " + request.getRequestURL().toString());
|
| 21248 |
ashik.ali |
359 |
try {
|
| 21426 |
ashik.ali |
360 |
User user = userRepository.selectById(userAddRoleRequest.getUserId());
|
| 21469 |
amit.gupta |
361 |
|
| 22032 |
ashik.ali |
362 |
/*
|
|
|
363 |
* Role role = null; try { role =
|
|
|
364 |
* roleRepository.selectByNameAndType(userAddRoleRequest.getRole().
|
|
|
365 |
* getName(), userAddRoleRequest.getRole().getType()); } catch
|
|
|
366 |
* (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
367 |
* role = new Role();
|
|
|
368 |
* role.setName(userAddRoleRequest.getRole().getName());
|
|
|
369 |
* role.setType(userAddRoleRequest.getRole().getType());
|
|
|
370 |
* roleRepository.persist(role); }
|
|
|
371 |
*/
|
| 21426 |
ashik.ali |
372 |
Permission permission = new Permission();
|
| 22011 |
ashik.ali |
373 |
permission.setType(userAddRoleRequest.getPermissionType());
|
|
|
374 |
permission.setRoleType(userAddRoleRequest.getRoleType());
|
| 21426 |
ashik.ali |
375 |
permissionRepository.persist(permission);
|
|
|
376 |
UserRole userRole = new UserRole();
|
| 22011 |
ashik.ali |
377 |
userRole.setRoleType(userAddRoleRequest.getRoleType());
|
| 21426 |
ashik.ali |
378 |
userRole.setUserId(user.getId());
|
|
|
379 |
userRoleRepository.persist(userRole);
|
| 21448 |
ashik.ali |
380 |
return responseSender.ok("");
|
| 21469 |
amit.gupta |
381 |
} catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
382 |
LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
|
| 21448 |
ashik.ali |
383 |
return responseSender.badRequest(profitMandiBusinessException);
|
| 21248 |
ashik.ali |
384 |
}
|
|
|
385 |
}
|
| 21469 |
amit.gupta |
386 |
|
|
|
387 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_REMOVE, method = RequestMethod.DELETE)
|
| 22011 |
ashik.ali |
388 |
public ResponseEntity<?> removeRole(HttpServletRequest request, @RequestParam(name = "roleType") RoleType roleType,
|
| 21469 |
amit.gupta |
389 |
@RequestParam(name = "userId") int userId) {
|
|
|
390 |
LOGGER.info("requested url : " + request.getRequestURL().toString());
|
| 21248 |
ashik.ali |
391 |
try {
|
| 21426 |
ashik.ali |
392 |
userRepository.selectById(userId);
|
| 22011 |
ashik.ali |
393 |
userRoleRepository.deleteByUserAndRoleType(userId, roleType);
|
|
|
394 |
permissionRepository.deleteByRoleType(roleType);
|
| 21448 |
ashik.ali |
395 |
return responseSender.ok("");
|
| 21469 |
amit.gupta |
396 |
} catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
397 |
LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
|
| 21448 |
ashik.ali |
398 |
return responseSender.badRequest(profitMandiBusinessException);
|
| 21248 |
ashik.ali |
399 |
}
|
|
|
400 |
}
|
| 21469 |
amit.gupta |
401 |
|
|
|
402 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_ALL, method = RequestMethod.GET)
|
|
|
403 |
public ResponseEntity<?> getAllRoles(HttpServletRequest request, @RequestParam(name = "id") int id) {
|
|
|
404 |
LOGGER.info("requested url : " + request.getRequestURL().toString());
|
| 21448 |
ashik.ali |
405 |
return responseSender.ok(userRoleRepository.selectRolesByUserId(id));
|
| 21248 |
ashik.ali |
406 |
}
|
| 21414 |
kshitij.so |
407 |
|
|
|
408 |
@RequestMapping(value = ProfitMandiConstants.URL_ADMIN_TOKEN, method = RequestMethod.POST)
|
| 21469 |
amit.gupta |
409 |
public ResponseEntity<?> getAdminToken(HttpServletRequest request,
|
|
|
410 |
@RequestParam(name = "adminToken") String adminToken, @RequestParam(name = "emailId") String emailId) {
|
|
|
411 |
LOGGER.info("requested url : " + request.getRequestURL().toString());
|
|
|
412 |
if (!adminToken.equals(validAdminToken)) {
|
|
|
413 |
final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(LocalDateTime.now(),
|
|
|
414 |
request.getRequestURL().toString(), HttpStatus.FORBIDDEN.toString(), HttpStatus.FORBIDDEN,
|
|
|
415 |
ResponseStatus.FAILURE, null);
|
| 21414 |
kshitij.so |
416 |
return new ResponseEntity<>(profitMandiResponse, HttpStatus.FORBIDDEN);
|
|
|
417 |
}
|
| 21469 |
amit.gupta |
418 |
|
| 21414 |
kshitij.so |
419 |
Map<String, Object> responseMap = new HashMap<>(2);
|
| 21469 |
amit.gupta |
420 |
try {
|
| 21414 |
kshitij.so |
421 |
User user = userRepository.selectByEmailId(emailId);
|
| 22011 |
ashik.ali |
422 |
List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
|
|
|
423 |
String[] roleTypes = new String[userRoles.size()];
|
| 21414 |
kshitij.so |
424 |
int index = 0;
|
| 22011 |
ashik.ali |
425 |
for (UserRole userRole : userRoles) {
|
|
|
426 |
roleTypes[index++] = userRole.getRoleType().toString();
|
| 21414 |
kshitij.so |
427 |
}
|
|
|
428 |
responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), roleTypes));
|
|
|
429 |
responseMap.put(ProfitMandiConstants.REGISTERED, true);
|
| 21469 |
amit.gupta |
430 |
} catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
431 |
responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(emailId));
|
| 21414 |
kshitij.so |
432 |
responseMap.put(ProfitMandiConstants.REGISTERED, false);
|
|
|
433 |
}
|
| 21448 |
ashik.ali |
434 |
return responseSender.ok(responseMap);
|
| 21469 |
amit.gupta |
435 |
|
| 21414 |
kshitij.so |
436 |
}
|
| 22032 |
ashik.ali |
437 |
|
| 21248 |
ashik.ali |
438 |
}
|