Subversion Repositories SmartDukaan

Rev

Rev 37139 | Rev 37729 | 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
 
35337 aman 48
    @Override
49
    public void sentMailForTrialUser(TrialForm trialForm) throws Exception {
50
        String body =
35362 aman 51
                "Dear Team,<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>" +
53
                        "<strong>Applicant Name:</strong> " + trialForm.getFirstName() + " " + trialForm.getLastName() + "<br>" +
54
                        "<strong>Business Name:</strong> " + trialForm.getBusinessName() + "<br>" +
55
                        "<strong>Mobile:</strong> " + trialForm.getMobile() + "<br>" +
56
                        "<strong>Email:</strong> " + trialForm.getEmail() + "<br>" +
57
                        "<strong>GST Number:</strong> " + trialForm.getGstNumber() + "<br><br>" +
58
                        "<strong>Address:</strong><br>" +
59
                        trialForm.getAddressLine1() + "<br>" +
60
                        (trialForm.getAddressLine2() != null ? trialForm.getAddressLine2() + "<br>" : "") +
61
                        trialForm.getCity() + ", " + trialForm.getState() + " - " + trialForm.getPincode() + "<br><br>" +
35499 aman 62
                        "<strong>Date:</strong> " + LocalDate.now() + "<br>" +
35362 aman 63
                        "<strong>Required Actions:</strong><br>" +
35418 aman 64
                        " 1. Verify Details: Validate KYC documents and basic business information.<br>" +
65
                        " 2. Assess Potential: Check the location feasibility and estimated sales potential.<br>" +
66
                        " 3. Assign Territory: Assign the appropriate Area Sales Manager (ASM) and Business Manager (BM) in the system.<br><br>" +
35362 aman 67
                        "Regards,<br>SmartDukaan Team";
35337 aman 68
 
37139 ranu 69
        String[] emailTo = {"kamini.sharma@smartdukaan.com", "rajat.bharadwaj@smartdukaan.com", "akhil.kumar@smartdukaan.com"};
35485 aman 70
        String[] cc = {"vaibhav.tandon@smartdukaan.com", "tarun.verma@smartdukaan.com"};
35337 aman 71
        LOGGER.info("RegistrationBody - " + body);
36399 amit 72
        emailService.sendMailWithAttachments(gmailRelaySender, emailTo, cc, "New Trial Registration: " + trialForm.getBusinessName(), body);
35337 aman 73
 
74
 
75
    }
35360 aman 76
 
35391 aman 77
    public void sentMailToTrialUser(TrialForm trialForm) throws Exception {
78
 
79
        String applicantName = trialForm.getFirstName() +
80
                (trialForm.getLastName() != null ? " " + trialForm.getLastName() : "");
81
 
82
        String body =
83
                "Hi " + applicantName + ",<br><br>" +
35499 aman 84
                        "We have successfully received your <strong>SmartDukaan registration application</strong>.<br><br>" +
35391 aman 85
 
35499 aman 86
                        "<strong>Current Status:</strong> <strong>Verification in Progress</strong><br><br>" +
87
 
88
                        "Your application is currently under review. Before activating your account, our team is verifying the " +
89
                        "documents you submitted, your business potential, and your location. Taaki onboarding bilkul smooth " +
90
                        "aur transparent ho.<br><br>" +
91
 
92
                        "<strong>What Happens Next?</strong><br>" +
36008 aman 93
                        "1. Our team will complete the verification process (approx. 2-3 days).<br>" +
35499 aman 94
                        "2. Once approved, you will receive your <strong>Login ID, Password,</strong> and onboarding details " +
95
                        "on your registered email address.<br>" +
96
                        "3. Simply log in using these details and start enjoying the <strong>SmartDukaan advantage</strong> immediately.<br><br>" +
97
 
98
                        "For any assistance, feel free to contact us:<br>" +
99
                        "<strong>Toll-Free:</strong> 1800-270-0273<br>" +
100
                        "<strong>Email:</strong> care@smartdukaan.com<br><br>" +
101
 
102
                        "Thank you for choosing <strong>SmartDukaan</strong>. We will connect with you shortly.<br><br>" +
103
 
35391 aman 104
                        "Regards,<br>" +
105
                        "Team SmartDukaan";
106
 
35499 aman 107
 
35391 aman 108
        String[] emailTo = {trialForm.getEmail()};
35758 vikas 109
        String[] cc = {"sdtech@smartdukaan.com"}; // tarun.verma@smartdukaan.com
35391 aman 110
        LOGGER.info("RegistrationBody - " + body);
35642 aman 111
        emailService.sendMailWithAttachments(
36399 amit 112
                gmailRelaySender,
35391 aman 113
                emailTo,
114
                cc,
35995 aman 115
                "Application Received - Verification in Progress",
35391 aman 116
                body
117
        );
118
    }
119
 
35360 aman 120
    public void sentMailForTrialUserToSales(TrialForm trialForm) throws Exception {
121
        String body =
35362 aman 122
                "<strong>Sales Team Action:</strong><br>" +
123
                        "A new verified partner lead has been assigned to your territory. They have successfully passed the initial verification stage.<br><br>" +
124
                        "<strong>Lead Information:</strong><br>" +
125
                        "<strong>Applicant Name:</strong> " + trialForm.getFirstName() + " " + trialForm.getLastName() + "<br>" +
126
                        "<strong>Business Name:</strong> " + trialForm.getBusinessName() + "<br>" +
127
                        "<strong>Mobile:</strong> " + trialForm.getMobile() + "<br>" +
128
                        "<strong>Email:</strong> " + trialForm.getEmail() + "<br>" +
129
                        "<strong>GST Number:</strong> " + trialForm.getGstNumber() + "<br><br>" +
130
                        "<strong>Address:</strong><br>" +
131
                        trialForm.getAddressLine1() + "<br>" +
132
                        (trialForm.getAddressLine2() != null ? trialForm.getAddressLine2() + "<br>" : "") +
133
                        trialForm.getCity() + ", " + trialForm.getState() + " - " + trialForm.getPincode() + "<br><br>" +
134
                        "<strong>Strategic Objective:</strong> The partner has expressed initial interest. Your goal is to move them beyond trial onboarding.<br><br>" +
135
                        "<strong>Next Steps:</strong><br>" +
35418 aman 136
                        " 1. Contact Immediately: Call or visit the store within the next 24 hours.<br>" +
137
                        " 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>" +
138
                        " 3. Update Status: Log your interaction and the partner's interest level in the app immediately after the meeting.<br><br>" +
35362 aman 139
                        "Good Luck,<br>" +
140
                        "Regards,<br>SmartDukaan Team";
141
 
35360 aman 142
        List<AuthUser> salesAuthUserList = csService.getAuthUserIds(ProfitMandiConstants.TICKET_CATEGORY_SALES, Arrays.asList(EscalationType.L3, EscalationType.L4, EscalationType.L5));
143
        List<String> salesEmailList = new ArrayList<>();
144
        for (AuthUser authUser : salesAuthUserList) {
145
            if (authUser.getEmailId() != null && !authUser.getEmailId().trim().isEmpty()) {
146
                salesEmailList.add(authUser.getEmailId());
147
            }
148
        }
149
        AuthUser bmAuthUser = authRepository.selectById(trialForm.getBmId());
150
        AuthUser asmAuthUser = authRepository.selectById(trialForm.getAsmId());
151
 
152
        String[] emailTo = new String[]{bmAuthUser.getEmailId()};
153
        List<String> ccList = new ArrayList<>(salesEmailList);
154
        ccList.add(asmAuthUser.getEmailId());
35485 aman 155
        ccList.add("tarun.verma@smartdukaan.com");
35360 aman 156
        String[] cc = ccList.stream()
157
                .filter(email -> !email.trim().isEmpty())
158
                .toArray(String[]::new);
159
        LOGGER.info("RegistrationBody - " + body);
160
        LOGGER.info("cc list - " + Arrays.toString(cc));
36399 amit 161
        emailService.sendMailWithAttachments(gmailRelaySender, emailTo, cc, "NEW LEAD ASSIGNED: " + trialForm.getBusinessName() + "- Call/Visit ASAP", body);
35360 aman 162
 
163
 
164
    }
165
 
166
    public void sentMailForStoreCodeCreation(TrialForm trialForm) throws Exception {
167
        String body =
35362 aman 168
                "The trial user has been verified.<br><br>" +
169
                        "Please create Store Code for: <strong>" + trialForm.getBusinessName() + "</strong><br>" +
170
                        "Mobile Number: <strong>" + trialForm.getMobile() + "</strong><br><br>" +
171
                        "Regards,<br>SmartDukaan Team";
172
 
35360 aman 173
        String[] emailTo = {"gaurav.sharma@smartdukaan.com"};
174
        LOGGER.info("RegistrationBody - " + body);
35485 aman 175
        String[] cc = {"devkinandan.lal@smartdukaan.com", "tarun.verma@smartdukaan.com"};
35360 aman 176
        LOGGER.info("cc list - " + Arrays.toString(cc));
36399 amit 177
        emailService.sendMailWithAttachments(gmailRelaySender, emailTo, cc, "Store Code Creation Required for trial user" + trialForm.getBusinessName(), body);
35360 aman 178
 
179
 
180
    }
35391 aman 181
 
182
    public void sentRejectionMailToTrialUser(TrialForm trialForm) throws Exception {
183
 
184
        String applicantName = trialForm.getFirstName() +
185
                (trialForm.getLastName() != null ? " " + trialForm.getLastName() : "");
186
 
187
        String body =
188
                "Dear " + applicantName + ",<br><br>" +
35499 aman 189
                        "After a detailed review of your application, we regret to inform you that your " +
190
                        "<strong>30-Day Free Trial request</strong> cannot be approved based on the current " +
191
                        "eligibility criteria.<br><br>" +
35391 aman 192
 
35499 aman 193
                        "If you would like to understand the reason for this decision or discuss any concerns, " +
194
                        "please feel free to reach out to us at <strong>care@smartdukaan.com</strong> or call our " +
195
                        "Toll-Free number <strong>1800-270-0273</strong>.<br><br>" +
35391 aman 196
 
35499 aman 197
                        "Thank you for applying. We wish you continued success in your business journey. " +
198
                        "Aur agar future mein humari policies change hoti hain, hum aapse zaroor sampark karenge.<br><br>" +
35391 aman 199
 
200
                        "Regards,<br>" +
35499 aman 201
                        "Team SmartDukaan";
35391 aman 202
 
35499 aman 203
 
35391 aman 204
        String[] emailTo = {trialForm.getEmail()};
35758 vikas 205
        String[] cc = {"sdtech@smartdukaan.com"};
35391 aman 206
 
35642 aman 207
        emailService.sendMailWithAttachments(
36399 amit 208
                gmailRelaySender,
35391 aman 209
                emailTo,
210
                cc,
35499 aman 211
                "Update on Your SmartDukaan Free Trial Request",
35391 aman 212
                body
213
        );
214
    }
215
 
216
    public void sendUpgradeRequestMail(TrialForm trialForm) throws Exception {
217
 
218
        String applicantName = trialForm.getFirstName() +
219
                (trialForm.getLastName() != null ? " " + trialForm.getLastName() : "");
220
 
221
        String body =
222
                "Dear " + applicantName + ",<br><br>" +
35499 aman 223
                        "Thank you for expressing your interest in the <strong>SmartDukaan Franchise Partner Program</strong>.<br><br>" +
35391 aman 224
 
35499 aman 225
                        "We have successfully received your request. Our team will connect with you shortly to discuss the " +
226
                        "requirements, eligibility, and the next steps. Taaki hum milkar ek strong aur long-term partnership " +
227
                        "build kar saken.<br><br>" +
35391 aman 228
 
35499 aman 229
                        "We look forward to taking this conversation forward.<br><br>" +
35391 aman 230
 
231
                        "Regards,<br>" +
35499 aman 232
                        "Team SmartDukaan";
35391 aman 233
 
35499 aman 234
 
35391 aman 235
        String[] emailTo = {trialForm.getEmail()};
35758 vikas 236
        String[] cc = {"sdtech@smartdukaan.com"};
35391 aman 237
 
35642 aman 238
        emailService.sendMailWithAttachments(
36399 amit 239
                gmailRelaySender,
35391 aman 240
                emailTo,
241
                cc,
35499 aman 242
                "We’ve Received Your Interest in the SmartDukaan Franchise",
35391 aman 243
                body
244
        );
245
    }
246
 
247
    public void sendInternalFranchiseUpgradeMail(TrialForm trialForm) throws Exception {
248
 
249
        String applicantName = trialForm.getFirstName() +
250
                (trialForm.getLastName() != null ? " " + trialForm.getLastName() : "");
251
 
252
        String body =
253
                "Hi Team,<br><br>" +
254
                        "We have received a new interest request for the <strong>SmartDukaan Franchise Partner upgrade</strong> via the app.<br><br>" +
255
 
256
                        "<strong>Trial User Details:</strong><br>" +
257
                        "Name: " + applicantName + "<br>" +
258
                        "Business Name: " + trialForm.getBusinessName() + "<br>" +
259
                        "Registered Mobile: " + trialForm.getMobile() + "<br>" +
260
                        "User ID: " + trialForm.getId() + "<br>" +
261
                        "Request Date: " + trialForm.getUpdatedOn() + "<br><br>" +
262
 
263
                        "<strong>Action Required:</strong><br>" +
264
                        "Please review the user's profile and initiate the verification/onboarding process immediately.<br><br>" +
265
 
266
                        "Best,<br>" +
267
                        "SmartDukaan System";
35418 aman 268
        List<AuthUser> salesAuthUserList = csService.getAuthUserIds(ProfitMandiConstants.TICKET_CATEGORY_SALES, Arrays.asList(EscalationType.L3, EscalationType.L4, EscalationType.L5));
269
        List<String> salesEmailList = new ArrayList<>();
270
        for (AuthUser authUser : salesAuthUserList) {
271
            if (authUser.getEmailId() != null && !authUser.getEmailId().trim().isEmpty()) {
272
                salesEmailList.add(authUser.getEmailId());
273
            }
274
        }
275
        AuthUser bmAuthUser = authRepository.selectById(trialForm.getBmId());
276
        AuthUser asmAuthUser = authRepository.selectById(trialForm.getAsmId());
35391 aman 277
 
35418 aman 278
        String[] emailTo = new String[]{bmAuthUser.getEmailId()};
279
        List<String> ccList = new ArrayList<>(salesEmailList);
280
        ccList.add(asmAuthUser.getEmailId());
35485 aman 281
        ccList.add("tarun.verma@smartdukaan.com");
35418 aman 282
        String[] cc = ccList.stream()
283
                .filter(email -> !email.trim().isEmpty())
284
                .toArray(String[]::new);
35642 aman 285
        emailService.sendMailWithAttachments(
36399 amit 286
                gmailRelaySender,
35391 aman 287
                emailTo,
288
                cc,
289
                "Franchise Upgrade Request - " + applicantName + ", " + trialForm.getBusinessName(),
290
                body
291
        );
292
    }
293
 
37266 ranu 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
 
35337 aman 375
}