Subversion Repositories SmartDukaan

Rev

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

Rev 35927 Rev 35948
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.util.ArrayList;
5
import java.util.ArrayList;
5
import java.util.List;
6
import java.util.List;
6
 
7
 
7
import javax.mail.internet.InternetAddress;
8
import javax.mail.internet.InternetAddress;
8
import javax.mail.internet.MimeMessage;
9
import javax.mail.internet.MimeMessage;
Line 15... Line 16...
15
import org.springframework.http.ResponseEntity;
16
import org.springframework.http.ResponseEntity;
16
import org.springframework.mail.javamail.JavaMailSender;
17
import org.springframework.mail.javamail.JavaMailSender;
17
import org.springframework.mail.javamail.MimeMessageHelper;
18
import org.springframework.mail.javamail.MimeMessageHelper;
18
import org.springframework.stereotype.Controller;
19
import org.springframework.stereotype.Controller;
19
import org.springframework.transaction.annotation.Transactional;
20
import org.springframework.transaction.annotation.Transactional;
-
 
21
import org.springframework.web.bind.annotation.PathVariable;
20
import org.springframework.web.bind.annotation.RequestBody;
22
import org.springframework.web.bind.annotation.RequestBody;
21
import org.springframework.web.bind.annotation.RequestMapping;
23
import org.springframework.web.bind.annotation.RequestMapping;
22
import org.springframework.web.bind.annotation.RequestMethod;
24
import org.springframework.web.bind.annotation.RequestMethod;
23
import org.springframework.web.bind.annotation.RequestParam;
25
import org.springframework.web.bind.annotation.RequestParam;
24
 
26
 
Line 27... Line 29...
27
import com.spice.profitmandi.common.web.util.ResponseSender;
29
import com.spice.profitmandi.common.web.util.ResponseSender;
28
import com.spice.profitmandi.dao.entity.auth.AuthUser;
30
import com.spice.profitmandi.dao.entity.auth.AuthUser;
29
import com.spice.profitmandi.dao.entity.dtr.User;
31
import com.spice.profitmandi.dao.entity.dtr.User;
30
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
32
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
31
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
33
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
-
 
34
import com.spice.profitmandi.dao.entity.dtr.Retailer;
-
 
35
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
32
import com.spice.profitmandi.dao.repository.cs.CallbackRequestRepository;
36
import com.spice.profitmandi.dao.repository.cs.CallbackRequestRepository;
33
import com.spice.profitmandi.dao.repository.cs.CsService;
37
import com.spice.profitmandi.dao.repository.cs.CsService;
-
 
38
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
-
 
39
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
34
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
40
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
35
import com.spice.profitmandi.web.req.CallbackRequest;
41
import com.spice.profitmandi.web.req.CallbackRequest;
-
 
42
import com.spice.profitmandi.web.req.ResolveCallbackRequest;
-
 
43
import com.spice.profitmandi.web.res.AssignedRequestResponse;
-
 
44
import com.spice.profitmandi.web.res.CallbackRequestResponse;
36
import com.spice.profitmandi.web.res.SupportTeamResponse;
45
import com.spice.profitmandi.web.res.SupportTeamResponse;
37
import com.spice.profitmandi.web.res.SupportTeamResponse.ContactDetail;
46
import com.spice.profitmandi.web.res.SupportTeamResponse.ContactDetail;
38
 
47
 
39
import io.swagger.annotations.ApiImplicitParam;
48
import io.swagger.annotations.ApiImplicitParam;
40
import io.swagger.annotations.ApiImplicitParams;
49
import io.swagger.annotations.ApiImplicitParams;
Line 62... Line 71...
62
    private JavaMailSender mailSender;
71
    private JavaMailSender mailSender;
63
 
72
 
64
    @Autowired
73
    @Autowired
65
    private CallbackRequestRepository callbackRequestRepository;
74
    private CallbackRequestRepository callbackRequestRepository;
66
 
75
 
-
 
76
    @Autowired
-
 
77
    private RetailerRepository retailerRepository;
-
 
78
 
-
 
79
    @Autowired
-
 
80
    private FofoStoreRepository fofoStoreRepository;
-
 
81
 
67
    @RequestMapping(value = "/support/team", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
82
    @RequestMapping(value = "/support/team", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
68
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
83
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
69
    @ApiOperation(value = "Get assigned support team for a FOFO store grouped by escalation level")
84
    @ApiOperation(value = "Get assigned support team for a FOFO store grouped by escalation level")
70
    public ResponseEntity<?> getSupportTeam(HttpServletRequest request, @RequestParam(value = "fofoId") int fofoId) throws Throwable {
85
    public ResponseEntity<?> getSupportTeam(HttpServletRequest request, @RequestParam(value = "fofoId") int fofoId) throws Throwable {
71
        LOGGER.info("Getting support team for fofoId: {}", fofoId);
86
        LOGGER.info("Getting support team for fofoId: {}", fofoId);
Line 129... Line 144...
129
            int bmAuthUserId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L2, fofoId);
144
            int bmAuthUserId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L2, fofoId);
130
            if (bmAuthUserId > 0) {
145
            if (bmAuthUserId > 0) {
131
                AuthUser user = authRepository.selectById(bmAuthUserId);
146
                AuthUser user = authRepository.selectById(bmAuthUserId);
132
                if (user != null) {
147
                if (user != null) {
133
                    l2Contacts.add(buildContact(user, "BM", "#1e6091",
148
                    l2Contacts.add(buildContact(user, "BM", "#1e6091",
134
                            "Handles: Orders, Stock, Schemes", false, false, true, false));
149
                            "Handles: Orders, Stock, Schemes", true, false, false, true));
135
                }
150
                }
136
            }
151
            }
137
        } catch (Exception e) {
152
        } catch (Exception e) {
138
            LOGGER.warn("Could not fetch BM for fofoId: {}", fofoId, e);
153
            LOGGER.warn("Could not fetch BM for fofoId: {}", fofoId, e);
139
        }
154
        }
Line 142... Line 157...
142
        try {
157
        try {
143
            int affordAuthUserId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_FINANCIAL_SERVICES, EscalationType.L2, fofoId);
158
            int affordAuthUserId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_FINANCIAL_SERVICES, EscalationType.L2, fofoId);
144
            if (affordAuthUserId > 0) {
159
            if (affordAuthUserId > 0) {
145
                AuthUser user = authRepository.selectById(affordAuthUserId);
160
                AuthUser user = authRepository.selectById(affordAuthUserId);
146
                if (user != null) {
161
                if (user != null) {
147
                    l2Contacts.add(buildContact(user, "AFFORDABILITY & OPERATIONS", "#d97706",
162
                    l2Contacts.add(buildContact(user, "Finance Partner Mapping", "#d97706",
148
                            "Credit, payment plans, and operations", false, false, true, false));
163
                            "Credit, payment plans, and operations", false, false, true, false));
149
                }
164
                }
150
            }
165
            }
151
        } catch (Exception e) {
166
        } catch (Exception e) {
152
            LOGGER.warn("Could not fetch Affordability Manager for fofoId: {}", fofoId, e);
167
            LOGGER.warn("Could not fetch Affordability Manager for fofoId: {}", fofoId, e);
Line 263... Line 278...
263
        LOGGER.info("Callback request email sent to: {}", contactUser.getEmailId());
278
        LOGGER.info("Callback request email sent to: {}", contactUser.getEmailId());
264
 
279
 
265
        return responseSender.ok(true);
280
        return responseSender.ok(true);
266
    }
281
    }
267
 
282
 
-
 
283
    @RequestMapping(value = "/support/callback-requests", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
-
 
284
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
-
 
285
    @ApiOperation(value = "Get callback requests for the logged-in fofo associate")
-
 
286
    public ResponseEntity<?> getCallbackRequests(HttpServletRequest request) throws Throwable {
-
 
287
        UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
-
 
288
        int fofoId = userInfo.getRetailerId();
-
 
289
        LOGGER.info("Getting callback requests for fofoId: {}", fofoId);
-
 
290
 
-
 
291
        List<com.spice.profitmandi.dao.entity.cs.CallbackRequest> callbackRequests = callbackRequestRepository.selectAllByFofoId(fofoId);
-
 
292
 
-
 
293
        List<CallbackRequestResponse> responseList = new ArrayList<>();
-
 
294
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM yyyy, hh:mm a");
-
 
295
 
-
 
296
        for (com.spice.profitmandi.dao.entity.cs.CallbackRequest entity : callbackRequests) {
-
 
297
            CallbackRequestResponse res = new CallbackRequestResponse();
-
 
298
            res.setId(entity.getId());
-
 
299
            res.setContactId(entity.getContactId());
-
 
300
            res.setContactName(entity.getContactName());
-
 
301
            res.setContactInitials(getInitials(entity.getContactName()));
-
 
302
            res.setContactRole(entity.getContactRole());
-
 
303
            res.setSubject(entity.getSubject());
-
 
304
            res.setMessage(entity.getMessage());
-
 
305
            res.setTiming(entity.getTiming());
-
 
306
            res.setTimingLabel("11-15".equals(entity.getTiming()) ? "11:00 AM - 3:00 PM" : "3:00 PM - 7:00 PM");
-
 
307
            res.setStatus(entity.getStatus());
-
 
308
            if (entity.getCreateTimestamp() != null) {
-
 
309
                res.setCreateTimestamp(entity.getCreateTimestamp().format(formatter));
-
 
310
            }
-
 
311
            responseList.add(res);
-
 
312
        }
-
 
313
 
-
 
314
        return responseSender.ok(responseList);
-
 
315
    }
-
 
316
 
-
 
317
    @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")})
-
 
319
    @ApiOperation(value = "Get callback requests assigned to the logged-in internal user")
-
 
320
    public ResponseEntity<?> getAssignedRequests(HttpServletRequest request) throws Throwable {
-
 
321
        UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
-
 
322
        int contactId = userInfo.getUserId();
-
 
323
        LOGGER.info("Getting assigned requests for contactId: {}", contactId);
-
 
324
 
-
 
325
        List<com.spice.profitmandi.dao.entity.cs.CallbackRequest> callbackRequests = callbackRequestRepository.selectAllByContactId(contactId);
-
 
326
 
-
 
327
        List<AssignedRequestResponse> responseList = new ArrayList<>();
-
 
328
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM yyyy, hh:mm a");
-
 
329
 
-
 
330
        for (com.spice.profitmandi.dao.entity.cs.CallbackRequest entity : callbackRequests) {
-
 
331
            AssignedRequestResponse res = new AssignedRequestResponse();
-
 
332
            res.setId(entity.getId());
-
 
333
            res.setSubject(entity.getSubject());
-
 
334
            res.setMessage(entity.getMessage());
-
 
335
            res.setTiming(entity.getTiming());
-
 
336
            res.setTimingLabel("11-15".equals(entity.getTiming()) ? "11:00 AM - 3:00 PM" : "3:00 PM - 7:00 PM");
-
 
337
            res.setStatus(entity.getStatus());
-
 
338
            res.setResolutionNotes(entity.getResolutionNotes());
-
 
339
 
-
 
340
            if (entity.getCreateTimestamp() != null) {
-
 
341
                res.setCreateTimestamp(entity.getCreateTimestamp().format(formatter));
-
 
342
            }
-
 
343
            if (entity.getUpdateTimestamp() != null) {
-
 
344
                res.setUpdateTimestamp(entity.getUpdateTimestamp().format(formatter));
-
 
345
            }
-
 
346
 
-
 
347
            // Fetch retailer info
-
 
348
            try {
-
 
349
                User retailerUser = userRepository.selectById(entity.getUserId());
-
 
350
                if (retailerUser != null) {
-
 
351
                    res.setRetailerName(retailerUser.getFirstName());
-
 
352
                    res.setRetailerPhone(retailerUser.getMobileNumber());
-
 
353
                }
-
 
354
            } catch (Exception e) {
-
 
355
                LOGGER.warn("Could not fetch user for userId: {}", entity.getUserId(), e);
-
 
356
            }
-
 
357
 
-
 
358
            // Fetch store info
-
 
359
            try {
-
 
360
                Retailer retailer = retailerRepository.selectById(entity.getFofoId());
-
 
361
                if (retailer != null) {
-
 
362
                    res.setStoreName(retailer.getName());
-
 
363
                }
-
 
364
                FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(entity.getFofoId());
-
 
365
                if (fofoStore != null) {
-
 
366
                    res.setStoreCode(fofoStore.getCode());
-
 
367
                }
-
 
368
            } catch (Exception e) {
-
 
369
                LOGGER.warn("Could not fetch store info for fofoId: {}", entity.getFofoId(), e);
-
 
370
            }
-
 
371
 
-
 
372
            responseList.add(res);
-
 
373
        }
-
 
374
 
-
 
375
        return responseSender.ok(responseList);
-
 
376
    }
-
 
377
 
-
 
378
    @RequestMapping(value = "/support/assigned-requests/{id}/resolve", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
-
 
379
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
-
 
380
    @ApiOperation(value = "Update status and resolution notes for a callback request")
-
 
381
    public ResponseEntity<?> resolveAssignedRequest(HttpServletRequest request, @PathVariable("id") int id,
-
 
382
                                                     @RequestBody ResolveCallbackRequest resolveRequest) throws Throwable {
-
 
383
        UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
-
 
384
        LOGGER.info("Resolving callback request id: {} by userId: {} status: {}", id, userInfo.getUserId(), resolveRequest.getStatus());
-
 
385
 
-
 
386
        com.spice.profitmandi.dao.entity.cs.CallbackRequest entity = callbackRequestRepository.selectById(id);
-
 
387
        if (entity == null) {
-
 
388
            LOGGER.warn("Callback request not found for id: {}", id);
-
 
389
            return responseSender.ok(false);
-
 
390
        }
-
 
391
 
-
 
392
        entity.setStatus(resolveRequest.getStatus());
-
 
393
        entity.setResolutionNotes(resolveRequest.getResolutionNotes());
-
 
394
        entity.setUpdateTimestamp(LocalDateTime.now());
-
 
395
        callbackRequestRepository.persist(entity);
-
 
396
 
-
 
397
        LOGGER.info("Callback request id: {} resolved with status: {}", id, resolveRequest.getStatus());
-
 
398
        return responseSender.ok(true);
-
 
399
    }
-
 
400
 
268
    private ContactDetail buildContact(AuthUser authUser, String badge, String badgeColor,
401
    private ContactDetail buildContact(AuthUser authUser, String badge, String badgeColor,
269
                                       String description, boolean showCall, boolean showMessage,
402
                                       String description, boolean showCall, boolean showMessage,
270
                                       boolean showCallback, boolean expanded) {
403
                                       boolean showCallback, boolean expanded) {
271
        ContactDetail contact = new ContactDetail();
404
        ContactDetail contact = new ContactDetail();
272
        contact.setId(authUser.getId());
405
        contact.setId(authUser.getId());
Line 294... Line 427...
294
        if (lastName != null && !lastName.isEmpty()) {
427
        if (lastName != null && !lastName.isEmpty()) {
295
            initials.append(lastName.charAt(0));
428
            initials.append(lastName.charAt(0));
296
        }
429
        }
297
        return initials.toString().toUpperCase();
430
        return initials.toString().toUpperCase();
298
    }
431
    }
-
 
432
 
-
 
433
    private String getInitials(String fullName) {
-
 
434
        if (fullName == null || fullName.isEmpty()) {
-
 
435
            return "";
-
 
436
        }
-
 
437
        String[] parts = fullName.trim().split("\\s+");
-
 
438
        StringBuilder initials = new StringBuilder();
-
 
439
        initials.append(parts[0].charAt(0));
-
 
440
        if (parts.length > 1) {
-
 
441
            initials.append(parts[parts.length - 1].charAt(0));
-
 
442
        }
-
 
443
        return initials.toString().toUpperCase();
-
 
444
    }
299
}
445
}