Subversion Repositories SmartDukaan

Rev

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