Subversion Repositories SmartDukaan

Rev

Rev 22502 | Rev 22554 | 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;
21855 amit.gupta 4
import java.util.ArrayList;
21428 amit.gupta 5
import java.util.HashMap;
21855 amit.gupta 6
import java.util.List;
21277 ashik.ali 7
import java.util.Map;
22032 ashik.ali 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
 
22015 ashik.ali 25
import com.eclipsesource.json.Json;
21855 amit.gupta 26
import com.eclipsesource.json.JsonArray;
27
import com.eclipsesource.json.JsonObject;
28
import com.eclipsesource.json.JsonValue;
29
import com.google.gson.Gson;
21248 ashik.ali 30
import com.spice.profitmandi.common.ResponseCodeHolder;
21855 amit.gupta 31
import com.spice.profitmandi.common.enumuration.SchemeType;
21248 ashik.ali 32
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
33
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21740 ashik.ali 34
import com.spice.profitmandi.common.model.ProfitMandiResponse;
22492 amit.gupta 35
import com.spice.profitmandi.common.model.RegisteredUserInfo;
21740 ashik.ali 36
import com.spice.profitmandi.common.model.ResponseStatus;
21469 amit.gupta 37
import com.spice.profitmandi.common.model.UserInfo;
21282 ashik.ali 38
import com.spice.profitmandi.common.util.JWTUtil;
21855 amit.gupta 39
import com.spice.profitmandi.common.web.client.RestClient;
21740 ashik.ali 40
import com.spice.profitmandi.common.web.util.ResponseSender;
21735 ashik.ali 41
import com.spice.profitmandi.dao.entity.dtr.Permission;
42
import com.spice.profitmandi.dao.entity.dtr.Retailer;
43
import com.spice.profitmandi.dao.entity.dtr.User;
44
import com.spice.profitmandi.dao.entity.dtr.UserRole;
22355 ashik.ali 45
import com.spice.profitmandi.dao.entity.user.Address;
21735 ashik.ali 46
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
21643 ashik.ali 47
import com.spice.profitmandi.dao.model.UserCart;
21735 ashik.ali 48
import com.spice.profitmandi.dao.repository.dtr.PermissionRepository;
49
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
50
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
51
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
52
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
22355 ashik.ali 53
import com.spice.profitmandi.dao.repository.user.AddressRepository;
21784 amit.gupta 54
import com.spice.profitmandi.service.UserService;
21469 amit.gupta 55
import com.spice.profitmandi.web.enumuration.UserStatus;
21277 ashik.ali 56
import com.spice.profitmandi.web.processor.GoogleLoginProcessor;
21426 ashik.ali 57
import com.spice.profitmandi.web.req.UserAddRoleRequest;
21366 kshitij.so 58
import com.spice.profitmandi.web.req.UserRequest;
21855 amit.gupta 59
import com.spice.profitmandi.web.res.Notification;
21248 ashik.ali 60
 
21469 amit.gupta 61
import io.swagger.annotations.ApiImplicitParam;
62
import io.swagger.annotations.ApiImplicitParams;
63
 
21248 ashik.ali 64
/**
65
 * @author ashikali
66
 *
67
 */
68
@Controller
22037 amit.gupta 69
@Transactional(rollbackFor=Throwable.class)
21248 ashik.ali 70
public class UserController {
21469 amit.gupta 71
 
21448 ashik.ali 72
	@Autowired
73
	ResponseSender<?> responseSender;
21469 amit.gupta 74
 
75
	private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
76
 
21855 amit.gupta 77
	@Value("${notifications.api.host}")
78
	private String nodeHost;
79
	@Value("${notifications.api.port}")
80
	private int nodePort;
81
 
21414 kshitij.so 82
	@Value("${admin.token}")
83
	private String validAdminToken;
21469 amit.gupta 84
 
21278 ashik.ali 85
	@Autowired
86
	UserRepository userRepository;
22355 ashik.ali 87
 
88
	@Autowired
89
	com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
21469 amit.gupta 90
 
21278 ashik.ali 91
	@Autowired
21485 amit.gupta 92
	RetailerRepository retailerRepository;
93
 
94
	@Autowired
21426 ashik.ali 95
	UserRoleRepository userRoleRepository;
21469 amit.gupta 96
 
21426 ashik.ali 97
	@Autowired
21485 amit.gupta 98
	UserAccountRepository userAccountRepository;
99
 
100
	@Autowired
21426 ashik.ali 101
	PermissionRepository permissionRepository;
22355 ashik.ali 102
 
103
	@Autowired
104
	AddressRepository addressRepository;
21469 amit.gupta 105
 
21426 ashik.ali 106
	@Autowired
21278 ashik.ali 107
	GoogleLoginProcessor googleLoginProcessor;
21855 amit.gupta 108
 
22473 ashik.ali 109
 
21855 amit.gupta 110
 
21784 amit.gupta 111
	@Autowired
112
	UserService userService;
21469 amit.gupta 113
 
21277 ashik.ali 114
	@SuppressWarnings("unchecked")
21469 amit.gupta 115
	@RequestMapping(value = ProfitMandiConstants.URL_USER_GOOGLE_LOGIN, method = RequestMethod.POST)
116
	public ResponseEntity<?> googleLogin(HttpServletRequest request) {
117
		LOGGER.info("requested url : " + request.getRequestURL().toString());
118
		final Map<String, Object> googleLoginMap = (Map<String, Object>) request
119
				.getAttribute(ProfitMandiConstants.GOOGLE_LOGIN_MAP);
21277 ashik.ali 120
		request.removeAttribute(ProfitMandiConstants.GOOGLE_LOGIN_MAP);
121
		try {
21448 ashik.ali 122
			return responseSender.ok(googleLoginProcessor.process(googleLoginMap));
21469 amit.gupta 123
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
124
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 125
			return responseSender.badRequest(profitMandiBusinessException);
21277 ashik.ali 126
		}
127
	}
21469 amit.gupta 128
 
129
	@RequestMapping(value = ProfitMandiConstants.URL_USER_TOKEN_IS_EXPIRED, method = RequestMethod.GET)
130
	public ResponseEntity<?> tokenIsExpired(HttpServletRequest request, @RequestParam(name = "token") String token) {
131
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21282 ashik.ali 132
		try {
21448 ashik.ali 133
			return responseSender.ok(JWTUtil.isExpired(token));
21469 amit.gupta 134
 
135
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
136
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 137
			return responseSender.badRequest(profitMandiBusinessException);
21282 ashik.ali 138
		}
139
	}
21469 amit.gupta 140
 
22355 ashik.ali 141
	@RequestMapping(value = ProfitMandiConstants.URL_USER_DETAIL_BY_TOKEN, method = RequestMethod.GET)
21469 amit.gupta 142
	@ApiImplicitParams({
143
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
144
	public ResponseEntity<?> tokenInfo(HttpServletRequest request) throws Throwable {
145
		Map<String, Object> responseMap = new HashMap<>();
146
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
21483 amit.gupta 147
		User user = null;
21855 amit.gupta 148
		if (userInfo.getUserId() > -1) {
21483 amit.gupta 149
			user = userRepository.selectById(userInfo.getUserId());
150
		} else {
151
			try {
152
				user = userRepository.selectByEmailId(userInfo.getEmail());
153
			} catch (ProfitMandiBusinessException e1) {
21526 amit.gupta 154
				LOGGER.info("Uneregistered user", userInfo.getEmail());
21483 amit.gupta 155
			}
156
		}
157
		if (user != null) {
21491 amit.gupta 158
			responseMap.put(ProfitMandiConstants.EMAIL_ID, user.getEmailId());
21526 amit.gupta 159
			responseMap.put(ProfitMandiConstants.USER_ID, user.getId());
22017 amit.gupta 160
			responseMap.put(ProfitMandiConstants.USER_NAME, user.getFirstName() + " " + user.getLastName());
22011 ashik.ali 161
			List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
22035 ashik.ali 162
			LOGGER.info("userRoles {} ", userRoles);
22011 ashik.ali 163
			String[] roleTypes = new String[userRoles.size()];
22032 ashik.ali 164
 
165
			// generate new token if roles have been updated
166
			if (userInfo.getRoleNames() == null || userRoles.size() != userInfo.getRoleNames().size()) {
167
				// String[] roleTypes = new String[userRoles.size()];
21483 amit.gupta 168
				int index = 0;
22011 ashik.ali 169
				for (UserRole userRole : userRoles) {
170
					roleTypes[index++] = userRole.getRoleType().toString();
21483 amit.gupta 171
				}
172
				String newToken = JWTUtil.create(user.getId(), roleTypes);
173
				responseMap.put("newAuthToken", newToken);
174
			}
22032 ashik.ali 175
 
21469 amit.gupta 176
			// if user is retailer
22032 ashik.ali 177
			if (userRoles.stream().anyMatch(new Predicate<UserRole>() {
178
				@Override
179
				public boolean test(UserRole t) {
22035 ashik.ali 180
					return t.getRoleType() == RoleType.RETAILER;
22032 ashik.ali 181
				}
182
			})) {
22035 ashik.ali 183
 
22032 ashik.ali 184
				UserCart uc = userAccountRepository.getUserCart(userInfo.getUserId());
185
				Retailer retailer = retailerRepository.selectById(uc.getUserId());
22370 amit.gupta 186
				com.spice.profitmandi.dao.entity.user.User saholicUser = userUserRepository.selectById(uc.getUserId());
187
				if(saholicUser.getAddressId() != null){
188
					Address address = addressRepository.selectById(saholicUser.getAddressId());
189
					responseMap.put(ProfitMandiConstants.ADDRESS, address);
190
				}
21485 amit.gupta 191
				// if retailer is activated 1 then verified retailer
192
				// else if migrated is 1 then old retailer
22497 amit.gupta 193
				// also lets incoporte old process i.e is user is activated then also retailer is verified retailer
21485 amit.gupta 194
				// else retailer is not verifed
22497 amit.gupta 195
				if (retailer.isActive() || user.isActivated()) {
22032 ashik.ali 196
					if (retailer.isFofo()) {
22017 amit.gupta 197
						responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.FOFO.getValue());
198
					} else {
199
						responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.VERIFIED_RETAILER.getValue());
200
					}
22032 ashik.ali 201
				} else if (retailer.isMigrated()) {
21485 amit.gupta 202
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.RETAILER.getValue());
21469 amit.gupta 203
				} else {
21485 amit.gupta 204
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_VERIFIED_RETAILER.getValue());
21469 amit.gupta 205
				}
22032 ashik.ali 206
			} else if (userRoles.stream().anyMatch(new Predicate<UserRole>() {
207
				@Override
208
				public boolean test(UserRole t) {
22035 ashik.ali 209
					return t.getRoleType() == RoleType.USER;
22032 ashik.ali 210
				}
211
			})) {
22502 amit.gupta 212
				responseMap.put("userInfo", getRegisteredUserInfo(user));
21469 amit.gupta 213
				responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.REGISTERED.getValue());
22032 ashik.ali 214
			}
21491 amit.gupta 215
		} else {
216
			responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_REGISTERED.getValue());
217
			responseMap.put(ProfitMandiConstants.EMAIL_ID, userInfo.getEmail());
21469 amit.gupta 218
		}
22032 ashik.ali 219
 
21469 amit.gupta 220
		return responseSender.ok(responseMap);
221
	}
22492 amit.gupta 222
 
22502 amit.gupta 223
	private RegisteredUserInfo getRegisteredUserInfo(User user) throws Throwable {
22492 amit.gupta 224
		RegisteredUserInfo ri = new RegisteredUserInfo();
225
		ri.setCity(user.getCity());
226
		ri.setFirstName(user.getFirstName());
227
		ri.setLastName(user.getLastName());
22493 amit.gupta 228
		ri.setPhone(user.getMobileNumber());
22492 amit.gupta 229
		ri.setPinCode(user.getPinCode());
230
		return ri;
231
	}
21469 amit.gupta 232
 
233
	@RequestMapping(value = ProfitMandiConstants.URL_USER, method = RequestMethod.POST)
21501 amit.gupta 234
	@ApiImplicitParams({
22032 ashik.ali 235
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
236
	public ResponseEntity<?> createUser(HttpServletRequest request, @RequestBody UserRequest userRequest)
237
			throws Throwable {
21469 amit.gupta 238
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21368 kshitij.so 239
		User user = new User();
240
		user.setFirstName(userRequest.getFirstName());
241
		user.setLastName(userRequest.getLastName());
242
		user.setCity(userRequest.getCity());
243
		user.setPinCode(Integer.valueOf(userRequest.getPinCode()));
21708 amit.gupta 244
		user.setMobileNumber(userRequest.getMobieNumber());
21368 kshitij.so 245
		user.setEmailId(userRequest.getEmailId());
246
		user.setUsername("");
247
		user.setPassword("");
248
		user.setMobile_verified(false);
249
		user.setReferral_url("");
250
		user.setGroup_id(1);
22504 amit.gupta 251
		user.setStatus(1);
21368 kshitij.so 252
		user.setActivated(false);
21501 amit.gupta 253
		user.setCreateTimestamp(LocalDateTime.now());
254
		user.setUpdateTimestamp(LocalDateTime.now());
22032 ashik.ali 255
		try {
21503 ashik.ali 256
			userRepository.persist(user);
257
			UserRole userRole = new UserRole();
22011 ashik.ali 258
			userRole.setRoleType(RoleType.USER);
21503 ashik.ali 259
			userRole.setUserId(user.getId());
260
			userRoleRepository.persist(userRole);
261
			return responseSender.ok(ResponseCodeHolder.getMessage("USR_OK_1000"));
262
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
263
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
264
			return responseSender.badRequest(profitMandiBusinessException);
265
		}
21278 ashik.ali 266
	}
21469 amit.gupta 267
 
268
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ALL, method = RequestMethod.GET)
22032 ashik.ali 269
	public ResponseEntity<?> getAll(HttpServletRequest request,
270
			@RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber,
271
			@RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize) {
21469 amit.gupta 272
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21497 ashik.ali 273
		return responseSender.ok(userRepository.selectAll(pageNumber, pageSize));
21248 ashik.ali 274
	}
21469 amit.gupta 275
 
276
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ID, method = RequestMethod.GET)
277
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id) {
278
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 279
		try {
21448 ashik.ali 280
			return responseSender.ok(userRepository.selectById(id));
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_MOBILE_NUMBER, method = RequestMethod.GET)
288
	public ResponseEntity<?> getByMobileNumber(HttpServletRequest request,
289
			@RequestParam(name = "mobileNumber") String mobileNumber) {
290
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 291
		try {
21448 ashik.ali 292
			return responseSender.ok(userRepository.selectByMobileNumber(mobileNumber));
21469 amit.gupta 293
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
294
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 295
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 296
		}
297
	}
21469 amit.gupta 298
 
21784 amit.gupta 299
	@ApiImplicitParams({
21501 amit.gupta 300
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
301
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ACTIVATE, method = RequestMethod.POST)
302
	public ResponseEntity<?> activateUser(HttpServletRequest request,
21784 amit.gupta 303
			@RequestParam(name = "activationCode") String activationCode) throws Throwable {
21855 amit.gupta 304
		int userId = (int) request.getAttribute("userId");
21784 amit.gupta 305
		UserCart uc = userAccountRepository.getUserCart(userId);
306
		return responseSender.ok(userService.updateActivation(userId, uc.getUserId(), activationCode));
307
	}
21855 amit.gupta 308
 
309
	@ApiImplicitParams({
310
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
311
	@RequestMapping(value = ProfitMandiConstants.URL_USER_NOTIFICATIONS, method = RequestMethod.GET)
312
	public ResponseEntity<?> getNofitications(HttpServletRequest request,
313
			@RequestParam(name = "androidId") String androidId, @RequestParam(name = "pageNumber") int pageNumber,
314
			@RequestParam(name = "pageSize") int pageSize) throws Throwable {
315
		int userId = (int) request.getAttribute("userId");
316
		String restResponse = null;
317
		Map<String, String> params = new HashMap<>();
318
 
319
		String uri = "/getAllNotifications";
320
		params.put("user_id", userId + "");
321
		params.put("android_id", androidId);
322
		params.put("limit", pageSize + "");
323
		params.put("offset", "" + ((pageNumber - 1) * pageSize));
324
		try {
325
			RestClient rc = new RestClient(SchemeType.HTTP, nodeHost, nodePort);
326
			restResponse = rc.get(uri, params);
327
		} catch (Exception | ProfitMandiBusinessException e) {
328
			LOGGER.error("Unable to data from node server", e);
329
			return responseSender.internalServerError(e);
330
		}
331
 
332
		JsonArray result_json = Json.parse(restResponse).asArray();
333
 
334
		List<Notification> notifications = new ArrayList<>();
335
 
336
		for (JsonValue j : result_json) {
337
			notifications.add(toNotifiaction(j.asObject()));
338
		}
339
 
340
		return responseSender.ok(notifications);
341
	}
342
 
343
	private Notification toNotifiaction(JsonObject jsonObject) {
344
		Notification n = (Notification) (new Gson().fromJson(jsonObject.toString(), Notification.class));
21995 amit.gupta 345
		if (n.getStatus().equals("opened") || n.getStatus().equals("referrer") || n.getStatus().equals("seen")) {
21855 amit.gupta 346
			n.setSeen(true);
347
		}
348
		return n;
349
	}
350
 
21426 ashik.ali 351
	@RequestMapping(value = ProfitMandiConstants.URL_USER_IS_EXIST_MOBILE_NUMBER, method = RequestMethod.GET)
21469 amit.gupta 352
	public ResponseEntity<?> isMobileNumberExist(HttpServletRequest request,
353
			@RequestParam(name = "mobileNumber") String mobileNumber) {
354
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21448 ashik.ali 355
		return responseSender.ok(userRepository.isExistByMobileNumber(mobileNumber));
21426 ashik.ali 356
	}
21469 amit.gupta 357
 
358
	@RequestMapping(value = ProfitMandiConstants.URL_USER_EMAIL_ID, method = RequestMethod.GET)
359
	public ResponseEntity<?> getByEmailId(HttpServletRequest request, @RequestParam(name = "emailId") String emailId) {
360
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 361
		try {
21448 ashik.ali 362
			return responseSender.ok(userRepository.selectByEmailId(emailId));
21469 amit.gupta 363
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
364
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 365
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 366
		}
367
	}
21469 amit.gupta 368
 
369
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_ADD, method = RequestMethod.POST)
370
	public ResponseEntity<?> addRole(HttpServletRequest request, @RequestBody UserAddRoleRequest userAddRoleRequest) {
371
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 372
		try {
21426 ashik.ali 373
			User user = userRepository.selectById(userAddRoleRequest.getUserId());
21469 amit.gupta 374
 
22032 ashik.ali 375
			/*
376
			 * Role role = null; try { role =
377
			 * roleRepository.selectByNameAndType(userAddRoleRequest.getRole().
378
			 * getName(), userAddRoleRequest.getRole().getType()); } catch
379
			 * (ProfitMandiBusinessException profitMandiBusinessException) {
380
			 * role = new Role();
381
			 * role.setName(userAddRoleRequest.getRole().getName());
382
			 * role.setType(userAddRoleRequest.getRole().getType());
383
			 * roleRepository.persist(role); }
384
			 */
21426 ashik.ali 385
			Permission permission = new Permission();
22011 ashik.ali 386
			permission.setType(userAddRoleRequest.getPermissionType());
387
			permission.setRoleType(userAddRoleRequest.getRoleType());
21426 ashik.ali 388
			permissionRepository.persist(permission);
389
			UserRole userRole = new UserRole();
22011 ashik.ali 390
			userRole.setRoleType(userAddRoleRequest.getRoleType());
21426 ashik.ali 391
			userRole.setUserId(user.getId());
392
			userRoleRepository.persist(userRole);
21448 ashik.ali 393
			return responseSender.ok("");
21469 amit.gupta 394
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
395
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 396
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 397
		}
398
	}
21469 amit.gupta 399
 
400
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_REMOVE, method = RequestMethod.DELETE)
22011 ashik.ali 401
	public ResponseEntity<?> removeRole(HttpServletRequest request, @RequestParam(name = "roleType") RoleType roleType,
21469 amit.gupta 402
			@RequestParam(name = "userId") int userId) {
403
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 404
		try {
21426 ashik.ali 405
			userRepository.selectById(userId);
22011 ashik.ali 406
			userRoleRepository.deleteByUserAndRoleType(userId, roleType);
407
			permissionRepository.deleteByRoleType(roleType);
21448 ashik.ali 408
			return responseSender.ok("");
21469 amit.gupta 409
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
410
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 411
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 412
		}
413
	}
21469 amit.gupta 414
 
415
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_ALL, method = RequestMethod.GET)
416
	public ResponseEntity<?> getAllRoles(HttpServletRequest request, @RequestParam(name = "id") int id) {
417
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21448 ashik.ali 418
		return responseSender.ok(userRoleRepository.selectRolesByUserId(id));
21248 ashik.ali 419
	}
21414 kshitij.so 420
 
421
	@RequestMapping(value = ProfitMandiConstants.URL_ADMIN_TOKEN, method = RequestMethod.POST)
21469 amit.gupta 422
	public ResponseEntity<?> getAdminToken(HttpServletRequest request,
423
			@RequestParam(name = "adminToken") String adminToken, @RequestParam(name = "emailId") String emailId) {
424
		LOGGER.info("requested url : " + request.getRequestURL().toString());
425
		if (!adminToken.equals(validAdminToken)) {
426
			final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(LocalDateTime.now(),
427
					request.getRequestURL().toString(), HttpStatus.FORBIDDEN.toString(), HttpStatus.FORBIDDEN,
428
					ResponseStatus.FAILURE, null);
21414 kshitij.so 429
			return new ResponseEntity<>(profitMandiResponse, HttpStatus.FORBIDDEN);
430
		}
21469 amit.gupta 431
 
21414 kshitij.so 432
		Map<String, Object> responseMap = new HashMap<>(2);
21469 amit.gupta 433
		try {
21414 kshitij.so 434
			User user = userRepository.selectByEmailId(emailId);
22011 ashik.ali 435
			List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
436
			String[] roleTypes = new String[userRoles.size()];
21414 kshitij.so 437
			int index = 0;
22011 ashik.ali 438
			for (UserRole userRole : userRoles) {
439
				roleTypes[index++] = userRole.getRoleType().toString();
21414 kshitij.so 440
			}
441
			responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), roleTypes));
442
			responseMap.put(ProfitMandiConstants.REGISTERED, true);
21469 amit.gupta 443
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
444
			responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(emailId));
21414 kshitij.so 445
			responseMap.put(ProfitMandiConstants.REGISTERED, false);
446
		}
21448 ashik.ali 447
		return responseSender.ok(responseMap);
21469 amit.gupta 448
 
21414 kshitij.so 449
	}
22032 ashik.ali 450
 
21248 ashik.ali 451
}