Subversion Repositories SmartDukaan

Rev

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

Rev 35948 Rev 35969
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
3
import java.time.LocalDateTime;
3
import java.time.LocalDateTime;
4
import java.time.format.DateTimeFormatter;
4
import java.time.format.DateTimeFormatter;
5
import java.util.ArrayList;
5
import java.util.ArrayList;
-
 
6
import java.util.Collections;
6
import java.util.List;
7
import java.util.List;
7
 
8
 
8
import javax.mail.internet.InternetAddress;
9
import javax.mail.internet.InternetAddress;
9
import javax.mail.internet.MimeMessage;
10
import javax.mail.internet.MimeMessage;
10
import javax.servlet.http.HttpServletRequest;
11
import javax.servlet.http.HttpServletRequest;
Line 31... Line 32...
31
import com.spice.profitmandi.dao.entity.dtr.User;
32
import com.spice.profitmandi.dao.entity.dtr.User;
32
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
33
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
33
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
34
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
34
import com.spice.profitmandi.dao.entity.dtr.Retailer;
35
import com.spice.profitmandi.dao.entity.dtr.Retailer;
35
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
36
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
-
 
37
import com.spice.profitmandi.dao.entity.cs.CallbackRequestReply;
-
 
38
import com.spice.profitmandi.dao.repository.cs.CallbackRequestReplyRepository;
36
import com.spice.profitmandi.dao.repository.cs.CallbackRequestRepository;
39
import com.spice.profitmandi.dao.repository.cs.CallbackRequestRepository;
37
import com.spice.profitmandi.dao.repository.cs.CsService;
40
import com.spice.profitmandi.dao.repository.cs.CsService;
38
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
41
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
39
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
42
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
40
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
43
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
-
 
44
import com.spice.profitmandi.web.req.CallbackReplyRequest;
41
import com.spice.profitmandi.web.req.CallbackRequest;
45
import com.spice.profitmandi.web.req.CallbackRequest;
42
import com.spice.profitmandi.web.req.ResolveCallbackRequest;
46
import com.spice.profitmandi.web.req.ResolveCallbackRequest;
43
import com.spice.profitmandi.web.res.AssignedRequestResponse;
47
import com.spice.profitmandi.web.res.AssignedRequestResponse;
-
 
48
import com.spice.profitmandi.web.res.CallbackReplyResponse;
44
import com.spice.profitmandi.web.res.CallbackRequestResponse;
49
import com.spice.profitmandi.web.res.CallbackRequestResponse;
45
import com.spice.profitmandi.web.res.SupportTeamResponse;
50
import com.spice.profitmandi.web.res.SupportTeamResponse;
46
import com.spice.profitmandi.web.res.SupportTeamResponse.ContactDetail;
51
import com.spice.profitmandi.web.res.SupportTeamResponse.ContactDetail;
47
 
52
 
48
import io.swagger.annotations.ApiImplicitParam;
53
import io.swagger.annotations.ApiImplicitParam;
Line 72... Line 77...
72
 
77
 
73
    @Autowired
78
    @Autowired
74
    private CallbackRequestRepository callbackRequestRepository;
79
    private CallbackRequestRepository callbackRequestRepository;
75
 
80
 
76
    @Autowired
81
    @Autowired
-
 
82
    private CallbackRequestReplyRepository callbackRequestReplyRepository;
-
 
83
 
-
 
84
    @Autowired
77
    private RetailerRepository retailerRepository;
85
    private RetailerRepository retailerRepository;
78
 
86
 
79
    @Autowired
87
    @Autowired
80
    private FofoStoreRepository fofoStoreRepository;
88
    private FofoStoreRepository fofoStoreRepository;
81
 
89
 
Line 109... Line 117...
109
            int abmAuthUserId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L1, fofoId);
117
            int abmAuthUserId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L1, fofoId);
110
            if (abmAuthUserId > 0) {
118
            if (abmAuthUserId > 0) {
111
                AuthUser user = authRepository.selectById(abmAuthUserId);
119
                AuthUser user = authRepository.selectById(abmAuthUserId);
112
                if (user != null) {
120
                if (user != null) {
113
                    l1Contacts.add(buildContact(user, "ASM", "#1a7a4c",
121
                    l1Contacts.add(buildContact(user, "ASM", "#1a7a4c",
114
                            "Sales support and regional queries", true, true, false, false));
122
                            "Sales support and regional queries", true, true, false, true));
115
                }
123
                }
116
            }
124
            }
117
        } catch (Exception e) {
125
        } catch (Exception e) {
118
            LOGGER.warn("Could not fetch ABM for fofoId: {}", fofoId, e);
126
            LOGGER.warn("Could not fetch ABM for fofoId: {}", fofoId, e);
119
        }
127
        }
Line 130... Line 138...
130
            int rbmManagerAuthUserId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_RBM, EscalationType.L2, fofoId);
138
            int rbmManagerAuthUserId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_RBM, EscalationType.L2, fofoId);
131
            if (rbmManagerAuthUserId > 0) {
139
            if (rbmManagerAuthUserId > 0) {
132
                AuthUser user = authRepository.selectById(rbmManagerAuthUserId);
140
                AuthUser user = authRepository.selectById(rbmManagerAuthUserId);
133
                if (user != null) {
141
                if (user != null) {
134
                    l2Contacts.add(buildContact(user, "RBM MANAGER", "#7c3aed",
142
                    l2Contacts.add(buildContact(user, "RBM MANAGER", "#7c3aed",
135
                            "For escalated regional matters", false, false, true, true));
143
                            "For escalated regional matters", true, true, false, true));
136
                }
144
                }
137
            }
145
            }
138
        } catch (Exception e) {
146
        } catch (Exception e) {
139
            LOGGER.warn("Could not fetch RBM Manager for fofoId: {}", fofoId, e);
147
            LOGGER.warn("Could not fetch RBM Manager for fofoId: {}", fofoId, e);
140
        }
148
        }
Line 144... Line 152...
144
            int bmAuthUserId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L2, fofoId);
152
            int bmAuthUserId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L2, fofoId);
145
            if (bmAuthUserId > 0) {
153
            if (bmAuthUserId > 0) {
146
                AuthUser user = authRepository.selectById(bmAuthUserId);
154
                AuthUser user = authRepository.selectById(bmAuthUserId);
147
                if (user != null) {
155
                if (user != null) {
148
                    l2Contacts.add(buildContact(user, "BM", "#1e6091",
156
                    l2Contacts.add(buildContact(user, "BM", "#1e6091",
149
                            "Handles: Orders, Stock, Schemes", true, false, false, true));
157
                            "Handles: Orders, Stock, Schemes", true, true, false, true));
150
                }
158
                }
151
            }
159
            }
152
        } catch (Exception e) {
160
        } catch (Exception e) {
153
            LOGGER.warn("Could not fetch BM for fofoId: {}", fofoId, e);
161
            LOGGER.warn("Could not fetch BM for fofoId: {}", fofoId, e);
154
        }
162
        }
155
 
163
 
156
        // Affordability Operations = FINANCIAL_SERVICES category, L2
164
        // Finance Partner Mapping Operations = FINANCIAL_SERVICES category, L2
157
        try {
165
        try {
158
            int affordAuthUserId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_FINANCIAL_SERVICES, EscalationType.L2, fofoId);
166
            int affordAuthUserId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_FINANCIAL_SERVICES, EscalationType.L2, fofoId);
159
            if (affordAuthUserId > 0) {
167
            if (affordAuthUserId > 0) {
160
                AuthUser user = authRepository.selectById(affordAuthUserId);
168
                AuthUser user = authRepository.selectById(affordAuthUserId);
161
                if (user != null) {
169
                if (user != null) {
162
                    l2Contacts.add(buildContact(user, "Finance Partner Mapping", "#d97706",
170
                    l2Contacts.add(buildContact(user, "Finance Partner Mapping", "#d97706",
163
                            "Credit, payment plans, and operations", false, false, true, false));
171
                            "Credit, payment plans, and operations", false, false, true, false));
164
                }
172
                }
165
            }
173
            }
166
        } catch (Exception e) {
174
        } catch (Exception e) {
167
            LOGGER.warn("Could not fetch Affordability Manager for fofoId: {}", fofoId, e);
175
            LOGGER.warn("Could not fetch Finance Partner Mapping Manager for fofoId: {}", fofoId, e);
168
        }
176
        }
169
 
177
 
170
        // Branding Manager = DESIGN category, L1
178
        // Branding Manager = DESIGN category, L1
171
        try {
179
        try {
172
            int brandingAuthUserId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_DESIGN, EscalationType.L1, fofoId);
180
            int brandingAuthUserId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_DESIGN, EscalationType.L1, fofoId);
Line 303... Line 311...
303
            res.setSubject(entity.getSubject());
311
            res.setSubject(entity.getSubject());
304
            res.setMessage(entity.getMessage());
312
            res.setMessage(entity.getMessage());
305
            res.setTiming(entity.getTiming());
313
            res.setTiming(entity.getTiming());
306
            res.setTimingLabel("11-15".equals(entity.getTiming()) ? "11:00 AM - 3:00 PM" : "3:00 PM - 7:00 PM");
314
            res.setTimingLabel("11-15".equals(entity.getTiming()) ? "11:00 AM - 3:00 PM" : "3:00 PM - 7:00 PM");
307
            res.setStatus(entity.getStatus());
315
            res.setStatus(entity.getStatus());
-
 
316
            res.setResolutionNotes(entity.getResolutionNotes());
-
 
317
            res.setReplyCount(callbackRequestReplyRepository.countByCallbackRequestId(entity.getId()));
-
 
318
            res.setUnread(entity.isUnread());
308
            if (entity.getCreateTimestamp() != null) {
319
            if (entity.getCreateTimestamp() != null) {
309
                res.setCreateTimestamp(entity.getCreateTimestamp().format(formatter));
320
                res.setCreateTimestamp(entity.getCreateTimestamp().format(formatter));
310
            }
321
            }
311
            responseList.add(res);
322
            responseList.add(res);
312
        }
323
        }
313
 
324
 
314
        return responseSender.ok(responseList);
325
        return responseSender.ok(responseList);
315
    }
326
    }
316
 
327
 
-
 
328
    @RequestMapping(value = "/support/callback-requests/unread-count", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
-
 
329
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
-
 
330
    @ApiOperation(value = "Get count of unread callback requests for the logged-in retailer")
-
 
331
    public ResponseEntity<?> getUnreadCount(HttpServletRequest request) throws Throwable {
-
 
332
        UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
-
 
333
        int fofoId = userInfo.getRetailerId();
-
 
334
        LOGGER.info("Getting unread callback request count for fofoId: {}", fofoId);
-
 
335
        long count = callbackRequestRepository.countUnreadByFofoId(fofoId);
-
 
336
        return responseSender.ok(count);
-
 
337
    }
-
 
338
 
-
 
339
    @RequestMapping(value = "/support/callback-requests/mark-read", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
-
 
340
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
-
 
341
    @ApiOperation(value = "Mark all callback requests as read for the logged-in retailer")
-
 
342
    public ResponseEntity<?> markRequestsAsRead(HttpServletRequest request) throws Throwable {
-
 
343
        UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
-
 
344
        int fofoId = userInfo.getRetailerId();
-
 
345
        LOGGER.info("Marking all callback requests as read for fofoId: {}", fofoId);
-
 
346
        callbackRequestRepository.markAllReadByFofoId(fofoId);
-
 
347
        return responseSender.ok(true);
-
 
348
    }
-
 
349
 
317
    @RequestMapping(value = "/support/assigned-requests", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
350
    @RequestMapping(value = "/support/assigned-requests", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
318
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
351
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
319
    @ApiOperation(value = "Get callback requests assigned to the logged-in internal user")
352
    @ApiOperation(value = "Get callback requests assigned to the logged-in internal user")
320
    public ResponseEntity<?> getAssignedRequests(HttpServletRequest request) throws Throwable {
353
    public ResponseEntity<?> getAssignedRequests(HttpServletRequest request) throws Throwable {
321
        UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
354
        UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
Line 334... Line 367...
334
            res.setMessage(entity.getMessage());
367
            res.setMessage(entity.getMessage());
335
            res.setTiming(entity.getTiming());
368
            res.setTiming(entity.getTiming());
336
            res.setTimingLabel("11-15".equals(entity.getTiming()) ? "11:00 AM - 3:00 PM" : "3:00 PM - 7:00 PM");
369
            res.setTimingLabel("11-15".equals(entity.getTiming()) ? "11:00 AM - 3:00 PM" : "3:00 PM - 7:00 PM");
337
            res.setStatus(entity.getStatus());
370
            res.setStatus(entity.getStatus());
338
            res.setResolutionNotes(entity.getResolutionNotes());
371
            res.setResolutionNotes(entity.getResolutionNotes());
-
 
372
            res.setReplyCount(callbackRequestReplyRepository.countByCallbackRequestId(entity.getId()));
339
 
373
 
340
            if (entity.getCreateTimestamp() != null) {
374
            if (entity.getCreateTimestamp() != null) {
341
                res.setCreateTimestamp(entity.getCreateTimestamp().format(formatter));
375
                res.setCreateTimestamp(entity.getCreateTimestamp().format(formatter));
342
            }
376
            }
343
            if (entity.getUpdateTimestamp() != null) {
377
            if (entity.getUpdateTimestamp() != null) {
Line 396... Line 430...
396
 
430
 
397
        LOGGER.info("Callback request id: {} resolved with status: {}", id, resolveRequest.getStatus());
431
        LOGGER.info("Callback request id: {} resolved with status: {}", id, resolveRequest.getStatus());
398
        return responseSender.ok(true);
432
        return responseSender.ok(true);
399
    }
433
    }
400
 
434
 
-
 
435
    @RequestMapping(value = "/support/callback-requests/{id}/replies", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
-
 
436
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
-
 
437
    @ApiOperation(value = "Get all replies for a callback request")
-
 
438
    public ResponseEntity<?> getReplies(HttpServletRequest request, @PathVariable("id") int id) throws Throwable {
-
 
439
        LOGGER.info("Getting replies for callback request id: {}", id);
-
 
440
 
-
 
441
        List<CallbackRequestReply> replies = callbackRequestReplyRepository.selectAllByCallbackRequestId(id);
-
 
442
        Collections.reverse(replies); // oldest first for chat order
-
 
443
 
-
 
444
        List<CallbackReplyResponse> responseList = new ArrayList<>();
-
 
445
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM yyyy, hh:mm a");
-
 
446
 
-
 
447
        for (CallbackRequestReply reply : replies) {
-
 
448
            CallbackReplyResponse res = new CallbackReplyResponse();
-
 
449
            res.setId(reply.getId());
-
 
450
            res.setMessage(reply.getMessage());
-
 
451
            res.setSenderType(reply.getSenderType());
-
 
452
            res.setSenderName(reply.getSenderName());
-
 
453
            if (reply.getCreateTimestamp() != null) {
-
 
454
                res.setCreateTimestamp(reply.getCreateTimestamp().format(formatter));
-
 
455
            }
-
 
456
            responseList.add(res);
-
 
457
        }
-
 
458
 
-
 
459
        return responseSender.ok(responseList);
-
 
460
    }
-
 
461
 
-
 
462
    @RequestMapping(value = "/support/callback-requests/{id}/reply", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
-
 
463
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
-
 
464
    @ApiOperation(value = "Add a reply to a callback request")
-
 
465
    public ResponseEntity<?> addReply(HttpServletRequest request, @PathVariable("id") int id,
-
 
466
                                       @RequestBody CallbackReplyRequest replyRequest) throws Throwable {
-
 
467
        UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
-
 
468
 
-
 
469
        com.spice.profitmandi.dao.entity.cs.CallbackRequest callbackReq = callbackRequestRepository.selectById(id);
-
 
470
        if (callbackReq == null) {
-
 
471
            LOGGER.warn("Callback request not found for id: {}", id);
-
 
472
            return responseSender.ok(false);
-
 
473
        }
-
 
474
 
-
 
475
        // Determine sender type: if userId matches the request creator, it's RETAILER; otherwise INTERNAL
-
 
476
        String senderType = (userInfo.getUserId() == callbackReq.getUserId()) ? "RETAILER" : "INTERNAL";
-
 
477
        User senderUser = userRepository.selectById(userInfo.getUserId());
-
 
478
        String senderName = senderUser != null ? senderUser.getFirstName() : "Unknown";
-
 
479
 
-
 
480
        LOGGER.info("Adding reply to callback request id: {} by userId: {} senderType: {}", id, userInfo.getUserId(), senderType);
-
 
481
 
-
 
482
        CallbackRequestReply reply = new CallbackRequestReply();
-
 
483
        reply.setCallbackRequestId(id);
-
 
484
        reply.setMessage(replyRequest.getMessage());
-
 
485
        reply.setSenderType(senderType);
-
 
486
        reply.setSenderName(senderName);
-
 
487
        reply.setSenderId(userInfo.getUserId());
-
 
488
        reply.setCreateTimestamp(LocalDateTime.now());
-
 
489
        callbackRequestReplyRepository.persist(reply);
-
 
490
 
-
 
491
        // Update the parent request's updateTimestamp
-
 
492
        callbackReq.setUpdateTimestamp(LocalDateTime.now());
-
 
493
        // Mark as unread for the retailer when an internal user replies
-
 
494
        if ("INTERNAL".equals(senderType)) {
-
 
495
            callbackReq.setUnread(true);
-
 
496
        }
-
 
497
        callbackRequestRepository.persist(callbackReq);
-
 
498
 
-
 
499
        LOGGER.info("Reply added with id: {} to callback request id: {}", reply.getId(), id);
-
 
500
        return responseSender.ok(true);
-
 
501
    }
-
 
502
 
401
    private ContactDetail buildContact(AuthUser authUser, String badge, String badgeColor,
503
    private ContactDetail buildContact(AuthUser authUser, String badge, String badgeColor,
402
                                       String description, boolean showCall, boolean showMessage,
504
                                       String description, boolean showCall, boolean showMessage,
403
                                       boolean showCallback, boolean expanded) {
505
                                       boolean showCallback, boolean expanded) {
404
        ContactDetail contact = new ContactDetail();
506
        ContactDetail contact = new ContactDetail();
405
        contact.setId(authUser.getId());
507
        contact.setId(authUser.getId());