Subversion Repositories SmartDukaan

Rev

Rev 22234 | Rev 22370 | 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());
22355 ashik.ali 164
			int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
165
			com.spice.profitmandi.dao.entity.user.User saholicUser = userUserRepository.selectById(retailerId);
166
			if(saholicUser.getAddressId() != null){
167
				Address address = addressRepository.selectById(saholicUser.getAddressId());
168
				responseMap.put(ProfitMandiConstants.ADDRESS, address);
169
			}
22011 ashik.ali 170
			List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
22035 ashik.ali 171
			LOGGER.info("userRoles {} ", userRoles);
22011 ashik.ali 172
			String[] roleTypes = new String[userRoles.size()];
22032 ashik.ali 173
 
174
			// generate new token if roles have been updated
175
			if (userInfo.getRoleNames() == null || userRoles.size() != userInfo.getRoleNames().size()) {
176
				// String[] roleTypes = new String[userRoles.size()];
21483 amit.gupta 177
				int index = 0;
22011 ashik.ali 178
				for (UserRole userRole : userRoles) {
179
					roleTypes[index++] = userRole.getRoleType().toString();
21483 amit.gupta 180
				}
181
				String newToken = JWTUtil.create(user.getId(), roleTypes);
182
				responseMap.put("newAuthToken", newToken);
183
			}
22032 ashik.ali 184
 
21469 amit.gupta 185
			// if user is retailer
22032 ashik.ali 186
			if (userRoles.stream().anyMatch(new Predicate<UserRole>() {
187
				@Override
188
				public boolean test(UserRole t) {
22035 ashik.ali 189
					return t.getRoleType() == RoleType.RETAILER;
22032 ashik.ali 190
				}
191
			})) {
22035 ashik.ali 192
 
22032 ashik.ali 193
				UserCart uc = userAccountRepository.getUserCart(userInfo.getUserId());
194
				Retailer retailer = retailerRepository.selectById(uc.getUserId());
21485 amit.gupta 195
				// if retailer is activated 1 then verified retailer
196
				// else if migrated is 1 then old retailer
197
				// else retailer is not verifed
198
				if (retailer.isActive()) {
22032 ashik.ali 199
					if (retailer.isFofo()) {
22017 amit.gupta 200
						responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.FOFO.getValue());
201
					} else {
202
						responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.VERIFIED_RETAILER.getValue());
203
					}
22032 ashik.ali 204
				} else if (retailer.isMigrated()) {
21485 amit.gupta 205
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.RETAILER.getValue());
21469 amit.gupta 206
				} else {
21485 amit.gupta 207
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_VERIFIED_RETAILER.getValue());
21469 amit.gupta 208
				}
22032 ashik.ali 209
			} else if (userRoles.stream().anyMatch(new Predicate<UserRole>() {
210
				@Override
211
				public boolean test(UserRole t) {
22035 ashik.ali 212
					return t.getRoleType() == RoleType.USER;
22032 ashik.ali 213
				}
214
			})) {
21469 amit.gupta 215
				responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.REGISTERED.getValue());
22032 ashik.ali 216
			}
21491 amit.gupta 217
		} else {
218
			responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_REGISTERED.getValue());
219
			responseMap.put(ProfitMandiConstants.EMAIL_ID, userInfo.getEmail());
21469 amit.gupta 220
		}
22032 ashik.ali 221
 
21469 amit.gupta 222
		return responseSender.ok(responseMap);
223
	}
224
 
225
	@RequestMapping(value = ProfitMandiConstants.URL_USER, method = RequestMethod.POST)
21501 amit.gupta 226
	@ApiImplicitParams({
22032 ashik.ali 227
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
228
	public ResponseEntity<?> createUser(HttpServletRequest request, @RequestBody UserRequest userRequest)
229
			throws Throwable {
21469 amit.gupta 230
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21368 kshitij.so 231
		User user = new User();
232
		user.setFirstName(userRequest.getFirstName());
233
		user.setLastName(userRequest.getLastName());
234
		user.setCity(userRequest.getCity());
235
		user.setPinCode(Integer.valueOf(userRequest.getPinCode()));
21708 amit.gupta 236
		user.setMobileNumber(userRequest.getMobieNumber());
21368 kshitij.so 237
		user.setEmailId(userRequest.getEmailId());
238
		user.setUsername("");
239
		user.setPassword("");
240
		user.setMobile_verified(false);
241
		user.setReferral_url("");
242
		user.setGroup_id(1);
243
		user.setStatus(0);
244
		user.setActivated(false);
21501 amit.gupta 245
		user.setCreateTimestamp(LocalDateTime.now());
246
		user.setUpdateTimestamp(LocalDateTime.now());
22032 ashik.ali 247
		try {
21503 ashik.ali 248
			userRepository.persist(user);
249
			UserRole userRole = new UserRole();
22011 ashik.ali 250
			userRole.setRoleType(RoleType.USER);
21503 ashik.ali 251
			userRole.setUserId(user.getId());
252
			userRoleRepository.persist(userRole);
253
			return responseSender.ok(ResponseCodeHolder.getMessage("USR_OK_1000"));
254
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
255
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
256
			return responseSender.badRequest(profitMandiBusinessException);
257
		}
21278 ashik.ali 258
	}
21469 amit.gupta 259
 
260
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ALL, method = RequestMethod.GET)
22032 ashik.ali 261
	public ResponseEntity<?> getAll(HttpServletRequest request,
262
			@RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber,
263
			@RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize) {
21469 amit.gupta 264
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21497 ashik.ali 265
		return responseSender.ok(userRepository.selectAll(pageNumber, pageSize));
21248 ashik.ali 266
	}
21469 amit.gupta 267
 
268
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ID, method = RequestMethod.GET)
269
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id) {
270
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 271
		try {
21448 ashik.ali 272
			return responseSender.ok(userRepository.selectById(id));
21469 amit.gupta 273
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
274
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 275
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 276
		}
277
	}
21469 amit.gupta 278
 
279
	@RequestMapping(value = ProfitMandiConstants.URL_USER_MOBILE_NUMBER, method = RequestMethod.GET)
280
	public ResponseEntity<?> getByMobileNumber(HttpServletRequest request,
281
			@RequestParam(name = "mobileNumber") String mobileNumber) {
282
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 283
		try {
21448 ashik.ali 284
			return responseSender.ok(userRepository.selectByMobileNumber(mobileNumber));
21469 amit.gupta 285
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
286
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 287
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 288
		}
289
	}
21469 amit.gupta 290
 
21784 amit.gupta 291
	@ApiImplicitParams({
21501 amit.gupta 292
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
293
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ACTIVATE, method = RequestMethod.POST)
294
	public ResponseEntity<?> activateUser(HttpServletRequest request,
21784 amit.gupta 295
			@RequestParam(name = "activationCode") String activationCode) throws Throwable {
21855 amit.gupta 296
		int userId = (int) request.getAttribute("userId");
21784 amit.gupta 297
		UserCart uc = userAccountRepository.getUserCart(userId);
298
		return responseSender.ok(userService.updateActivation(userId, uc.getUserId(), activationCode));
299
	}
21855 amit.gupta 300
 
301
	@ApiImplicitParams({
302
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
303
	@RequestMapping(value = ProfitMandiConstants.URL_USER_NOTIFICATIONS, method = RequestMethod.GET)
304
	public ResponseEntity<?> getNofitications(HttpServletRequest request,
305
			@RequestParam(name = "androidId") String androidId, @RequestParam(name = "pageNumber") int pageNumber,
306
			@RequestParam(name = "pageSize") int pageSize) throws Throwable {
307
		int userId = (int) request.getAttribute("userId");
308
		String restResponse = null;
309
		Map<String, String> params = new HashMap<>();
310
 
311
		String uri = "/getAllNotifications";
312
		params.put("user_id", userId + "");
313
		params.put("android_id", androidId);
314
		params.put("limit", pageSize + "");
315
		params.put("offset", "" + ((pageNumber - 1) * pageSize));
316
		try {
317
			RestClient rc = new RestClient(SchemeType.HTTP, nodeHost, nodePort);
318
			restResponse = rc.get(uri, params);
319
		} catch (Exception | ProfitMandiBusinessException e) {
320
			LOGGER.error("Unable to data from node server", e);
321
			return responseSender.internalServerError(e);
322
		}
323
 
324
		JsonArray result_json = Json.parse(restResponse).asArray();
325
 
326
		List<Notification> notifications = new ArrayList<>();
327
 
328
		for (JsonValue j : result_json) {
329
			notifications.add(toNotifiaction(j.asObject()));
330
		}
331
 
332
		return responseSender.ok(notifications);
333
	}
334
 
335
	private Notification toNotifiaction(JsonObject jsonObject) {
336
		Notification n = (Notification) (new Gson().fromJson(jsonObject.toString(), Notification.class));
21995 amit.gupta 337
		if (n.getStatus().equals("opened") || n.getStatus().equals("referrer") || n.getStatus().equals("seen")) {
21855 amit.gupta 338
			n.setSeen(true);
339
		}
340
		return n;
341
	}
342
 
21426 ashik.ali 343
	@RequestMapping(value = ProfitMandiConstants.URL_USER_IS_EXIST_MOBILE_NUMBER, method = RequestMethod.GET)
21469 amit.gupta 344
	public ResponseEntity<?> isMobileNumberExist(HttpServletRequest request,
345
			@RequestParam(name = "mobileNumber") String mobileNumber) {
346
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21448 ashik.ali 347
		return responseSender.ok(userRepository.isExistByMobileNumber(mobileNumber));
21426 ashik.ali 348
	}
21469 amit.gupta 349
 
350
	@RequestMapping(value = ProfitMandiConstants.URL_USER_EMAIL_ID, method = RequestMethod.GET)
351
	public ResponseEntity<?> getByEmailId(HttpServletRequest request, @RequestParam(name = "emailId") String emailId) {
352
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 353
		try {
21448 ashik.ali 354
			return responseSender.ok(userRepository.selectByEmailId(emailId));
21469 amit.gupta 355
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
356
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 357
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 358
		}
359
	}
21469 amit.gupta 360
 
361
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_ADD, method = RequestMethod.POST)
362
	public ResponseEntity<?> addRole(HttpServletRequest request, @RequestBody UserAddRoleRequest userAddRoleRequest) {
363
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 364
		try {
21426 ashik.ali 365
			User user = userRepository.selectById(userAddRoleRequest.getUserId());
21469 amit.gupta 366
 
22032 ashik.ali 367
			/*
368
			 * Role role = null; try { role =
369
			 * roleRepository.selectByNameAndType(userAddRoleRequest.getRole().
370
			 * getName(), userAddRoleRequest.getRole().getType()); } catch
371
			 * (ProfitMandiBusinessException profitMandiBusinessException) {
372
			 * role = new Role();
373
			 * role.setName(userAddRoleRequest.getRole().getName());
374
			 * role.setType(userAddRoleRequest.getRole().getType());
375
			 * roleRepository.persist(role); }
376
			 */
21426 ashik.ali 377
			Permission permission = new Permission();
22011 ashik.ali 378
			permission.setType(userAddRoleRequest.getPermissionType());
379
			permission.setRoleType(userAddRoleRequest.getRoleType());
21426 ashik.ali 380
			permissionRepository.persist(permission);
381
			UserRole userRole = new UserRole();
22011 ashik.ali 382
			userRole.setRoleType(userAddRoleRequest.getRoleType());
21426 ashik.ali 383
			userRole.setUserId(user.getId());
384
			userRoleRepository.persist(userRole);
21448 ashik.ali 385
			return responseSender.ok("");
21469 amit.gupta 386
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
387
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 388
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 389
		}
390
	}
21469 amit.gupta 391
 
392
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_REMOVE, method = RequestMethod.DELETE)
22011 ashik.ali 393
	public ResponseEntity<?> removeRole(HttpServletRequest request, @RequestParam(name = "roleType") RoleType roleType,
21469 amit.gupta 394
			@RequestParam(name = "userId") int userId) {
395
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 396
		try {
21426 ashik.ali 397
			userRepository.selectById(userId);
22011 ashik.ali 398
			userRoleRepository.deleteByUserAndRoleType(userId, roleType);
399
			permissionRepository.deleteByRoleType(roleType);
21448 ashik.ali 400
			return responseSender.ok("");
21469 amit.gupta 401
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
402
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 403
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 404
		}
405
	}
21469 amit.gupta 406
 
407
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_ALL, method = RequestMethod.GET)
408
	public ResponseEntity<?> getAllRoles(HttpServletRequest request, @RequestParam(name = "id") int id) {
409
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21448 ashik.ali 410
		return responseSender.ok(userRoleRepository.selectRolesByUserId(id));
21248 ashik.ali 411
	}
21414 kshitij.so 412
 
413
	@RequestMapping(value = ProfitMandiConstants.URL_ADMIN_TOKEN, method = RequestMethod.POST)
21469 amit.gupta 414
	public ResponseEntity<?> getAdminToken(HttpServletRequest request,
415
			@RequestParam(name = "adminToken") String adminToken, @RequestParam(name = "emailId") String emailId) {
416
		LOGGER.info("requested url : " + request.getRequestURL().toString());
417
		if (!adminToken.equals(validAdminToken)) {
418
			final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(LocalDateTime.now(),
419
					request.getRequestURL().toString(), HttpStatus.FORBIDDEN.toString(), HttpStatus.FORBIDDEN,
420
					ResponseStatus.FAILURE, null);
21414 kshitij.so 421
			return new ResponseEntity<>(profitMandiResponse, HttpStatus.FORBIDDEN);
422
		}
21469 amit.gupta 423
 
21414 kshitij.so 424
		Map<String, Object> responseMap = new HashMap<>(2);
21469 amit.gupta 425
		try {
21414 kshitij.so 426
			User user = userRepository.selectByEmailId(emailId);
22011 ashik.ali 427
			List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
428
			String[] roleTypes = new String[userRoles.size()];
21414 kshitij.so 429
			int index = 0;
22011 ashik.ali 430
			for (UserRole userRole : userRoles) {
431
				roleTypes[index++] = userRole.getRoleType().toString();
21414 kshitij.so 432
			}
433
			responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), roleTypes));
434
			responseMap.put(ProfitMandiConstants.REGISTERED, true);
21469 amit.gupta 435
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
436
			responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(emailId));
21414 kshitij.so 437
			responseMap.put(ProfitMandiConstants.REGISTERED, false);
438
		}
21448 ashik.ali 439
		return responseSender.ok(responseMap);
21469 amit.gupta 440
 
21414 kshitij.so 441
	}
22032 ashik.ali 442
 
22355 ashik.ali 443
	@RequestMapping(value = ProfitMandiConstants.URL_ADMIN_MIGRATE, method = RequestMethod.GET)
22175 amit.gupta 444
	@ApiImplicitParams({
445
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
22355 ashik.ali 446
	public ResponseEntity<?> migrate(HttpServletRequest request, @RequestParam(name = "userId") int userId) {
21503 ashik.ali 447
		LOGGER.info("requested url : " + request.getRequestURL().toString());
22355 ashik.ali 448
		migrationUtil.migrateUserToRetailer();
21503 ashik.ali 449
		return responseSender.ok(ResponseCodeHolder.getMessage("OK_1000"));
450
	}
21248 ashik.ali 451
}