Subversion Repositories SmartDukaan

Rev

Rev 36792 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 36792 Rev 36818
Line 156... Line 156...
156
 
156
 
157
    @Autowired
157
    @Autowired
158
    private BeatRepository beatRepository;
158
    private BeatRepository beatRepository;
159
 
159
 
160
    @Autowired
160
    @Autowired
-
 
161
    private org.springframework.mail.javamail.JavaMailSender gmailRelaySender;
-
 
162
 
-
 
163
    @Autowired
161
    private Mongo mongoClient;
164
    private Mongo mongoClient;
162
 
165
 
163
    @Autowired
166
    @Autowired
164
    private ResponseSender<?> responseSender;
167
    private ResponseSender<?> responseSender;
165
 
168
 
Line 1069... Line 1072...
1069
        model.addAttribute("authIdAndAuthUserMap", authIdAndAuthUserMap);
1072
        model.addAttribute("authIdAndAuthUserMap", authIdAndAuthUserMap);
1070
 
1073
 
1071
 
1074
 
1072
        model.addAttribute("lead", lead);
1075
        model.addAttribute("lead", lead);
1073
 
1076
 
-
 
1077
        // Best-effort hierarchy email — fired after the lead+activity are persisted
-
 
1078
        // so a mail failure never blocks the assignment. Covers every editLead path
-
 
1079
        // (followUp, notInterested, etc.) so every assignment is announced upward.
-
 
1080
        try {
-
 
1081
            sendLeadAssignmentEmail(lead, AuthUser, authUser,
-
 
1082
                    createLeadActivityRequest.getStatus() != null ? createLeadActivityRequest.getStatus().toString() : null);
-
 
1083
        } catch (Exception e) {
-
 
1084
            LOGGER.warn("Lead-assignment email fan-out failed (lead id {}): {}", lead.getId(), e.getMessage());
-
 
1085
        }
-
 
1086
 
1074
        return "edit-lead";
1087
        return "edit-lead";
1075
 
1088
 
1076
    }
1089
    }
1077
 
1090
 
-
 
1091
    // Hierarchy-wide email when a lead is assigned. Recipients:
-
 
1092
    //   - the assignee themselves (L1)
-
 
1093
    //   - every manager above the assignee, capped at L4
-
 
1094
    // L5 / Final / super-admins are intentionally NOT copied on lead-management mails.
-
 
1095
    private void sendLeadAssignmentEmail(Lead lead, AuthUser assignee, AuthUser requestedBy, String status) {
-
 
1096
        if (gmailRelaySender == null || lead == null || assignee == null) return;
-
 
1097
 
-
 
1098
        java.util.LinkedHashSet<String> recipients = new java.util.LinkedHashSet<>();
-
 
1099
        if (assignee.getEmailId() != null && !assignee.getEmailId().isEmpty()) {
-
 
1100
            recipients.add(assignee.getEmailId());
-
 
1101
        }
-
 
1102
        try {
-
 
1103
            for (AuthUser m : authService.getAllManagers(assignee.getId())) {
-
 
1104
                if (m.getEmailId() == null || m.getEmailId().isEmpty()) continue;
-
 
1105
                // Cap chain at L4 — L5 / Final never receive lead-management mails.
-
 
1106
                com.spice.profitmandi.dao.enumuration.cs.EscalationType lvl = highestEscalation(m.getId());
-
 
1107
                if (lvl != null
-
 
1108
                        && com.spice.profitmandi.dao.enumuration.cs.EscalationType.L4.isGreaterThanEqualTo(lvl)) {
-
 
1109
                    recipients.add(m.getEmailId());
-
 
1110
                }
-
 
1111
            }
-
 
1112
        } catch (Exception ignored) {
-
 
1113
        }
-
 
1114
        if (recipients.isEmpty()) return;
-
 
1115
 
-
 
1116
        // TEST OVERRIDE — uncomment to route all lead-assignment mail to one inbox.
-
 
1117
        // recipients.clear();
-
 
1118
        // recipients.add("ranu.rajput@smartdukaan.com");
-
 
1119
 
-
 
1120
        String leadName = ((lead.getFirstName() != null ? lead.getFirstName() : "")
-
 
1121
                + " " + (lead.getLastName() != null ? lead.getLastName() : "")).trim();
-
 
1122
        if (leadName.isEmpty()) leadName = "Lead #" + lead.getId();
-
 
1123
        String assigneeName = ((assignee.getFirstName() != null ? assignee.getFirstName() : "")
-
 
1124
                + " " + (assignee.getLastName() != null ? assignee.getLastName() : "")).trim();
-
 
1125
 
-
 
1126
        String subject = "Lead assigned — " + leadName + " → " + assigneeName;
-
 
1127
        StringBuilder text = new StringBuilder();
-
 
1128
        text.append("A lead has been assigned.\n\n");
-
 
1129
        text.append("Lead:        ").append(leadName).append("\n");
-
 
1130
        if (lead.getLeadMobile() != null) text.append("Mobile:      ").append(lead.getLeadMobile()).append("\n");
-
 
1131
        if (lead.getOutLetName() != null) text.append("Outlet:      ").append(lead.getOutLetName()).append("\n");
-
 
1132
        if (lead.getCity() != null || lead.getState() != null) {
-
 
1133
            text.append("Location:    ")
-
 
1134
                    .append(lead.getCity() != null ? lead.getCity() : "")
-
 
1135
                    .append(lead.getCity() != null && lead.getState() != null ? ", " : "")
-
 
1136
                    .append(lead.getState() != null ? lead.getState() : "")
-
 
1137
                    .append("\n");
-
 
1138
        }
-
 
1139
        text.append("Assignee:    ").append(assigneeName).append("\n");
-
 
1140
        if (status != null) text.append("Status:      ").append(status).append("\n");
-
 
1141
        if (requestedBy != null) {
-
 
1142
            String rbName = ((requestedBy.getFirstName() != null ? requestedBy.getFirstName() : "")
-
 
1143
                    + " " + (requestedBy.getLastName() != null ? requestedBy.getLastName() : "")).trim();
-
 
1144
            text.append("Assigned by: ").append(rbName.isEmpty() ? ("#" + requestedBy.getId()) : rbName).append("\n");
-
 
1145
        }
-
 
1146
 
-
 
1147
        try {
-
 
1148
            javax.mail.internet.MimeMessage message = gmailRelaySender.createMimeMessage();
-
 
1149
            org.springframework.mail.javamail.MimeMessageHelper helper = new org.springframework.mail.javamail.MimeMessageHelper(message, true);
-
 
1150
            helper.setSubject(subject);
-
 
1151
            helper.setText(text.toString());
-
 
1152
            helper.setTo(recipients.toArray(new String[0]));
-
 
1153
            helper.setFrom(new javax.mail.internet.InternetAddress("sdtech@smartdukaan.com", "SD Tech"));
-
 
1154
            gmailRelaySender.send(message);
-
 
1155
        } catch (Exception e) {
-
 
1156
            LOGGER.warn("Lead-assignment mail send failed: {}", e.getMessage());
-
 
1157
        }
-
 
1158
    }
-
 
1159
 
-
 
1160
    // Returns the user's highest escalation across all positions (or null).
-
 
1161
    // Used to gate the chain-mail cap.
-
 
1162
    private com.spice.profitmandi.dao.enumuration.cs.EscalationType highestEscalation(int authUserId) {
-
 
1163
        com.spice.profitmandi.dao.enumuration.cs.EscalationType highest = null;
-
 
1164
        try {
-
 
1165
            for (com.spice.profitmandi.dao.entity.cs.Position p : positionRepository.selectPositionByAuthId(authUserId)) {
-
 
1166
                if (highest == null || p.getEscalationType().isGreaterThanEqualTo(highest)) {
-
 
1167
                    highest = p.getEscalationType();
-
 
1168
                }
-
 
1169
            }
-
 
1170
        } catch (Exception ignored) {
-
 
1171
        }
-
 
1172
        return highest;
-
 
1173
    }
-
 
1174
 
1078
    @RequestMapping(value = "/downloadIvoryLead", method = RequestMethod.GET)
1175
    @RequestMapping(value = "/downloadIvoryLead", method = RequestMethod.GET)
1079
    public ResponseEntity<?> downloadDelayDayTemplate(HttpServletRequest request) throws Exception {
1176
    public ResponseEntity<?> downloadDelayDayTemplate(HttpServletRequest request) throws Exception {
1080
        List<String> stateNames = stateRepository.selectAll().stream().map(x -> x.getName()).collect(Collectors.toList());
1177
        List<String> stateNames = stateRepository.selectAll().stream().map(x -> x.getName()).collect(Collectors.toList());
1081
 
1178
 
1082
        List<List<?>> rows = new ArrayList<>();
1179
        List<List<?>> rows = new ArrayList<>();