async addDocumentsData(id: number, data: any): Promise { try { if (!data.document_type) { throw new Error("document_type is required."); } const companyExists = await LeadDetails.findOne({ where: { id: data.company_id } }); if (!companyExists) { throw new Error(`Company with ID ${data.company_id} does not exist.`); } const userExists = await User.findOne({ where: { id } }); if (!userExists) { throw new Error(`User with ID ${id} does not exist.`); } let newRecord; if (data.document_type === "mandateDocument") { newRecord = await LeadsMandate.create({ company_id: data.company_id, reference_number: data.reference_number, document_id: data.document_id, mandate_creation_date: data.mandate_creation_date, mandate_number: data.mandate_number, igst: data.igst, gross_fees: data.gross_fees, tds: data.tds, net_fees_receivable: data.net_fees_receivable, created_by: id, }); } else if (data.document_type === "piDocument") { newRecord = await LeadsPi.create({ company_id: data.company_id, performa_invoice_number: data.performa_invoice_number, pi_creation_date: data.pi_creation_date, document_id: data.document_id, created_by: id, }); } else if (data.document_type === 'taxInvoice') { newRecord = await LeadsTax.create({ company_id: data.company_id, invoice_number: data.invoice_number, mandate_order_number: data.mandate_order_number, series_number: data.series_number, pi_creation_date: data.pi_creation_date, mandate_date: data.mandate_date, mode_of_payment: data.mode_of_payment, document_id: data.document_id, created_by: id, }) } else { throw new Error("Invalid document_type. Must be either 'mandate' or 'pi' or tax"); } return newRecord; } catch (error) { console.error("Error adding document data:", error); throw error; } } async send_email(req,body) { try { const subject = body.documentType; const bodyContent = `Hi, please find attachment for ${body.documentType} document` const leadsResponse = await LeadDetails.findOne({ where: { id: body.companyId, }, attributes: [ "primary_contact_person_email_id", "company_email", "secondary_contact_person_email_id", ] }) let to; if(leadsResponse.dataValues.primary_contact_person_email_id) { to = leadsResponse.dataValues.primary_contact_person_email_id } else if(leadsResponse.dataValues.company_email) { to = leadsResponse.dataValues.company_email } else if(leadsResponse.dataValues.secondary_contact_person_email_id) { to = leadsResponse.dataValues.secondary_contact_person_email_id } let pdfLink; if(body.documentType === 'mandateDocument') { const documentData = await LeadsMandate.findOne({ where: { company_id: body.companyId }, attributes: [ "document_id" ], order: [['created_at','DESC']], }) const documentId = documentData.dataValues.document_id; pdfLink = await this.dmsService.GET_SHAREPOINT_DOWNLOAD_LINK(documentId) } else if(body.documentType === 'piDocument') { const documentData = await LeadsPi.findOne({ where: { company_id: body.companyId }, attributes: [ "document_id" ], order: [['created_at','DESC']], }) const documentId = documentData.dataValues.document_id; pdfLink = await this.dmsService.GET_SHAREPOINT_DOWNLOAD_LINK(documentId) } else if(body.documentType === 'taxInvoice') { const documentData = await LeadsTax.findOne({ where: { company_id: body.companyId }, attributes: [ "document_id" ], order: [['created_at','DESC']], }) const documentId = documentData.dataValues.document_id; pdfLink = await this.dmsService.GET_SHAREPOINT_DOWNLOAD_LINK(documentId) } const response = await LeadsManagementHelper.SEND_MAIL(to, subject, bodyContent, pdfLink) return { success: true, result: "email sent" }; } catch (error) { console.log("Error in sending mail:", error); return { success: false, error: String(error) }; } }