Subversion Repositories SmartDukaan

Rev

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