Subversion Repositories SmartDukaan

Rev

Rev 25483 | Rev 25488 | 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
 
25300 tejbeer 3
import java.io.IOException;
21248 ashik.ali 4
import java.time.LocalDateTime;
21855 amit.gupta 5
import java.util.ArrayList;
25366 tejbeer 6
import java.util.Arrays;
25300 tejbeer 7
import java.util.Base64;
21428 amit.gupta 8
import java.util.HashMap;
23786 amit.gupta 9
import java.util.HashSet;
21855 amit.gupta 10
import java.util.List;
21277 ashik.ali 11
import java.util.Map;
23786 amit.gupta 12
import java.util.Set;
21248 ashik.ali 13
 
14
import javax.servlet.http.HttpServletRequest;
15
 
25300 tejbeer 16
import org.apache.http.client.ClientProtocolException;
23786 amit.gupta 17
import org.apache.logging.log4j.LogManager;
23859 ashik.ali 18
import org.apache.logging.log4j.Logger;
21278 ashik.ali 19
import org.springframework.beans.factory.annotation.Autowired;
21414 kshitij.so 20
import org.springframework.beans.factory.annotation.Value;
25366 tejbeer 21
import org.springframework.http.MediaType;
21248 ashik.ali 22
import org.springframework.http.ResponseEntity;
23
import org.springframework.stereotype.Controller;
21702 ashik.ali 24
import org.springframework.transaction.annotation.Transactional;
21366 kshitij.so 25
import org.springframework.web.bind.annotation.RequestBody;
21248 ashik.ali 26
import org.springframework.web.bind.annotation.RequestMapping;
27
import org.springframework.web.bind.annotation.RequestMethod;
28
import org.springframework.web.bind.annotation.RequestParam;
29
 
30
import com.spice.profitmandi.common.ResponseCodeHolder;
31
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
25366 tejbeer 32
import com.spice.profitmandi.common.model.CustomRetailer;
22930 ashik.ali 33
import com.spice.profitmandi.common.model.GoogleLoginRequest;
21248 ashik.ali 34
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22492 amit.gupta 35
import com.spice.profitmandi.common.model.RegisteredUserInfo;
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;
25366 tejbeer 40
import com.spice.profitmandi.dao.entity.auth.AuthUser;
21735 ashik.ali 41
import com.spice.profitmandi.dao.entity.dtr.Retailer;
23858 ashik.ali 42
import com.spice.profitmandi.dao.entity.dtr.Role;
21735 ashik.ali 43
import com.spice.profitmandi.dao.entity.dtr.User;
25366 tejbeer 44
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
21735 ashik.ali 45
import com.spice.profitmandi.dao.entity.dtr.UserRole;
22355 ashik.ali 46
import com.spice.profitmandi.dao.entity.user.Address;
25458 tejbeer 47
import com.spice.profitmandi.dao.entity.user.Promoter;
48
import com.spice.profitmandi.dao.entity.user.Refferal;
21735 ashik.ali 49
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
21643 ashik.ali 50
import com.spice.profitmandi.dao.model.UserCart;
25366 tejbeer 51
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
25458 tejbeer 52
import com.spice.profitmandi.dao.repository.dtr.RefferalRepository;
21735 ashik.ali 53
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
23786 amit.gupta 54
import com.spice.profitmandi.dao.repository.dtr.RoleRepository;
21735 ashik.ali 55
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
56
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
57
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
22355 ashik.ali 58
import com.spice.profitmandi.dao.repository.user.AddressRepository;
25458 tejbeer 59
import com.spice.profitmandi.dao.repository.user.PromoterRepository;
24491 amit.gupta 60
import com.spice.profitmandi.service.AuthService;
23798 amit.gupta 61
import com.spice.profitmandi.service.authentication.RoleManager;
25366 tejbeer 62
import com.spice.profitmandi.service.user.RetailerService;
23787 amit.gupta 63
import com.spice.profitmandi.service.user.UserService;
21469 amit.gupta 64
import com.spice.profitmandi.web.enumuration.UserStatus;
21277 ashik.ali 65
import com.spice.profitmandi.web.processor.GoogleLoginProcessor;
25458 tejbeer 66
import com.spice.profitmandi.web.req.CreateRefferalRequest;
21366 kshitij.so 67
import com.spice.profitmandi.web.req.UserRequest;
25366 tejbeer 68
import com.spice.profitmandi.web.res.Partner;
21248 ashik.ali 69
 
21469 amit.gupta 70
import io.swagger.annotations.ApiImplicitParam;
71
import io.swagger.annotations.ApiImplicitParams;
72
 
21248 ashik.ali 73
/**
74
 * @author ashikali
75
 *
76
 */
77
@Controller
25366 tejbeer 78
@Transactional(rollbackFor = Throwable.class)
21248 ashik.ali 79
public class UserController {
21469 amit.gupta 80
 
21448 ashik.ali 81
	@Autowired
22930 ashik.ali 82
	private ResponseSender<?> responseSender;
21469 amit.gupta 83
 
23568 govind 84
	private static final Logger LOGGER = LogManager.getLogger(UserController.class);
21469 amit.gupta 85
 
21855 amit.gupta 86
	@Value("${notifications.api.host}")
87
	private String nodeHost;
25366 tejbeer 88
 
21855 amit.gupta 89
	@Value("${notifications.api.port}")
90
	private int nodePort;
91
 
21414 kshitij.so 92
	@Value("${admin.token}")
93
	private String validAdminToken;
21469 amit.gupta 94
 
21278 ashik.ali 95
	@Autowired
22873 ashik.ali 96
	private UserRepository userRepository;
25366 tejbeer 97
 
22355 ashik.ali 98
	@Autowired
22873 ashik.ali 99
	private com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
21469 amit.gupta 100
 
21278 ashik.ali 101
	@Autowired
22873 ashik.ali 102
	private RetailerRepository retailerRepository;
21485 amit.gupta 103
 
104
	@Autowired
22873 ashik.ali 105
	private UserRoleRepository userRoleRepository;
21469 amit.gupta 106
 
21426 ashik.ali 107
	@Autowired
22873 ashik.ali 108
	private UserAccountRepository userAccountRepository;
25366 tejbeer 109
 
22355 ashik.ali 110
	@Autowired
24491 amit.gupta 111
	private AuthService authService;
25366 tejbeer 112
 
24491 amit.gupta 113
	@Autowired
22873 ashik.ali 114
	private AddressRepository addressRepository;
21469 amit.gupta 115
 
21426 ashik.ali 116
	@Autowired
22873 ashik.ali 117
	private GoogleLoginProcessor googleLoginProcessor;
21855 amit.gupta 118
 
21784 amit.gupta 119
	@Autowired
22873 ashik.ali 120
	private UserService userService;
25366 tejbeer 121
 
23786 amit.gupta 122
	@Autowired
23858 ashik.ali 123
	private RestClient restClient;
25366 tejbeer 124
 
23858 ashik.ali 125
	@Autowired
23798 amit.gupta 126
	private RoleManager roleManager;
25366 tejbeer 127
 
23786 amit.gupta 128
	@Autowired
129
	private RoleRepository roleRepository;
21469 amit.gupta 130
 
25366 tejbeer 131
	@Autowired
132
	private AuthRepository authRepository;
133
 
134
	@Autowired
135
	private RetailerService retailerService;
136
 
25458 tejbeer 137
	@Autowired
138
	private RefferalRepository refferalRepository;
139
 
140
	@Autowired
141
	private PromoterRepository promoterRepository;
142
 
21469 amit.gupta 143
	@RequestMapping(value = ProfitMandiConstants.URL_USER_GOOGLE_LOGIN, method = RequestMethod.POST)
25366 tejbeer 144
	public ResponseEntity<?> googleLogin(HttpServletRequest request, @RequestBody GoogleLoginRequest googleLoginRequest)
145
			throws ProfitMandiBusinessException {
23698 amit.gupta 146
		LOGGER.info("requested url : " + request.getRequestURL().toString());
24494 amit.gupta 147
		Map<String, Object> resultMap = googleLoginProcessor.process(googleLoginRequest);
148
		LOGGER.info("Response sent is {}", resultMap);
23698 amit.gupta 149
		return responseSender.ok(googleLoginProcessor.process(googleLoginRequest));
21277 ashik.ali 150
	}
21469 amit.gupta 151
 
152
	@RequestMapping(value = ProfitMandiConstants.URL_USER_TOKEN_IS_EXPIRED, method = RequestMethod.GET)
25366 tejbeer 153
	public ResponseEntity<?> tokenIsExpired(HttpServletRequest request, @RequestParam(name = "token") String token)
154
			throws ProfitMandiBusinessException {
21469 amit.gupta 155
		LOGGER.info("requested url : " + request.getRequestURL().toString());
22930 ashik.ali 156
		return responseSender.ok(JWTUtil.isExpired(token));
21282 ashik.ali 157
	}
21469 amit.gupta 158
 
22355 ashik.ali 159
	@RequestMapping(value = ProfitMandiConstants.URL_USER_DETAIL_BY_TOKEN, method = RequestMethod.GET)
21469 amit.gupta 160
	@ApiImplicitParams({
161
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
162
	public ResponseEntity<?> tokenInfo(HttpServletRequest request) throws Throwable {
163
		Map<String, Object> responseMap = new HashMap<>();
164
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
21483 amit.gupta 165
		User user = null;
21855 amit.gupta 166
		if (userInfo.getUserId() > -1) {
21483 amit.gupta 167
			user = userRepository.selectById(userInfo.getUserId());
25366 tejbeer 168
			if (!(userInfo.getEmail() == null || user.getEmailId().equalsIgnoreCase(userInfo.getEmail()))) {
24491 amit.gupta 169
				String userName = null;
25366 tejbeer 170
				if (user.getSecondryEmailId() != null && user.getSecondryEmailId().equals(userInfo.getEmail())) {
24491 amit.gupta 171
					userName = user.getFirstName() + " " + user.getLastName();
172
				} else {
173
					userName = authService.getNameByEmailId(userInfo.getEmail());
174
				}
175
				responseMap.put(ProfitMandiConstants.USER_ID, userInfo.getUserId());
176
				responseMap.put(ProfitMandiConstants.USER_NAME, userName);
177
				responseMap.put(ProfitMandiConstants.USER_STATUS, "fofoAssociate");
178
				responseMap.put(ProfitMandiConstants.EMAIL_ID, userInfo.getEmail());
179
				return responseSender.ok(responseMap);
180
			}
21483 amit.gupta 181
		} else {
182
			try {
183
				user = userRepository.selectByEmailId(userInfo.getEmail());
184
			} catch (ProfitMandiBusinessException e1) {
185
			}
25366 tejbeer 186
			if (user == null) {
23204 ashik.ali 187
				try {
188
					user = userRepository.selectBySecondryEmailId(userInfo.getEmail());
189
				} catch (ProfitMandiBusinessException e1) {
190
					LOGGER.info("Uneregistered user", userInfo.getEmail());
191
				}
192
			}
21483 amit.gupta 193
		}
194
		if (user != null) {
23858 ashik.ali 195
			int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
21491 amit.gupta 196
			responseMap.put(ProfitMandiConstants.EMAIL_ID, user.getEmailId());
21526 amit.gupta 197
			responseMap.put(ProfitMandiConstants.USER_ID, user.getId());
22017 amit.gupta 198
			responseMap.put(ProfitMandiConstants.USER_NAME, user.getFirstName() + " " + user.getLastName());
23786 amit.gupta 199
			List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(user.getId());
25366 tejbeer 200
			// LOGGER.info("userRoles {} ", userRoles);
22032 ashik.ali 201
 
202
			// generate new token if roles have been updated
23786 amit.gupta 203
			if (userInfo.getRoleIds() == null || roleIds.size() != userInfo.getRoleIds().size()) {
23858 ashik.ali 204
				String[] roleIdStrings = new String[roleIds.size()];
22603 amit.gupta 205
				int index = 0;
23786 amit.gupta 206
				for (int roleId : roleIds) {
23858 ashik.ali 207
					roleIdStrings[index++] = String.valueOf(roleId);
22603 amit.gupta 208
				}
23858 ashik.ali 209
				String newToken = JWTUtil.create(user.getId(), retailerId, roleIdStrings);
21483 amit.gupta 210
				responseMap.put("newAuthToken", newToken);
211
			}
23858 ashik.ali 212
			// if user is retailer
23786 amit.gupta 213
			Set<Integer> roleIdsSet = new HashSet<Integer>(roleIds);
23858 ashik.ali 214
			if (roleManager.isRetailer(roleIdsSet)) {
22032 ashik.ali 215
				UserCart uc = userAccountRepository.getUserCart(userInfo.getUserId());
216
				Retailer retailer = retailerRepository.selectById(uc.getUserId());
22370 amit.gupta 217
				com.spice.profitmandi.dao.entity.user.User saholicUser = userUserRepository.selectById(uc.getUserId());
25366 tejbeer 218
				if (saholicUser.getAddressId() != null) {
22370 amit.gupta 219
					Address address = addressRepository.selectById(saholicUser.getAddressId());
220
					responseMap.put(ProfitMandiConstants.ADDRESS, address);
221
				}
21485 amit.gupta 222
				// if retailer is activated 1 then verified retailer
223
				// else if migrated is 1 then old retailer
25366 tejbeer 224
				// also lets incoporte old process i.e is user is activated then also retailer
225
				// is verified retailer
21485 amit.gupta 226
				// else retailer is not verifed
22497 amit.gupta 227
				if (retailer.isActive() || user.isActivated()) {
23798 amit.gupta 228
					if (roleManager.isPartner(roleIdsSet)) {
22017 amit.gupta 229
						responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.FOFO.getValue());
230
					} else {
231
						responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.VERIFIED_RETAILER.getValue());
232
					}
22032 ashik.ali 233
				} else if (retailer.isMigrated()) {
21485 amit.gupta 234
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.RETAILER.getValue());
21469 amit.gupta 235
				} else {
21485 amit.gupta 236
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_VERIFIED_RETAILER.getValue());
21469 amit.gupta 237
				}
23798 amit.gupta 238
			} else if (roleManager.isUser(roleIdsSet)) {
22502 amit.gupta 239
				responseMap.put("userInfo", getRegisteredUserInfo(user));
21469 amit.gupta 240
				responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.REGISTERED.getValue());
22032 ashik.ali 241
			}
21491 amit.gupta 242
		} else {
243
			responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_REGISTERED.getValue());
244
			responseMap.put(ProfitMandiConstants.EMAIL_ID, userInfo.getEmail());
21469 amit.gupta 245
		}
22032 ashik.ali 246
 
21469 amit.gupta 247
		return responseSender.ok(responseMap);
248
	}
25366 tejbeer 249
 
22502 amit.gupta 250
	private RegisteredUserInfo getRegisteredUserInfo(User user) throws Throwable {
22492 amit.gupta 251
		RegisteredUserInfo ri = new RegisteredUserInfo();
252
		ri.setCity(user.getCity());
253
		ri.setFirstName(user.getFirstName());
254
		ri.setLastName(user.getLastName());
22493 amit.gupta 255
		ri.setPhone(user.getMobileNumber());
22492 amit.gupta 256
		ri.setPinCode(user.getPinCode());
22845 amit.gupta 257
		ri.setState(user.getState());
22492 amit.gupta 258
		return ri;
259
	}
21469 amit.gupta 260
 
261
	@RequestMapping(value = ProfitMandiConstants.URL_USER, method = RequestMethod.POST)
21501 amit.gupta 262
	@ApiImplicitParams({
22032 ashik.ali 263
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
264
	public ResponseEntity<?> createUser(HttpServletRequest request, @RequestBody UserRequest userRequest)
22930 ashik.ali 265
			throws ProfitMandiBusinessException {
21469 amit.gupta 266
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21368 kshitij.so 267
		User user = new User();
268
		user.setFirstName(userRequest.getFirstName());
269
		user.setLastName(userRequest.getLastName());
270
		user.setCity(userRequest.getCity());
271
		user.setPinCode(Integer.valueOf(userRequest.getPinCode()));
22845 amit.gupta 272
		user.setState(userRequest.getState());
21708 amit.gupta 273
		user.setMobileNumber(userRequest.getMobieNumber());
21368 kshitij.so 274
		user.setEmailId(userRequest.getEmailId());
275
		user.setUsername("");
276
		user.setPassword("");
277
		user.setMobile_verified(false);
278
		user.setReferral_url("");
279
		user.setGroup_id(1);
22504 amit.gupta 280
		user.setStatus(1);
21368 kshitij.so 281
		user.setActivated(false);
21501 amit.gupta 282
		user.setCreateTimestamp(LocalDateTime.now());
283
		user.setUpdateTimestamp(LocalDateTime.now());
22930 ashik.ali 284
		userRepository.persist(user);
23858 ashik.ali 285
		Role role = roleRepository.selectByName(RoleType.USER.toString());
22930 ashik.ali 286
		UserRole userRole = new UserRole();
23858 ashik.ali 287
		userRole.setRoleId(role.getId());
22930 ashik.ali 288
		userRole.setUserId(user.getId());
289
		userRoleRepository.persist(userRole);
290
		return responseSender.ok(ResponseCodeHolder.getMessage("USR_OK_1000"));
25366 tejbeer 291
 
21278 ashik.ali 292
	}
21469 amit.gupta 293
 
294
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ID, method = RequestMethod.GET)
25366 tejbeer 295
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id)
296
			throws ProfitMandiBusinessException {
21469 amit.gupta 297
		LOGGER.info("requested url : " + request.getRequestURL().toString());
22930 ashik.ali 298
		return responseSender.ok(userRepository.selectById(id));
21248 ashik.ali 299
	}
21469 amit.gupta 300
 
301
	@RequestMapping(value = ProfitMandiConstants.URL_USER_MOBILE_NUMBER, method = RequestMethod.GET)
302
	public ResponseEntity<?> getByMobileNumber(HttpServletRequest request,
25366 tejbeer 303
			@RequestParam(name = "mobileNumber") String mobileNumber) throws ProfitMandiBusinessException {
21469 amit.gupta 304
		LOGGER.info("requested url : " + request.getRequestURL().toString());
22930 ashik.ali 305
		return responseSender.ok(userRepository.selectByMobileNumber(mobileNumber));
21248 ashik.ali 306
	}
21469 amit.gupta 307
 
21784 amit.gupta 308
	@ApiImplicitParams({
21501 amit.gupta 309
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
310
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ACTIVATE, method = RequestMethod.POST)
311
	public ResponseEntity<?> activateUser(HttpServletRequest request,
21784 amit.gupta 312
			@RequestParam(name = "activationCode") String activationCode) throws Throwable {
21855 amit.gupta 313
		int userId = (int) request.getAttribute("userId");
21784 amit.gupta 314
		UserCart uc = userAccountRepository.getUserCart(userId);
315
		return responseSender.ok(userService.updateActivation(userId, uc.getUserId(), activationCode));
316
	}
21855 amit.gupta 317
 
25458 tejbeer 318
	/*
319
	 * @ApiImplicitParams({
320
	 * 
321
	 * @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true,
322
	 * dataType = "string", paramType = "header") })
323
	 * 
324
	 * @RequestMapping(value = ProfitMandiConstants.URL_USER_NOTIFICATIONS, method =
325
	 * RequestMethod.GET) public ResponseEntity<?>
326
	 * getNofitications(HttpServletRequest request,
327
	 * 
328
	 * @RequestParam(name = "androidId") String androidId, @RequestParam(name =
329
	 * "pageNumber") int pageNumber,
330
	 * 
331
	 * @RequestParam(name = "pageSize") int pageSize) throws
332
	 * ProfitMandiBusinessException { int userId = (int)
333
	 * request.getAttribute("userId"); String restResponse = null; Map<String,
334
	 * String> params = new HashMap<>();
335
	 * 
336
	 * String uri = "/getAllNotifications"; params.put("user_id", userId + "");
337
	 * params.put("android_id", androidId); params.put("limit", pageSize + "");
338
	 * params.put("offset", "" + ((pageNumber - 1) * pageSize)); try { restResponse
339
	 * = restClient.get(SchemeType.HTTP, nodeHost, nodePort, uri, params); } catch
340
	 * (HttpHostConnectException e) { throw new ProfitMandiBusinessException("", "",
341
	 * "Could not Connect to host"); }
342
	 * 
343
	 * JsonArray result_json = Json.parse(restResponse).asArray();
344
	 * 
345
	 * List<Notification> notifications = new ArrayList<>();
346
	 * 
347
	 * for (JsonValue j : result_json) {
348
	 * notifications.add(toNotifiaction(j.asObject())); }
349
	 * 
350
	 * return responseSender.ok(notifications); }
351
	 * 
352
	 * private Notification toNotifiaction(JsonObject jsonObject) { Notification n =
353
	 * (Notification) (new Gson().fromJson(jsonObject.toString(),
354
	 * Notification.class)); if (n.getStatus().equals("opened") ||
355
	 * n.getStatus().equals("referrer") || n.getStatus().equals("seen")) {
356
	 * n.setSeen(true); } return n; }
357
	 */
21855 amit.gupta 358
 
21426 ashik.ali 359
	@RequestMapping(value = ProfitMandiConstants.URL_USER_IS_EXIST_MOBILE_NUMBER, method = RequestMethod.GET)
21469 amit.gupta 360
	public ResponseEntity<?> isMobileNumberExist(HttpServletRequest request,
361
			@RequestParam(name = "mobileNumber") String mobileNumber) {
362
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21448 ashik.ali 363
		return responseSender.ok(userRepository.isExistByMobileNumber(mobileNumber));
21426 ashik.ali 364
	}
21469 amit.gupta 365
 
366
	@RequestMapping(value = ProfitMandiConstants.URL_USER_EMAIL_ID, method = RequestMethod.GET)
25366 tejbeer 367
	public ResponseEntity<?> getByEmailId(HttpServletRequest request, @RequestParam(name = "emailId") String emailId)
368
			throws ProfitMandiBusinessException {
21469 amit.gupta 369
		LOGGER.info("requested url : " + request.getRequestURL().toString());
23204 ashik.ali 370
		User user = null;
25366 tejbeer 371
		try {
23204 ashik.ali 372
			user = userRepository.selectByEmailId(emailId);
25366 tejbeer 373
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23204 ashik.ali 374
			user = userRepository.selectBySecondryEmailId(emailId);
375
		}
376
		return responseSender.ok(user);
21248 ashik.ali 377
	}
21469 amit.gupta 378
 
379
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_ALL, method = RequestMethod.GET)
380
	public ResponseEntity<?> getAllRoles(HttpServletRequest request, @RequestParam(name = "id") int id) {
381
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21448 ashik.ali 382
		return responseSender.ok(userRoleRepository.selectRolesByUserId(id));
21248 ashik.ali 383
	}
25366 tejbeer 384
 
23204 ashik.ali 385
	@ApiImplicitParams({
25366 tejbeer 386
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
23204 ashik.ali 387
	@RequestMapping(value = ProfitMandiConstants.URL_USER_SECONDRY_EMAIL_ID_AND_FOFO_STORE_CODE, method = RequestMethod.GET)
25366 tejbeer 388
	public ResponseEntity<?> getSecondryEmailAndStoreCode(HttpServletRequest request)
389
			throws ProfitMandiBusinessException {
23204 ashik.ali 390
		LOGGER.info("requested url : " + request.getRequestURL().toString());
391
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
392
		return responseSender.ok(userService.getEmailsAndFofoStoreCodeByUserId(userId));
393
	}
25366 tejbeer 394
 
23204 ashik.ali 395
	@ApiImplicitParams({
25366 tejbeer 396
			@ApiImplicitParam(name = "Auth-Token", value = "-Token", required = true, dataType = "string", paramType = "header") })
23858 ashik.ali 397
	@RequestMapping(value = ProfitMandiConstants.URL_USER_SECONDRY_EMAIL_ID, method = RequestMethod.PUT)
23204 ashik.ali 398
	public ResponseEntity<?> updateSecondryEmailId(HttpServletRequest request,
25366 tejbeer 399
			@RequestParam(name = ProfitMandiConstants.SECONDRY_EMAIL_ID) String secondryEmailId) throws Throwable {
400
		int userId = (int) request.getAttribute("userId");
401
		User user = userRepository.selectById(userId);
402
		user.setSecondryEmailId(secondryEmailId);
403
		userRepository.persist(user);
404
		return responseSender.ok(ResponseCodeHolder.getMessage("USR_OK_1001"));
405
	}
21414 kshitij.so 406
 
407
	@RequestMapping(value = ProfitMandiConstants.URL_ADMIN_TOKEN, method = RequestMethod.POST)
21469 amit.gupta 408
	public ResponseEntity<?> getAdminToken(HttpServletRequest request,
409
			@RequestParam(name = "adminToken") String adminToken, @RequestParam(name = "emailId") String emailId) {
410
		LOGGER.info("requested url : " + request.getRequestURL().toString());
411
		if (!adminToken.equals(validAdminToken)) {
22930 ashik.ali 412
			return responseSender.forbidden(null);
21414 kshitij.so 413
		}
21469 amit.gupta 414
 
21414 kshitij.so 415
		Map<String, Object> responseMap = new HashMap<>(2);
23204 ashik.ali 416
		User user = null;
21469 amit.gupta 417
		try {
23204 ashik.ali 418
			user = userRepository.selectByEmailId(emailId);
419
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
25366 tejbeer 420
 
23204 ashik.ali 421
		}
25366 tejbeer 422
 
423
		if (user == null) {
424
			try {
23204 ashik.ali 425
				user = userRepository.selectBySecondryEmailId(emailId);
25366 tejbeer 426
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
427
 
23204 ashik.ali 428
			}
429
		}
25366 tejbeer 430
		if (user != null) {
23858 ashik.ali 431
			int retailerId = 0;
432
			try {
433
				retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
434
			} catch (ProfitMandiBusinessException e) {
435
				// TODO Auto-generated catch block
436
				e.printStackTrace();
437
			}
438
			List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(user.getId());
439
			String[] roleIdsString = new String[roleIds.size()];
21414 kshitij.so 440
			int index = 0;
23858 ashik.ali 441
			for (int roleId : roleIds) {
442
				roleIdsString[index++] = String.valueOf(roleId);
21414 kshitij.so 443
			}
23858 ashik.ali 444
			responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), retailerId, roleIdsString));
21414 kshitij.so 445
			responseMap.put(ProfitMandiConstants.REGISTERED, true);
25366 tejbeer 446
		} else {
21469 amit.gupta 447
			responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(emailId));
21414 kshitij.so 448
			responseMap.put(ProfitMandiConstants.REGISTERED, false);
449
		}
21448 ashik.ali 450
		return responseSender.ok(responseMap);
21469 amit.gupta 451
 
21414 kshitij.so 452
	}
25366 tejbeer 453
 
25300 tejbeer 454
	@RequestMapping(value = "/mobileappsettings", method = RequestMethod.POST)
25366 tejbeer 455
	public ResponseEntity<?> mobileAppSettings(HttpServletRequest request, @RequestParam(name = "t") int timestamp,
456
			@RequestParam(name = "imeinumber") String imeinumber)
457
			throws ProfitMandiBusinessException, ClientProtocolException, IOException {
458
 
459
		final String uri = "http://api.profittill.com/mobileappsettings?t=" + timestamp + "&imeinumber=" + imeinumber;
25300 tejbeer 460
		final String BASIC_AUTH = "Basic " + Base64.getEncoder().encodeToString("dtr:dtr18Feb2015".getBytes());
461
		Map<String, String> headers = new HashMap<>();
462
		Map<String, String> params = new HashMap<>();
463
		headers.put("Authorization", BASIC_AUTH);
464
		return responseSender.ok(restClient.post(uri, params, headers));
465
	}
22032 ashik.ali 466
 
25366 tejbeer 467
	@RequestMapping(value = "/getPartners", method = RequestMethod.GET)
468
	@ApiImplicitParams({
469
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
470
	public ResponseEntity<?> getPartners(HttpServletRequest request, @RequestParam(name = "gmailId") String gmailId,
471
			@RequestParam(value = "offset") int offset, @RequestParam(value = "limit") int limit)
472
			throws ProfitMandiBusinessException {
473
		AuthUser authUser = authRepository.selectByGmailId(gmailId);
474
 
475
		Map<String, List<String>> storeGuyMap = new HashMap<>();
476
		for (Map.Entry<String, List<String>> entry : storeManagerMap.entrySet()) {
477
			String storeEmail = entry.getKey();
478
			List<String> storeGuys = entry.getValue();
479
			for (String storeGuy : storeGuys) {
480
				if (!storeGuyMap.containsKey(storeGuy)) {
481
					storeGuyMap.put(storeGuy, new ArrayList<>());
482
				}
483
				storeGuyMap.get(storeGuy).add(storeEmail);
484
			}
485
 
486
		}
487
		storeGuyMap.remove("");
488
 
25483 tejbeer 489
		List<String> emails = storeGuyMap.get(authUser.getEmailId().toLowerCase());
25366 tejbeer 490
		LOGGER.info("emails" + emails);
25458 tejbeer 491
		List<User> users = userRepository.selectAllByEmails(emails, offset, limit);
25366 tejbeer 492
		List<Partner> partners = new ArrayList<>();
493
		for (User user : users) {
25458 tejbeer 494
 
25366 tejbeer 495
			UserAccount uc = userAccountRepository.selectSaholicByUserId(user.getId());
25458 tejbeer 496
			com.spice.profitmandi.dao.entity.user.User userInfo = userUserRepository.selectById(uc.getAccountKey());
25366 tejbeer 497
			CustomRetailer customRetailer = retailerService.getFofoRetailer(userInfo.getId());
498
 
499
			Partner partner = new Partner();
500
			partner.setBusinessName(customRetailer.getBusinessName());
501
			partner.setPartnerId(customRetailer.getPartnerId());
502
			partner.setCartId(customRetailer.getCartId());
503
			partner.setEmail(customRetailer.getEmail());
504
			partner.setGstNumber(customRetailer.getGstNumber());
505
			partner.setDisplayName(customRetailer.getDisplayName());
506
			partner.setCity(customRetailer.getAddress().getCity());
507
			partner.setUserId(user.getId());
508
			partners.add(partner);
509
		}
510
		LOGGER.info("partners" + partners);
511
		return responseSender.ok(partners);
512
	}
513
 
514
	public static final Map<String, String> nameEmail = new HashMap<>();
515
 
516
	public static final Map<String, List<String>> storeManagerMap = new HashMap<>();
517
 
518
	static {
519
		nameEmail.put("Amod", "amod.sen@smartdukaan.com");
520
		nameEmail.put("Parmod", "parmod.kumar@smartdukaan.com");
521
		nameEmail.put("Anis", "md.anis@smartdukaan.com");
522
		nameEmail.put("Manoj", "manoj.singh@smartdukaan.com");
523
		nameEmail.put("Adeel", "adeel.yazdani@smartdukaan.com");
524
		nameEmail.put("Mohinder", "mohinder.mutreja@smartdukaan.com");
525
		nameEmail.put("Dharmendar", "dharmender.verma@smartdukaan.com");
526
		nameEmail.put("Rajat", "rajat.gupta@smartdukaan.com");
527
		nameEmail.put("Ankit", "ankit.bhatia@smartdukaan.com");
528
		nameEmail.put("Rajit", "rajit.alag@smartdukaan.com");
529
		nameEmail.put("Gulshan", "gulshan.kumar@smartdukaan.com");
530
		nameEmail.put("PramodSharma", "parmod.sharma@smartdukaan.com");
531
		// nameEmail.put("Gulshan","");
25458 tejbeer 532
		storeManagerMap.put("themobileplanet03@gmail.com",
533
				Arrays.asList("amod.sen@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
534
		storeManagerMap.put("mobileplanet7599@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
535
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
536
		storeManagerMap.put("robintelecom.snp@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
537
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
538
		storeManagerMap.put("smartconnect4141@gmail.com",
539
				Arrays.asList("amod.sen@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
540
		storeManagerMap.put("bhimsain919@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
541
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
542
		storeManagerMap.put("babitaranirk@gmail.com",
543
				Arrays.asList("parmod.sharma@smartdukaan.com", "", "mohinder.mutreja@smartdukaan.com"));
544
		storeManagerMap.put("dharmendery079@gmail.com",
545
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
546
		storeManagerMap.put("kkv.enterprises1@gmail.com",
547
				Arrays.asList("parmod.sharma@smartdukaan.com", "", "mohinder.mutreja@smartdukaan.com"));
548
		storeManagerMap.put("gulshersaifi74226@gmail.com",
549
				Arrays.asList("amod.sen@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
550
		storeManagerMap.put("deepak.lalwani1985@gmail.com",
551
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
552
		storeManagerMap.put("gupta.smartdukaan@gmail.com",
553
				Arrays.asList("amod.sen@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
554
		storeManagerMap.put("sachinindri2006@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
555
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
556
		storeManagerMap.put("kambojanil83@gmail.com",
557
				Arrays.asList("parmod.sharma@smartdukaan.com", "", "mohinder.mutreja@smartdukaan.com"));
558
		storeManagerMap.put("uppal.neeraj82@gmail.com",
559
				Arrays.asList("", "ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
560
		storeManagerMap.put("thefonehousekarnal@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
561
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
562
		storeManagerMap.put("richa9910763006@gmail.com",
563
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
564
		storeManagerMap.put("smartdukangzb@gmail.com",
565
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
566
		storeManagerMap.put("lovelymobile183@gmail.com",
567
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
568
		storeManagerMap.put("smartdukaan.sonepat@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
569
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
570
		storeManagerMap.put("arunmittal299@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
571
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
572
		storeManagerMap.put("gtc01100@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
573
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
574
		storeManagerMap.put("sumittyagi1975@gmail.com",
575
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
576
		storeManagerMap.put("metrofurniture342@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
577
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
578
		storeManagerMap.put("suryaelectronicskaithal@gmail.com",
579
				Arrays.asList("Parmod", "", "mohinder.mutreja@smartdukaan.com"));
580
		storeManagerMap.put("mmtelecomsec16@gmail.com",
581
				Arrays.asList("amod.sen@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
582
		storeManagerMap.put("omsonsindia08@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
583
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
584
		storeManagerMap.put("smartenterprisespv@gmail.com",
585
				Arrays.asList("amod.sen@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
586
		storeManagerMap.put("cachaitnya@gmail.com",
587
				Arrays.asList("amod.sen@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
588
		storeManagerMap.put("ajaykumarkansal03@gmail.com",
589
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
590
		storeManagerMap.put("spurwar73@gmail.com", Arrays.asList("md.anis@smartdukaan.com",
591
				"rajat.gupta@smartdukaan.com", "adeel.yazdani@smartdukaan.com"));
592
		storeManagerMap.put("Shreemobile.ind@gmail.com", Arrays.asList("md.anis@smartdukaan.com",
593
				"rajat.gupta@smartdukaan.com", "adeel.yazdani@smartdukaan.com"));
594
		storeManagerMap.put("felixenterprises2017@rediffmail.com",
595
				Arrays.asList("", "dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
596
		storeManagerMap.put("nareandergupta@gmail.com",
597
				Arrays.asList("", "dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
598
		storeManagerMap.put("shreebalajielectronic2019@gmail.com",
599
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
600
		storeManagerMap.put("saranshary@gmail.com",
601
				Arrays.asList("", "dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
602
		storeManagerMap.put("gambhirsmartphone@gmail.com",
603
				Arrays.asList("", "gulshan.kumar@smartdukaan.com", "adeel.yazdani@smartdukaan.com"));
604
		storeManagerMap.put("Priyankaenterprises9910@gmail.com",
605
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
606
		storeManagerMap.put("pawan.dhimaan@gmail.com",
607
				Arrays.asList("", "dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
608
		storeManagerMap.put("lakshaydhulla62@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
609
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
610
		storeManagerMap.put("parastelecom.stp@gmail.com", Arrays.asList("md.anis@smartdukaan.com",
611
				"rajat.gupta@smartdukaan.com", "adeel.yazdani@smartdukaan.com"));
612
		storeManagerMap.put("talwarmukesh298@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
613
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
614
		storeManagerMap.put("sahilnarang0009@gmail.com",
615
				Arrays.asList("parmod.sharma@smartdukaan.com", "", "mohinder.mutreja@smartdukaan.com"));
616
		storeManagerMap.put("supertelecomjind@gmail.com",
617
				Arrays.asList("", "ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
618
		storeManagerMap.put("saketnagpal@gmail.com",
619
				Arrays.asList("", "rajit.alag@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
620
		storeManagerMap.put("DEEPAKGOYAL702.DG@GMAIL.COM",
621
				Arrays.asList("", "ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
622
		storeManagerMap.put("sambhav350@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
623
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
624
		storeManagerMap.put("mayankarora98133@gmail.com",
625
				Arrays.asList("", "ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
626
		storeManagerMap.put("vijaymobilityandmoew@gmail.com",
627
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
628
		storeManagerMap.put("amitv70003@gmail.com",
629
				Arrays.asList("", "ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
630
		storeManagerMap.put("newagelucknow.123@gmail.com", Arrays.asList("md.anis@smartdukaan.com",
631
				"rajat.gupta@smartdukaan.com", "adeel.yazdani@smartdukaan.com"));
632
		storeManagerMap.put("Newagetechnologygomtinagar@gmail.com", Arrays.asList("md.anis@smartdukaan.com",
633
				"rajat.gupta@smartdukaan.com", "adeel.yazdani@smartdukaan.com"));
634
		storeManagerMap.put("rohitbatra106@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
635
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
636
		storeManagerMap.put("sonisunil9050873061@gmail.com",
637
				Arrays.asList("", "ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
638
		storeManagerMap.put("moderncohsr@gmail.com",
639
				Arrays.asList("", "ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
640
		storeManagerMap.put("raghubir.ngh@gmail.com",
641
				Arrays.asList("", "ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
642
		storeManagerMap.put("aman007singla@gmail.com",
643
				Arrays.asList("", "rajit.alag@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
25366 tejbeer 644
	}
25458 tejbeer 645
 
646
	@RequestMapping(value = "/user/refferal", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
647
	@ApiImplicitParams({
648
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
649
	public ResponseEntity<?> RefferalUser(HttpServletRequest request,
650
			@RequestBody CreateRefferalRequest createRefferalRequest) throws Exception {
651
		Refferal refferal = new Refferal();
652
		refferal.setFirstName(createRefferalRequest.getFirstName());
653
		refferal.setLastName(createRefferalRequest.getLastName());
654
		refferal.setMobile(createRefferalRequest.getMobile());
655
		refferal.setState(createRefferalRequest.getState());
656
		refferal.setCity(createRefferalRequest.getCity());
657
		refferal.setCreatedTimestamp(LocalDateTime.now());
658
		refferal.setUpdatedTimestamp(LocalDateTime.now());
659
		refferal.setStatus("pending");
660
		if (createRefferalRequest.isFofoAssociate()) {
661
			AuthUser authUser = authRepository.selectByGmailId(createRefferalRequest.getReffereeEmail());
25483 tejbeer 662
			if (authUser == null) {
25458 tejbeer 663
				Promoter promoter = promoterRepository.selectByEmailId(createRefferalRequest.getReffereeEmail());
664
				refferal.setRefereeName(promoter.getName());
25483 tejbeer 665
				refferal.setRefereeEmail(promoter.getEmail());
25458 tejbeer 666
				refferal.setRefereeMobile(promoter.getMobile());
667
			} else {
668
				refferal.setRefereeName(authUser.getFirstName());
25483 tejbeer 669
				refferal.setRefereeEmail(authUser.getGmailId());
25458 tejbeer 670
				refferal.setRefereeMobile(authUser.getMobileNumber());
671
			}
672
		} else {
673
			User user = userRepository.selectByEmailId(createRefferalRequest.getReffereeEmail());
674
			refferal.setRefereeName(user.getFirstName());
25483 tejbeer 675
			refferal.setRefereeEmail(user.getEmailId());
25458 tejbeer 676
			refferal.setRefereeMobile(user.getMobileNumber());
677
		}
678
 
679
		refferalRepository.persist(refferal);
680
		return responseSender.ok(true);
681
	}
21248 ashik.ali 682
}