| 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 |
|
| 26396 |
amit.gupta |
95 |
public Map<String, Object> process(String token) throws ProfitMandiBusinessException {
|
| 21556 |
ashik.ali |
96 |
Map<String, String> params = new HashMap<>();
|
|
|
97 |
params.put(ProfitMandiConstants.ID_TOKEN, token);
|
| 26673 |
amit.gupta |
98 |
Map<String, String> headers = new HashMap<>();
|
| 22355 |
ashik.ali |
99 |
headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
|
| 23860 |
ashik.ali |
100 |
String responseString = null;
|
| 21556 |
ashik.ali |
101 |
try {
|
| 23532 |
amit.gupta |
102 |
responseString = restClient.get(SchemeType.HTTPS, V3_HOST_NAME, PORT_NUMBER, V3_URI, params, headers);
|
| 23860 |
ashik.ali |
103 |
} catch (HttpHostConnectException e) {
|
|
|
104 |
// TODO Auto-generated catch block
|
|
|
105 |
e.printStackTrace();
|
| 23532 |
amit.gupta |
106 |
}
|
|
|
107 |
try {
|
| 21556 |
ashik.ali |
108 |
JsonNode rootNode = objectMapper.readTree(responseString);
|
| 26396 |
amit.gupta |
109 |
String email = rootNode.get("email").asText();
|
|
|
110 |
|
|
|
111 |
String name = authService.getNameByEmailId(email);
|
|
|
112 |
|
|
|
113 |
Map<String, Object> responseMap = new HashMap<>(2);
|
|
|
114 |
LOGGER.info("User Name from getNameByEmailId({}) is {}", email, name);
|
|
|
115 |
if (name != null) {
|
|
|
116 |
User registeredUser = null;
|
|
|
117 |
AuthUser authUser = authRepository.selectByGmailId(email);
|
|
|
118 |
|
|
|
119 |
if (authRepository.selectByGmailId(email) != null) {
|
|
|
120 |
registeredUser = userRepository.selectByEmailId(authUser.getEmailId());
|
|
|
121 |
} else if (promoterRepository.isExistByEmailId(email)) {
|
|
|
122 |
Promoter promoter = promoterRepository.selectByEmailId(email);
|
|
|
123 |
int userId = userAccountRepository.selectUserIdByRetailerId(promoter.getRetailerId());
|
|
|
124 |
registeredUser = userRepository.selectById(userId);
|
|
|
125 |
} else if (userRepository.isExistBySecondryEmailId(email)) {
|
|
|
126 |
registeredUser = userRepository.selectBySecondryEmailId(email);
|
|
|
127 |
}
|
|
|
128 |
LOGGER.info("4");
|
|
|
129 |
List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(registeredUser.getId());
|
|
|
130 |
String[] roleTypes = new String[roleIds.size()];
|
|
|
131 |
int index = 0;
|
|
|
132 |
for (int roleId : roleIds) {
|
|
|
133 |
roleTypes[index++] = String.valueOf(roleId);
|
|
|
134 |
}
|
|
|
135 |
int retailerId;
|
|
|
136 |
try {
|
| 27258 |
amit.gupta |
137 |
retailerId = userAccountRepository.selectRetailerIdByUserId(registeredUser.getId());
|
|
|
138 |
} catch (Exception e) {
|
| 27257 |
amit.gupta |
139 |
Set<Integer> authUserPartnerSet = csService.getAuthUserPartnerIdMapping().get(authUser.getEmailId());
|
| 27268 |
amit.gupta |
140 |
if(authUserPartnerSet != null && authUserPartnerSet.size() > 0) {
|
| 27257 |
amit.gupta |
141 |
retailerId = authUserPartnerSet.stream().findFirst().get();
|
| 28100 |
amit.gupta |
142 |
FofoStore fs = fofoStoreRepository.selectByRetailerId(retailerId);
|
|
|
143 |
retailerId = ProfitMandiConstants.WAREHOUSE_NSSPL_PARTNER_MAP.get(fs.getWarehouseId());
|
| 27258 |
amit.gupta |
144 |
} else {
|
|
|
145 |
com.spice.profitmandi.dao.entity.user.User user = userUserRepository.selectByEmailId(Utils.SYSTEM_PARTNER);
|
|
|
146 |
retailerId = user.getId();
|
|
|
147 |
}
|
| 26396 |
amit.gupta |
148 |
}
|
|
|
149 |
responseMap.put(ProfitMandiConstants.TOKEN,
|
|
|
150 |
JWTUtil.create(email, registeredUser.getId(), retailerId, roleTypes));
|
|
|
151 |
LOGGER.info(
|
|
|
152 |
"Param value for email, registeredUser.getId(), retailerId, roleTypes are {}, {}, {} and {}",
|
|
|
153 |
email, registeredUser.getId(), retailerId, Arrays.asList(roleTypes));
|
|
|
154 |
responseMap.put(ProfitMandiConstants.REGISTERED, true);
|
|
|
155 |
return responseMap;
|
|
|
156 |
}
|
|
|
157 |
|
| 23204 |
ashik.ali |
158 |
User user = null;
|
| 24491 |
amit.gupta |
159 |
try {
|
| 26396 |
amit.gupta |
160 |
user = userRepository.selectByEmailId(email);
|
| 24491 |
amit.gupta |
161 |
} catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
162 |
|
|
|
163 |
}
|
|
|
164 |
if (user == null) {
|
| 26396 |
amit.gupta |
165 |
try {
|
|
|
166 |
user = userRepository.selectByEmailId(email);
|
|
|
167 |
} catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
168 |
responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(email));
|
|
|
169 |
responseMap.put(ProfitMandiConstants.REGISTERED, false);
|
|
|
170 |
}
|
|
|
171 |
} else {
|
|
|
172 |
List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(user.getId());
|
|
|
173 |
int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
|
|
|
174 |
String[] roleTypes = new String[roleIds.size()];
|
|
|
175 |
int index = 0;
|
|
|
176 |
for (int roleId : roleIds) {
|
|
|
177 |
roleTypes[index++] = String.valueOf(roleId);
|
|
|
178 |
}
|
|
|
179 |
responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), retailerId, roleTypes));
|
|
|
180 |
responseMap.put(ProfitMandiConstants.REGISTERED, true);
|
| 23204 |
ashik.ali |
181 |
}
|
| 24491 |
amit.gupta |
182 |
|
| 26396 |
amit.gupta |
183 |
return responseMap;
|
|
|
184 |
|
| 24491 |
amit.gupta |
185 |
} catch (JsonProcessingException jsonProcessingException) {
|
| 21556 |
ashik.ali |
186 |
throw new ProfitMandiBusinessException("", "", "VE_1001");
|
| 24491 |
amit.gupta |
187 |
} catch (IOException ioException) {
|
| 21556 |
ashik.ali |
188 |
throw new ProfitMandiBusinessException("", "", "VE_1001");
|
|
|
189 |
}
|
|
|
190 |
}
|
| 26590 |
amit.gupta |
191 |
|
|
|
192 |
public Map<String, Object> processStore(String storeCode) throws ProfitMandiBusinessException {
|
|
|
193 |
Map<String, Object> responseMap = new HashMap<>();
|
| 26599 |
amit.gupta |
194 |
storeCode = storeCode.toUpperCase();
|
| 26598 |
amit.gupta |
195 |
int retailerId = retailerService.getStoreCodeRetailerMap().get(storeCode);
|
| 26590 |
amit.gupta |
196 |
int userId = userAccountRepository.selectUserIdByRetailerId(retailerId);
|
|
|
197 |
List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(userId);
|
|
|
198 |
|
|
|
199 |
String[] roleTypes = new String[roleIds.size()];
|
|
|
200 |
int index = 0;
|
|
|
201 |
for (int roleId : roleIds) {
|
|
|
202 |
roleTypes[index++] = String.valueOf(roleId);
|
|
|
203 |
}
|
|
|
204 |
responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(userId, retailerId, roleTypes));
|
|
|
205 |
responseMap.put(ProfitMandiConstants.REGISTERED, true);
|
|
|
206 |
return responseMap;
|
|
|
207 |
}
|
| 21277 |
ashik.ali |
208 |
}
|