Subversion Repositories SmartDukaan

Rev

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

Rev 37139 Rev 37266
Line 2... Line 2...
2
 
2
 
3
import com.spice.profitmandi.common.model.ProfitMandiConstants;
3
import com.spice.profitmandi.common.model.ProfitMandiConstants;
4
import com.spice.profitmandi.common.services.EmailService;
4
import com.spice.profitmandi.common.services.EmailService;
5
import com.spice.profitmandi.dao.entity.auth.AuthUser;
5
import com.spice.profitmandi.dao.entity.auth.AuthUser;
6
import com.spice.profitmandi.dao.entity.fofo.TrialForm;
6
import com.spice.profitmandi.dao.entity.fofo.TrialForm;
-
 
7
import com.spice.profitmandi.dao.entity.user.Lead;
7
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
8
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
-
 
9
import com.spice.profitmandi.dao.enumuration.dtr.LeadStatus;
8
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
10
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
9
import com.spice.profitmandi.dao.repository.cs.CsService;
11
import com.spice.profitmandi.dao.repository.cs.CsService;
-
 
12
import com.spice.profitmandi.dao.repository.dtr.LeadRepository;
10
import com.spice.profitmandi.dao.service.loiForm.LoiFormServiceImpl;
13
import com.spice.profitmandi.dao.service.loiForm.LoiFormServiceImpl;
11
import org.apache.logging.log4j.LogManager;
14
import org.apache.logging.log4j.LogManager;
12
import org.apache.logging.log4j.Logger;
15
import org.apache.logging.log4j.Logger;
13
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.mail.javamail.JavaMailSender;
17
import org.springframework.mail.javamail.JavaMailSender;
15
import org.springframework.stereotype.Service;
18
import org.springframework.stereotype.Service;
-
 
19
import org.springframework.transaction.annotation.Propagation;
-
 
20
import org.springframework.transaction.annotation.Transactional;
16
 
21
 
17
import java.time.LocalDate;
22
import java.time.LocalDate;
-
 
23
import java.time.LocalDateTime;
18
import java.util.ArrayList;
24
import java.util.ArrayList;
19
import java.util.Arrays;
25
import java.util.Arrays;
20
import java.util.List;
26
import java.util.List;
21
 
27
 
22
@Service
28
@Service
Line 34... Line 40...
34
    CsService csService;
40
    CsService csService;
35
 
41
 
36
    @Autowired
42
    @Autowired
37
    EmailService emailService;
43
    EmailService emailService;
38
 
44
 
-
 
45
    @Autowired
-
 
46
    LeadRepository leadRepository;
-
 
47
 
39
    @Override
48
    @Override
40
    public void sentMailForTrialUser(TrialForm trialForm) throws Exception {
49
    public void sentMailForTrialUser(TrialForm trialForm) throws Exception {
41
        String body =
50
        String body =
42
                "Dear Team,<br><br>" +
51
                "Dear Team,<br><br>" +
43
                        "A new partner has registered via the SmartDukaan Partners App. Please review the details below and initiate the verification process immediately.<br><br>" +
52
                        "A new partner has registered via the SmartDukaan Partners App. Please review the details below and initiate the verification process immediately.<br><br>" +
Line 280... Line 289...
280
                "Franchise Upgrade Request - " + applicantName + ", " + trialForm.getBusinessName(),
289
                "Franchise Upgrade Request - " + applicantName + ", " + trialForm.getBusinessName(),
281
                body
290
                body
282
        );
291
        );
283
    }
292
    }
284
 
293
 
-
 
294
    @Override
-
 
295
    @Transactional(propagation = Propagation.REQUIRES_NEW)
-
 
296
    public void createLeadAndNotifyBgcForTrial(TrialForm trialForm) {
-
 
297
        // REQUIRES_NEW: this add-on runs in an isolated transaction. If the Lead
-
 
298
        // persist or the BGC mail fails, the outer trial-registration transaction
-
 
299
        // is completely unaffected — no rollback, no session poisoning.
-
 
300
        // Step 1 — persist a Lead in lead management. Failure is logged but never
-
 
301
        // rolls back the trial registration (called after trialForm is already saved).
-
 
302
        try {
-
 
303
            Lead existing = null; // no dedup by mobile — a single trial can generate its own lead row
-
 
304
            Lead lead = new Lead();
-
 
305
            lead.setFirstName(trialForm.getFirstName() != null ? trialForm.getFirstName() : "");
-
 
306
            lead.setLastName(trialForm.getLastName() != null ? trialForm.getLastName() : "");
-
 
307
            lead.setLeadMobile(trialForm.getMobile());
-
 
308
            lead.setOutLetName(trialForm.getBusinessName() != null ? trialForm.getBusinessName() : "");
-
 
309
            lead.setCity(trialForm.getCity() != null ? trialForm.getCity() : "");
-
 
310
            lead.setState(trialForm.getState() != null ? trialForm.getState() : "");
-
 
311
            String addr = (trialForm.getAddressLine1() != null ? trialForm.getAddressLine1() : "")
-
 
312
                    + (trialForm.getAddressLine2() != null && !trialForm.getAddressLine2().isEmpty()
-
 
313
                    ? ", " + trialForm.getAddressLine2() : "");
-
 
314
            lead.setAddress(addr);
-
 
315
            lead.setSource("Trial Registration");
-
 
316
            lead.setStatus(LeadStatus.pending);
-
 
317
            lead.setColor("yellow");
-
 
318
            lead.setCreatedBy("system");
-
 
319
            lead.setCreatedTimestamp(LocalDateTime.now());
-
 
320
            lead.setUpdatedTimestamp(LocalDateTime.now());
-
 
321
            leadRepository.persist(lead);
-
 
322
            LOGGER.info("Trial-to-Lead created: leadId={} for trialMobile={}", lead.getId(), trialForm.getMobile());
-
 
323
        } catch (Exception e) {
-
 
324
            LOGGER.error("Failed to create Lead from Trial for mobile=" + trialForm.getMobile(), e);
-
 
325
        }
-
 
326
 
-
 
327
        // Step 2 — email BGC L2 + L3 with tarun.verma + kamini.sharma in CC.
-
 
328
        try {
-
 
329
            List<AuthUser> bgcUsers = csService.getAuthUserIds(
-
 
330
                    ProfitMandiConstants.TICKET_CATEGORY_BGC,
-
 
331
                    Arrays.asList(EscalationType.L2, EscalationType.L3));
-
 
332
            List<String> bgcEmails = new ArrayList<>();
-
 
333
            for (AuthUser u : bgcUsers) {
-
 
334
                if (u.getEmailId() != null && !u.getEmailId().trim().isEmpty()) {
-
 
335
                    bgcEmails.add(u.getEmailId());
-
 
336
                }
-
 
337
            }
-
 
338
            if (bgcEmails.isEmpty()) {
-
 
339
                LOGGER.warn("No BGC L2/L3 users found for trial mobile={} — skipping BGC mail", trialForm.getMobile());
-
 
340
                return;
-
 
341
            }
-
 
342
            String[] emailTo = bgcEmails.toArray(new String[0]);
-
 
343
            String[] cc = new String[]{"tarun.verma@smartdukaan.com", "kamini.sharma@smartdukaan.com"};
-
 
344
 
-
 
345
            String applicantName = trialForm.getFirstName()
-
 
346
                    + (trialForm.getLastName() != null ? " " + trialForm.getLastName() : "");
-
 
347
            String body =
-
 
348
                    "Dear BGC Team,<br><br>" +
-
 
349
                            "A new lead has been generated from a trial registration. Please initiate the background check process.<br><br>" +
-
 
350
                            "<strong>Applicant Name:</strong> " + applicantName + "<br>" +
-
 
351
                            "<strong>Business Name:</strong> " + (trialForm.getBusinessName() != null ? trialForm.getBusinessName() : "-") + "<br>" +
-
 
352
                            "<strong>Mobile:</strong> " + trialForm.getMobile() + "<br>" +
-
 
353
                            "<strong>Email:</strong> " + (trialForm.getEmail() != null ? trialForm.getEmail() : "-") + "<br>" +
-
 
354
                            "<strong>GST Number:</strong> " + (trialForm.getGstNumber() != null ? trialForm.getGstNumber() : "-") + "<br><br>" +
-
 
355
                            "<strong>Address:</strong><br>" +
-
 
356
                            (trialForm.getAddressLine1() != null ? trialForm.getAddressLine1() + "<br>" : "") +
-
 
357
                            (trialForm.getAddressLine2() != null && !trialForm.getAddressLine2().isEmpty() ? trialForm.getAddressLine2() + "<br>" : "") +
-
 
358
                            (trialForm.getCity() != null ? trialForm.getCity() : "-") + ", "
-
 
359
                            + (trialForm.getState() != null ? trialForm.getState() : "-") + " - "
-
 
360
                            + (trialForm.getPincode() != null ? trialForm.getPincode() : "-") + "<br><br>" +
-
 
361
                            "<strong>Source:</strong> Trial Registration<br><br>" +
-
 
362
                            "Please review and initiate the BGC verification at the earliest.<br><br>" +
-
 
363
                            "Regards,<br>SmartDukaan Team";
-
 
364
 
-
 
365
            emailService.sendMailWithAttachments(gmailRelaySender, emailTo, cc,
-
 
366
                    "New Lead for BGC - " + (trialForm.getBusinessName() != null ? trialForm.getBusinessName() : applicantName),
-
 
367
                    body);
-
 
368
            LOGGER.info("BGC L2/L3 lead-notification mail sent for trial mobile={} to={} cc={}",
-
 
369
                    trialForm.getMobile(), Arrays.toString(emailTo), Arrays.toString(cc));
-
 
370
        } catch (Exception e) {
-
 
371
            LOGGER.error("Failed to send BGC L2/L3 mail for trial mobile=" + trialForm.getMobile(), e);
-
 
372
        }
-
 
373
    }
-
 
374
 
285
}
375
}