Subversion Repositories SmartDukaan

Rev

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