Subversion Repositories SmartDukaan

Rev

Rev 25509 | Rev 25597 | 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;
25488 tejbeer 4
import java.time.LocalDate;
21248 ashik.ali 5
import java.time.LocalDateTime;
25488 tejbeer 6
import java.time.YearMonth;
21855 amit.gupta 7
import java.util.ArrayList;
25366 tejbeer 8
import java.util.Arrays;
25300 tejbeer 9
import java.util.Base64;
21428 amit.gupta 10
import java.util.HashMap;
23786 amit.gupta 11
import java.util.HashSet;
21855 amit.gupta 12
import java.util.List;
21277 ashik.ali 13
import java.util.Map;
23786 amit.gupta 14
import java.util.Set;
25488 tejbeer 15
import java.util.stream.Collectors;
21248 ashik.ali 16
 
17
import javax.servlet.http.HttpServletRequest;
18
 
25300 tejbeer 19
import org.apache.http.client.ClientProtocolException;
23786 amit.gupta 20
import org.apache.logging.log4j.LogManager;
23859 ashik.ali 21
import org.apache.logging.log4j.Logger;
21278 ashik.ali 22
import org.springframework.beans.factory.annotation.Autowired;
21414 kshitij.so 23
import org.springframework.beans.factory.annotation.Value;
25366 tejbeer 24
import org.springframework.http.MediaType;
21248 ashik.ali 25
import org.springframework.http.ResponseEntity;
26
import org.springframework.stereotype.Controller;
21702 ashik.ali 27
import org.springframework.transaction.annotation.Transactional;
21366 kshitij.so 28
import org.springframework.web.bind.annotation.RequestBody;
21248 ashik.ali 29
import org.springframework.web.bind.annotation.RequestMapping;
30
import org.springframework.web.bind.annotation.RequestMethod;
31
import org.springframework.web.bind.annotation.RequestParam;
32
 
33
import com.spice.profitmandi.common.ResponseCodeHolder;
34
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
25366 tejbeer 35
import com.spice.profitmandi.common.model.CustomRetailer;
22930 ashik.ali 36
import com.spice.profitmandi.common.model.GoogleLoginRequest;
21248 ashik.ali 37
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22492 amit.gupta 38
import com.spice.profitmandi.common.model.RegisteredUserInfo;
21469 amit.gupta 39
import com.spice.profitmandi.common.model.UserInfo;
21282 ashik.ali 40
import com.spice.profitmandi.common.util.JWTUtil;
21855 amit.gupta 41
import com.spice.profitmandi.common.web.client.RestClient;
21740 ashik.ali 42
import com.spice.profitmandi.common.web.util.ResponseSender;
25366 tejbeer 43
import com.spice.profitmandi.dao.entity.auth.AuthUser;
21735 ashik.ali 44
import com.spice.profitmandi.dao.entity.dtr.Retailer;
23858 ashik.ali 45
import com.spice.profitmandi.dao.entity.dtr.Role;
21735 ashik.ali 46
import com.spice.profitmandi.dao.entity.dtr.User;
25366 tejbeer 47
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
21735 ashik.ali 48
import com.spice.profitmandi.dao.entity.dtr.UserRole;
25580 tejbeer 49
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
22355 ashik.ali 50
import com.spice.profitmandi.dao.entity.user.Address;
25458 tejbeer 51
import com.spice.profitmandi.dao.entity.user.Promoter;
52
import com.spice.profitmandi.dao.entity.user.Refferal;
25488 tejbeer 53
import com.spice.profitmandi.dao.enumuration.dtr.RefferalStatus;
21735 ashik.ali 54
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
25488 tejbeer 55
import com.spice.profitmandi.dao.model.DateRangeModel;
21643 ashik.ali 56
import com.spice.profitmandi.dao.model.UserCart;
25366 tejbeer 57
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
25458 tejbeer 58
import com.spice.profitmandi.dao.repository.dtr.RefferalRepository;
21735 ashik.ali 59
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
23786 amit.gupta 60
import com.spice.profitmandi.dao.repository.dtr.RoleRepository;
21735 ashik.ali 61
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
62
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
63
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
25580 tejbeer 64
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
22355 ashik.ali 65
import com.spice.profitmandi.dao.repository.user.AddressRepository;
25458 tejbeer 66
import com.spice.profitmandi.dao.repository.user.PromoterRepository;
24491 amit.gupta 67
import com.spice.profitmandi.service.AuthService;
23798 amit.gupta 68
import com.spice.profitmandi.service.authentication.RoleManager;
25366 tejbeer 69
import com.spice.profitmandi.service.user.RetailerService;
23787 amit.gupta 70
import com.spice.profitmandi.service.user.UserService;
21469 amit.gupta 71
import com.spice.profitmandi.web.enumuration.UserStatus;
21277 ashik.ali 72
import com.spice.profitmandi.web.processor.GoogleLoginProcessor;
25458 tejbeer 73
import com.spice.profitmandi.web.req.CreateRefferalRequest;
25488 tejbeer 74
import com.spice.profitmandi.web.req.RefferalEarning;
75
import com.spice.profitmandi.web.req.RefferalEarningModel;
21366 kshitij.so 76
import com.spice.profitmandi.web.req.UserRequest;
25366 tejbeer 77
import com.spice.profitmandi.web.res.Partner;
21248 ashik.ali 78
 
21469 amit.gupta 79
import io.swagger.annotations.ApiImplicitParam;
80
import io.swagger.annotations.ApiImplicitParams;
81
 
21248 ashik.ali 82
/**
83
 * @author ashikali
84
 *
85
 */
86
@Controller
25366 tejbeer 87
@Transactional(rollbackFor = Throwable.class)
21248 ashik.ali 88
public class UserController {
21469 amit.gupta 89
 
21448 ashik.ali 90
	@Autowired
22930 ashik.ali 91
	private ResponseSender<?> responseSender;
21469 amit.gupta 92
 
23568 govind 93
	private static final Logger LOGGER = LogManager.getLogger(UserController.class);
21469 amit.gupta 94
 
21855 amit.gupta 95
	@Value("${notifications.api.host}")
96
	private String nodeHost;
25366 tejbeer 97
 
21855 amit.gupta 98
	@Value("${notifications.api.port}")
99
	private int nodePort;
100
 
21414 kshitij.so 101
	@Value("${admin.token}")
102
	private String validAdminToken;
21469 amit.gupta 103
 
21278 ashik.ali 104
	@Autowired
22873 ashik.ali 105
	private UserRepository userRepository;
25366 tejbeer 106
 
22355 ashik.ali 107
	@Autowired
22873 ashik.ali 108
	private com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
21469 amit.gupta 109
 
21278 ashik.ali 110
	@Autowired
22873 ashik.ali 111
	private RetailerRepository retailerRepository;
21485 amit.gupta 112
 
113
	@Autowired
22873 ashik.ali 114
	private UserRoleRepository userRoleRepository;
21469 amit.gupta 115
 
21426 ashik.ali 116
	@Autowired
22873 ashik.ali 117
	private UserAccountRepository userAccountRepository;
25366 tejbeer 118
 
22355 ashik.ali 119
	@Autowired
24491 amit.gupta 120
	private AuthService authService;
25366 tejbeer 121
 
24491 amit.gupta 122
	@Autowired
22873 ashik.ali 123
	private AddressRepository addressRepository;
21469 amit.gupta 124
 
21426 ashik.ali 125
	@Autowired
22873 ashik.ali 126
	private GoogleLoginProcessor googleLoginProcessor;
21855 amit.gupta 127
 
21784 amit.gupta 128
	@Autowired
22873 ashik.ali 129
	private UserService userService;
25366 tejbeer 130
 
23786 amit.gupta 131
	@Autowired
23858 ashik.ali 132
	private RestClient restClient;
25366 tejbeer 133
 
23858 ashik.ali 134
	@Autowired
23798 amit.gupta 135
	private RoleManager roleManager;
25366 tejbeer 136
 
23786 amit.gupta 137
	@Autowired
138
	private RoleRepository roleRepository;
21469 amit.gupta 139
 
25366 tejbeer 140
	@Autowired
141
	private AuthRepository authRepository;
142
 
143
	@Autowired
144
	private RetailerService retailerService;
145
 
25458 tejbeer 146
	@Autowired
147
	private RefferalRepository refferalRepository;
148
 
149
	@Autowired
150
	private PromoterRepository promoterRepository;
151
 
25580 tejbeer 152
	@Autowired
153
	private PartnerTypeChangeService partnerTypeChangeService;
154
 
21469 amit.gupta 155
	@RequestMapping(value = ProfitMandiConstants.URL_USER_GOOGLE_LOGIN, method = RequestMethod.POST)
25366 tejbeer 156
	public ResponseEntity<?> googleLogin(HttpServletRequest request, @RequestBody GoogleLoginRequest googleLoginRequest)
157
			throws ProfitMandiBusinessException {
23698 amit.gupta 158
		LOGGER.info("requested url : " + request.getRequestURL().toString());
24494 amit.gupta 159
		Map<String, Object> resultMap = googleLoginProcessor.process(googleLoginRequest);
160
		LOGGER.info("Response sent is {}", resultMap);
23698 amit.gupta 161
		return responseSender.ok(googleLoginProcessor.process(googleLoginRequest));
21277 ashik.ali 162
	}
21469 amit.gupta 163
 
164
	@RequestMapping(value = ProfitMandiConstants.URL_USER_TOKEN_IS_EXPIRED, method = RequestMethod.GET)
25366 tejbeer 165
	public ResponseEntity<?> tokenIsExpired(HttpServletRequest request, @RequestParam(name = "token") String token)
166
			throws ProfitMandiBusinessException {
21469 amit.gupta 167
		LOGGER.info("requested url : " + request.getRequestURL().toString());
22930 ashik.ali 168
		return responseSender.ok(JWTUtil.isExpired(token));
21282 ashik.ali 169
	}
21469 amit.gupta 170
 
22355 ashik.ali 171
	@RequestMapping(value = ProfitMandiConstants.URL_USER_DETAIL_BY_TOKEN, method = RequestMethod.GET)
21469 amit.gupta 172
	@ApiImplicitParams({
173
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
174
	public ResponseEntity<?> tokenInfo(HttpServletRequest request) throws Throwable {
175
		Map<String, Object> responseMap = new HashMap<>();
176
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
21483 amit.gupta 177
		User user = null;
21855 amit.gupta 178
		if (userInfo.getUserId() > -1) {
21483 amit.gupta 179
			user = userRepository.selectById(userInfo.getUserId());
25366 tejbeer 180
			if (!(userInfo.getEmail() == null || user.getEmailId().equalsIgnoreCase(userInfo.getEmail()))) {
24491 amit.gupta 181
				String userName = null;
25366 tejbeer 182
				if (user.getSecondryEmailId() != null && user.getSecondryEmailId().equals(userInfo.getEmail())) {
24491 amit.gupta 183
					userName = user.getFirstName() + " " + user.getLastName();
184
				} else {
185
					userName = authService.getNameByEmailId(userInfo.getEmail());
186
				}
187
				responseMap.put(ProfitMandiConstants.USER_ID, userInfo.getUserId());
188
				responseMap.put(ProfitMandiConstants.USER_NAME, userName);
189
				responseMap.put(ProfitMandiConstants.USER_STATUS, "fofoAssociate");
190
				responseMap.put(ProfitMandiConstants.EMAIL_ID, userInfo.getEmail());
191
				return responseSender.ok(responseMap);
192
			}
21483 amit.gupta 193
		} else {
194
			try {
195
				user = userRepository.selectByEmailId(userInfo.getEmail());
196
			} catch (ProfitMandiBusinessException e1) {
197
			}
25366 tejbeer 198
			if (user == null) {
23204 ashik.ali 199
				try {
200
					user = userRepository.selectBySecondryEmailId(userInfo.getEmail());
201
				} catch (ProfitMandiBusinessException e1) {
202
					LOGGER.info("Uneregistered user", userInfo.getEmail());
203
				}
204
			}
21483 amit.gupta 205
		}
206
		if (user != null) {
23858 ashik.ali 207
			int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
21491 amit.gupta 208
			responseMap.put(ProfitMandiConstants.EMAIL_ID, user.getEmailId());
21526 amit.gupta 209
			responseMap.put(ProfitMandiConstants.USER_ID, user.getId());
22017 amit.gupta 210
			responseMap.put(ProfitMandiConstants.USER_NAME, user.getFirstName() + " " + user.getLastName());
23786 amit.gupta 211
			List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(user.getId());
25366 tejbeer 212
			// LOGGER.info("userRoles {} ", userRoles);
22032 ashik.ali 213
 
214
			// generate new token if roles have been updated
23786 amit.gupta 215
			if (userInfo.getRoleIds() == null || roleIds.size() != userInfo.getRoleIds().size()) {
23858 ashik.ali 216
				String[] roleIdStrings = new String[roleIds.size()];
22603 amit.gupta 217
				int index = 0;
23786 amit.gupta 218
				for (int roleId : roleIds) {
23858 ashik.ali 219
					roleIdStrings[index++] = String.valueOf(roleId);
22603 amit.gupta 220
				}
23858 ashik.ali 221
				String newToken = JWTUtil.create(user.getId(), retailerId, roleIdStrings);
21483 amit.gupta 222
				responseMap.put("newAuthToken", newToken);
223
			}
23858 ashik.ali 224
			// if user is retailer
23786 amit.gupta 225
			Set<Integer> roleIdsSet = new HashSet<Integer>(roleIds);
23858 ashik.ali 226
			if (roleManager.isRetailer(roleIdsSet)) {
22032 ashik.ali 227
				UserCart uc = userAccountRepository.getUserCart(userInfo.getUserId());
228
				Retailer retailer = retailerRepository.selectById(uc.getUserId());
22370 amit.gupta 229
				com.spice.profitmandi.dao.entity.user.User saholicUser = userUserRepository.selectById(uc.getUserId());
25366 tejbeer 230
				if (saholicUser.getAddressId() != null) {
22370 amit.gupta 231
					Address address = addressRepository.selectById(saholicUser.getAddressId());
232
					responseMap.put(ProfitMandiConstants.ADDRESS, address);
233
				}
21485 amit.gupta 234
				// if retailer is activated 1 then verified retailer
235
				// else if migrated is 1 then old retailer
25366 tejbeer 236
				// also lets incoporte old process i.e is user is activated then also retailer
237
				// is verified retailer
21485 amit.gupta 238
				// else retailer is not verifed
22497 amit.gupta 239
				if (retailer.isActive() || user.isActivated()) {
23798 amit.gupta 240
					if (roleManager.isPartner(roleIdsSet)) {
22017 amit.gupta 241
						responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.FOFO.getValue());
242
					} else {
243
						responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.VERIFIED_RETAILER.getValue());
244
					}
22032 ashik.ali 245
				} else if (retailer.isMigrated()) {
21485 amit.gupta 246
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.RETAILER.getValue());
21469 amit.gupta 247
				} else {
21485 amit.gupta 248
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_VERIFIED_RETAILER.getValue());
21469 amit.gupta 249
				}
23798 amit.gupta 250
			} else if (roleManager.isUser(roleIdsSet)) {
22502 amit.gupta 251
				responseMap.put("userInfo", getRegisteredUserInfo(user));
21469 amit.gupta 252
				responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.REGISTERED.getValue());
22032 ashik.ali 253
			}
21491 amit.gupta 254
		} else {
255
			responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_REGISTERED.getValue());
256
			responseMap.put(ProfitMandiConstants.EMAIL_ID, userInfo.getEmail());
21469 amit.gupta 257
		}
22032 ashik.ali 258
 
21469 amit.gupta 259
		return responseSender.ok(responseMap);
260
	}
25366 tejbeer 261
 
22502 amit.gupta 262
	private RegisteredUserInfo getRegisteredUserInfo(User user) throws Throwable {
22492 amit.gupta 263
		RegisteredUserInfo ri = new RegisteredUserInfo();
264
		ri.setCity(user.getCity());
265
		ri.setFirstName(user.getFirstName());
266
		ri.setLastName(user.getLastName());
22493 amit.gupta 267
		ri.setPhone(user.getMobileNumber());
22492 amit.gupta 268
		ri.setPinCode(user.getPinCode());
22845 amit.gupta 269
		ri.setState(user.getState());
22492 amit.gupta 270
		return ri;
271
	}
21469 amit.gupta 272
 
273
	@RequestMapping(value = ProfitMandiConstants.URL_USER, method = RequestMethod.POST)
21501 amit.gupta 274
	@ApiImplicitParams({
22032 ashik.ali 275
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
276
	public ResponseEntity<?> createUser(HttpServletRequest request, @RequestBody UserRequest userRequest)
22930 ashik.ali 277
			throws ProfitMandiBusinessException {
21469 amit.gupta 278
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21368 kshitij.so 279
		User user = new User();
280
		user.setFirstName(userRequest.getFirstName());
281
		user.setLastName(userRequest.getLastName());
282
		user.setCity(userRequest.getCity());
283
		user.setPinCode(Integer.valueOf(userRequest.getPinCode()));
22845 amit.gupta 284
		user.setState(userRequest.getState());
21708 amit.gupta 285
		user.setMobileNumber(userRequest.getMobieNumber());
21368 kshitij.so 286
		user.setEmailId(userRequest.getEmailId());
287
		user.setUsername("");
288
		user.setPassword("");
289
		user.setMobile_verified(false);
290
		user.setReferral_url("");
291
		user.setGroup_id(1);
22504 amit.gupta 292
		user.setStatus(1);
21368 kshitij.so 293
		user.setActivated(false);
21501 amit.gupta 294
		user.setCreateTimestamp(LocalDateTime.now());
295
		user.setUpdateTimestamp(LocalDateTime.now());
22930 ashik.ali 296
		userRepository.persist(user);
23858 ashik.ali 297
		Role role = roleRepository.selectByName(RoleType.USER.toString());
22930 ashik.ali 298
		UserRole userRole = new UserRole();
23858 ashik.ali 299
		userRole.setRoleId(role.getId());
22930 ashik.ali 300
		userRole.setUserId(user.getId());
301
		userRoleRepository.persist(userRole);
302
		return responseSender.ok(ResponseCodeHolder.getMessage("USR_OK_1000"));
25366 tejbeer 303
 
21278 ashik.ali 304
	}
21469 amit.gupta 305
 
306
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ID, method = RequestMethod.GET)
25366 tejbeer 307
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id)
308
			throws ProfitMandiBusinessException {
21469 amit.gupta 309
		LOGGER.info("requested url : " + request.getRequestURL().toString());
22930 ashik.ali 310
		return responseSender.ok(userRepository.selectById(id));
21248 ashik.ali 311
	}
21469 amit.gupta 312
 
313
	@RequestMapping(value = ProfitMandiConstants.URL_USER_MOBILE_NUMBER, method = RequestMethod.GET)
314
	public ResponseEntity<?> getByMobileNumber(HttpServletRequest request,
25366 tejbeer 315
			@RequestParam(name = "mobileNumber") String mobileNumber) throws ProfitMandiBusinessException {
21469 amit.gupta 316
		LOGGER.info("requested url : " + request.getRequestURL().toString());
22930 ashik.ali 317
		return responseSender.ok(userRepository.selectByMobileNumber(mobileNumber));
21248 ashik.ali 318
	}
21469 amit.gupta 319
 
21784 amit.gupta 320
	@ApiImplicitParams({
21501 amit.gupta 321
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
322
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ACTIVATE, method = RequestMethod.POST)
323
	public ResponseEntity<?> activateUser(HttpServletRequest request,
21784 amit.gupta 324
			@RequestParam(name = "activationCode") String activationCode) throws Throwable {
21855 amit.gupta 325
		int userId = (int) request.getAttribute("userId");
21784 amit.gupta 326
		UserCart uc = userAccountRepository.getUserCart(userId);
327
		return responseSender.ok(userService.updateActivation(userId, uc.getUserId(), activationCode));
328
	}
21855 amit.gupta 329
 
25458 tejbeer 330
	/*
331
	 * @ApiImplicitParams({
332
	 * 
333
	 * @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true,
334
	 * dataType = "string", paramType = "header") })
335
	 * 
336
	 * @RequestMapping(value = ProfitMandiConstants.URL_USER_NOTIFICATIONS, method =
337
	 * RequestMethod.GET) public ResponseEntity<?>
338
	 * getNofitications(HttpServletRequest request,
339
	 * 
340
	 * @RequestParam(name = "androidId") String androidId, @RequestParam(name =
341
	 * "pageNumber") int pageNumber,
342
	 * 
343
	 * @RequestParam(name = "pageSize") int pageSize) throws
344
	 * ProfitMandiBusinessException { int userId = (int)
345
	 * request.getAttribute("userId"); String restResponse = null; Map<String,
346
	 * String> params = new HashMap<>();
347
	 * 
348
	 * String uri = "/getAllNotifications"; params.put("user_id", userId + "");
349
	 * params.put("android_id", androidId); params.put("limit", pageSize + "");
350
	 * params.put("offset", "" + ((pageNumber - 1) * pageSize)); try { restResponse
351
	 * = restClient.get(SchemeType.HTTP, nodeHost, nodePort, uri, params); } catch
352
	 * (HttpHostConnectException e) { throw new ProfitMandiBusinessException("", "",
353
	 * "Could not Connect to host"); }
354
	 * 
355
	 * JsonArray result_json = Json.parse(restResponse).asArray();
356
	 * 
357
	 * List<Notification> notifications = new ArrayList<>();
358
	 * 
359
	 * for (JsonValue j : result_json) {
360
	 * notifications.add(toNotifiaction(j.asObject())); }
361
	 * 
362
	 * return responseSender.ok(notifications); }
363
	 * 
364
	 * private Notification toNotifiaction(JsonObject jsonObject) { Notification n =
365
	 * (Notification) (new Gson().fromJson(jsonObject.toString(),
366
	 * Notification.class)); if (n.getStatus().equals("opened") ||
367
	 * n.getStatus().equals("referrer") || n.getStatus().equals("seen")) {
368
	 * n.setSeen(true); } return n; }
369
	 */
21855 amit.gupta 370
 
21426 ashik.ali 371
	@RequestMapping(value = ProfitMandiConstants.URL_USER_IS_EXIST_MOBILE_NUMBER, method = RequestMethod.GET)
21469 amit.gupta 372
	public ResponseEntity<?> isMobileNumberExist(HttpServletRequest request,
373
			@RequestParam(name = "mobileNumber") String mobileNumber) {
374
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21448 ashik.ali 375
		return responseSender.ok(userRepository.isExistByMobileNumber(mobileNumber));
21426 ashik.ali 376
	}
21469 amit.gupta 377
 
378
	@RequestMapping(value = ProfitMandiConstants.URL_USER_EMAIL_ID, method = RequestMethod.GET)
25366 tejbeer 379
	public ResponseEntity<?> getByEmailId(HttpServletRequest request, @RequestParam(name = "emailId") String emailId)
380
			throws ProfitMandiBusinessException {
21469 amit.gupta 381
		LOGGER.info("requested url : " + request.getRequestURL().toString());
23204 ashik.ali 382
		User user = null;
25366 tejbeer 383
		try {
23204 ashik.ali 384
			user = userRepository.selectByEmailId(emailId);
25366 tejbeer 385
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23204 ashik.ali 386
			user = userRepository.selectBySecondryEmailId(emailId);
387
		}
388
		return responseSender.ok(user);
21248 ashik.ali 389
	}
21469 amit.gupta 390
 
391
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_ALL, method = RequestMethod.GET)
392
	public ResponseEntity<?> getAllRoles(HttpServletRequest request, @RequestParam(name = "id") int id) {
393
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21448 ashik.ali 394
		return responseSender.ok(userRoleRepository.selectRolesByUserId(id));
21248 ashik.ali 395
	}
25366 tejbeer 396
 
23204 ashik.ali 397
	@ApiImplicitParams({
25366 tejbeer 398
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
23204 ashik.ali 399
	@RequestMapping(value = ProfitMandiConstants.URL_USER_SECONDRY_EMAIL_ID_AND_FOFO_STORE_CODE, method = RequestMethod.GET)
25366 tejbeer 400
	public ResponseEntity<?> getSecondryEmailAndStoreCode(HttpServletRequest request)
401
			throws ProfitMandiBusinessException {
23204 ashik.ali 402
		LOGGER.info("requested url : " + request.getRequestURL().toString());
403
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
404
		return responseSender.ok(userService.getEmailsAndFofoStoreCodeByUserId(userId));
405
	}
25366 tejbeer 406
 
23204 ashik.ali 407
	@ApiImplicitParams({
25366 tejbeer 408
			@ApiImplicitParam(name = "Auth-Token", value = "-Token", required = true, dataType = "string", paramType = "header") })
23858 ashik.ali 409
	@RequestMapping(value = ProfitMandiConstants.URL_USER_SECONDRY_EMAIL_ID, method = RequestMethod.PUT)
23204 ashik.ali 410
	public ResponseEntity<?> updateSecondryEmailId(HttpServletRequest request,
25366 tejbeer 411
			@RequestParam(name = ProfitMandiConstants.SECONDRY_EMAIL_ID) String secondryEmailId) throws Throwable {
412
		int userId = (int) request.getAttribute("userId");
413
		User user = userRepository.selectById(userId);
414
		user.setSecondryEmailId(secondryEmailId);
415
		userRepository.persist(user);
416
		return responseSender.ok(ResponseCodeHolder.getMessage("USR_OK_1001"));
417
	}
21414 kshitij.so 418
 
419
	@RequestMapping(value = ProfitMandiConstants.URL_ADMIN_TOKEN, method = RequestMethod.POST)
21469 amit.gupta 420
	public ResponseEntity<?> getAdminToken(HttpServletRequest request,
421
			@RequestParam(name = "adminToken") String adminToken, @RequestParam(name = "emailId") String emailId) {
422
		LOGGER.info("requested url : " + request.getRequestURL().toString());
423
		if (!adminToken.equals(validAdminToken)) {
22930 ashik.ali 424
			return responseSender.forbidden(null);
21414 kshitij.so 425
		}
21469 amit.gupta 426
 
21414 kshitij.so 427
		Map<String, Object> responseMap = new HashMap<>(2);
23204 ashik.ali 428
		User user = null;
21469 amit.gupta 429
		try {
23204 ashik.ali 430
			user = userRepository.selectByEmailId(emailId);
431
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
25366 tejbeer 432
 
23204 ashik.ali 433
		}
25366 tejbeer 434
 
435
		if (user == null) {
436
			try {
23204 ashik.ali 437
				user = userRepository.selectBySecondryEmailId(emailId);
25366 tejbeer 438
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
439
 
23204 ashik.ali 440
			}
441
		}
25366 tejbeer 442
		if (user != null) {
23858 ashik.ali 443
			int retailerId = 0;
444
			try {
445
				retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
446
			} catch (ProfitMandiBusinessException e) {
447
				// TODO Auto-generated catch block
448
				e.printStackTrace();
449
			}
450
			List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(user.getId());
451
			String[] roleIdsString = new String[roleIds.size()];
21414 kshitij.so 452
			int index = 0;
23858 ashik.ali 453
			for (int roleId : roleIds) {
454
				roleIdsString[index++] = String.valueOf(roleId);
21414 kshitij.so 455
			}
23858 ashik.ali 456
			responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), retailerId, roleIdsString));
21414 kshitij.so 457
			responseMap.put(ProfitMandiConstants.REGISTERED, true);
25366 tejbeer 458
		} else {
21469 amit.gupta 459
			responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(emailId));
21414 kshitij.so 460
			responseMap.put(ProfitMandiConstants.REGISTERED, false);
461
		}
21448 ashik.ali 462
		return responseSender.ok(responseMap);
21469 amit.gupta 463
 
21414 kshitij.so 464
	}
25366 tejbeer 465
 
25300 tejbeer 466
	@RequestMapping(value = "/mobileappsettings", method = RequestMethod.POST)
25366 tejbeer 467
	public ResponseEntity<?> mobileAppSettings(HttpServletRequest request, @RequestParam(name = "t") int timestamp,
468
			@RequestParam(name = "imeinumber") String imeinumber)
469
			throws ProfitMandiBusinessException, ClientProtocolException, IOException {
470
 
471
		final String uri = "http://api.profittill.com/mobileappsettings?t=" + timestamp + "&imeinumber=" + imeinumber;
25300 tejbeer 472
		final String BASIC_AUTH = "Basic " + Base64.getEncoder().encodeToString("dtr:dtr18Feb2015".getBytes());
473
		Map<String, String> headers = new HashMap<>();
474
		Map<String, String> params = new HashMap<>();
475
		headers.put("Authorization", BASIC_AUTH);
476
		return responseSender.ok(restClient.post(uri, params, headers));
477
	}
22032 ashik.ali 478
 
25366 tejbeer 479
	@RequestMapping(value = "/getPartners", method = RequestMethod.GET)
480
	@ApiImplicitParams({
481
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
482
	public ResponseEntity<?> getPartners(HttpServletRequest request, @RequestParam(name = "gmailId") String gmailId,
483
			@RequestParam(value = "offset") int offset, @RequestParam(value = "limit") int limit)
484
			throws ProfitMandiBusinessException {
485
		AuthUser authUser = authRepository.selectByGmailId(gmailId);
486
 
487
		Map<String, List<String>> storeGuyMap = new HashMap<>();
488
		for (Map.Entry<String, List<String>> entry : storeManagerMap.entrySet()) {
489
			String storeEmail = entry.getKey();
490
			List<String> storeGuys = entry.getValue();
491
			for (String storeGuy : storeGuys) {
492
				if (!storeGuyMap.containsKey(storeGuy)) {
493
					storeGuyMap.put(storeGuy, new ArrayList<>());
494
				}
495
				storeGuyMap.get(storeGuy).add(storeEmail);
496
			}
497
 
498
		}
499
		storeGuyMap.remove("");
500
 
25483 tejbeer 501
		List<String> emails = storeGuyMap.get(authUser.getEmailId().toLowerCase());
25366 tejbeer 502
		LOGGER.info("emails" + emails);
25458 tejbeer 503
		List<User> users = userRepository.selectAllByEmails(emails, offset, limit);
25366 tejbeer 504
		List<Partner> partners = new ArrayList<>();
505
		for (User user : users) {
25458 tejbeer 506
 
25366 tejbeer 507
			UserAccount uc = userAccountRepository.selectSaholicByUserId(user.getId());
25458 tejbeer 508
			com.spice.profitmandi.dao.entity.user.User userInfo = userUserRepository.selectById(uc.getAccountKey());
25366 tejbeer 509
			CustomRetailer customRetailer = retailerService.getFofoRetailer(userInfo.getId());
510
 
511
			Partner partner = new Partner();
512
			partner.setBusinessName(customRetailer.getBusinessName());
513
			partner.setPartnerId(customRetailer.getPartnerId());
514
			partner.setCartId(customRetailer.getCartId());
515
			partner.setEmail(customRetailer.getEmail());
516
			partner.setGstNumber(customRetailer.getGstNumber());
517
			partner.setDisplayName(customRetailer.getDisplayName());
518
			partner.setCity(customRetailer.getAddress().getCity());
519
			partner.setUserId(user.getId());
520
			partners.add(partner);
521
		}
522
		LOGGER.info("partners" + partners);
523
		return responseSender.ok(partners);
524
	}
525
 
526
	public static final Map<String, String> nameEmail = new HashMap<>();
527
 
528
	public static final Map<String, List<String>> storeManagerMap = new HashMap<>();
529
 
530
	static {
531
		nameEmail.put("Amod", "amod.sen@smartdukaan.com");
532
		nameEmail.put("Parmod", "parmod.kumar@smartdukaan.com");
533
		nameEmail.put("Anis", "md.anis@smartdukaan.com");
534
		nameEmail.put("Manoj", "manoj.singh@smartdukaan.com");
535
		nameEmail.put("Adeel", "adeel.yazdani@smartdukaan.com");
536
		nameEmail.put("Mohinder", "mohinder.mutreja@smartdukaan.com");
537
		nameEmail.put("Dharmendar", "dharmender.verma@smartdukaan.com");
538
		nameEmail.put("Rajat", "rajat.gupta@smartdukaan.com");
539
		nameEmail.put("Ankit", "ankit.bhatia@smartdukaan.com");
540
		nameEmail.put("Rajit", "rajit.alag@smartdukaan.com");
541
		nameEmail.put("Gulshan", "gulshan.kumar@smartdukaan.com");
542
		nameEmail.put("PramodSharma", "parmod.sharma@smartdukaan.com");
25509 tejbeer 543
		nameEmail.put("Sohan", "sohan.lal@smartdukaan.com");
544
		nameEmail.put("Ajay", "ajay.kumar@smartdukaan.com");
25366 tejbeer 545
		// nameEmail.put("Gulshan","");
25509 tejbeer 546
 
25580 tejbeer 547
		storeManagerMap.put("themobileplanet03@gmail.com",
548
				Arrays.asList("amod.sen@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
549
		storeManagerMap.put("mobileplanet7599@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
550
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
551
		storeManagerMap.put("robintelecom.snp@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
552
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
553
		storeManagerMap.put("smartconnect4141@gmail.com",
554
				Arrays.asList("amod.sen@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
555
		storeManagerMap.put("bhimsain919@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
556
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
557
		storeManagerMap.put("babitaranirk@gmail.com",
558
				Arrays.asList("parmod.sharma@smartdukaan.com", "", "mohinder.mutreja@smartdukaan.com"));
559
		storeManagerMap.put("dharmendery079@gmail.com",
560
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
561
		storeManagerMap.put("kkv.enterprises1@gmail.com",
562
				Arrays.asList("parmod.sharma@smartdukaan.com", "", "mohinder.mutreja@smartdukaan.com"));
563
		storeManagerMap.put("gulshersaifi74226@gmail.com",
564
				Arrays.asList("amod.sen@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
565
		storeManagerMap.put("deepak.lalwani1985@gmail.com",
566
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
567
		storeManagerMap.put("gupta.smartdukaan@gmail.com",
568
				Arrays.asList("amod.sen@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
569
		storeManagerMap.put("sachinindri2006@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
570
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
571
		storeManagerMap.put("kambojanil83@gmail.com",
572
				Arrays.asList("parmod.sharma@smartdukaan.com", "", "mohinder.mutreja@smartdukaan.com"));
573
		storeManagerMap.put("uppal.neeraj82@gmail.com",
574
				Arrays.asList("", "ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
575
		storeManagerMap.put("thefonehousekarnal@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
576
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
577
		storeManagerMap.put("richa9910763006@gmail.com",
578
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
579
		storeManagerMap.put("smartdukangzb@gmail.com",
580
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
581
		storeManagerMap.put("lovelymobile183@gmail.com",
582
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
583
		storeManagerMap.put("smartdukaan.sonepat@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
584
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
585
		storeManagerMap.put("arunmittal299@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
586
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
587
		storeManagerMap.put("gtc01100@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
588
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
589
		storeManagerMap.put("sumittyagi1975@gmail.com",
590
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
591
		storeManagerMap.put("metrofurniture342@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
592
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
593
		storeManagerMap.put("suryaelectronicskaithal@gmail.com",
594
				Arrays.asList("Parmod", "", "mohinder.mutreja@smartdukaan.com"));
595
		storeManagerMap.put("mmtelecomsec16@gmail.com",
596
				Arrays.asList("amod.sen@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
597
		storeManagerMap.put("omsonsindia08@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
598
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
599
		storeManagerMap.put("smartenterprisespv@gmail.com",
600
				Arrays.asList("amod.sen@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
601
		storeManagerMap.put("cachaitnya@gmail.com",
602
				Arrays.asList("amod.sen@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
603
		storeManagerMap.put("ajaykumarkansal03@gmail.com",
604
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
605
		storeManagerMap.put("spurwar73@gmail.com", Arrays.asList("md.anis@smartdukaan.com",
606
				"rajat.gupta@smartdukaan.com", "adeel.yazdani@smartdukaan.com"));
607
		storeManagerMap.put("Shreemobile.ind@gmail.com", Arrays.asList("md.anis@smartdukaan.com",
608
				"rajat.gupta@smartdukaan.com", "adeel.yazdani@smartdukaan.com"));
609
		storeManagerMap.put("felixenterprises2017@rediffmail.com",
610
				Arrays.asList("", "dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
611
		storeManagerMap.put("nareandergupta@gmail.com",
612
				Arrays.asList("", "dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
613
		storeManagerMap.put("shreebalajielectronic2019@gmail.com",
614
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
615
		storeManagerMap.put("saranshary@gmail.com",
616
				Arrays.asList("", "dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
617
		storeManagerMap.put("gambhirsmartphone@gmail.com",
618
				Arrays.asList("", "gulshan.kumar@smartdukaan.com", "adeel.yazdani@smartdukaan.com"));
619
		storeManagerMap.put("Priyankaenterprises9910@gmail.com",
620
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
621
		storeManagerMap.put("pawan.dhimaan@gmail.com",
622
				Arrays.asList("", "dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
623
		storeManagerMap.put("lakshaydhulla62@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
624
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
625
		storeManagerMap.put("parastelecom.stp@gmail.com", Arrays.asList("md.anis@smartdukaan.com",
626
				"rajat.gupta@smartdukaan.com", "adeel.yazdani@smartdukaan.com"));
627
		storeManagerMap.put("talwarmukesh298@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
628
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
629
		storeManagerMap.put("sahilnarang0009@gmail.com",
630
				Arrays.asList("parmod.sharma@smartdukaan.com", "", "mohinder.mutreja@smartdukaan.com"));
631
		storeManagerMap.put("supertelecomjind@gmail.com",
632
				Arrays.asList("", "ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
633
		storeManagerMap.put("saketnagpal@gmail.com",
634
				Arrays.asList("", "rajit.alag@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
635
		storeManagerMap.put("DEEPAKGOYAL702.DG@GMAIL.COM",
636
				Arrays.asList("", "ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
637
		storeManagerMap.put("sambhav350@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
638
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
639
		storeManagerMap.put("mayankarora98133@gmail.com",
640
				Arrays.asList("", "ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
641
		storeManagerMap.put("vijaymobilityandmoew@gmail.com",
642
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
643
		storeManagerMap.put("amitv70003@gmail.com",
644
				Arrays.asList("", "ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
645
		storeManagerMap.put("newagelucknow.123@gmail.com", Arrays.asList("md.anis@smartdukaan.com",
646
				"rajat.gupta@smartdukaan.com", "adeel.yazdani@smartdukaan.com"));
647
		storeManagerMap.put("Newagetechnologygomtinagar@gmail.com", Arrays.asList("md.anis@smartdukaan.com",
648
				"rajat.gupta@smartdukaan.com", "adeel.yazdani@smartdukaan.com"));
649
		storeManagerMap.put("rohitbatra106@gmail.com", Arrays.asList("parmod.sharma@smartdukaan.com",
650
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
651
		storeManagerMap.put("sonisunil9050873061@gmail.com",
652
				Arrays.asList("", "ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
653
		storeManagerMap.put("moderncohsr@gmail.com",
654
				Arrays.asList("", "ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
655
		storeManagerMap.put("raghubir.ngh@gmail.com",
656
				Arrays.asList("", "ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
657
		storeManagerMap.put("aman007singla@gmail.com",
658
				Arrays.asList("", "rajit.alag@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
659
 
25509 tejbeer 660
		storeManagerMap.put("suryaelectronicskaithal@gmail.com", Arrays.asList("sohan.lal@smartdukaan.com",
661
				"ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
662
		storeManagerMap.put("amitv70003@gmail.com", Arrays.asList("sohan.lal@smartdukaan.com",
663
				"ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
664
		storeManagerMap.put("uppal.neeraj82@gmail.com",
665
				Arrays.asList("", "ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
666
		storeManagerMap.put("DEEPAKGOYAL702.DG@GMAIL.COM", Arrays.asList("sohan.lal@smartdukaan.com",
667
				"ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
668
		storeManagerMap.put("supertelecomjind@gmail.com", Arrays.asList("sohan.lal@smartdukaan.com",
669
				"ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
670
		storeManagerMap.put("saranshary@gmail.com",
671
				Arrays.asList("", "ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
672
		storeManagerMap.put("sonisunil9050873061@gmail.com", Arrays.asList("sohan.lal@smartdukaan.com",
673
				"ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
674
		storeManagerMap.put("mayankarora98133@gmail.com",
675
				Arrays.asList("", "ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
25458 tejbeer 676
		storeManagerMap.put("babitaranirk@gmail.com",
25509 tejbeer 677
				Arrays.asList("", "ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
678
		storeManagerMap.put("KK Enterprises",
679
				Arrays.asList("", "dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
25580 tejbeer 680
		storeManagerMap.put("kambojanil83@gmail.com", Arrays.asList("ajay.kumar@smartdukaan.com",
681
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
25509 tejbeer 682
		storeManagerMap.put("robintelecom.snp@gmail.com",
683
				Arrays.asList("", "dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
25580 tejbeer 684
		storeManagerMap.put("gtc01100@gmail.com", Arrays.asList("ajay.kumar@smartdukaan.com",
685
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
686
		storeManagerMap.put("sahilnarang0009@gmail.com", Arrays.asList("ajay.kumar@smartdukaan.com",
687
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
688
		storeManagerMap.put("metrofurniture342@gmail.com", Arrays.asList("ajay.kumar@smartdukaan.com",
689
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
690
		storeManagerMap.put("omsonsindia08@gmail.com", Arrays.asList("ajay.kumar@smartdukaan.com",
691
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
692
		storeManagerMap.put("sachinindri2006@gmail.com", Arrays.asList("ajay.kumar@smartdukaan.com",
693
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
25509 tejbeer 694
		storeManagerMap.put("smartdukaan.sonepat@gmail.com",
695
				Arrays.asList("", "dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
25580 tejbeer 696
		storeManagerMap.put("sambhav350@gmail.com", Arrays.asList("ajay.kumar@smartdukaan.com",
697
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
698
		storeManagerMap.put("talwarmukesh298@gmail.com", Arrays.asList("ajay.kumar@smartdukaan.com",
699
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
25509 tejbeer 700
		storeManagerMap.put("bhimsain919@gmail.com",
701
				Arrays.asList("", "dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
25580 tejbeer 702
		storeManagerMap.put("rohitbatra106@gmail.com", Arrays.asList("ajay.kumar@smartdukaan.com",
703
				"dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
25509 tejbeer 704
		storeManagerMap.put("thefonehousekarnal@gmail.com",
705
				Arrays.asList("", "dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
706
		storeManagerMap.put("mobileplanet7599@gmail.com",
707
				Arrays.asList("", "dharmender.verma@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
25580 tejbeer 708
		storeManagerMap.put("raghubir.ngh@gmail.com", Arrays.asList("ajay.kumar@smartdukaan.com",
709
				"rajit.alag@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
710
		storeManagerMap.put("saketnagpal@gmail.com", Arrays.asList("ajay.kumar@smartdukaan.com",
711
				"rajit.alag@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
25458 tejbeer 712
		storeManagerMap.put("dharmendery079@gmail.com",
713
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
25509 tejbeer 714
		storeManagerMap.put("sumittyagi1975@gmail.com",
715
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
25458 tejbeer 716
		storeManagerMap.put("deepak.lalwani1985@gmail.com",
717
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
25509 tejbeer 718
		storeManagerMap.put("Priyankaenterprises9910@gmail.com",
719
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
25458 tejbeer 720
		storeManagerMap.put("richa9910763006@gmail.com",
721
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
722
		storeManagerMap.put("lovelymobile183@gmail.com",
723
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
724
		storeManagerMap.put("ajaykumarkansal03@gmail.com",
725
				Arrays.asList("manoj.singh@smartdukaan.com", "", "adeel.yazdani@smartdukaan.com"));
25509 tejbeer 726
		storeManagerMap.put("gupta.smartdukaan@gmail.com", Arrays.asList("", "", "adeel.yazdani@smartdukaan.com"));
727
		storeManagerMap.put("smartenterprisespv@gmail.com", Arrays.asList("", "", "adeel.yazdani@smartdukaan.com"));
728
		storeManagerMap.put("ysinghal34@gmail.com", Arrays.asList("", "", "adeel.yazdani@smartdukaan.com"));
729
		storeManagerMap.put("dishatelecommars@gmail.com", Arrays.asList("", "", "adeel.yazdani@smartdukaan.com"));
730
		storeManagerMap.put("gulshersaifi74226@gmail.com", Arrays.asList("", "", "adeel.yazdani@smartdukaan.com"));
731
		storeManagerMap.put("mmtelecomsec16@gmail.com", Arrays.asList("", "", "adeel.yazdani@smartdukaan.com"));
732
		storeManagerMap.put("cachaitnya@gmail.com", Arrays.asList("", "", "adeel.yazdani@smartdukaan.com"));
733
		storeManagerMap.put("KHURANAMOBILE753@GMAIL.COM", Arrays.asList("sohan.lal@smartdukaan.com",
734
				"ankit.bhatia@smartdukaan.com", "mohinder.mutreja@smartdukaan.com"));
735
 
25366 tejbeer 736
	}
25458 tejbeer 737
 
738
	@RequestMapping(value = "/user/refferal", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
739
	@ApiImplicitParams({
740
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
741
	public ResponseEntity<?> RefferalUser(HttpServletRequest request,
742
			@RequestBody CreateRefferalRequest createRefferalRequest) throws Exception {
743
		Refferal refferal = new Refferal();
744
		refferal.setFirstName(createRefferalRequest.getFirstName());
745
		refferal.setLastName(createRefferalRequest.getLastName());
746
		refferal.setMobile(createRefferalRequest.getMobile());
747
		refferal.setState(createRefferalRequest.getState());
748
		refferal.setCity(createRefferalRequest.getCity());
749
		refferal.setCreatedTimestamp(LocalDateTime.now());
750
		refferal.setUpdatedTimestamp(LocalDateTime.now());
25496 tejbeer 751
		refferal.setStatus(RefferalStatus.pending);
25458 tejbeer 752
		if (createRefferalRequest.isFofoAssociate()) {
753
			AuthUser authUser = authRepository.selectByGmailId(createRefferalRequest.getReffereeEmail());
25483 tejbeer 754
			if (authUser == null) {
25458 tejbeer 755
				Promoter promoter = promoterRepository.selectByEmailId(createRefferalRequest.getReffereeEmail());
756
				refferal.setRefereeName(promoter.getName());
25483 tejbeer 757
				refferal.setRefereeEmail(promoter.getEmail());
25458 tejbeer 758
				refferal.setRefereeMobile(promoter.getMobile());
759
			} else {
760
				refferal.setRefereeName(authUser.getFirstName());
25483 tejbeer 761
				refferal.setRefereeEmail(authUser.getGmailId());
25458 tejbeer 762
				refferal.setRefereeMobile(authUser.getMobileNumber());
763
			}
764
		} else {
765
			User user = userRepository.selectByEmailId(createRefferalRequest.getReffereeEmail());
766
			refferal.setRefereeName(user.getFirstName());
25483 tejbeer 767
			refferal.setRefereeEmail(user.getEmailId());
25458 tejbeer 768
			refferal.setRefereeMobile(user.getMobileNumber());
769
		}
770
 
771
		refferalRepository.persist(refferal);
772
		return responseSender.ok(true);
773
	}
25488 tejbeer 774
 
775
	@RequestMapping(value = "/user/refferalAmount", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
776
	@ApiImplicitParams({
777
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
778
	public ResponseEntity<?> RefferalAmount(HttpServletRequest request, @RequestParam String refereeEmail)
779
			throws Exception {
780
		LocalDateTime ldt = LocalDate.now().atStartOfDay().withDayOfMonth(16);
781
		DateRangeModel drm = new DateRangeModel();
782
		List<RefferalEarningModel> refferAmountModel = new ArrayList<RefferalEarningModel>();
783
		RefferalEarningModel rfm = new RefferalEarningModel();
784
		long currentMonthEstimaterefferal = 0;
785
		long currentMonthConfirmedrefferal = 0;
786
		if (LocalDateTime.now().isBefore(ldt)) {
787
			List<Refferal> allPendingRefferalBeforeCurrentMonth = refferalRepository
788
					.selectByEmailIdAndStatusAndbeforeCurrentMonth(refereeEmail, RefferalStatus.pending);
789
			drm.setStartDate(LocalDateTime.now().minusMonths(1).withDayOfMonth(16));
790
			drm.setEndDate(LocalDateTime.now().withDayOfMonth(1));
791
 
792
			List<Refferal> previousMonthapprovedRefferalAfterSixteen = refferalRepository
793
					.selectByEmailIdAndStatusAndDateRange(refereeEmail, RefferalStatus.approved, drm);
794
			List<Refferal> previousMonthtransferredRefferalAfterSixteen = refferalRepository
795
					.selectByEmailIdAndStatusAndDateRange(refereeEmail, RefferalStatus.transferred, drm);
796
 
797
			drm.setStartDate(LocalDateTime.now().minusMonths(1).withDayOfMonth(1));
798
			drm.setEndDate(LocalDateTime.now().minusMonths(1).withDayOfMonth(16));
799
 
800
			List<Refferal> previousMonthapprovedRefferalbeforeSixteen = refferalRepository
801
					.selectByEmailIdAndStatusAndDateRanges(refereeEmail, RefferalStatus.approved, drm);
802
			List<Refferal> previousMonthtransferredRefferalbeforeSixteen = refferalRepository
803
					.selectByEmailIdAndStatusAndDateRanges(refereeEmail, RefferalStatus.transferred, drm);
804
			long pendingRefferalBeforeCurrentMonth = allPendingRefferalBeforeCurrentMonth.size();
805
			long previousapprovedRefferalAfterSixteen = previousMonthapprovedRefferalAfterSixteen.size();
806
			long previoustransferredRefferalAfterSixteen = previousMonthtransferredRefferalAfterSixteen.size();
807
			long previousapprovedRefferalbeforeSixteen = previousMonthapprovedRefferalbeforeSixteen.size();
808
			long previoustransferredRefferalbeforeSixteen = previousMonthtransferredRefferalbeforeSixteen.size();
809
			long previousMonthEstimateRefferal = pendingRefferalBeforeCurrentMonth
810
					+ previousapprovedRefferalAfterSixteen + previousapprovedRefferalbeforeSixteen;
811
			long previousMonthConfirmedRefferal = previousapprovedRefferalAfterSixteen
812
					+ previoustransferredRefferalAfterSixteen + previousapprovedRefferalbeforeSixteen
813
					+ previoustransferredRefferalbeforeSixteen;
25490 tejbeer 814
			rfm = refferAmountEarning(pendingRefferalBeforeCurrentMonth, previousMonthConfirmedRefferal,
25488 tejbeer 815
					YearMonth.now().minusMonths(1));
816
			refferAmountModel.add(rfm);
817
			LOGGER.info("previousMonthPendingApprovedRefferal" + previousMonthEstimateRefferal);
818
			LOGGER.info("previousMonthApprovedTransfferedRefferal" + previousMonthConfirmedRefferal);
819
 
25509 tejbeer 820
			List<Refferal> pendingofCurrentMonth = refferalRepository
821
					.selectByEmailIdAndStatusAndBetweenDates(refereeEmail, RefferalStatus.pending, YearMonth.now());
25488 tejbeer 822
			List<Refferal> approvedofCurrentMonth = refferalRepository.selectByEmailIdAndStatusAndDatesBetweenTimestamp(
823
					refereeEmail, RefferalStatus.approved, YearMonth.now());
824
			List<Refferal> transferredofCurrentMonth = refferalRepository
825
					.selectByEmailIdAndStatusAndDatesBetweenTimestamp(refereeEmail, RefferalStatus.transferred,
826
							YearMonth.now());
25490 tejbeer 827
			LOGGER.info("pendingofCurrentMonth" + pendingofCurrentMonth);
25509 tejbeer 828
			long pendingofCurrentMonthCount = pendingofCurrentMonth.size();
25488 tejbeer 829
			currentMonthEstimaterefferal = pendingofCurrentMonth.size() + approvedofCurrentMonth.size();
830
			currentMonthConfirmedrefferal = approvedofCurrentMonth.size() + transferredofCurrentMonth.size();
25509 tejbeer 831
 
25490 tejbeer 832
			rfm = refferAmountEarning(pendingofCurrentMonthCount, currentMonthConfirmedrefferal, YearMonth.now());
25488 tejbeer 833
 
834
			refferAmountModel.add(rfm);
835
 
836
		} else if (LocalDateTime.now().isAfter(ldt)) {
837
			drm.setStartDate(LocalDateTime.now().minusMonths(1).withDayOfMonth(1));
25496 tejbeer 838
			drm.setEndDate(LocalDateTime.now().minusMonths(1).withDayOfMonth(16));
25488 tejbeer 839
 
840
			List<Refferal> previousMonthapprovedRefferalbeforeSixteen = refferalRepository
841
					.selectByEmailIdAndStatusAndDateRanges(refereeEmail, RefferalStatus.approved, drm);
842
			List<Refferal> previousMonthtransferredRefferalbeforeSixteen = refferalRepository
843
					.selectByEmailIdAndStatusAndDateRanges(refereeEmail, RefferalStatus.transferred, drm);
844
 
845
			drm.setStartDate(LocalDateTime.now().minusMonths(1).withDayOfMonth(16));
846
			drm.setEndDate(LocalDateTime.now().withDayOfMonth(16));
847
 
848
			List<Refferal> previousAndcurrentMonthapprovedRefferal = refferalRepository
849
					.selectByEmailIdAndStatusAndDateRange(refereeEmail, RefferalStatus.approved, drm);
850
 
851
			LocalDateTime currentMonthdate = LocalDateTime.now().withDayOfMonth(1);
852
 
853
			List<Refferal> preCurrMonthapproved = previousAndcurrentMonthapprovedRefferal.stream()
854
					.filter(x -> x.getCreatedTimestamp().isBefore(currentMonthdate)).collect(Collectors.toList());
855
			List<Refferal> previousAndcurrentMonthtransferredRefferal = refferalRepository
856
					.selectByEmailIdAndStatusAndDateRange(refereeEmail, RefferalStatus.transferred, drm);
857
			List<Refferal> preCurrMonthtransferred = previousAndcurrentMonthtransferredRefferal.stream()
858
					.filter(x -> x.getCreatedTimestamp().isBefore(currentMonthdate)).collect(Collectors.toList());
859
 
860
			long approvedRefferal = previousMonthapprovedRefferalbeforeSixteen.size()
861
					+ previousMonthtransferredRefferalbeforeSixteen.size() + preCurrMonthapproved.size()
862
					+ preCurrMonthtransferred.size();
863
			rfm = new RefferalEarningModel();
864
			if (approvedRefferal <= 4) {
865
				rfm.setActualEarning(approvedRefferal * 5000);
866
			} else if (approvedRefferal >= 5 && approvedRefferal <= 9) {
867
				rfm.setActualEarning(approvedRefferal * 10000);
868
			} else if (approvedRefferal > 10) {
869
				rfm.setActualEarning(approvedRefferal * 200000);
870
			}
871
			rfm.setYearMonth(YearMonth.now().minusMonths(1));
25490 tejbeer 872
			rfm.setPendingRefferal(0);
25509 tejbeer 873
			rfm.setApprovedRefferal(approvedRefferal);
25488 tejbeer 874
			refferAmountModel.add(rfm);
875
			List<Refferal> allpendingRefferal = refferalRepository.selectByEmailIdAndStatus(refereeEmail,
876
					RefferalStatus.pending);
877
			LOGGER.info("allpendingRefferal" + allpendingRefferal);
878
 
879
			drm.setStartDate(LocalDateTime.now().withDayOfMonth(16));
880
			drm.setEndDate(LocalDateTime.now().plusMonths(1).withDayOfMonth(1));
881
 
882
			List<Refferal> currentMonthapprovedRefferalAfterSixteen = refferalRepository
883
					.selectByEmailIdAndStatusAndDateRange(refereeEmail, RefferalStatus.approved, drm);
884
			List<Refferal> currentMonthtransferredRefferalAfterSixteen = refferalRepository
885
					.selectByEmailIdAndStatusAndDateRange(refereeEmail, RefferalStatus.transferred, drm);
886
			LOGGER.info("currentMonthapprovedRefferalAfterSixteen" + currentMonthapprovedRefferalAfterSixteen);
887
			drm.setStartDate(LocalDateTime.now().withDayOfMonth(1));
888
			drm.setEndDate(LocalDateTime.now().withDayOfMonth(16));
889
 
890
			List<Refferal> currentMonthapprovedRefferalbeforeSixteen = refferalRepository
891
					.selectByEmailIdAndStatusAndDateRanges(refereeEmail, RefferalStatus.approved, drm);
892
			List<Refferal> currentMonthtransferredRefferalbeforeSixteen = refferalRepository
893
					.selectByEmailIdAndStatusAndDateRanges(refereeEmail, RefferalStatus.transferred, drm);
894
			LOGGER.info("currentMonthapprovedRefferalbeforeSixteen" + currentMonthapprovedRefferalbeforeSixteen);
25509 tejbeer 895
			long pendingrefferalcount = allpendingRefferal.size();
25488 tejbeer 896
			currentMonthConfirmedrefferal = currentMonthapprovedRefferalAfterSixteen.size()
897
					+ currentMonthtransferredRefferalAfterSixteen.size()
898
					+ currentMonthapprovedRefferalbeforeSixteen.size()
899
					+ currentMonthtransferredRefferalbeforeSixteen.size();
25509 tejbeer 900
 
901
			rfm = refferAmountEarning(pendingrefferalcount, currentMonthConfirmedrefferal, YearMonth.now());
25488 tejbeer 902
			refferAmountModel.add(rfm);
903
			LOGGER.info("currentMonthpendingApprovedrefferal" + currentMonthEstimaterefferal);
904
 
905
			LOGGER.info("currentMonthapprovedTransferredrefferal" + currentMonthConfirmedrefferal);
906
 
907
		}
908
		RefferalEarning re = new RefferalEarning();
909
		re.setTimestamp(LocalDateTime.now());
910
		re.setRefferalEarningModel(refferAmountModel);
911
		return responseSender.ok(re);
912
	}
913
 
25490 tejbeer 914
	private RefferalEarningModel refferAmountEarning(long pendingRefferalValue, long confirmedRefferalValue,
25488 tejbeer 915
			YearMonth yearMonth) {
916
		RefferalEarningModel rfm = new RefferalEarningModel();
917
		rfm.setYearMonth(yearMonth);
25490 tejbeer 918
		rfm.setPendingRefferal(pendingRefferalValue);
919
		rfm.setApprovedRefferal(confirmedRefferalValue);
25509 tejbeer 920
		long estimateRefferalValue = pendingRefferalValue + confirmedRefferalValue;
25488 tejbeer 921
		if (estimateRefferalValue <= 4) {
922
			rfm.setMaximumEarning(estimateRefferalValue * 5000);
923
		} else if (estimateRefferalValue >= 5 && estimateRefferalValue <= 9) {
924
			rfm.setMaximumEarning(estimateRefferalValue * 10000);
925
		} else if (estimateRefferalValue > 10) {
926
			rfm.setMaximumEarning(200000);
927
		}
928
 
929
		if (confirmedRefferalValue <= 4) {
930
			rfm.setMinimumEarning(confirmedRefferalValue * 5000);
931
		} else if (confirmedRefferalValue >= 5 && confirmedRefferalValue <= 9) {
932
			rfm.setMinimumEarning(confirmedRefferalValue * 10000);
933
		} else if (confirmedRefferalValue > 10) {
934
			rfm.setMinimumEarning(200000);
935
		}
936
		return rfm;
937
	}
938
 
25580 tejbeer 939
	@RequestMapping(value = "/user/partnerTarget", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
940
	@ApiImplicitParams({
941
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
942
	public ResponseEntity<?> PartnerTarget(HttpServletRequest request) throws Exception {
943
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
944
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
945
		PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(retailerId, LocalDate.now());
946
 
947
		return responseSender.ok(partnerType);
948
	}
949
 
21248 ashik.ali 950
}