Spaces:
Sleeping
Sleeping
| import nodemailer from 'nodemailer'; | |
| import * as xlsx from 'xlsx'; | |
| import { getDb } from '../db'; | |
| import dotenv from 'dotenv'; | |
| dotenv.config(); | |
| const transporter = nodemailer.createTransport({ | |
| service: 'gmail', | |
| auth: { | |
| user: process.env.EMAIL_USER, | |
| pass: process.env.EMAIL_PASS, | |
| }, | |
| }); | |
| export async function exportToBuffer(): Promise<Buffer> { | |
| const db = await getDb(); | |
| const transactions = await db.all('SELECT * FROM transactions'); | |
| const wallets = await db.all('SELECT * FROM wallets'); | |
| const exchanges = await db.all('SELECT * FROM exchanges'); | |
| const loans = await db.all('SELECT * FROM loans'); | |
| const wb = xlsx.utils.book_new(); | |
| xlsx.utils.book_append_sheet(wb, xlsx.utils.json_to_sheet(transactions), 'Transactions'); | |
| xlsx.utils.book_append_sheet(wb, xlsx.utils.json_to_sheet(wallets), 'Wallets'); | |
| xlsx.utils.book_append_sheet(wb, xlsx.utils.json_to_sheet(exchanges), 'Exchanges'); | |
| xlsx.utils.book_append_sheet(wb, xlsx.utils.json_to_sheet(loans), 'Loans'); | |
| return xlsx.write(wb, { type: 'buffer', bookType: 'xlsx' }); | |
| } | |
| export async function performWeeklyBackup() { | |
| console.log('[Backup] Starting weekly export...'); | |
| try { | |
| const buffer = await exportToBuffer(); | |
| const date = new Date().toISOString().split('T')[0]; | |
| const filename = `backup-${date}.xlsx`; | |
| const mailOptions = { | |
| from: process.env.EMAIL_USER, | |
| to: process.env.EMAIL_TO, | |
| subject: `Weekly Wallet Backup - ${date}`, | |
| text: 'Please find the attached weekly backup of your wallet data.', | |
| attachments: [ | |
| { | |
| filename: filename, | |
| content: buffer, | |
| }, | |
| ], | |
| }; | |
| const info = await transporter.sendMail(mailOptions); | |
| console.log('[Backup] Email sent successfully:', info.messageId); | |
| } catch (error) { | |
| console.error('[Backup] Error during weekly backup:', error); | |
| } | |
| } | |