Subversion Repositories SmartDukaan

Rev

Rev 27268 | Rev 28100 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21277 ashik.ali 1
package com.spice.profitmandi.web.processor;
2
 
3
import java.io.IOException;
4
import java.time.LocalDateTime;
24491 amit.gupta 5
import java.util.Arrays;
21277 ashik.ali 6
import java.util.HashMap;
21278 ashik.ali 7
import java.util.Iterator;
22011 ashik.ali 8
import java.util.List;
21277 ashik.ali 9
import java.util.Map;
27043 amit.gupta 10
import java.util.Set;
21277 ashik.ali 11
 
23532 amit.gupta 12
import org.apache.http.conn.HttpHostConnectException;
24491 amit.gupta 13
import org.apache.logging.log4j.LogManager;
14
import org.apache.logging.log4j.Logger;
21278 ashik.ali 15
import org.springframework.beans.factory.annotation.Autowired;
22355 ashik.ali 16
import org.springframework.http.HttpHeaders;
17
import org.springframework.http.MediaType;
21282 ashik.ali 18
import org.springframework.stereotype.Component;
21278 ashik.ali 19
 
21277 ashik.ali 20
import com.fasterxml.jackson.core.JsonProcessingException;
21
import com.fasterxml.jackson.databind.JsonNode;
22
import com.fasterxml.jackson.databind.ObjectMapper;
21556 ashik.ali 23
import com.spice.profitmandi.common.enumuration.SchemeType;
21277 ashik.ali 24
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
22931 ashik.ali 25
import com.spice.profitmandi.common.model.GoogleLoginRequest;
21277 ashik.ali 26
import com.spice.profitmandi.common.model.ProfitMandiConstants;
27
import com.spice.profitmandi.common.util.JWTUtil;
24491 amit.gupta 28
import com.spice.profitmandi.common.util.Utils;
21556 ashik.ali 29
import com.spice.profitmandi.common.web.client.RestClient;
25384 tejbeer 30
import com.spice.profitmandi.dao.entity.auth.AuthUser;
21735 ashik.ali 31
import com.spice.profitmandi.dao.entity.dtr.SocialUser;
32
import com.spice.profitmandi.dao.entity.dtr.User;
28094 amit.gupta 33
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
24491 amit.gupta 34
import com.spice.profitmandi.dao.entity.user.Promoter;
21735 ashik.ali 35
import com.spice.profitmandi.dao.enumuration.dtr.Gender;
36
import com.spice.profitmandi.dao.enumuration.dtr.SocialType;
24491 amit.gupta 37
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
27043 amit.gupta 38
import com.spice.profitmandi.dao.repository.cs.CsService;
28094 amit.gupta 39
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
21735 ashik.ali 40
import com.spice.profitmandi.dao.repository.dtr.SocialUserRepository;
23860 ashik.ali 41
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
21735 ashik.ali 42
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
22011 ashik.ali 43
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
24491 amit.gupta 44
import com.spice.profitmandi.dao.repository.user.PromoterRepository;
45
import com.spice.profitmandi.service.AuthService;
26590 amit.gupta 46
import com.spice.profitmandi.service.user.RetailerService;
21277 ashik.ali 47
 
21282 ashik.ali 48
@Component
21277 ashik.ali 49
public class GoogleLoginProcessor {
24491 amit.gupta 50
 
21556 ashik.ali 51
	private static final String V1_HOST_NAME = "content.googleapis.com";
52
	private static final String V1_URI = "/plus/v1/people/me";
53
	private static final String V3_HOST_NAME = "www.googleapis.com";
54
	private static final String V3_URI = "/oauth2/v3/tokeninfo";
24491 amit.gupta 55
	private static final Logger LOGGER = LogManager.getLogger(GoogleLoginProcessor.class);
21277 ashik.ali 56
	private static final int PORT_NUMBER = 443;
57
	private final ObjectMapper objectMapper = new ObjectMapper();
24491 amit.gupta 58
 
21278 ashik.ali 59
	@Autowired
22931 ashik.ali 60
	private SocialUserRepository socialUserRepository;
24491 amit.gupta 61
 
21278 ashik.ali 62
	@Autowired
22931 ashik.ali 63
	private UserRepository userRepository;
24491 amit.gupta 64
 
65
	@Autowired
66
	private AuthService authService;
25388 tejbeer 67
 
22011 ashik.ali 68
	@Autowired
25388 tejbeer 69
	com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
70
 
71
	@Autowired
24491 amit.gupta 72
	private AuthRepository authRepository;
25388 tejbeer 73
 
26590 amit.gupta 74
 
23860 ashik.ali 75
	@Autowired
24491 amit.gupta 76
	private UserRoleRepository userRoleRepository;
77
 
78
	@Autowired
79
	private PromoterRepository promoterRepository;
80
 
81
	@Autowired
23860 ashik.ali 82
	private UserAccountRepository userAccountRepository;
27043 amit.gupta 83
 
84
	@Autowired
28094 amit.gupta 85
	private FofoStoreRepository fofoStoreRepository;
86
 
87
	@Autowired
27043 amit.gupta 88
	private CsService csService;
24491 amit.gupta 89
 
23860 ashik.ali 90
	@Autowired
91
	private RestClient restClient;
26590 amit.gupta 92
	@Autowired
93
	private RetailerService retailerService;
25388 tejbeer 94
 
24491 amit.gupta 95
	public Map<String, Object> process(GoogleLoginRequest googleLoginRequest) throws ProfitMandiBusinessException {
24495 amit.gupta 96
		LOGGER.info("1");
21277 ashik.ali 97
		Map<String, String> params = new HashMap<>();
22931 ashik.ali 98
		params.put(ProfitMandiConstants.ACCESS_TOKEN, googleLoginRequest.getToken());
22355 ashik.ali 99
		Map<String, String> headers = new HashMap<>(1);
100
		headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
23860 ashik.ali 101
		String responseString = null;
21277 ashik.ali 102
		try {
23532 amit.gupta 103
			responseString = restClient.get(SchemeType.HTTPS, V1_HOST_NAME, PORT_NUMBER, V1_URI, params, headers);
104
		} catch (HttpHostConnectException e) {
105
			throw new ProfitMandiBusinessException("", "", "Could not connect to remote host");
106
		}
107
		try {
21278 ashik.ali 108
			JsonNode rootNode = objectMapper.readTree(responseString);
21277 ashik.ali 109
			SocialUser socialUser = new SocialUser();
24491 amit.gupta 110
			if (rootNode.has("emails")) {
21278 ashik.ali 111
				JsonNode emails = rootNode.get("emails");
24491 amit.gupta 112
				if (emails.isArray()) {
21286 ashik.ali 113
					Iterator<JsonNode> emailsIterator = emails.elements();
24491 amit.gupta 114
					if (emailsIterator.hasNext()) {
21278 ashik.ali 115
						JsonNode email = emailsIterator.next();
24491 amit.gupta 116
						if (email.has("value")) {
25954 amit.gupta 117
							/*if (ProfitMandiConstants.BLOCKED_EMAILS.contains(email.get("value").asText())) {
25388 tejbeer 118
								throw new ProfitMandiBusinessException("User Account", email.get("value").asText(),
119
										"User is temporarily suspended.");
25954 amit.gupta 120
							}*/
21278 ashik.ali 121
							socialUser.setEmailId(email.get("value").asText());
122
						}
123
					}
124
				}
24491 amit.gupta 125
				// socialUser.setEmailId(rootNode.get("email").asText());
21277 ashik.ali 126
			}
24495 amit.gupta 127
			LOGGER.info("2");
24491 amit.gupta 128
			if (!socialUserRepository.isExistByEmailId(socialUser.getEmailId())) {
129
				if (rootNode.has("displayName")) {
22024 ashik.ali 130
					socialUser.setName(rootNode.get("displayName").asText());
131
				}
24491 amit.gupta 132
				if (rootNode.has("gender")) {
22024 ashik.ali 133
					String genderName = rootNode.get("gender").asText();
24491 amit.gupta 134
					switch (genderName) {
135
					case "male": {
136
						socialUser.setGender(Gender.MALE);
137
						break;
21278 ashik.ali 138
					}
24491 amit.gupta 139
					case "female": {
140
						socialUser.setGender(Gender.FEMALE);
141
						break;
142
					}
143
					}
21278 ashik.ali 144
				}
22024 ashik.ali 145
				socialUser.setCreateTimestamp(LocalDateTime.now());
146
				socialUser.setType(SocialType.GOOGLE);
147
				socialUser.setUpdateTimestamp(LocalDateTime.now());
148
				socialUserRepository.persist(socialUser);
21278 ashik.ali 149
			}
24491 amit.gupta 150
			Map<String, Object> responseMap = new HashMap<>(2);
151
 
24495 amit.gupta 152
			LOGGER.info("3");
24491 amit.gupta 153
			String name = authService.getNameByEmailId(socialUser.getEmailId());
24493 amit.gupta 154
			LOGGER.info("User Name from getNameByEmailId({}) is {}", socialUser.getEmailId(), name);
25388 tejbeer 155
			if (name != null) {
24491 amit.gupta 156
				User registeredUser = null;
25384 tejbeer 157
				AuthUser authUser = authRepository.selectByGmailId(socialUser.getEmailId());
25388 tejbeer 158
 
159
				if (authRepository.selectByGmailId(socialUser.getEmailId()) != null) {
25384 tejbeer 160
					registeredUser = userRepository.selectByEmailId(authUser.getEmailId());
25388 tejbeer 161
				} else if (promoterRepository.isExistByEmailId(socialUser.getEmailId())) {
24491 amit.gupta 162
					Promoter promoter = promoterRepository.selectByEmailId(socialUser.getEmailId());
24493 amit.gupta 163
					int userId = userAccountRepository.selectUserIdByRetailerId(promoter.getRetailerId());
164
					registeredUser = userRepository.selectById(userId);
25388 tejbeer 165
				} else if (userRepository.isExistBySecondryEmailId(socialUser.getEmailId())) {
24491 amit.gupta 166
					registeredUser = userRepository.selectBySecondryEmailId(socialUser.getEmailId());
167
				}
24495 amit.gupta 168
				LOGGER.info("4");
24491 amit.gupta 169
				List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(registeredUser.getId());
170
				String[] roleTypes = new String[roleIds.size()];
171
				int index = 0;
172
				for (int roleId : roleIds) {
173
					roleTypes[index++] = String.valueOf(roleId);
174
				}
25388 tejbeer 175
				int retailerId;
176
				try {
177
					retailerId = userAccountRepository.selectRetailerIdByUserId(registeredUser.getId());
27257 amit.gupta 178
					LOGGER.info("173 - Auth User Email {}, Retailer Id - {}", authUser.getName(), retailerId);
25388 tejbeer 179
				} catch (Exception e) {
27043 amit.gupta 180
					Set<Integer> authUserPartnerSet = csService.getAuthUserPartnerIdMapping().get(authUser.getEmailId());
181
					if(authUserPartnerSet.size() > 0) {
182
						retailerId = authUserPartnerSet.stream().findFirst().get();
28094 amit.gupta 183
						FofoStore fs = fofoStoreRepository.selectByRetailerId(retailerId);
184
						retailerId = ProfitMandiConstants.WAREHOUSE_NSSPL_PARTNER_MAP.get(fs.getWarehouseId());
27256 amit.gupta 185
						LOGGER.info("178 - Auth User Email {}, Retailer Id - {}", socialUser.getEmailId(), retailerId);
27043 amit.gupta 186
					} else {
187
						com.spice.profitmandi.dao.entity.user.User user = userUserRepository.selectByEmailId(Utils.SYSTEM_PARTNER);
188
						retailerId = user.getId();
27256 amit.gupta 189
						LOGGER.info("Auth User Email {}, Retailer Id - {}", socialUser.getEmailId(), retailerId);
27043 amit.gupta 190
					}
25388 tejbeer 191
				}
192
				responseMap.put(ProfitMandiConstants.TOKEN,
193
						JWTUtil.create(socialUser.getEmailId(), registeredUser.getId(), retailerId, roleTypes));
194
				LOGGER.info(
195
						"Param value for socialUser.getEmailId(), registeredUser.getId(), retailerId, roleTypes are {}, {}, {} and {}",
196
						socialUser.getEmailId(), registeredUser.getId(), retailerId, Arrays.asList(roleTypes));
24491 amit.gupta 197
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
198
				return responseMap;
199
			}
25388 tejbeer 200
 
23204 ashik.ali 201
			User user = null;
24491 amit.gupta 202
			try {
23204 ashik.ali 203
				user = userRepository.selectByEmailId(socialUser.getEmailId());
24491 amit.gupta 204
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
205
 
23204 ashik.ali 206
			}
24491 amit.gupta 207
			if (user == null) {
208
				try {
23204 ashik.ali 209
					user = userRepository.selectByEmailId(socialUser.getEmailId());
24491 amit.gupta 210
				} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23204 ashik.ali 211
					responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(socialUser.getEmailId()));
212
					responseMap.put(ProfitMandiConstants.REGISTERED, false);
213
				}
24527 amit.gupta 214
			} else {
23860 ashik.ali 215
				List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(user.getId());
216
				int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
217
				String[] roleTypes = new String[roleIds.size()];
21282 ashik.ali 218
				int index = 0;
23860 ashik.ali 219
				for (int roleId : roleIds) {
220
					roleTypes[index++] = String.valueOf(roleId);
21282 ashik.ali 221
				}
23860 ashik.ali 222
				responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), retailerId, roleTypes));
21277 ashik.ali 223
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
224
			}
24491 amit.gupta 225
 
21277 ashik.ali 226
			return responseMap;
24491 amit.gupta 227
		} catch (
228
 
229
		JsonProcessingException jsonProcessingException) {
230
			// LOGGER.error("Json parse exception of "+json,jsonProcessingException);
21277 ashik.ali 231
			throw new ProfitMandiBusinessException("", "", "VE_1001");
24491 amit.gupta 232
		} catch (IOException ioException) {
233
			// LOGGER.error("IO Exception occurred while parsing json String");
21277 ashik.ali 234
			throw new ProfitMandiBusinessException("", "", "VE_1001");
235
		}
236
	}
24491 amit.gupta 237
 
26396 amit.gupta 238
	public Map<String, Object> process(String token) throws ProfitMandiBusinessException {
21556 ashik.ali 239
		Map<String, String> params = new HashMap<>();
240
		params.put(ProfitMandiConstants.ID_TOKEN, token);
26673 amit.gupta 241
		Map<String, String> headers = new HashMap<>();
22355 ashik.ali 242
		headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
23860 ashik.ali 243
		String responseString = null;
21556 ashik.ali 244
		try {
23532 amit.gupta 245
			responseString = restClient.get(SchemeType.HTTPS, V3_HOST_NAME, PORT_NUMBER, V3_URI, params, headers);
23860 ashik.ali 246
		} catch (HttpHostConnectException e) {
247
			// TODO Auto-generated catch block
248
			e.printStackTrace();
23532 amit.gupta 249
		}
250
		try {
21556 ashik.ali 251
			JsonNode rootNode = objectMapper.readTree(responseString);
26396 amit.gupta 252
			String email = rootNode.get("email").asText();
253
 
254
			String name = authService.getNameByEmailId(email);
255
 
256
			Map<String, Object> responseMap = new HashMap<>(2);
257
			LOGGER.info("User Name from getNameByEmailId({}) is {}", email, name);
258
			if (name != null) {
259
				User registeredUser = null;
260
				AuthUser authUser = authRepository.selectByGmailId(email);
261
 
262
				if (authRepository.selectByGmailId(email) != null) {
263
					registeredUser = userRepository.selectByEmailId(authUser.getEmailId());
264
				} else if (promoterRepository.isExistByEmailId(email)) {
265
					Promoter promoter = promoterRepository.selectByEmailId(email);
266
					int userId = userAccountRepository.selectUserIdByRetailerId(promoter.getRetailerId());
267
					registeredUser = userRepository.selectById(userId);
268
				} else if (userRepository.isExistBySecondryEmailId(email)) {
269
					registeredUser = userRepository.selectBySecondryEmailId(email);
270
				}
271
				LOGGER.info("4");
272
				List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(registeredUser.getId());
273
				String[] roleTypes = new String[roleIds.size()];
274
				int index = 0;
275
				for (int roleId : roleIds) {
276
					roleTypes[index++] = String.valueOf(roleId);
277
				}
278
				int retailerId;
279
				try {
27258 amit.gupta 280
					retailerId = userAccountRepository.selectRetailerIdByUserId(registeredUser.getId());
281
				} catch (Exception e) {
27257 amit.gupta 282
					Set<Integer> authUserPartnerSet = csService.getAuthUserPartnerIdMapping().get(authUser.getEmailId());
27268 amit.gupta 283
					if(authUserPartnerSet != null && authUserPartnerSet.size() > 0) {
27257 amit.gupta 284
						retailerId = authUserPartnerSet.stream().findFirst().get();
27258 amit.gupta 285
					} else {
286
						com.spice.profitmandi.dao.entity.user.User user = userUserRepository.selectByEmailId(Utils.SYSTEM_PARTNER);
287
						retailerId = user.getId();
288
					}
26396 amit.gupta 289
				}
290
				responseMap.put(ProfitMandiConstants.TOKEN,
291
						JWTUtil.create(email, registeredUser.getId(), retailerId, roleTypes));
292
				LOGGER.info(
293
						"Param value for email, registeredUser.getId(), retailerId, roleTypes are {}, {}, {} and {}",
294
						email, registeredUser.getId(), retailerId, Arrays.asList(roleTypes));
295
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
296
				return responseMap;
297
			}
298
 
23204 ashik.ali 299
			User user = null;
24491 amit.gupta 300
			try {
26396 amit.gupta 301
				user = userRepository.selectByEmailId(email);
24491 amit.gupta 302
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
303
 
304
			}
305
			if (user == null) {
26396 amit.gupta 306
				try {
307
					user = userRepository.selectByEmailId(email);
308
				} catch (ProfitMandiBusinessException profitMandiBusinessException) {
309
					responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(email));
310
					responseMap.put(ProfitMandiConstants.REGISTERED, false);
311
				}
312
			} else {
313
				List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(user.getId());
314
				int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
315
				String[] roleTypes = new String[roleIds.size()];
316
				int index = 0;
317
				for (int roleId : roleIds) {
318
					roleTypes[index++] = String.valueOf(roleId);
319
				}
320
				responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), retailerId, roleTypes));
321
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
23204 ashik.ali 322
			}
24491 amit.gupta 323
 
26396 amit.gupta 324
			return responseMap;
325
 
24491 amit.gupta 326
		} catch (JsonProcessingException jsonProcessingException) {
21556 ashik.ali 327
			throw new ProfitMandiBusinessException("", "", "VE_1001");
24491 amit.gupta 328
		} catch (IOException ioException) {
21556 ashik.ali 329
			throw new ProfitMandiBusinessException("", "", "VE_1001");
330
		}
331
	}
26590 amit.gupta 332
 
333
	public Map<String, Object>  processStore(String storeCode) throws ProfitMandiBusinessException {
334
		Map<String, Object> responseMap = new HashMap<>();
26599 amit.gupta 335
		storeCode = storeCode.toUpperCase();
26598 amit.gupta 336
		int retailerId = retailerService.getStoreCodeRetailerMap().get(storeCode);
26590 amit.gupta 337
		int userId = userAccountRepository.selectUserIdByRetailerId(retailerId);
338
		List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(userId);
339
 
340
		String[] roleTypes = new String[roleIds.size()];
341
		int index = 0;
342
		for (int roleId : roleIds) {
343
			roleTypes[index++] = String.valueOf(roleId);
344
		}
345
		responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(userId, retailerId, roleTypes));
346
		responseMap.put(ProfitMandiConstants.REGISTERED, true);
347
		return responseMap;
348
	}
21277 ashik.ali 349
}