Subversion Repositories SmartDukaan

Rev

Rev 37266 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
35337 aman 1
package com.spice.profitmandi.dao.service;
2
 
35360 aman 3
import com.spice.profitmandi.common.model.ProfitMandiConstants;
35642 aman 4
import com.spice.profitmandi.common.services.EmailService;
35360 aman 5
import com.spice.profitmandi.dao.entity.auth.AuthUser;
35337 aman 6
import com.spice.profitmandi.dao.entity.fofo.TrialForm;
37266 ranu 7
import com.spice.profitmandi.dao.entity.user.Lead;
35360 aman 8
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
37266 ranu 9
import com.spice.profitmandi.dao.enumuration.dtr.LeadStatus;
35360 aman 10
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
11
import com.spice.profitmandi.dao.repository.cs.CsService;
37266 ranu 12
import com.spice.profitmandi.dao.repository.dtr.LeadRepository;
35337 aman 13
import com.spice.profitmandi.dao.service.loiForm.LoiFormServiceImpl;
14
import org.apache.logging.log4j.LogManager;
15
import org.apache.logging.log4j.Logger;
16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.mail.javamail.JavaMailSender;
18
import org.springframework.stereotype.Service;
37266 ranu 19
import org.springframework.transaction.annotation.Propagation;
20
import org.springframework.transaction.annotation.Transactional;
35337 aman 21
 
35499 aman 22
import java.time.LocalDate;
37266 ranu 23
import java.time.LocalDateTime;
35360 aman 24
import java.util.ArrayList;
25
import java.util.Arrays;
26
import java.util.List;
27
 
35337 aman 28
@Service
35360 aman 29
public class TrialServiceImpl implements TrialService {
35337 aman 30
 
31
    private static final Logger LOGGER = LogManager.getLogger(LoiFormServiceImpl.class);
32
 
33
    @Autowired
36399 amit 34
    JavaMailSender gmailRelaySender;
35337 aman 35
 
35360 aman 36
    @Autowired
37
    AuthRepository authRepository;
38
 
39
    @Autowired
40
    CsService csService;
41
 
35642 aman 42
    @Autowired
43
    EmailService emailService;
44
 
37266 ranu 45
    @Autowired
46
    LeadRepository leadRepository;
47
 
37729 amit 48
    @Autowired
49
    private com.spice.profitmandi.dao.service.lead.LeadCreationService leadCreationService;
50
 
35337 aman 51
    @Override
52
    public void sentMailForTrialUser(TrialForm trialForm) throws Exception {
53
        String body =
35362 aman 54
                "Dear Team,<br><br>" +
55
                        "A new partner has registered via the SmartDukaan Partners App. Please review the details below and initiate the verification process immediately.<br><br>" +
56
                        "<strong>Applicant Name:</strong> " + trialForm.getFirstName() + " " + trialForm.getLastName() + "<br>" +
57
                        "<strong>Business Name:</strong> " + trialForm.getBusinessName() + "<br>" +
58
                        "<strong>Mobile:</strong> " + trialForm.getMobile() + "<br>" +
59
                        "<strong>Email:</strong> " + trialForm.getEmail() + "<br>" +
60
                        "<strong>GST Number:</strong> " + trialForm.getGstNumber() + "<br><br>" +
61
                        "<strong>Address:</strong><br>" +
62
                        trialForm.getAddressLine1() + "<br>" +
63
                        (trialForm.getAddressLine2() != null ? trialForm.getAddressLine2() + "<br>" : "") +
64
                        trialForm.getCity() + ", " + trialForm.getState() + " - " + trialForm.getPincode() + "<br><br>" +
35499 aman 65
                        "<strong>Date:</strong> " + LocalDate.now() + "<br>" +
35362 aman 66
                        "<strong>Required Actions:</strong><br>" +
35418 aman 67
                        " 1. Verify Details: Validate KYC documents and basic business information.<br>" +
68
                        " 2. Assess Potential: Check the location feasibility and estimated sales potential.<br>" +
69
                        " 3. Assign Territory: Assign the appropriate Area Sales Manager (ASM) and Business Manager (BM) in the system.<br><br>" +
35362 aman 70
                        "Regards,<br>SmartDukaan Team";
35337 aman 71
 
37139 ranu 72
        String[] emailTo = {"kamini.sharma@smartdukaan.com", "rajat.bharadwaj@smartdukaan.com", "akhil.kumar@smartdukaan.com"};
35485 aman 73
        String[] cc = {"vaibhav.tandon@smartdukaan.com", "tarun.verma@smartdukaan.com"};
35337 aman 74
        LOGGER.info("RegistrationBody - " + body);
36399 amit 75
        emailService.sendMailWithAttachments(gmailRelaySender, emailTo, cc, "New Trial Registration: " + trialForm.getBusinessName(), body);
35337 aman 76
 
77
 
78
    }
35360 aman 79
 
35391 aman 80
    public void sentMailToTrialUser(TrialForm trialForm) throws Exception {
81
 
82
        String applicantName = trialForm.getFirstName() +
83
                (trialForm.getLastName() != null ? " " + trialForm.getLastName() : "");
84
 
85
        String body =
86
                "Hi " + applicantName + ",<br><br>" +
35499 aman 87
                        "We have successfully received your <strong>SmartDukaan registration application</strong>.<br><br>" +
35391 aman 88
 
35499 aman 89
                        "<strong>Current Status:</strong> <strong>Verification in Progress</strong><br><br>" +
90
 
91
                        "Your application is currently under review. Before activating your account, our team is verifying the " +
92
                        "documents you submitted, your business potential, and your location. Taaki onboarding bilkul smooth " +
93
                        "aur transparent ho.<br><br>" +
94
 
95
                        "<strong>What Happens Next?</strong><br>" +
36008 aman 96
                        "1. Our team will complete the verification process (approx. 2-3 days).<br>" +
35499 aman 97
                        "2. Once approved, you will receive your <strong>Login ID, Password,</strong> and onboarding details " +
98
                        "on your registered email address.<br>" +
99
                        "3. Simply log in using these details and start enjoying the <strong>SmartDukaan advantage</strong> immediately.<br><br>" +
100
 
101
                        "For any assistance, feel free to contact us:<br>" +
102
                        "<strong>Toll-Free:</strong> 1800-270-0273<br>" +
103
                        "<strong>Email:</strong> care@smartdukaan.com<br><br>" +
104
 
105
                        "Thank you for choosing <strong>SmartDukaan</strong>. We will connect with you shortly.<br><br>" +
106
 
35391 aman 107
                        "Regards,<br>" +
108
                        "Team SmartDukaan";
109
 
35499 aman 110
 
35391 aman 111
        String[] emailTo = {trialForm.getEmail()};
35758 vikas 112
        String[] cc = {"sdtech@smartdukaan.com"}; // tarun.verma@smartdukaan.com
35391 aman 113
        LOGGER.info("RegistrationBody - " + body);
35642 aman 114
        emailService.sendMailWithAttachments(
36399 amit 115
                gmailRelaySender,
35391 aman 116
                emailTo,
117
                cc,
35995 aman 118
                "Application Received - Verification in Progress",
35391 aman 119
                body
120
        );
121
    }
122
 
35360 aman 123
    public void sentMailForTrialUserToSales(TrialForm trialForm) throws Exception {
124
        String body =
35362 aman 125
                "<strong>Sales Team Action:</strong><br>" +
126
                        "A new verified partner lead has been assigned to your territory. They have successfully passed the initial verification stage.<br><br>" +
127
                        "<strong>Lead Information:</strong><br>" +
128
                        "<strong>Applicant Name:</strong> " + trialForm.getFirstName() + " " + trialForm.getLastName() + "<br>" +
129
                        "<strong>Business Name:</strong> " + trialForm.getBusinessName() + "<br>" +
130
                        "<strong>Mobile:</strong> " + trialForm.getMobile() + "<br>" +
131
                        "<strong>Email:</strong> " + trialForm.getEmail() + "<br>" +
132
                        "<strong>GST Number:</strong> " + trialForm.getGstNumber() + "<br><br>" +
133
                        "<strong>Address:</strong><br>" +
134
                        trialForm.getAddressLine1() + "<br>" +
135
                        (trialForm.getAddressLine2() != null ? trialForm.getAddressLine2() + "<br>" : "") +
136
                        trialForm.getCity() + ", " + trialForm.getState() + " - " + trialForm.getPincode() + "<br><br>" +
137
                        "<strong>Strategic Objective:</strong> The partner has expressed initial interest. Your goal is to move them beyond trial onboarding.<br><br>" +
138
                        "<strong>Next Steps:</strong><br>" +
35418 aman 139
                        " 1. Contact Immediately: Call or visit the store within the next 24 hours.<br>" +
140
                        " 2. The Pitch: Explain the SmartDukaan ecosystem and pitch the Full Franchise Partner Model. Emphasize the long-term higher margins, income boost, brand access, tech support, and branding benefits over the trial version.<br>" +
141
                        " 3. Update Status: Log your interaction and the partner's interest level in the app immediately after the meeting.<br><br>" +
35362 aman 142
                        "Good Luck,<br>" +
143
                        "Regards,<br>SmartDukaan Team";
144
 
35360 aman 145
        List<AuthUser> salesAuthUserList = csService.getAuthUserIds(ProfitMandiConstants.TICKET_CATEGORY_SALES, Arrays.asList(EscalationType.L3, EscalationType.L4, EscalationType.L5));
146
        List<String> salesEmailList = new ArrayList<>();
147
        for (AuthUser authUser : salesAuthUserList) {
148
            if (authUser.getEmailId() != null && !authUser.getEmailId().trim().isEmpty()) {
149
                salesEmailList.add(authUser.getEmailId());
150
            }
151
        }
152
        AuthUser bmAuthUser = authRepository.selectById(trialForm.getBmId());
153
        AuthUser asmAuthUser = authRepository.selectById(trialForm.getAsmId());
154
 
155
        String[] emailTo = new String[]{bmAuthUser.getEmailId()};
156
        List<String> ccList = new ArrayList<>(salesEmailList);
157
        ccList.add(asmAuthUser.getEmailId());
35485 aman 158
        ccList.add("tarun.verma@smartdukaan.com");
35360 aman 159
        String[] cc = ccList.stream()
160
                .filter(email -> !email.trim().isEmpty())
161
                .toArray(String[]::new);
162
        LOGGER.info("RegistrationBody - " + body);
163
        LOGGER.info("cc list - " + Arrays.toString(cc));
36399 amit 164
        emailService.sendMailWithAttachments(gmailRelaySender, emailTo, cc, "NEW LEAD ASSIGNED: " + trialForm.getBusinessName() + "- Call/Visit ASAP", body);
35360 aman 165
 
166
 
167
    }
168
 
169
    public void sentMailForStoreCodeCreation(TrialForm trialForm) throws Exception {
170
        String body =
35362 aman 171
                "The trial user has been verified.<br><br>" +
172
                        "Please create Store Code for: <strong>" + trialForm.getBusinessName() + "</strong><br>" +
173
                        "Mobile Number: <strong>" + trialForm.getMobile() + "</strong><br><br>" +
174
                        "Regards,<br>SmartDukaan Team";
175
 
35360 aman 176
        String[] emailTo = {"gaurav.sharma@smartdukaan.com"};
177
        LOGGER.info("RegistrationBody - " + body);
35485 aman 178
        String[] cc = {"devkinandan.lal@smartdukaan.com", "tarun.verma@smartdukaan.com"};
35360 aman 179
        LOGGER.info("cc list - " + Arrays.toString(cc));
36399 amit 180
        emailService.sendMailWithAttachments(gmailRelaySender, emailTo, cc, "Store Code Creation Required for trial user" + trialForm.getBusinessName(), body);
35360 aman 181
 
182
 
183
    }
35391 aman 184
 
185
    public void sentRejectionMailToTrialUser(TrialForm trialForm) throws Exception {
186
 
187
        String applicantName = trialForm.getFirstName() +
188
                (trialForm.getLastName() != null ? " " + trialForm.getLastName() : "");
189
 
190
        String body =
191
                "Dear " + applicantName + ",<br><br>" +
35499 aman 192
                        "After a detailed review of your application, we regret to inform you that your " +
193
                        "<strong>30-Day Free Trial request</strong> cannot be approved based on the current " +
194
                        "eligibility criteria.<br><br>" +
35391 aman 195
 
35499 aman 196
                        "If you would like to understand the reason for this decision or discuss any concerns, " +
197
                        "please feel free to reach out to us at <strong>care@smartdukaan.com</strong> or call our " +
198
                        "Toll-Free number <strong>1800-270-0273</strong>.<br><br>" +
35391 aman 199
 
35499 aman 200
                        "Thank you for applying. We wish you continued success in your business journey. " +
201
                        "Aur agar future mein humari policies change hoti hain, hum aapse zaroor sampark karenge.<br><br>" +
35391 aman 202
 
203
                        "Regards,<br>" +
35499 aman 204
                        "Team SmartDukaan";
35391 aman 205
 
35499 aman 206
 
35391 aman 207
        String[] emailTo = {trialForm.getEmail()};
35758 vikas 208
        String[] cc = {"sdtech@smartdukaan.com"};
35391 aman 209
 
35642 aman 210
        emailService.sendMailWithAttachments(
36399 amit 211
                gmailRelaySender,
35391 aman 212
                emailTo,
213
                cc,
35499 aman 214
                "Update on Your SmartDukaan Free Trial Request",
35391 aman 215
                body
216
        );
217
    }
218
 
219
    public void sendUpgradeRequestMail(TrialForm trialForm) throws Exception {
220
 
221
        String applicantName = trialForm.getFirstName() +
222
                (trialForm.getLastName() != null ? " " + trialForm.getLastName() : "");
223
 
224
        String body =
225
                "Dear " + applicantName + ",<br><br>" +
35499 aman 226
                        "Thank you for expressing your interest in the <strong>SmartDukaan Franchise Partner Program</strong>.<br><br>" +
35391 aman 227
 
35499 aman 228
                        "We have successfully received your request. Our team will connect with you shortly to discuss the " +
229
                        "requirements, eligibility, and the next steps. Taaki hum milkar ek strong aur long-term partnership " +
230
                        "build kar saken.<br><br>" +
35391 aman 231
 
35499 aman 232
                        "We look forward to taking this conversation forward.<br><br>" +
35391 aman 233
 
234
                        "Regards,<br>" +
35499 aman 235
                        "Team SmartDukaan";
35391 aman 236
 
35499 aman 237
 
35391 aman 238
        String[] emailTo = {trialForm.getEmail()};
35758 vikas 239
        String[] cc = {"sdtech@smartdukaan.com"};
35391 aman 240
 
35642 aman 241
        emailService.sendMailWithAttachments(
36399 amit 242
                gmailRelaySender,
35391 aman 243
                emailTo,
244
                cc,
35499 aman 245
                "We’ve Received Your Interest in the SmartDukaan Franchise",
35391 aman 246
                body
247
        );
248
    }
249
 
250
    public void sendInternalFranchiseUpgradeMail(TrialForm trialForm) throws Exception {
251
 
252
        String applicantName = trialForm.getFirstName() +
253
                (trialForm.getLastName() != null ? " " + trialForm.getLastName() : "");
254
 
255
        String body =
256
                "Hi Team,<br><br>" +
257
                        "We have received a new interest request for the <strong>SmartDukaan Franchise Partner upgrade</strong> via the app.<br><br>" +
258
 
259
                        "<strong>Trial User Details:</strong><br>" +
260
                        "Name: " + applicantName + "<br>" +
261
                        "Business Name: " + trialForm.getBusinessName() + "<br>" +
262
                        "Registered Mobile: " + trialForm.getMobile() + "<br>" +
263
                        "User ID: " + trialForm.getId() + "<br>" +
264
                        "Request Date: " + trialForm.getUpdatedOn() + "<br><br>" +
265
 
266
                        "<strong>Action Required:</strong><br>" +
267
                        "Please review the user's profile and initiate the verification/onboarding process immediately.<br><br>" +
268
 
269
                        "Best,<br>" +
270
                        "SmartDukaan System";
35418 aman 271
        List<AuthUser> salesAuthUserList = csService.getAuthUserIds(ProfitMandiConstants.TICKET_CATEGORY_SALES, Arrays.asList(EscalationType.L3, EscalationType.L4, EscalationType.L5));
272
        List<String> salesEmailList = new ArrayList<>();
273
        for (AuthUser authUser : salesAuthUserList) {
274
            if (authUser.getEmailId() != null && !authUser.getEmailId().trim().isEmpty()) {
275
                salesEmailList.add(authUser.getEmailId());
276
            }
277
        }
278
        AuthUser bmAuthUser = authRepository.selectById(trialForm.getBmId());
279
        AuthUser asmAuthUser = authRepository.selectById(trialForm.getAsmId());
35391 aman 280
 
35418 aman 281
        String[] emailTo = new String[]{bmAuthUser.getEmailId()};
282
        List<String> ccList = new ArrayList<>(salesEmailList);
283
        ccList.add(asmAuthUser.getEmailId());
35485 aman 284
        ccList.add("tarun.verma@smartdukaan.com");
35418 aman 285
        String[] cc = ccList.stream()
286
                .filter(email -> !email.trim().isEmpty())
287
                .toArray(String[]::new);
35642 aman 288
        emailService.sendMailWithAttachments(
36399 amit 289
                gmailRelaySender,
35391 aman 290
                emailTo,
291
                cc,
292
                "Franchise Upgrade Request - " + applicantName + ", " + trialForm.getBusinessName(),
293
                body
294
        );
295
    }
296
 
37266 ranu 297
    @Override
298
    @Transactional(propagation = Propagation.REQUIRES_NEW)
299
    public void createLeadAndNotifyBgcForTrial(TrialForm trialForm) {
300
        // REQUIRES_NEW: this add-on runs in an isolated transaction. If the Lead
301
        // persist or the BGC mail fails, the outer trial-registration transaction
302
        // is completely unaffected — no rollback, no session poisoning.
303
        // Step 1 — persist a Lead in lead management. Failure is logged but never
304
        // rolls back the trial registration (called after trialForm is already saved).
305
        try {
306
            Lead existing = null; // no dedup by mobile — a single trial can generate its own lead row
307
            Lead lead = new Lead();
308
            lead.setFirstName(trialForm.getFirstName() != null ? trialForm.getFirstName() : "");
309
            lead.setLastName(trialForm.getLastName() != null ? trialForm.getLastName() : "");
310
            lead.setLeadMobile(trialForm.getMobile());
311
            lead.setOutLetName(trialForm.getBusinessName() != null ? trialForm.getBusinessName() : "");
312
            lead.setCity(trialForm.getCity() != null ? trialForm.getCity() : "");
313
            lead.setState(trialForm.getState() != null ? trialForm.getState() : "");
314
            String addr = (trialForm.getAddressLine1() != null ? trialForm.getAddressLine1() : "")
315
                    + (trialForm.getAddressLine2() != null && !trialForm.getAddressLine2().isEmpty()
316
                    ? ", " + trialForm.getAddressLine2() : "");
317
            lead.setAddress(addr);
318
            lead.setSource("Trial Registration");
319
            lead.setStatus(LeadStatus.pending);
320
            lead.setColor("yellow");
321
            lead.setCreatedBy("system");
322
            lead.setCreatedTimestamp(LocalDateTime.now());
323
            lead.setUpdatedTimestamp(LocalDateTime.now());
37729 amit 324
            // Unattended: a trial signup has no agent to offer an override to. A collision is
325
            // skipped and mailed to the Call Center desk rather than creating a second live lead.
326
            leadCreationService.createUnattended(lead);
37266 ranu 327
            LOGGER.info("Trial-to-Lead created: leadId={} for trialMobile={}", lead.getId(), trialForm.getMobile());
328
        } catch (Exception e) {
329
            LOGGER.error("Failed to create Lead from Trial for mobile=" + trialForm.getMobile(), e);
330
        }
331
 
332
        // Step 2 — email BGC L2 + L3 with tarun.verma + kamini.sharma in CC.
333
        try {
334
            List<AuthUser> bgcUsers = csService.getAuthUserIds(
335
                    ProfitMandiConstants.TICKET_CATEGORY_BGC,
336
                    Arrays.asList(EscalationType.L2, EscalationType.L3));
337
            List<String> bgcEmails = new ArrayList<>();
338
            for (AuthUser u : bgcUsers) {
339
                if (u.getEmailId() != null && !u.getEmailId().trim().isEmpty()) {
340
                    bgcEmails.add(u.getEmailId());
341
                }
342
            }
343
            if (bgcEmails.isEmpty()) {
344
                LOGGER.warn("No BGC L2/L3 users found for trial mobile={} — skipping BGC mail", trialForm.getMobile());
345
                return;
346
            }
347
            String[] emailTo = bgcEmails.toArray(new String[0]);
348
            String[] cc = new String[]{"tarun.verma@smartdukaan.com", "kamini.sharma@smartdukaan.com"};
349
 
350
            String applicantName = trialForm.getFirstName()
351
                    + (trialForm.getLastName() != null ? " " + trialForm.getLastName() : "");
352
            String body =
353
                    "Dear BGC Team,<br><br>" +
354
                            "A new lead has been generated from a trial registration. Please initiate the background check process.<br><br>" +
355
                            "<strong>Applicant Name:</strong> " + applicantName + "<br>" +
356
                            "<strong>Business Name:</strong> " + (trialForm.getBusinessName() != null ? trialForm.getBusinessName() : "-") + "<br>" +
357
                            "<strong>Mobile:</strong> " + trialForm.getMobile() + "<br>" +
358
                            "<strong>Email:</strong> " + (trialForm.getEmail() != null ? trialForm.getEmail() : "-") + "<br>" +
359
                            "<strong>GST Number:</strong> " + (trialForm.getGstNumber() != null ? trialForm.getGstNumber() : "-") + "<br><br>" +
360
                            "<strong>Address:</strong><br>" +
361
                            (trialForm.getAddressLine1() != null ? trialForm.getAddressLine1() + "<br>" : "") +
362
                            (trialForm.getAddressLine2() != null && !trialForm.getAddressLine2().isEmpty() ? trialForm.getAddressLine2() + "<br>" : "") +
363
                            (trialForm.getCity() != null ? trialForm.getCity() : "-") + ", "
364
                            + (trialForm.getState() != null ? trialForm.getState() : "-") + " - "
365
                            + (trialForm.getPincode() != null ? trialForm.getPincode() : "-") + "<br><br>" +
366
                            "<strong>Source:</strong> Trial Registration<br><br>" +
367
                            "Please review and initiate the BGC verification at the earliest.<br><br>" +
368
                            "Regards,<br>SmartDukaan Team";
369
 
370
            emailService.sendMailWithAttachments(gmailRelaySender, emailTo, cc,
371
                    "New Lead for BGC - " + (trialForm.getBusinessName() != null ? trialForm.getBusinessName() : applicantName),
372
                    body);
373
            LOGGER.info("BGC L2/L3 lead-notification mail sent for trial mobile={} to={} cc={}",
374
                    trialForm.getMobile(), Arrays.toString(emailTo), Arrays.toString(cc));
375
        } catch (Exception e) {
376
            LOGGER.error("Failed to send BGC L2/L3 mail for trial mobile=" + trialForm.getMobile(), e);
377
        }
378
    }
379
 
35337 aman 380
}