Subversion Repositories SmartDukaan

Rev

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