Subversion Repositories SmartDukaan

Rev

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

Rev 21784 Rev 21855
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
3
import java.time.LocalDateTime;
3
import java.time.LocalDateTime;
-
 
4
import java.util.ArrayList;
4
import java.util.HashMap;
5
import java.util.HashMap;
-
 
6
import java.util.List;
5
import java.util.Map;
7
import java.util.Map;
6
import java.util.Set;
8
import java.util.Set;
7
import java.util.function.Predicate;
9
import java.util.function.Predicate;
8
 
10
 
9
import javax.servlet.http.HttpServletRequest;
11
import javax.servlet.http.HttpServletRequest;
Line 19... Line 21...
19
import org.springframework.web.bind.annotation.RequestBody;
21
import org.springframework.web.bind.annotation.RequestBody;
20
import org.springframework.web.bind.annotation.RequestMapping;
22
import org.springframework.web.bind.annotation.RequestMapping;
21
import org.springframework.web.bind.annotation.RequestMethod;
23
import org.springframework.web.bind.annotation.RequestMethod;
22
import org.springframework.web.bind.annotation.RequestParam;
24
import org.springframework.web.bind.annotation.RequestParam;
23
 
25
 
-
 
26
import com.eclipsesource.json.Json;
-
 
27
import com.eclipsesource.json.JsonArray;
-
 
28
import com.eclipsesource.json.JsonObject;
-
 
29
import com.eclipsesource.json.JsonValue;
-
 
30
import com.google.gson.Gson;
24
import com.spice.profitmandi.common.ResponseCodeHolder;
31
import com.spice.profitmandi.common.ResponseCodeHolder;
-
 
32
import com.spice.profitmandi.common.enumuration.SchemeType;
25
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
33
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
26
import com.spice.profitmandi.common.model.ProfitMandiConstants;
34
import com.spice.profitmandi.common.model.ProfitMandiConstants;
27
import com.spice.profitmandi.common.model.ProfitMandiResponse;
35
import com.spice.profitmandi.common.model.ProfitMandiResponse;
28
import com.spice.profitmandi.common.model.ResponseStatus;
36
import com.spice.profitmandi.common.model.ResponseStatus;
29
import com.spice.profitmandi.common.model.UserInfo;
37
import com.spice.profitmandi.common.model.UserInfo;
30
import com.spice.profitmandi.common.util.JWTUtil;
38
import com.spice.profitmandi.common.util.JWTUtil;
-
 
39
import com.spice.profitmandi.common.web.client.RestClient;
31
import com.spice.profitmandi.common.web.util.ResponseSender;
40
import com.spice.profitmandi.common.web.util.ResponseSender;
32
import com.spice.profitmandi.dao.entity.dtr.Permission;
41
import com.spice.profitmandi.dao.entity.dtr.Permission;
33
import com.spice.profitmandi.dao.entity.dtr.Retailer;
42
import com.spice.profitmandi.dao.entity.dtr.Retailer;
34
import com.spice.profitmandi.dao.entity.dtr.Role;
43
import com.spice.profitmandi.dao.entity.dtr.Role;
35
import com.spice.profitmandi.dao.entity.dtr.User;
44
import com.spice.profitmandi.dao.entity.dtr.User;
Line 46... Line 55...
46
import com.spice.profitmandi.service.UserService;
55
import com.spice.profitmandi.service.UserService;
47
import com.spice.profitmandi.web.enumuration.UserStatus;
56
import com.spice.profitmandi.web.enumuration.UserStatus;
48
import com.spice.profitmandi.web.processor.GoogleLoginProcessor;
57
import com.spice.profitmandi.web.processor.GoogleLoginProcessor;
49
import com.spice.profitmandi.web.req.UserAddRoleRequest;
58
import com.spice.profitmandi.web.req.UserAddRoleRequest;
50
import com.spice.profitmandi.web.req.UserRequest;
59
import com.spice.profitmandi.web.req.UserRequest;
-
 
60
import com.spice.profitmandi.web.res.DealObjectResponse;
-
 
61
import com.spice.profitmandi.web.res.DealsResponse;
-
 
62
import com.spice.profitmandi.web.res.Notification;
51
 
63
 
52
import io.swagger.annotations.ApiImplicitParam;
64
import io.swagger.annotations.ApiImplicitParam;
53
import io.swagger.annotations.ApiImplicitParams;
65
import io.swagger.annotations.ApiImplicitParams;
54
 
66
 
55
/**
67
/**
Line 63... Line 75...
63
	@Autowired
75
	@Autowired
64
	ResponseSender<?> responseSender;
76
	ResponseSender<?> responseSender;
65
 
77
 
66
	private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
78
	private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
67
 
79
 
-
 
80
	@Value("${notifications.api.host}")
-
 
81
	private String nodeHost;
-
 
82
	@Value("${notifications.api.port}")
-
 
83
	private int nodePort;
-
 
84
 
68
	@Value("${admin.token}")
85
	@Value("${admin.token}")
69
	private String validAdminToken;
86
	private String validAdminToken;
70
 
87
 
71
	@Autowired
88
	@Autowired
72
	UserRepository userRepository;
89
	UserRepository userRepository;
Line 86... Line 103...
86
	@Autowired
103
	@Autowired
87
	PermissionRepository permissionRepository;
104
	PermissionRepository permissionRepository;
88
 
105
 
89
	@Autowired
106
	@Autowired
90
	GoogleLoginProcessor googleLoginProcessor;
107
	GoogleLoginProcessor googleLoginProcessor;
91
	
108
 
92
	@Autowired
109
	@Autowired
93
	UserToRetailerMigrationUtil userToRetailerMigrationUtil;
110
	UserToRetailerMigrationUtil userToRetailerMigrationUtil;
94
	
111
 
95
	@Autowired
112
	@Autowired
96
	UserService userService;
113
	UserService userService;
97
 
114
 
98
	@SuppressWarnings("unchecked")
115
	@SuppressWarnings("unchecked")
99
	@RequestMapping(value = ProfitMandiConstants.URL_USER_GOOGLE_LOGIN, method = RequestMethod.POST)
116
	@RequestMapping(value = ProfitMandiConstants.URL_USER_GOOGLE_LOGIN, method = RequestMethod.POST)
Line 127... Line 144...
127
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
144
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
128
	public ResponseEntity<?> tokenInfo(HttpServletRequest request) throws Throwable {
145
	public ResponseEntity<?> tokenInfo(HttpServletRequest request) throws Throwable {
129
		Map<String, Object> responseMap = new HashMap<>();
146
		Map<String, Object> responseMap = new HashMap<>();
130
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
147
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
131
		User user = null;
148
		User user = null;
132
		if(userInfo.getUserId()>-1){
149
		if (userInfo.getUserId() > -1) {
133
			user = userRepository.selectById(userInfo.getUserId());
150
			user = userRepository.selectById(userInfo.getUserId());
134
		} else {
151
		} else {
135
			try {
152
			try {
136
				user = userRepository.selectByEmailId(userInfo.getEmail());
153
				user = userRepository.selectByEmailId(userInfo.getEmail());
137
			} catch (ProfitMandiBusinessException e1) {
154
			} catch (ProfitMandiBusinessException e1) {
Line 139... Line 156...
139
			}
156
			}
140
		}
157
		}
141
		if (user != null) {
158
		if (user != null) {
142
			responseMap.put(ProfitMandiConstants.EMAIL_ID, user.getEmailId());
159
			responseMap.put(ProfitMandiConstants.EMAIL_ID, user.getEmailId());
143
			responseMap.put(ProfitMandiConstants.USER_ID, user.getId());
160
			responseMap.put(ProfitMandiConstants.USER_ID, user.getId());
144
			
161
 
145
			Set<Role> roles = user.getRoles();
162
			Set<Role> roles = user.getRoles();
146
			//generate new token if roles size is different
163
			// generate new token if roles size is different
147
			if(userInfo.getRoleNames() == null || roles.size() != userInfo.getRoleNames().size()) {
164
			if (userInfo.getRoleNames() == null || roles.size() != userInfo.getRoleNames().size()) {
148
				String[] roleTypes = new String[roles.size()];
165
				String[] roleTypes = new String[roles.size()];
149
				int index = 0;
166
				int index = 0;
150
				for (Role role : roles) {
167
				for (Role role : roles) {
151
					roleTypes[index++] = role.getType().toString();
168
					roleTypes[index++] = role.getType().toString();
152
				}
169
				}
153
				String newToken = JWTUtil.create(user.getId(), roleTypes);
170
				String newToken = JWTUtil.create(user.getId(), roleTypes);
154
				responseMap.put("newAuthToken", newToken);
171
				responseMap.put("newAuthToken", newToken);
155
			}
172
			}
156
			
173
 
157
			// if user is retailer
174
			// if user is retailer
158
			if (user.getRoles().stream().anyMatch(new Predicate<Role>() {
175
			if (user.getRoles().stream().anyMatch(new Predicate<Role>() {
159
				@Override
176
				@Override
160
				public boolean test(Role t) {
177
				public boolean test(Role t) {
161
					return t.getType().equals(RoleType.RETAILER);
178
					return t.getType().equals(RoleType.RETAILER);
162
				}
179
				}
163
			})) {
180
			})) {
164
				UserCart uc  = userAccountRepository.getUserCart(userInfo.getUserId());
181
				UserCart uc = userAccountRepository.getUserCart(userInfo.getUserId());
165
				Retailer retailer  = retailerRepository.selectById(uc.getUserId());
182
				Retailer retailer = retailerRepository.selectById(uc.getUserId());
166
				// if retailer is activated 1 then verified retailer
183
				// if retailer is activated 1 then verified retailer
167
				// else if migrated is 1 then old retailer
184
				// else if migrated is 1 then old retailer
168
				// else retailer is not verifed
185
				// else retailer is not verifed
169
				if (retailer.isActive()) {
186
				if (retailer.isActive()) {
170
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.VERIFIED_RETAILER.getValue());
187
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.VERIFIED_RETAILER.getValue());
171
				} else if (retailer.isMigrated()){
188
				} else if (retailer.isMigrated()) {
172
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.RETAILER.getValue());
189
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.RETAILER.getValue());
173
				} else {
190
				} else {
174
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_VERIFIED_RETAILER.getValue());
191
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_VERIFIED_RETAILER.getValue());
175
				}
192
				}
176
			} else if (user.getRoles().stream().anyMatch(new Predicate<Role>() {
193
			} else if (user.getRoles().stream().anyMatch(new Predicate<Role>() {
Line 183... Line 200...
183
			}
200
			}
184
		} else {
201
		} else {
185
			responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_REGISTERED.getValue());
202
			responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_REGISTERED.getValue());
186
			responseMap.put(ProfitMandiConstants.EMAIL_ID, userInfo.getEmail());
203
			responseMap.put(ProfitMandiConstants.EMAIL_ID, userInfo.getEmail());
187
		}
204
		}
188
	
205
 
189
		return responseSender.ok(responseMap);
206
		return responseSender.ok(responseMap);
190
	}
207
	}
191
 
208
 
192
	@RequestMapping(value = ProfitMandiConstants.URL_USER, method = RequestMethod.POST)
209
	@RequestMapping(value = ProfitMandiConstants.URL_USER, method = RequestMethod.POST)
193
	@ApiImplicitParams({
210
	@ApiImplicitParams({
194
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
211
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
195
	public ResponseEntity<?> createUser(HttpServletRequest request, @RequestBody UserRequest userRequest) throws Throwable{
212
	public ResponseEntity<?> createUser(HttpServletRequest request, @RequestBody UserRequest userRequest)
-
 
213
			throws Throwable {
196
		LOGGER.info("requested url : " + request.getRequestURL().toString());
214
		LOGGER.info("requested url : " + request.getRequestURL().toString());
197
		User user = new User();
215
		User user = new User();
198
		user.setFirstName(userRequest.getFirstName());
216
		user.setFirstName(userRequest.getFirstName());
199
		user.setLastName(userRequest.getLastName());
217
		user.setLastName(userRequest.getLastName());
200
		user.setCity(userRequest.getCity());
218
		user.setCity(userRequest.getCity());
Line 208... Line 226...
208
		user.setGroup_id(1);
226
		user.setGroup_id(1);
209
		user.setStatus(0);
227
		user.setStatus(0);
210
		user.setActivated(false);
228
		user.setActivated(false);
211
		user.setCreateTimestamp(LocalDateTime.now());
229
		user.setCreateTimestamp(LocalDateTime.now());
212
		user.setUpdateTimestamp(LocalDateTime.now());
230
		user.setUpdateTimestamp(LocalDateTime.now());
213
		try{
231
		try {
214
			userRepository.persist(user);
232
			userRepository.persist(user);
215
 
233
 
216
			UserRole userRole = new UserRole();
234
			UserRole userRole = new UserRole();
217
			userRole.setRoleId(RoleType.USER.getValue());
235
			userRole.setRoleId(RoleType.USER.getValue());
218
			userRole.setUserId(user.getId());
236
			userRole.setUserId(user.getId());
Line 223... Line 241...
223
			return responseSender.badRequest(profitMandiBusinessException);
241
			return responseSender.badRequest(profitMandiBusinessException);
224
		}
242
		}
225
	}
243
	}
226
 
244
 
227
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ALL, method = RequestMethod.GET)
245
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ALL, method = RequestMethod.GET)
-
 
246
	public ResponseEntity<?> getAll(HttpServletRequest request,
228
	public ResponseEntity<?> getAll(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber, @RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize) {
247
			@RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber,
-
 
248
			@RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize) {
229
		LOGGER.info("requested url : " + request.getRequestURL().toString());
249
		LOGGER.info("requested url : " + request.getRequestURL().toString());
230
		return responseSender.ok(userRepository.selectAll(pageNumber, pageSize));
250
		return responseSender.ok(userRepository.selectAll(pageNumber, pageSize));
231
	}
251
	}
232
 
252
 
233
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ID, method = RequestMethod.GET)
253
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ID, method = RequestMethod.GET)
Line 250... Line 270...
250
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
270
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
251
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
271
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
252
			return responseSender.badRequest(profitMandiBusinessException);
272
			return responseSender.badRequest(profitMandiBusinessException);
253
		}
273
		}
254
	}
274
	}
255
	
-
 
256
	
-
 
257
 
275
 
258
	@ApiImplicitParams({
276
	@ApiImplicitParams({
259
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
277
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
260
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ACTIVATE, method = RequestMethod.POST)
278
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ACTIVATE, method = RequestMethod.POST)
261
	public ResponseEntity<?> activateUser(HttpServletRequest request,
279
	public ResponseEntity<?> activateUser(HttpServletRequest request,
262
			@RequestParam(name = "activationCode") String activationCode) throws Throwable {
280
			@RequestParam(name = "activationCode") String activationCode) throws Throwable {
263
		int userId = (int)request.getAttribute("userId");
281
		int userId = (int) request.getAttribute("userId");
264
		UserCart uc = userAccountRepository.getUserCart(userId);
282
		UserCart uc = userAccountRepository.getUserCart(userId);
265
		return responseSender.ok(userService.updateActivation(userId, uc.getUserId(), activationCode));
283
		return responseSender.ok(userService.updateActivation(userId, uc.getUserId(), activationCode));
266
	}
284
	}
-
 
285
 
-
 
286
	@ApiImplicitParams({
-
 
287
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
-
 
288
	@RequestMapping(value = ProfitMandiConstants.URL_USER_NOTIFICATIONS, method = RequestMethod.GET)
-
 
289
	public ResponseEntity<?> getNofitications(HttpServletRequest request,
-
 
290
			@RequestParam(name = "androidId") String androidId, @RequestParam(name = "pageNumber") int pageNumber,
-
 
291
			@RequestParam(name = "pageSize") int pageSize) throws Throwable {
-
 
292
		int userId = (int) request.getAttribute("userId");
-
 
293
		String restResponse = null;
-
 
294
		Map<String, String> params = new HashMap<>();
-
 
295
 
-
 
296
		String uri = "/getAllNotifications";
-
 
297
		params.put("user_id", userId + "");
-
 
298
		params.put("android_id", androidId);
-
 
299
		params.put("limit", pageSize + "");
-
 
300
		params.put("offset", "" + ((pageNumber - 1) * pageSize));
-
 
301
		try {
-
 
302
			RestClient rc = new RestClient(SchemeType.HTTP, nodeHost, nodePort);
-
 
303
			restResponse = rc.get(uri, params);
-
 
304
		} catch (Exception | ProfitMandiBusinessException e) {
-
 
305
			LOGGER.error("Unable to data from node server", e);
-
 
306
			return responseSender.internalServerError(e);
-
 
307
		}
-
 
308
 
-
 
309
		JsonArray result_json = Json.parse(restResponse).asArray();
-
 
310
 
-
 
311
		List<Notification> notifications = new ArrayList<>();
-
 
312
 
-
 
313
		for (JsonValue j : result_json) {
-
 
314
			notifications.add(toNotifiaction(j.asObject()));
-
 
315
		}
-
 
316
 
-
 
317
		return responseSender.ok(notifications);
-
 
318
	}
-
 
319
 
-
 
320
	private Notification toNotifiaction(JsonObject jsonObject) {
-
 
321
		Notification n = (Notification) (new Gson().fromJson(jsonObject.toString(), Notification.class));
-
 
322
		if(n.getStatus().equals("opened") || n.getStatus().equals("referrer") || n.getStatus().equals("seen") ) {
-
 
323
			n.setSeen(true);
-
 
324
		}
-
 
325
		return n;
267
	
326
	}
-
 
327
 
268
	@RequestMapping(value = ProfitMandiConstants.URL_USER_IS_EXIST_MOBILE_NUMBER, method = RequestMethod.GET)
328
	@RequestMapping(value = ProfitMandiConstants.URL_USER_IS_EXIST_MOBILE_NUMBER, method = RequestMethod.GET)
269
	public ResponseEntity<?> isMobileNumberExist(HttpServletRequest request,
329
	public ResponseEntity<?> isMobileNumberExist(HttpServletRequest request,
270
			@RequestParam(name = "mobileNumber") String mobileNumber) {
330
			@RequestParam(name = "mobileNumber") String mobileNumber) {
271
		LOGGER.info("requested url : " + request.getRequestURL().toString());
331
		LOGGER.info("requested url : " + request.getRequestURL().toString());
272
		return responseSender.ok(userRepository.isExistByMobileNumber(mobileNumber));
332
		return responseSender.ok(userRepository.isExistByMobileNumber(mobileNumber));
Line 363... Line 423...
363
			responseMap.put(ProfitMandiConstants.REGISTERED, false);
423
			responseMap.put(ProfitMandiConstants.REGISTERED, false);
364
		}
424
		}
365
		return responseSender.ok(responseMap);
425
		return responseSender.ok(responseMap);
366
 
426
 
367
	}
427
	}
368
	
428
 
369
	@RequestMapping(value = ProfitMandiConstants.URL_ADMIN_MIGRATE, method = RequestMethod.GET)
429
	@RequestMapping(value = ProfitMandiConstants.URL_ADMIN_MIGRATE, method = RequestMethod.GET)
370
	public ResponseEntity<?> migrate(HttpServletRequest request) {
430
	public ResponseEntity<?> migrate(HttpServletRequest request) {
371
		LOGGER.info("requested url : " + request.getRequestURL().toString());
431
		LOGGER.info("requested url : " + request.getRequestURL().toString());
372
		userToRetailerMigrationUtil.migrate();
432
		userToRetailerMigrationUtil.migrate();
373
		return responseSender.ok(ResponseCodeHolder.getMessage("OK_1000"));
433
		return responseSender.ok(ResponseCodeHolder.getMessage("OK_1000"));