Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
21248 ashik.ali 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.time.LocalDateTime;
21428 amit.gupta 4
import java.util.HashMap;
21277 ashik.ali 5
import java.util.Map;
21428 amit.gupta 6
import java.util.Set;
21469 amit.gupta 7
import java.util.function.Predicate;
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
 
24
import com.spice.profitmandi.common.ResponseCodeHolder;
25
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
26
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21740 ashik.ali 27
import com.spice.profitmandi.common.model.ProfitMandiResponse;
28
import com.spice.profitmandi.common.model.ResponseStatus;
21469 amit.gupta 29
import com.spice.profitmandi.common.model.UserInfo;
21282 ashik.ali 30
import com.spice.profitmandi.common.util.JWTUtil;
21740 ashik.ali 31
import com.spice.profitmandi.common.web.util.ResponseSender;
21735 ashik.ali 32
import com.spice.profitmandi.dao.entity.dtr.Permission;
33
import com.spice.profitmandi.dao.entity.dtr.Retailer;
34
import com.spice.profitmandi.dao.entity.dtr.Role;
35
import com.spice.profitmandi.dao.entity.dtr.User;
36
import com.spice.profitmandi.dao.entity.dtr.UserRole;
37
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
21643 ashik.ali 38
import com.spice.profitmandi.dao.model.UserCart;
21735 ashik.ali 39
import com.spice.profitmandi.dao.repository.dtr.PermissionRepository;
40
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
41
import com.spice.profitmandi.dao.repository.dtr.RoleRepository;
42
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
43
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
44
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
21487 ashik.ali 45
import com.spice.profitmandi.dao.util.UserToRetailerMigrationUtil;
21469 amit.gupta 46
import com.spice.profitmandi.web.enumuration.UserStatus;
21277 ashik.ali 47
import com.spice.profitmandi.web.processor.GoogleLoginProcessor;
21426 ashik.ali 48
import com.spice.profitmandi.web.req.UserAddRoleRequest;
21366 kshitij.so 49
import com.spice.profitmandi.web.req.UserRequest;
21248 ashik.ali 50
 
21469 amit.gupta 51
import io.swagger.annotations.ApiImplicitParam;
52
import io.swagger.annotations.ApiImplicitParams;
53
 
21248 ashik.ali 54
/**
55
 * @author ashikali
56
 *
57
 */
58
@Controller
21702 ashik.ali 59
@Transactional
21248 ashik.ali 60
public class UserController {
21469 amit.gupta 61
 
21448 ashik.ali 62
	@Autowired
63
	ResponseSender<?> responseSender;
21469 amit.gupta 64
 
65
	private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
66
 
21414 kshitij.so 67
	@Value("${admin.token}")
68
	private String validAdminToken;
21469 amit.gupta 69
 
21278 ashik.ali 70
	@Autowired
71
	UserRepository userRepository;
21469 amit.gupta 72
 
21278 ashik.ali 73
	@Autowired
21485 amit.gupta 74
	RetailerRepository retailerRepository;
75
 
76
	@Autowired
21426 ashik.ali 77
	RoleRepository roleRepository;
21469 amit.gupta 78
 
21426 ashik.ali 79
	@Autowired
80
	UserRoleRepository userRoleRepository;
21469 amit.gupta 81
 
21426 ashik.ali 82
	@Autowired
21485 amit.gupta 83
	UserAccountRepository userAccountRepository;
84
 
85
	@Autowired
21426 ashik.ali 86
	PermissionRepository permissionRepository;
21469 amit.gupta 87
 
21426 ashik.ali 88
	@Autowired
21278 ashik.ali 89
	GoogleLoginProcessor googleLoginProcessor;
21487 ashik.ali 90
 
91
	@Autowired
92
	UserToRetailerMigrationUtil userToRetailerMigrationUtil;
21469 amit.gupta 93
 
21277 ashik.ali 94
	@SuppressWarnings("unchecked")
21469 amit.gupta 95
	@RequestMapping(value = ProfitMandiConstants.URL_USER_GOOGLE_LOGIN, method = RequestMethod.POST)
96
	public ResponseEntity<?> googleLogin(HttpServletRequest request) {
97
		LOGGER.info("requested url : " + request.getRequestURL().toString());
98
		final Map<String, Object> googleLoginMap = (Map<String, Object>) request
99
				.getAttribute(ProfitMandiConstants.GOOGLE_LOGIN_MAP);
21277 ashik.ali 100
		request.removeAttribute(ProfitMandiConstants.GOOGLE_LOGIN_MAP);
101
		try {
21448 ashik.ali 102
			return responseSender.ok(googleLoginProcessor.process(googleLoginMap));
21469 amit.gupta 103
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
104
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 105
			return responseSender.badRequest(profitMandiBusinessException);
21277 ashik.ali 106
		}
107
	}
21469 amit.gupta 108
 
109
	@RequestMapping(value = ProfitMandiConstants.URL_USER_TOKEN_IS_EXPIRED, method = RequestMethod.GET)
110
	public ResponseEntity<?> tokenIsExpired(HttpServletRequest request, @RequestParam(name = "token") String token) {
111
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21282 ashik.ali 112
		try {
21448 ashik.ali 113
			return responseSender.ok(JWTUtil.isExpired(token));
21469 amit.gupta 114
 
115
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
116
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 117
			return responseSender.badRequest(profitMandiBusinessException);
21282 ashik.ali 118
		}
119
	}
21469 amit.gupta 120
 
121
	@RequestMapping(value = ProfitMandiConstants.URL_USER_TOKEN_INFO, method = RequestMethod.GET)
122
	@ApiImplicitParams({
123
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
124
	public ResponseEntity<?> tokenInfo(HttpServletRequest request) throws Throwable {
125
		Map<String, Object> responseMap = new HashMap<>();
126
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
21483 amit.gupta 127
		User user = null;
128
		if(userInfo.getUserId()>-1){
129
			user = userRepository.selectById(userInfo.getUserId());
130
		} else {
131
			try {
132
				user = userRepository.selectByEmailId(userInfo.getEmail());
133
			} catch (ProfitMandiBusinessException e1) {
21526 amit.gupta 134
				LOGGER.info("Uneregistered user", userInfo.getEmail());
21483 amit.gupta 135
			}
136
		}
137
		if (user != null) {
21491 amit.gupta 138
			responseMap.put(ProfitMandiConstants.EMAIL_ID, user.getEmailId());
21526 amit.gupta 139
			responseMap.put(ProfitMandiConstants.USER_ID, user.getId());
21483 amit.gupta 140
 
141
			Set<Role> roles = user.getRoles();
142
			//generate new token if roles size is different
143
			if(userInfo.getRoleNames() == null || roles.size() != userInfo.getRoleNames().size()) {
144
				String[] roleTypes = new String[roles.size()];
145
				int index = 0;
146
				for (Role role : roles) {
147
					roleTypes[index++] = role.getType().toString();
148
				}
149
				String newToken = JWTUtil.create(user.getId(), roleTypes);
150
				responseMap.put("newAuthToken", newToken);
151
			}
152
 
21469 amit.gupta 153
			// if user is retailer
154
			if (user.getRoles().stream().anyMatch(new Predicate<Role>() {
155
				@Override
156
				public boolean test(Role t) {
157
					return t.getType().equals(RoleType.RETAILER);
158
				}
159
			})) {
21485 amit.gupta 160
				UserCart uc  = userAccountRepository.getUserCart(userInfo.getUserId());
161
				Retailer retailer  = retailerRepository.selectById(uc.getUserId());
162
				// if retailer is activated 1 then verified retailer
163
				// else if migrated is 1 then old retailer
164
				// else retailer is not verifed
165
				if (retailer.isActive()) {
21469 amit.gupta 166
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.VERIFIED_RETAILER.getValue());
21485 amit.gupta 167
				} else if (retailer.isMigrated()){
168
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.RETAILER.getValue());
21469 amit.gupta 169
				} else {
21485 amit.gupta 170
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_VERIFIED_RETAILER.getValue());
21469 amit.gupta 171
				}
172
			} else if (user.getRoles().stream().anyMatch(new Predicate<Role>() {
173
				@Override
174
				public boolean test(Role t) {
175
					return t.getType().equals(RoleType.USER);
176
				}
177
			})) {
178
				responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.REGISTERED.getValue());
179
			}
21491 amit.gupta 180
		} else {
181
			responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_REGISTERED.getValue());
182
			responseMap.put(ProfitMandiConstants.EMAIL_ID, userInfo.getEmail());
21469 amit.gupta 183
		}
21526 amit.gupta 184
 
21469 amit.gupta 185
		return responseSender.ok(responseMap);
186
	}
187
 
188
	@RequestMapping(value = ProfitMandiConstants.URL_USER, method = RequestMethod.POST)
21501 amit.gupta 189
	@ApiImplicitParams({
190
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
191
	public ResponseEntity<?> createUser(HttpServletRequest request, @RequestBody UserRequest userRequest) throws Throwable{
21469 amit.gupta 192
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21368 kshitij.so 193
		User user = new User();
194
		user.setFirstName(userRequest.getFirstName());
195
		user.setLastName(userRequest.getLastName());
196
		user.setCity(userRequest.getCity());
197
		user.setPinCode(Integer.valueOf(userRequest.getPinCode()));
21708 amit.gupta 198
		user.setMobileNumber(userRequest.getMobieNumber());
21368 kshitij.so 199
		user.setEmailId(userRequest.getEmailId());
200
		user.setUsername("");
201
		user.setPassword("");
202
		user.setMobile_verified(false);
203
		user.setReferral_url("");
204
		user.setGroup_id(1);
205
		user.setStatus(0);
206
		user.setActivated(false);
21501 amit.gupta 207
		user.setCreateTimestamp(LocalDateTime.now());
208
		user.setUpdateTimestamp(LocalDateTime.now());
21503 ashik.ali 209
		try{
210
			userRepository.persist(user);
21414 kshitij.so 211
 
21503 ashik.ali 212
			UserRole userRole = new UserRole();
213
			userRole.setRoleId(RoleType.USER.getValue());
214
			userRole.setUserId(user.getId());
215
			userRoleRepository.persist(userRole);
216
			return responseSender.ok(ResponseCodeHolder.getMessage("USR_OK_1000"));
217
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
218
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
219
			return responseSender.badRequest(profitMandiBusinessException);
220
		}
21278 ashik.ali 221
	}
21469 amit.gupta 222
 
223
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ALL, method = RequestMethod.GET)
21497 ashik.ali 224
	public ResponseEntity<?> getAll(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber, @RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize) {
21469 amit.gupta 225
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21497 ashik.ali 226
		return responseSender.ok(userRepository.selectAll(pageNumber, pageSize));
21248 ashik.ali 227
	}
21469 amit.gupta 228
 
229
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ID, method = RequestMethod.GET)
230
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id) {
231
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 232
		try {
21448 ashik.ali 233
			return responseSender.ok(userRepository.selectById(id));
21469 amit.gupta 234
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
235
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 236
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 237
		}
238
	}
21469 amit.gupta 239
 
240
	@RequestMapping(value = ProfitMandiConstants.URL_USER_MOBILE_NUMBER, method = RequestMethod.GET)
241
	public ResponseEntity<?> getByMobileNumber(HttpServletRequest request,
242
			@RequestParam(name = "mobileNumber") String mobileNumber) {
243
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 244
		try {
21448 ashik.ali 245
			return responseSender.ok(userRepository.selectByMobileNumber(mobileNumber));
21469 amit.gupta 246
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
247
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 248
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 249
		}
250
	}
21469 amit.gupta 251
 
21511 ashik.ali 252
	/*@ApiImplicitParams({
21501 amit.gupta 253
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
254
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ACTIVATE, method = RequestMethod.POST)
255
	public ResponseEntity<?> activateUser(HttpServletRequest request,
256
			@RequestParam(name = "activationCode") String activationCode) {
257
		Map<String, Object> response = new HashMap<>();
258
		boolean activated = false;
259
 
21511 ashik.ali 260
		int userId = (int)request.getAttribute("userId");
21501 amit.gupta 261
		UserCart uc  = userAccountRepository.getUserCart(userId);
262
		Client client  = new TransactionClient().getClient();
263
		if (client.registerRsa(uc.getUserId(),activationCode)){
264
			activated = true;
265
		}
266
		else{
267
 
21511 ashik.ali 268
		}
21501 amit.gupta 269
		return responseSender.ok(null);
21511 ashik.ali 270
	}*/
21501 amit.gupta 271
 
21426 ashik.ali 272
	@RequestMapping(value = ProfitMandiConstants.URL_USER_IS_EXIST_MOBILE_NUMBER, method = RequestMethod.GET)
21469 amit.gupta 273
	public ResponseEntity<?> isMobileNumberExist(HttpServletRequest request,
274
			@RequestParam(name = "mobileNumber") String mobileNumber) {
275
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21448 ashik.ali 276
		return responseSender.ok(userRepository.isExistByMobileNumber(mobileNumber));
21426 ashik.ali 277
	}
21469 amit.gupta 278
 
279
	@RequestMapping(value = ProfitMandiConstants.URL_USER_EMAIL_ID, method = RequestMethod.GET)
280
	public ResponseEntity<?> getByEmailId(HttpServletRequest request, @RequestParam(name = "emailId") String emailId) {
281
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 282
		try {
21448 ashik.ali 283
			return responseSender.ok(userRepository.selectByEmailId(emailId));
21469 amit.gupta 284
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
285
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 286
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 287
		}
288
	}
21469 amit.gupta 289
 
290
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_ADD, method = RequestMethod.POST)
291
	public ResponseEntity<?> addRole(HttpServletRequest request, @RequestBody UserAddRoleRequest userAddRoleRequest) {
292
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 293
		try {
21426 ashik.ali 294
			User user = userRepository.selectById(userAddRoleRequest.getUserId());
21469 amit.gupta 295
 
21426 ashik.ali 296
			Role role = null;
21469 amit.gupta 297
			try {
298
				role = roleRepository.selectByNameAndType(userAddRoleRequest.getRole().getName(),
299
						userAddRoleRequest.getRole().getType());
300
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
21426 ashik.ali 301
				role = new Role();
302
				role.setName(userAddRoleRequest.getRole().getName());
303
				role.setType(userAddRoleRequest.getRole().getType());
304
				roleRepository.persist(role);
305
			}
306
			Permission permission = new Permission();
307
			permission.setType(userAddRoleRequest.getRole().getPermissionType());
308
			permission.setRoleId(role.getId());
309
			permissionRepository.persist(permission);
310
			UserRole userRole = new UserRole();
311
			userRole.setRoleId(role.getId());
312
			userRole.setUserId(user.getId());
313
			userRoleRepository.persist(userRole);
21448 ashik.ali 314
			return responseSender.ok("");
21469 amit.gupta 315
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
316
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 317
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 318
		}
319
	}
21469 amit.gupta 320
 
321
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_REMOVE, method = RequestMethod.DELETE)
322
	public ResponseEntity<?> removeRole(HttpServletRequest request, @RequestParam(name = "roleId") int roleId,
323
			@RequestParam(name = "userId") int userId) {
324
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 325
		try {
21426 ashik.ali 326
			roleRepository.selectById(roleId);
327
			userRepository.selectById(userId);
328
			userRoleRepository.deleteByUserAndRoleId(userId, roleId);
329
			permissionRepository.deleteByRoleId(roleId);
21448 ashik.ali 330
			return responseSender.ok("");
21469 amit.gupta 331
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
332
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 333
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 334
		}
335
	}
21469 amit.gupta 336
 
337
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_ALL, method = RequestMethod.GET)
338
	public ResponseEntity<?> getAllRoles(HttpServletRequest request, @RequestParam(name = "id") int id) {
339
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21448 ashik.ali 340
		return responseSender.ok(userRoleRepository.selectRolesByUserId(id));
21248 ashik.ali 341
	}
21414 kshitij.so 342
 
343
	@RequestMapping(value = ProfitMandiConstants.URL_ADMIN_TOKEN, method = RequestMethod.POST)
21469 amit.gupta 344
	public ResponseEntity<?> getAdminToken(HttpServletRequest request,
345
			@RequestParam(name = "adminToken") String adminToken, @RequestParam(name = "emailId") String emailId) {
346
		LOGGER.info("requested url : " + request.getRequestURL().toString());
347
		if (!adminToken.equals(validAdminToken)) {
348
			final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(LocalDateTime.now(),
349
					request.getRequestURL().toString(), HttpStatus.FORBIDDEN.toString(), HttpStatus.FORBIDDEN,
350
					ResponseStatus.FAILURE, null);
21414 kshitij.so 351
			return new ResponseEntity<>(profitMandiResponse, HttpStatus.FORBIDDEN);
352
		}
21469 amit.gupta 353
 
21414 kshitij.so 354
		Map<String, Object> responseMap = new HashMap<>(2);
21469 amit.gupta 355
		try {
21414 kshitij.so 356
			User user = userRepository.selectByEmailId(emailId);
357
			Set<Role> roles = user.getRoles();
358
			String[] roleTypes = new String[roles.size()];
359
			int index = 0;
21469 amit.gupta 360
			for (Role role : roles) {
21414 kshitij.so 361
				roleTypes[index++] = role.getType().toString();
362
			}
363
			responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), roleTypes));
364
			responseMap.put(ProfitMandiConstants.REGISTERED, true);
21469 amit.gupta 365
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
366
			responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(emailId));
21414 kshitij.so 367
			responseMap.put(ProfitMandiConstants.REGISTERED, false);
368
		}
21448 ashik.ali 369
		return responseSender.ok(responseMap);
21469 amit.gupta 370
 
21414 kshitij.so 371
	}
21503 ashik.ali 372
 
373
	@RequestMapping(value = ProfitMandiConstants.URL_ADMIN_MIGRATE, method = RequestMethod.GET)
374
	public ResponseEntity<?> migrate(HttpServletRequest request) {
375
		LOGGER.info("requested url : " + request.getRequestURL().toString());
376
		userToRetailerMigrationUtil.migrate();
377
		return responseSender.ok(ResponseCodeHolder.getMessage("OK_1000"));
378
	}
21248 ashik.ali 379
}