Subversion Repositories SmartDukaan

Rev

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