Subversion Repositories SmartDukaan

Rev

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