Subversion Repositories SmartDukaan

Rev

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