Subversion Repositories SmartDukaan

Rev

Rev 37728 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21561 ashik.ali 1
package com.spice.profitmandi.web.controller;
21555 kshitij.so 2
 
25008 amit.gupta 3
import com.google.gson.Gson;
4
import com.google.gson.reflect.TypeToken;
21561 ashik.ali 5
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
24016 amit.gupta 6
import com.spice.profitmandi.common.model.Location;
21561 ashik.ali 7
import com.spice.profitmandi.common.model.ProfitMandiConstants;
25011 amit.gupta 8
import com.spice.profitmandi.common.model.ProfitMandiResponse;
25008 amit.gupta 9
import com.spice.profitmandi.common.web.client.RestClient;
24383 amit.gupta 10
import com.spice.profitmandi.dao.entity.auth.AuthUser;
30299 amit.gupta 11
import com.spice.profitmandi.dao.entity.dtr.*;
24016 amit.gupta 12
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
25303 amit.gupta 13
import com.spice.profitmandi.dao.entity.user.Device;
22111 ashik.ali 14
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
15
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
24383 amit.gupta 16
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
25303 amit.gupta 17
import com.spice.profitmandi.dao.repository.catalog.DeviceRepository;
37780 amit 18
import com.spice.profitmandi.dao.repository.cs.DemoPartnerAccessService;
30299 amit.gupta 19
import com.spice.profitmandi.dao.repository.dtr.*;
24383 amit.gupta 20
import com.spice.profitmandi.service.AuthService;
24976 amit.gupta 21
import com.spice.profitmandi.service.authentication.RoleManager;
22
import com.spice.profitmandi.service.user.RetailerService;
34785 vikas.jang 23
import com.spice.profitmandi.service.user.UserService;
22139 amit.gupta 24
import com.spice.profitmandi.web.model.LoginDetails;
22069 ashik.ali 25
import com.spice.profitmandi.web.util.CookiesProcessor;
22111 ashik.ali 26
import com.spice.profitmandi.web.util.GoogleTokenUtil;
21574 ashik.ali 27
import com.spice.profitmandi.web.util.MVCResponseSender;
32570 amit.gupta 28
import org.apache.commons.lang.StringUtils;
30299 amit.gupta 29
import org.apache.http.NameValuePair;
30
import org.apache.http.client.utils.URLEncodedUtils;
31
import org.apache.http.message.BasicNameValuePair;
32
import org.apache.logging.log4j.LogManager;
33
import org.apache.logging.log4j.Logger;
34
import org.springframework.beans.factory.annotation.Autowired;
35
import org.springframework.beans.factory.annotation.Value;
36
import org.springframework.stereotype.Controller;
37
import org.springframework.ui.Model;
38
import org.springframework.ui.ModelMap;
39
import org.springframework.web.bind.annotation.*;
21561 ashik.ali 40
 
30299 amit.gupta 41
import javax.servlet.http.Cookie;
42
import javax.servlet.http.HttpServletRequest;
43
import javax.servlet.http.HttpServletResponse;
35458 amit 44
import org.springframework.transaction.annotation.Transactional;
30299 amit.gupta 45
import java.lang.reflect.Type;
46
import java.time.LocalDateTime;
47
import java.util.*;
48
import java.util.stream.Collectors;
49
 
21555 kshitij.so 50
@Controller
35458 amit 51
@Transactional(rollbackFor = Throwable.class)
21555 kshitij.so 52
public class LoginController {
53
 
32570 amit.gupta 54
    private static final Logger LOGGER = LogManager.getLogger(LoginController.class);
24016 amit.gupta 55
 
32570 amit.gupta 56
    @Autowired
57
    private GoogleTokenUtil googleTokenUtil;
24016 amit.gupta 58
 
32570 amit.gupta 59
    @Autowired
60
    private RoleManager roleManager;
26449 amit.gupta 61
 
32570 amit.gupta 62
    @Autowired
63
    private DeviceRepository deviceRepository;
24984 amit.gupta 64
 
32570 amit.gupta 65
    @Autowired
66
    private RetailerRepository retailerRepository;
24016 amit.gupta 67
 
32570 amit.gupta 68
    @Autowired
69
    private RetailerService retailerService;
24984 amit.gupta 70
 
32570 amit.gupta 71
    @Autowired
72
    private UserRepository userRepository;
24016 amit.gupta 73
 
32570 amit.gupta 74
    @Autowired
75
    private UserAccountRepository userAccountRepository;
24016 amit.gupta 76
 
32570 amit.gupta 77
    @Autowired
78
    private UserRoleRepository userRoleRepository;
24016 amit.gupta 79
 
32570 amit.gupta 80
    @Autowired
81
    private RoleRepository roleRepository;
24016 amit.gupta 82
 
32570 amit.gupta 83
    @Autowired
84
    private FofoStoreRepository fofoStoreRepository;
24016 amit.gupta 85
 
32570 amit.gupta 86
    @Autowired
87
    private MVCResponseSender mvcResponseSender;
25243 amit.gupta 88
 
32570 amit.gupta 89
    @Autowired
90
    private RestClient restClient;
24016 amit.gupta 91
 
32570 amit.gupta 92
    @Autowired
93
    private CookiesProcessor cookiesProcessor;
22079 amit.gupta 94
 
32570 amit.gupta 95
    @Value("${google.api.key}")
96
    private String googleApiKey;
24016 amit.gupta 97
 
32570 amit.gupta 98
    @Value("${app.token.url}")
99
    private String appTokenUrl;
24383 amit.gupta 100
 
32570 amit.gupta 101
    @Value("${angular.app.url}")
102
    private String appUrl;
24383 amit.gupta 103
 
36369 vikas 104
    @Value("${react.app.url}")
105
    private String reactAppUrl;
106
 
32570 amit.gupta 107
    @Value("${admin.token}")
108
    private String adminToken;
24016 amit.gupta 109
 
32570 amit.gupta 110
    @Autowired
111
    private AuthService authService;
26449 amit.gupta 112
 
32570 amit.gupta 113
    @Autowired
34785 vikas.jang 114
    private UserService userService;
115
 
116
    @Autowired
32570 amit.gupta 117
    private AuthRepository authRepository;
24016 amit.gupta 118
 
37780 amit 119
    @Autowired
120
    private DemoPartnerAccessService demoPartnerAccessService;
121
 
32570 amit.gupta 122
    @RequestMapping(value = "/login", method = RequestMethod.GET)
123
    public String loginPage(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
124
        LOGGER.info("Context Path is {}", request.getContextPath());
125
        try {
126
            LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
127
            LOGGER.info("Request session is already exist, should be redirect to as per roles assigned");
128
            String redirectUrl = null;
25243 amit.gupta 129
 
32570 amit.gupta 130
            Role roleRetailer = roleRepository.selectByName(RoleType.RETAILER.toString());
131
            Role roleFofo = roleRepository.selectByName(RoleType.FOFO.toString());
132
            Role roleFofoAdmin = roleRepository.selectByName(RoleType.FOFO_ADMIN.toString());
133
            if (fofoDetails.getRoleIds().contains(roleFofoAdmin.getId())) {
134
                redirectUrl = "/dashboard";
135
            } else if (fofoDetails.getRoleIds().contains(roleRetailer.getId())
136
                    && fofoDetails.getRoleIds().contains(roleFofo.getId())) {
137
                FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoDetails.getFofoId());
138
                if (!fofoStore.isActive()) {
139
                    cookiesProcessor.removeCookies(request, response);
140
                    throw new ProfitMandiBusinessException("", "", "");
141
                }
142
                redirectUrl = "/dashboard";
24016 amit.gupta 143
 
32570 amit.gupta 144
            } else {
145
                redirectUrl = "/login";
146
            }
147
            return "redirect:" + redirectUrl;
148
        } catch (ProfitMandiBusinessException profitMandiBusinessException) {
149
            model.addAttribute("googleApiKey", googleApiKey);
150
            model.addAttribute("appContextPath", request.getContextPath());
151
            return "login";
152
        }
153
    }
25303 amit.gupta 154
 
32570 amit.gupta 155
    @RequestMapping(value = "/", method = RequestMethod.GET)
156
    public String home() {
157
        return "redirect:/login";
158
    }
25243 amit.gupta 159
 
32570 amit.gupta 160
    @RequestMapping(value = "/mobileapp", method = RequestMethod.GET)
36369 vikas 161
    public String mobileApp(HttpServletRequest request, Model model, @RequestParam(defaultValue = "") String emailId, @RequestParam(defaultValue = "") String appType)
32570 amit.gupta 162
            throws Exception {
163
        LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
164
        if (roleManager.isAdmin(fofoDetails.getRoleIds())) {
32574 amit.gupta 165
            if (StringUtils.isEmpty(emailId)) {
32570 amit.gupta 166
                AuthUser authUser = authRepository.selectByEmailOrMobile(fofoDetails.getEmailId());
167
                model.addAttribute("authToken", getToken(authUser.getGmailId()));
168
            } else {
37780 amit 169
                validateSalesPartnerAccess(fofoDetails.getEmailId(), emailId);
32570 amit.gupta 170
                model.addAttribute("authToken", getToken(emailId));
171
                model.addAttribute("androidId", getAndroidId(emailId));
172
            }
173
        } else {
174
            emailId = fofoDetails.getEmailId();
175
            model.addAttribute("authToken", getToken(emailId));
176
            model.addAttribute("androidId", getAndroidId(emailId));
177
        }
34300 ranu 178
        model.addAttribute("internal", true);
36370 vikas 179
        if (appType.equals("react")) {
180
            model.addAttribute("appUrl", reactAppUrl);
181
        } else {
36369 vikas 182
            model.addAttribute("appUrl", appUrl);
183
        }
32570 amit.gupta 184
        return "mobileapp";
185
    }
25243 amit.gupta 186
 
37780 amit 187
    // Sales position holders may only take a partner's app token for their position or demo partners.
188
    private void validateSalesPartnerAccess(String callerEmail, String partnerEmail) throws Exception {
189
        AuthUser authUser = authRepository.selectByEmailOrMobile(callerEmail);
190
        if (authUser == null || !demoPartnerAccessService.isSalesUser(authUser.getId())) {
191
            return;
192
        }
193
        User partnerUser = userRepository.selectByEmailId(partnerEmail);
194
        int fofoId = userAccountRepository.selectRetailerIdByUserId(partnerUser.getId());
195
        if (!demoPartnerAccessService.isAllowed(authUser.getId(), fofoId)) {
196
            throw new ProfitMandiBusinessException("emailId", partnerEmail, "Partner is not mapped to you by position or demo access");
197
        }
198
    }
199
 
32570 amit.gupta 200
    private String getAndroidId(String emailId) throws Exception {
201
        User user = userRepository.selectByEmailId(emailId);
202
        List<Device> devices = deviceRepository.selectByUserIdAndModifiedTimestamp(Arrays.asList(user.getId()),
203
                LocalDateTime.now().minusYears(2), LocalDateTime.now());
204
        if (devices.size() > 0) {
205
            Device device = devices.get(0);
206
            return device.getAndroidId();
207
        } else {
208
            return "";
209
        }
210
    }
24976 amit.gupta 211
 
32570 amit.gupta 212
    private String getToken(String emailId) throws Exception {
213
        List<NameValuePair> nameValuePairs = new ArrayList<>();
214
        nameValuePairs.add(new BasicNameValuePair("adminToken", this.adminToken));
215
        nameValuePairs.add(new BasicNameValuePair("emailId", emailId));
216
        String queryString = URLEncodedUtils.format(nameValuePairs, "UTF-8");
217
        String response = restClient.post(this.appTokenUrl + queryString,
218
                new HashMap<>(), new HashMap<>());
219
        Type t = new TypeToken<ProfitMandiResponse<Map<String, Object>>>() {
220
        }.getType();
221
        ProfitMandiResponse<Map<String, Object>> apiResponse = new Gson().fromJson(response, t);
222
        if (apiResponse.getStatusCode().equals("200")) {
223
            return (new StringBuilder((String) apiResponse.getResponse().get("token")).reverse().toString());
224
        } else {
225
            throw new Exception("Unauthorised access");
226
        }
24383 amit.gupta 227
 
32570 amit.gupta 228
    }
24383 amit.gupta 229
 
32570 amit.gupta 230
    @RequestMapping(value = "/login-as-partner", method = RequestMethod.GET)
231
    public String adminLogin(HttpServletRequest request, Model model, HttpServletResponse response,
232
                             @RequestParam int fofoId) throws Exception {
233
        LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
234
        if (roleManager.isAdmin(fofoDetails.getRoleIds())) {
235
            int userId = userAccountRepository.selectUserIdByRetailerId(fofoId);
236
            User user = userRepository.selectById(userId);
237
            Set<Integer> roleIds = userRoleRepository.selectByUserId(userId).stream().map(x -> x.getRoleId())
238
                    .collect(Collectors.toSet());
239
            LoginDetails newFofoDetails = new LoginDetails();
240
            newFofoDetails.setFofoId(fofoId);
241
            newFofoDetails.setRoleIds(roleIds);
242
            newFofoDetails.setEmailId(user.getEmailId());
243
            model.addAttribute("response1", true);
244
            this.addCookiesToResponse(newFofoDetails, request, response);
245
        } else {
246
            throw new ProfitMandiBusinessException("", "", "");
247
        }
248
        return "response";
249
    }
24016 amit.gupta 250
 
32570 amit.gupta 251
    @RequestMapping(value = "/login-as-partner-readonly", method = RequestMethod.GET)
252
    public String readonlyLogin(HttpServletRequest request, Model model, HttpServletResponse response,
253
                                @RequestParam int fofoId) throws Exception {
254
        LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
255
        if (roleManager.isAdmin(fofoDetails.getRoleIds())) {
37780 amit 256
            AuthUser authUser = authRepository.selectByEmailOrMobile(fofoDetails.getEmailId());
257
            if (authUser == null || !demoPartnerAccessService.isAllowed(authUser.getId(), fofoId)) {
258
                throw new ProfitMandiBusinessException("fofoId", fofoId, "Partner is not mapped to you by position or demo access");
259
            }
32570 amit.gupta 260
            int userId = userAccountRepository.selectUserIdByRetailerId(fofoId);
261
            User user = userRepository.selectById(userId);
262
            Set<Integer> roleIds = userRoleRepository.selectByUserId(userId).stream().map(x -> x.getRoleId())
263
                    .collect(Collectors.toSet());
264
            LoginDetails newFofoDetails = new LoginDetails();
265
            newFofoDetails.setFofoId(fofoId);
266
            newFofoDetails.setRoleIds(roleIds);
267
            newFofoDetails.setEmailId(user.getEmailId());
268
            newFofoDetails.setReadOnly(true);
269
            model.addAttribute("response1", true);
270
            this.addCookiesToResponse(newFofoDetails, request, response);
271
        } else {
272
            throw new ProfitMandiBusinessException("", "", "");
273
        }
274
        return "response";
275
    }
24016 amit.gupta 276
 
32570 amit.gupta 277
    @RequestMapping(value = "/login", method = RequestMethod.POST)
278
    public String login(HttpServletRequest request, HttpServletResponse response,
279
                        @RequestParam(name = ProfitMandiConstants.TOKEN) String token,
280
                        @RequestParam(name = ProfitMandiConstants.EMAIL_ID_OR_MOBILE_NUMBER, defaultValue = "") String emailIdOrMobileNumber,
281
                        @RequestParam(name = "password", defaultValue = "") String password, Model model) throws Exception {
25275 amit.gupta 282
 
32570 amit.gupta 283
        LoginDetails fofoDetails = new LoginDetails();
284
        Set<Integer> roleIds = new HashSet<>();
285
        fofoDetails.setRoleIds(roleIds);
286
        String emailId = null;
287
        String name = null;
34785 vikas.jang 288
        User user = null;
32570 amit.gupta 289
        try {
290
            // if role is retailer then FOFO_ID is retailerId else it is userid as normal
291
            // user's wont have retailer id.
32571 amit.gupta 292
            if (StringUtils.isEmpty(token)) {
32570 amit.gupta 293
                if (authService.authenticate(emailIdOrMobileNumber, password)) {
294
                    AuthUser authUser = authRepository.selectByEmailOrMobile(emailIdOrMobileNumber);
295
                    if (authUser == null) {
34785 vikas.jang 296
                        throw new ProfitMandiBusinessException("Authentication", "Email or Mobile", "Invalid Email Or Mobile");
32570 amit.gupta 297
                    }
298
                    emailId = authUser.getEmailId();
299
                    name = authUser.getFirstName() + " " + authUser.getLastName();
300
                    authUser.setLastLoginTimestamp(LocalDateTime.now());
34785 vikas.jang 301
                } else {
302
                    // TODO: Add login with email and password
303
                    user = userService.authenticate(emailIdOrMobileNumber, password);
304
                    if (user != null) {
305
                        emailId = user.getEmailId();
306
                    } else {
307
                        LOGGER.error("User not found with given emailIdOrMobileNumber [{}]", emailIdOrMobileNumber);
308
                        model.addAttribute("response1", mvcResponseSender.createResponseString("RTLR_OK_1002", true,
309
                                request.getContextPath() + "/login", "Email"));
310
                        return "response";
311
                    }
32570 amit.gupta 312
                }
313
            } else {
314
                try {
315
                    emailId = googleTokenUtil.getEmailId(token);
316
                } catch (Exception e) {
317
                    // in case we cannot connect to google server.
318
                    emailId = emailIdOrMobileNumber;
319
                }
320
            }
321
            fofoDetails.setEmailId(emailId);
322
            fofoDetails.setFofoId(-1);
34785 vikas.jang 323
 
32570 amit.gupta 324
            if (user == null) {
325
                try {
34785 vikas.jang 326
                    user = userRepository.selectByEmailId(emailId);
32570 amit.gupta 327
                } catch (ProfitMandiBusinessException profitMandiBusinessException) {
34785 vikas.jang 328
                    LOGGER.error("User not found with given emailId [{}]", emailId);
329
                    try {
330
                        user = userRepository.selectBySecondryEmailId(emailId);
331
                    } catch (ProfitMandiBusinessException exception) {
332
                        LOGGER.error("User not found with given emailId", exception);
333
                        model.addAttribute("response1", mvcResponseSender.createResponseString("RTLR_OK_1002", true,
334
                                request.getContextPath() + "/login", "Email"));
335
                        return "response";
336
                    }
32570 amit.gupta 337
                }
338
            }
34785 vikas.jang 339
 
32570 amit.gupta 340
            if (user != null) {
341
                fofoDetails.setFofoId(user.getId());
342
                try {
343
                    List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
34785 vikas.jang 344
                    for (UserRole userRole : userRoles) {
345
                        roleIds.add(userRole.getRoleId());
32570 amit.gupta 346
                    }
347
                    List<Role> roles = roleRepository.selectByIds(roleIds);
348
                    for (Role role : roles) {
349
                        if (role.getName().equals(RoleType.RETAILER.toString())) {
34785 vikas.jang 350
                            UserAccount userAccounts = userAccountRepository.selectByUserIdType(user.getId(), AccountType.saholic);
32570 amit.gupta 351
                            Retailer retailer = retailerRepository.selectById(userAccounts.getAccountKey());
352
                            fofoDetails.setFofoId(retailer.getId());
353
                        }
354
                    }
355
                } catch (ProfitMandiBusinessException pmbe) {
356
                    LOGGER.error("Data Inconsistent", pmbe);
357
                }
358
            }
359
            String redirectUrl = null;
24976 amit.gupta 360
 
32570 amit.gupta 361
            Role roleFofo = roleRepository.selectByName(RoleType.FOFO.toString());
362
            Role roleFofoAdmin = roleRepository.selectByName(RoleType.FOFO_ADMIN.toString());
24016 amit.gupta 363
 
32570 amit.gupta 364
            if (fofoDetails.getRoleIds().contains(roleFofo.getId())) {
365
                redirectUrl = "/dashboard";
366
            } else {
367
                redirectUrl = "/login";
368
            }
24016 amit.gupta 369
 
32570 amit.gupta 370
            if (redirectUrl.equals("/dashboard")) {
371
                user.setLoginTimestamp(LocalDateTime.now());
372
                this.addCookiesToResponse(fofoDetails, request, response);
34785 vikas.jang 373
                LOGGER.info("Requested token email_id is valid, user login to system, shoud be redirect to {}", redirectUrl);
32570 amit.gupta 374
                model.addAttribute("response1", mvcResponseSender.createResponseString("RTLR_OK_1002", true,
375
                        request.getContextPath() + redirectUrl, name));
376
            } else {
377
                LOGGER.error("Requested token email_id is not valid, please try to login");
378
                /**
379
                 * TODO:Amit This redirect should be handle.
380
                 */
34785 vikas.jang 381
                throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, fofoDetails.getEmailId(), "RTLR_1000");
32570 amit.gupta 382
            }
383
            return "response";
384
        } catch (ProfitMandiBusinessException profitMandiBusinessException) {
37728 amit 385
            LOGGER.warn("Error : ", profitMandiBusinessException);
32570 amit.gupta 386
            model.addAttribute("response1",
387
                    mvcResponseSender.createResponseString(profitMandiBusinessException.getCode(), false, "/error"));
388
            return "response";
389
        } catch (Exception e) {
390
            e.printStackTrace();
391
            model.addAttribute("response1",
392
                    mvcResponseSender.createResponseString(
393
                            "You are not authorised! " + "Please contact the team if you feel this is incorrect", false,
394
                            "/error"));
395
            return "response";
396
        }
397
    }
24016 amit.gupta 398
 
32570 amit.gupta 399
    @PostMapping(value = "/forgetPassword")
34785 vikas.jang 400
    public String forgetPasswordPage(@RequestParam(name = ProfitMandiConstants.EMAIL_ID, defaultValue = "") String emailId, Model model) throws Exception {
401
        try {
402
            LOGGER.info("Forgetting password of: [{}]",emailId);
403
            authService.resetPassword(emailId);
404
        } catch (Exception exception){
405
            try {
406
                userService.resetPassword(emailId);
407
            } catch (Exception e){
34815 vikas 408
                LOGGER.info("Forgetting password of: [{}] - {}",emailId, e);
34785 vikas.jang 409
                throw new ProfitMandiBusinessException("Password Reset Email", emailId, "Could not send password reset mail. Password Could not be reset");
410
            }
411
        }
32570 amit.gupta 412
        model.addAttribute("response1", mvcResponseSender.createResponseString(true));
413
        LOGGER.info("completed");
414
        return "response";
415
    }
24984 amit.gupta 416
 
34785 vikas.jang 417
    private void addCookiesToResponse(LoginDetails fofoDetails, HttpServletRequest request, HttpServletResponse response) {
32570 amit.gupta 418
        List<String> roleIds = new ArrayList<>();
24016 amit.gupta 419
 
32570 amit.gupta 420
        for (int roleId : fofoDetails.getRoleIds()) {
421
            roleIds.add(String.valueOf(roleId));
422
        }
423
        Cookie cookieRoleIds = new Cookie(ProfitMandiConstants.ROLE_IDS, String.join("-", roleIds));
424
        cookieRoleIds.setDomain(request.getServerName());
425
        cookieRoleIds.setPath(request.getContextPath());
24016 amit.gupta 426
 
32570 amit.gupta 427
        Cookie cookieFofoId = new Cookie(ProfitMandiConstants.FOFO_ID, String.valueOf(fofoDetails.getFofoId()));
428
        cookieFofoId.setDomain(request.getServerName());
429
        cookieFofoId.setPath(request.getContextPath());
430
 
431
        Cookie cookieEmailId = new Cookie(ProfitMandiConstants.EMAIL_ID, fofoDetails.getEmailId());
432
        cookieEmailId.setDomain(request.getServerName());
433
        cookieEmailId.setPath(request.getContextPath());
434
 
435
        Cookie cookieReadOnly = new Cookie(ProfitMandiConstants.READONLY_KEY, fofoDetails.isReadOnly() + "");
436
        cookieReadOnly.setDomain(request.getServerName());
437
        cookieReadOnly.setPath(request.getContextPath());
438
 
439
        response.addCookie(cookieFofoId);
440
        response.addCookie(cookieEmailId);
441
        response.addCookie(cookieRoleIds);
442
        response.addCookie(cookieReadOnly);
443
    }
444
 
445
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
446
    public String logout(HttpServletRequest request, @ModelAttribute("model") ModelMap model,
447
                         HttpServletResponse response) throws Exception {
448
        try {
449
            LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
450
            User user = null;
451
            try {
452
                user = userRepository.selectByEmailId(loginDetails.getEmailId());
453
            } catch (ProfitMandiBusinessException profitMandiBusinessException) {
37728 amit 454
                LOGGER.warn("User not found with given emailId", profitMandiBusinessException);
32570 amit.gupta 455
            }
456
            if (user == null) {
457
                user = userRepository.selectBySecondryEmailId(loginDetails.getEmailId());
458
            }
459
            user.setLogoutTimestamp(LocalDateTime.now());
460
            cookiesProcessor.removeCookies(request, response);
461
            LOGGER.info("Logout is successfull, should be redirect to /login");
462
            return "redirect:/login";
463
        } catch (ProfitMandiBusinessException profitMandiBusinessException) {
464
            LOGGER.info("Error occured while removing requested cookies, should be redirect to /login");
465
            return "redirect:/login";
466
        }
467
    }
468
 
469
    @RequestMapping(value = "/partner/location", method = RequestMethod.PUT)
470
    public String setLocation(HttpServletRequest request, Model model, @RequestBody Location location)
471
            throws Exception {
472
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
473
        boolean response = true;
474
        try {
475
            FofoStore fs = fofoStoreRepository.selectByRetailerId(loginDetails.getFofoId());
476
            if (fs.getLatitude() == null) {
477
                fs.setLatitude(location.getLatitude());
478
                fs.setLongitude(location.getLongitude());
479
            }
480
            model.addAttribute("response1", response);
481
        } catch (Exception e) {
482
            LOGGER.error("FofoStore Code not found of fofoId {}", loginDetails.getFofoId());
483
        }
484
        return "response";
485
    }
21555 kshitij.so 486
}