Subversion Repositories SmartDukaan

Rev

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

Rev 21995 Rev 22011
Line 3... Line 3...
3
import java.time.LocalDateTime;
3
import java.time.LocalDateTime;
4
import java.util.ArrayList;
4
import java.util.ArrayList;
5
import java.util.HashMap;
5
import java.util.HashMap;
6
import java.util.List;
6
import java.util.List;
7
import java.util.Map;
7
import java.util.Map;
8
import java.util.Set;
-
 
9
import java.util.function.Predicate;
-
 
10
 
8
 
11
import javax.servlet.http.HttpServletRequest;
9
import javax.servlet.http.HttpServletRequest;
12
 
10
 
13
import org.slf4j.Logger;
11
import org.slf4j.Logger;
14
import org.slf4j.LoggerFactory;
12
import org.slf4j.LoggerFactory;
Line 21... Line 19...
21
import org.springframework.web.bind.annotation.RequestBody;
19
import org.springframework.web.bind.annotation.RequestBody;
22
import org.springframework.web.bind.annotation.RequestMapping;
20
import org.springframework.web.bind.annotation.RequestMapping;
23
import org.springframework.web.bind.annotation.RequestMethod;
21
import org.springframework.web.bind.annotation.RequestMethod;
24
import org.springframework.web.bind.annotation.RequestParam;
22
import org.springframework.web.bind.annotation.RequestParam;
25
 
23
 
26
import com.eclipsesource.json.Json;
-
 
-
 
24
 
27
import com.eclipsesource.json.JsonArray;
25
import com.eclipsesource.json.JsonArray;
28
import com.eclipsesource.json.JsonObject;
26
import com.eclipsesource.json.JsonObject;
29
import com.eclipsesource.json.JsonValue;
27
import com.eclipsesource.json.JsonValue;
30
import com.google.gson.Gson;
28
import com.google.gson.Gson;
31
import com.spice.profitmandi.common.ResponseCodeHolder;
29
import com.spice.profitmandi.common.ResponseCodeHolder;
Line 38... Line 36...
38
import com.spice.profitmandi.common.util.JWTUtil;
36
import com.spice.profitmandi.common.util.JWTUtil;
39
import com.spice.profitmandi.common.web.client.RestClient;
37
import com.spice.profitmandi.common.web.client.RestClient;
40
import com.spice.profitmandi.common.web.util.ResponseSender;
38
import com.spice.profitmandi.common.web.util.ResponseSender;
41
import com.spice.profitmandi.dao.entity.dtr.Permission;
39
import com.spice.profitmandi.dao.entity.dtr.Permission;
42
import com.spice.profitmandi.dao.entity.dtr.Retailer;
40
import com.spice.profitmandi.dao.entity.dtr.Retailer;
43
import com.spice.profitmandi.dao.entity.dtr.Role;
-
 
44
import com.spice.profitmandi.dao.entity.dtr.User;
41
import com.spice.profitmandi.dao.entity.dtr.User;
45
import com.spice.profitmandi.dao.entity.dtr.UserRole;
42
import com.spice.profitmandi.dao.entity.dtr.UserRole;
46
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
43
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
47
import com.spice.profitmandi.dao.model.UserCart;
44
import com.spice.profitmandi.dao.model.UserCart;
48
import com.spice.profitmandi.dao.repository.dtr.PermissionRepository;
45
import com.spice.profitmandi.dao.repository.dtr.PermissionRepository;
49
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
46
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
50
import com.spice.profitmandi.dao.repository.dtr.RoleRepository;
-
 
51
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
47
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
52
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
48
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
53
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
49
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
54
import com.spice.profitmandi.dao.util.UserToRetailerMigrationUtil;
50
import com.spice.profitmandi.dao.util.UserToRetailerMigrationUtil;
55
import com.spice.profitmandi.service.UserService;
51
import com.spice.profitmandi.service.UserService;
56
import com.spice.profitmandi.web.enumuration.UserStatus;
52
import com.spice.profitmandi.web.enumuration.UserStatus;
57
import com.spice.profitmandi.web.processor.GoogleLoginProcessor;
53
import com.spice.profitmandi.web.processor.GoogleLoginProcessor;
58
import com.spice.profitmandi.web.req.UserAddRoleRequest;
54
import com.spice.profitmandi.web.req.UserAddRoleRequest;
59
import com.spice.profitmandi.web.req.UserRequest;
55
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;
56
import com.spice.profitmandi.web.res.Notification;
63
 
57
 
64
import io.swagger.annotations.ApiImplicitParam;
58
import io.swagger.annotations.ApiImplicitParam;
65
import io.swagger.annotations.ApiImplicitParams;
59
import io.swagger.annotations.ApiImplicitParams;
66
 
60
 
Line 90... Line 84...
90
 
84
 
91
	@Autowired
85
	@Autowired
92
	RetailerRepository retailerRepository;
86
	RetailerRepository retailerRepository;
93
 
87
 
94
	@Autowired
88
	@Autowired
95
	RoleRepository roleRepository;
-
 
96
 
-
 
97
	@Autowired
-
 
98
	UserRoleRepository userRoleRepository;
89
	UserRoleRepository userRoleRepository;
99
 
90
 
100
	@Autowired
91
	@Autowired
101
	UserAccountRepository userAccountRepository;
92
	UserAccountRepository userAccountRepository;
102
 
93
 
Line 156... Line 147...
156
			}
147
			}
157
		}
148
		}
158
		if (user != null) {
149
		if (user != null) {
159
			responseMap.put(ProfitMandiConstants.EMAIL_ID, user.getEmailId());
150
			responseMap.put(ProfitMandiConstants.EMAIL_ID, user.getEmailId());
160
			responseMap.put(ProfitMandiConstants.USER_ID, user.getId());
151
			responseMap.put(ProfitMandiConstants.USER_ID, user.getId());
161
 
152
			
-
 
153
			List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
162
			Set<Role> roles = user.getRoles();
154
			String[] roleTypes = new String[userRoles.size()];
-
 
155
			
-
 
156
			
-
 
157
			
-
 
158
			
163
			// generate new token if roles size is different
159
			//generate new token if roles size is different
164
			if (userInfo.getRoleNames() == null || roles.size() != userInfo.getRoleNames().size()) {
160
			if(userInfo.getRoleNames() == null || userRoles.size() != userInfo.getRoleNames().size()) {
165
				String[] roleTypes = new String[roles.size()];
161
				//String[] roleTypes = new String[userRoles.size()];
166
				int index = 0;
162
				int index = 0;
167
				for (Role role : roles) {
163
				for (UserRole userRole : userRoles) {
168
					roleTypes[index++] = role.getType().toString();
164
					roleTypes[index++] = userRole.getRoleType().toString();
169
				}
165
				}
170
				String newToken = JWTUtil.create(user.getId(), roleTypes);
166
				String newToken = JWTUtil.create(user.getId(), roleTypes);
171
				responseMap.put("newAuthToken", newToken);
167
				responseMap.put("newAuthToken", newToken);
172
			}
168
			}
173
 
169
			
174
			// if user is retailer
170
			// if user is retailer
175
			if (user.getRoles().stream().anyMatch(new Predicate<Role>() {
-
 
176
				@Override
-
 
177
				public boolean test(Role t) {
-
 
178
					return t.getType().equals(RoleType.RETAILER);
-
 
179
				}
-
 
180
			})) {
-
 
181
				UserCart uc = userAccountRepository.getUserCart(userInfo.getUserId());
171
			UserCart uc  = userAccountRepository.getUserCart(userInfo.getUserId());
182
				Retailer retailer = retailerRepository.selectById(uc.getUserId());
172
				Retailer retailer  = retailerRepository.selectById(uc.getUserId());
183
				// if retailer is activated 1 then verified retailer
173
				// if retailer is activated 1 then verified retailer
184
				// else if migrated is 1 then old retailer
174
				// else if migrated is 1 then old retailer
185
				// else retailer is not verifed
175
				// else retailer is not verifed
186
				if (retailer.isActive()) {
176
				if (retailer.isActive()) {
187
					if(retailer.isFofo()) {
-
 
188
						responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.FOFO.getValue());
-
 
189
					} else {
-
 
190
						responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.VERIFIED_RETAILER.getValue());
177
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.VERIFIED_RETAILER.getValue());
191
					}
-
 
192
				} else if (retailer.isMigrated()) {
178
				} else if (retailer.isMigrated()){
193
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.RETAILER.getValue());
179
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.RETAILER.getValue());
194
				} else {
180
				} else {
195
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_VERIFIED_RETAILER.getValue());
181
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_VERIFIED_RETAILER.getValue());
196
				}
182
				}
197
			} else if (user.getRoles().stream().anyMatch(new Predicate<Role>() {
-
 
198
				@Override
-
 
199
				public boolean test(Role t) {
-
 
200
					return t.getType().equals(RoleType.USER);
-
 
201
				}
-
 
202
			})) {
183
			/*if () {
203
				responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.REGISTERED.getValue());
184
				responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.REGISTERED.getValue());
204
			}
185
			}*/
205
		} else {
186
		} else {
206
			responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_REGISTERED.getValue());
187
			responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_REGISTERED.getValue());
207
			responseMap.put(ProfitMandiConstants.EMAIL_ID, userInfo.getEmail());
188
			responseMap.put(ProfitMandiConstants.EMAIL_ID, userInfo.getEmail());
208
		}
189
		}
209
 
190
	
210
		return responseSender.ok(responseMap);
191
		return responseSender.ok(responseMap);
211
	}
192
	}
212
 
193
 
213
	@RequestMapping(value = ProfitMandiConstants.URL_USER, method = RequestMethod.POST)
194
	@RequestMapping(value = ProfitMandiConstants.URL_USER, method = RequestMethod.POST)
214
	@ApiImplicitParams({
195
	@ApiImplicitParams({
215
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
196
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
216
	public ResponseEntity<?> createUser(HttpServletRequest request, @RequestBody UserRequest userRequest)
197
	public ResponseEntity<?> createUser(HttpServletRequest request, @RequestBody UserRequest userRequest) throws Throwable{
217
			throws Throwable {
-
 
218
		LOGGER.info("requested url : " + request.getRequestURL().toString());
198
		LOGGER.info("requested url : " + request.getRequestURL().toString());
219
		User user = new User();
199
		User user = new User();
220
		user.setFirstName(userRequest.getFirstName());
200
		user.setFirstName(userRequest.getFirstName());
221
		user.setLastName(userRequest.getLastName());
201
		user.setLastName(userRequest.getLastName());
222
		user.setCity(userRequest.getCity());
202
		user.setCity(userRequest.getCity());
Line 230... Line 210...
230
		user.setGroup_id(1);
210
		user.setGroup_id(1);
231
		user.setStatus(0);
211
		user.setStatus(0);
232
		user.setActivated(false);
212
		user.setActivated(false);
233
		user.setCreateTimestamp(LocalDateTime.now());
213
		user.setCreateTimestamp(LocalDateTime.now());
234
		user.setUpdateTimestamp(LocalDateTime.now());
214
		user.setUpdateTimestamp(LocalDateTime.now());
235
		try {
215
		try{
236
			userRepository.persist(user);
216
			userRepository.persist(user);
237
 
-
 
238
			UserRole userRole = new UserRole();
217
			UserRole userRole = new UserRole();
239
			userRole.setRoleId(RoleType.USER.getValue());
218
			userRole.setRoleType(RoleType.USER);
240
			userRole.setUserId(user.getId());
219
			userRole.setUserId(user.getId());
241
			userRoleRepository.persist(userRole);
220
			userRoleRepository.persist(userRole);
242
			return responseSender.ok(ResponseCodeHolder.getMessage("USR_OK_1000"));
221
			return responseSender.ok(ResponseCodeHolder.getMessage("USR_OK_1000"));
243
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
222
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
244
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
223
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
245
			return responseSender.badRequest(profitMandiBusinessException);
224
			return responseSender.badRequest(profitMandiBusinessException);
246
		}
225
		}
247
	}
226
	}
248
 
227
 
249
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ALL, method = RequestMethod.GET)
228
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ALL, method = RequestMethod.GET)
250
	public ResponseEntity<?> getAll(HttpServletRequest request,
-
 
251
			@RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber,
229
	public ResponseEntity<?> getAll(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber, @RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize) {
252
			@RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize) {
-
 
253
		LOGGER.info("requested url : " + request.getRequestURL().toString());
230
		LOGGER.info("requested url : " + request.getRequestURL().toString());
254
		return responseSender.ok(userRepository.selectAll(pageNumber, pageSize));
231
		return responseSender.ok(userRepository.selectAll(pageNumber, pageSize));
255
	}
232
	}
256
 
233
 
257
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ID, method = RequestMethod.GET)
234
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ID, method = RequestMethod.GET)
Line 351... Line 328...
351
	public ResponseEntity<?> addRole(HttpServletRequest request, @RequestBody UserAddRoleRequest userAddRoleRequest) {
328
	public ResponseEntity<?> addRole(HttpServletRequest request, @RequestBody UserAddRoleRequest userAddRoleRequest) {
352
		LOGGER.info("requested url : " + request.getRequestURL().toString());
329
		LOGGER.info("requested url : " + request.getRequestURL().toString());
353
		try {
330
		try {
354
			User user = userRepository.selectById(userAddRoleRequest.getUserId());
331
			User user = userRepository.selectById(userAddRoleRequest.getUserId());
355
 
332
 
356
			Role role = null;
333
			/*Role role = null;
357
			try {
334
			try {
358
				role = roleRepository.selectByNameAndType(userAddRoleRequest.getRole().getName(),
335
				role = roleRepository.selectByNameAndType(userAddRoleRequest.getRole().getName(),
359
						userAddRoleRequest.getRole().getType());
336
						userAddRoleRequest.getRole().getType());
360
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
337
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
361
				role = new Role();
338
				role = new Role();
362
				role.setName(userAddRoleRequest.getRole().getName());
339
				role.setName(userAddRoleRequest.getRole().getName());
363
				role.setType(userAddRoleRequest.getRole().getType());
340
				role.setType(userAddRoleRequest.getRole().getType());
364
				roleRepository.persist(role);
341
				roleRepository.persist(role);
365
			}
342
			}*/
366
			Permission permission = new Permission();
343
			Permission permission = new Permission();
367
			permission.setType(userAddRoleRequest.getRole().getPermissionType());
344
			permission.setType(userAddRoleRequest.getPermissionType());
368
			permission.setRoleId(role.getId());
345
			permission.setRoleType(userAddRoleRequest.getRoleType());
369
			permissionRepository.persist(permission);
346
			permissionRepository.persist(permission);
370
			UserRole userRole = new UserRole();
347
			UserRole userRole = new UserRole();
371
			userRole.setRoleId(role.getId());
348
			userRole.setRoleType(userAddRoleRequest.getRoleType());
372
			userRole.setUserId(user.getId());
349
			userRole.setUserId(user.getId());
373
			userRoleRepository.persist(userRole);
350
			userRoleRepository.persist(userRole);
374
			return responseSender.ok("");
351
			return responseSender.ok("");
375
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
352
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
376
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
353
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
377
			return responseSender.badRequest(profitMandiBusinessException);
354
			return responseSender.badRequest(profitMandiBusinessException);
378
		}
355
		}
379
	}
356
	}
380
 
357
 
381
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_REMOVE, method = RequestMethod.DELETE)
358
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_REMOVE, method = RequestMethod.DELETE)
382
	public ResponseEntity<?> removeRole(HttpServletRequest request, @RequestParam(name = "roleId") int roleId,
359
	public ResponseEntity<?> removeRole(HttpServletRequest request, @RequestParam(name = "roleType") RoleType roleType,
383
			@RequestParam(name = "userId") int userId) {
360
			@RequestParam(name = "userId") int userId) {
384
		LOGGER.info("requested url : " + request.getRequestURL().toString());
361
		LOGGER.info("requested url : " + request.getRequestURL().toString());
385
		try {
362
		try {
386
			roleRepository.selectById(roleId);
-
 
387
			userRepository.selectById(userId);
363
			userRepository.selectById(userId);
388
			userRoleRepository.deleteByUserAndRoleId(userId, roleId);
364
			userRoleRepository.deleteByUserAndRoleType(userId, roleType);
389
			permissionRepository.deleteByRoleId(roleId);
365
			permissionRepository.deleteByRoleType(roleType);
390
			return responseSender.ok("");
366
			return responseSender.ok("");
391
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
367
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
392
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
368
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
393
			return responseSender.badRequest(profitMandiBusinessException);
369
			return responseSender.badRequest(profitMandiBusinessException);
394
		}
370
		}
Line 412... Line 388...
412
		}
388
		}
413
 
389
 
414
		Map<String, Object> responseMap = new HashMap<>(2);
390
		Map<String, Object> responseMap = new HashMap<>(2);
415
		try {
391
		try {
416
			User user = userRepository.selectByEmailId(emailId);
392
			User user = userRepository.selectByEmailId(emailId);
417
			Set<Role> roles = user.getRoles();
393
			List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
418
			String[] roleTypes = new String[roles.size()];
394
			String[] roleTypes = new String[userRoles.size()];
419
			int index = 0;
395
			int index = 0;
420
			for (Role role : roles) {
396
			for (UserRole userRole : userRoles) {
421
				roleTypes[index++] = role.getType().toString();
397
				roleTypes[index++] = userRole.getRoleType().toString();
422
			}
398
			}
423
			responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), roleTypes));
399
			responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), roleTypes));
424
			responseMap.put(ProfitMandiConstants.REGISTERED, true);
400
			responseMap.put(ProfitMandiConstants.REGISTERED, true);
425
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
401
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
426
			responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(emailId));
402
			responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(emailId));
427
			responseMap.put(ProfitMandiConstants.REGISTERED, false);
403
			responseMap.put(ProfitMandiConstants.REGISTERED, false);
428
		}
404
		}
429
		return responseSender.ok(responseMap);
405
		return responseSender.ok(responseMap);
430
 
406
 
431
	}
407
	}
432
 
408
	
433
	@RequestMapping(value = ProfitMandiConstants.URL_ADMIN_MIGRATE, method = RequestMethod.GET)
409
	@RequestMapping(value = ProfitMandiConstants.URL_ADMIN_MIGRATE, method = RequestMethod.GET)
434
	public ResponseEntity<?> migrate(HttpServletRequest request) {
410
	public ResponseEntity<?> migrate(HttpServletRequest request) {
435
		LOGGER.info("requested url : " + request.getRequestURL().toString());
411
		LOGGER.info("requested url : " + request.getRequestURL().toString());
436
		userToRetailerMigrationUtil.migrate();
412
		userToRetailerMigrationUtil.migrate();
437
		return responseSender.ok(ResponseCodeHolder.getMessage("OK_1000"));
413
		return responseSender.ok(ResponseCodeHolder.getMessage("OK_1000"));