Subversion Repositories SmartDukaan

Rev

Rev 28094 | Rev 28349 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 28094 Rev 28100
Line 90... Line 90...
90
	@Autowired
90
	@Autowired
91
	private RestClient restClient;
91
	private RestClient restClient;
92
	@Autowired
92
	@Autowired
93
	private RetailerService retailerService;
93
	private RetailerService retailerService;
94
 
94
 
95
	public Map<String, Object> process(GoogleLoginRequest googleLoginRequest) throws ProfitMandiBusinessException {
-
 
96
		LOGGER.info("1");
-
 
97
		Map<String, String> params = new HashMap<>();
-
 
98
		params.put(ProfitMandiConstants.ACCESS_TOKEN, googleLoginRequest.getToken());
-
 
99
		Map<String, String> headers = new HashMap<>(1);
-
 
100
		headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
-
 
101
		String responseString = null;
-
 
102
		try {
-
 
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 {
-
 
108
			JsonNode rootNode = objectMapper.readTree(responseString);
-
 
109
			SocialUser socialUser = new SocialUser();
-
 
110
			if (rootNode.has("emails")) {
-
 
111
				JsonNode emails = rootNode.get("emails");
-
 
112
				if (emails.isArray()) {
-
 
113
					Iterator<JsonNode> emailsIterator = emails.elements();
-
 
114
					if (emailsIterator.hasNext()) {
-
 
115
						JsonNode email = emailsIterator.next();
-
 
116
						if (email.has("value")) {
-
 
117
							/*if (ProfitMandiConstants.BLOCKED_EMAILS.contains(email.get("value").asText())) {
-
 
118
								throw new ProfitMandiBusinessException("User Account", email.get("value").asText(),
-
 
119
										"User is temporarily suspended.");
-
 
120
							}*/
-
 
121
							socialUser.setEmailId(email.get("value").asText());
-
 
122
						}
-
 
123
					}
-
 
124
				}
-
 
125
				// socialUser.setEmailId(rootNode.get("email").asText());
-
 
126
			}
-
 
127
			LOGGER.info("2");
-
 
128
			if (!socialUserRepository.isExistByEmailId(socialUser.getEmailId())) {
-
 
129
				if (rootNode.has("displayName")) {
-
 
130
					socialUser.setName(rootNode.get("displayName").asText());
-
 
131
				}
-
 
132
				if (rootNode.has("gender")) {
-
 
133
					String genderName = rootNode.get("gender").asText();
-
 
134
					switch (genderName) {
-
 
135
					case "male": {
-
 
136
						socialUser.setGender(Gender.MALE);
-
 
137
						break;
-
 
138
					}
-
 
139
					case "female": {
-
 
140
						socialUser.setGender(Gender.FEMALE);
-
 
141
						break;
-
 
142
					}
-
 
143
					}
-
 
144
				}
-
 
145
				socialUser.setCreateTimestamp(LocalDateTime.now());
-
 
146
				socialUser.setType(SocialType.GOOGLE);
-
 
147
				socialUser.setUpdateTimestamp(LocalDateTime.now());
-
 
148
				socialUserRepository.persist(socialUser);
-
 
149
			}
-
 
150
			Map<String, Object> responseMap = new HashMap<>(2);
-
 
151
 
-
 
152
			LOGGER.info("3");
-
 
153
			String name = authService.getNameByEmailId(socialUser.getEmailId());
-
 
154
			LOGGER.info("User Name from getNameByEmailId({}) is {}", socialUser.getEmailId(), name);
-
 
155
			if (name != null) {
-
 
156
				User registeredUser = null;
-
 
157
				AuthUser authUser = authRepository.selectByGmailId(socialUser.getEmailId());
-
 
158
 
-
 
159
				if (authRepository.selectByGmailId(socialUser.getEmailId()) != null) {
-
 
160
					registeredUser = userRepository.selectByEmailId(authUser.getEmailId());
-
 
161
				} else if (promoterRepository.isExistByEmailId(socialUser.getEmailId())) {
-
 
162
					Promoter promoter = promoterRepository.selectByEmailId(socialUser.getEmailId());
-
 
163
					int userId = userAccountRepository.selectUserIdByRetailerId(promoter.getRetailerId());
-
 
164
					registeredUser = userRepository.selectById(userId);
-
 
165
				} else if (userRepository.isExistBySecondryEmailId(socialUser.getEmailId())) {
-
 
166
					registeredUser = userRepository.selectBySecondryEmailId(socialUser.getEmailId());
-
 
167
				}
-
 
168
				LOGGER.info("4");
-
 
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
				}
-
 
175
				int retailerId;
-
 
176
				try {
-
 
177
					retailerId = userAccountRepository.selectRetailerIdByUserId(registeredUser.getId());
-
 
178
					LOGGER.info("173 - Auth User Email {}, Retailer Id - {}", authUser.getName(), retailerId);
-
 
179
				} catch (Exception e) {
-
 
180
					Set<Integer> authUserPartnerSet = csService.getAuthUserPartnerIdMapping().get(authUser.getEmailId());
-
 
181
					if(authUserPartnerSet.size() > 0) {
-
 
182
						retailerId = authUserPartnerSet.stream().findFirst().get();
-
 
183
						FofoStore fs = fofoStoreRepository.selectByRetailerId(retailerId);
-
 
184
						retailerId = ProfitMandiConstants.WAREHOUSE_NSSPL_PARTNER_MAP.get(fs.getWarehouseId());
-
 
185
						LOGGER.info("178 - Auth User Email {}, Retailer Id - {}", socialUser.getEmailId(), retailerId);
-
 
186
					} else {
-
 
187
						com.spice.profitmandi.dao.entity.user.User user = userUserRepository.selectByEmailId(Utils.SYSTEM_PARTNER);
-
 
188
						retailerId = user.getId();
-
 
189
						LOGGER.info("Auth User Email {}, Retailer Id - {}", socialUser.getEmailId(), retailerId);
-
 
190
					}
-
 
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));
-
 
197
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
-
 
198
				return responseMap;
-
 
199
			}
-
 
200
 
-
 
201
			User user = null;
-
 
202
			try {
-
 
203
				user = userRepository.selectByEmailId(socialUser.getEmailId());
-
 
204
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
-
 
205
 
-
 
206
			}
-
 
207
			if (user == null) {
-
 
208
				try {
-
 
209
					user = userRepository.selectByEmailId(socialUser.getEmailId());
-
 
210
				} catch (ProfitMandiBusinessException profitMandiBusinessException) {
-
 
211
					responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(socialUser.getEmailId()));
-
 
212
					responseMap.put(ProfitMandiConstants.REGISTERED, false);
-
 
213
				}
-
 
214
			} else {
-
 
215
				List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(user.getId());
-
 
216
				int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
-
 
217
				String[] roleTypes = new String[roleIds.size()];
-
 
218
				int index = 0;
-
 
219
				for (int roleId : roleIds) {
-
 
220
					roleTypes[index++] = String.valueOf(roleId);
-
 
221
				}
-
 
222
				responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), retailerId, roleTypes));
-
 
223
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
-
 
224
			}
-
 
225
 
-
 
226
			return responseMap;
-
 
227
		} catch (
-
 
228
 
-
 
229
		JsonProcessingException jsonProcessingException) {
-
 
230
			// LOGGER.error("Json parse exception of "+json,jsonProcessingException);
-
 
231
			throw new ProfitMandiBusinessException("", "", "VE_1001");
-
 
232
		} catch (IOException ioException) {
-
 
233
			// LOGGER.error("IO Exception occurred while parsing json String");
-
 
234
			throw new ProfitMandiBusinessException("", "", "VE_1001");
-
 
235
		}
-
 
236
	}
-
 
237
 
-
 
238
	public Map<String, Object> process(String token) throws ProfitMandiBusinessException {
95
	public Map<String, Object> process(String token) throws ProfitMandiBusinessException {
239
		Map<String, String> params = new HashMap<>();
96
		Map<String, String> params = new HashMap<>();
240
		params.put(ProfitMandiConstants.ID_TOKEN, token);
97
		params.put(ProfitMandiConstants.ID_TOKEN, token);
241
		Map<String, String> headers = new HashMap<>();
98
		Map<String, String> headers = new HashMap<>();
242
		headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
99
		headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
Line 280... Line 137...
280
					retailerId = userAccountRepository.selectRetailerIdByUserId(registeredUser.getId());
137
					retailerId = userAccountRepository.selectRetailerIdByUserId(registeredUser.getId());
281
				} catch (Exception e) {
138
				} catch (Exception e) {
282
					Set<Integer> authUserPartnerSet = csService.getAuthUserPartnerIdMapping().get(authUser.getEmailId());
139
					Set<Integer> authUserPartnerSet = csService.getAuthUserPartnerIdMapping().get(authUser.getEmailId());
283
					if(authUserPartnerSet != null && authUserPartnerSet.size() > 0) {
140
					if(authUserPartnerSet != null && authUserPartnerSet.size() > 0) {
284
						retailerId = authUserPartnerSet.stream().findFirst().get();
141
						retailerId = authUserPartnerSet.stream().findFirst().get();
-
 
142
						FofoStore fs = fofoStoreRepository.selectByRetailerId(retailerId);
-
 
143
						retailerId = ProfitMandiConstants.WAREHOUSE_NSSPL_PARTNER_MAP.get(fs.getWarehouseId());
285
					} else {
144
					} else {
286
						com.spice.profitmandi.dao.entity.user.User user = userUserRepository.selectByEmailId(Utils.SYSTEM_PARTNER);
145
						com.spice.profitmandi.dao.entity.user.User user = userUserRepository.selectByEmailId(Utils.SYSTEM_PARTNER);
287
						retailerId = user.getId();
146
						retailerId = user.getId();
288
					}
147
					}
289
				}
148
				}