-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathexport.ts
More file actions
181 lines (161 loc) · 4.95 KB
/
export.ts
File metadata and controls
181 lines (161 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* Data Export Module
* Support CSV and PDF export for group history and contribution records
*/
import { sorosaveClient } from "./sorosave";
export interface ContributionRecord {
date: string;
member: string;
amount: number;
status: "pending" | "completed" | "failed";
}
export interface GroupSummary {
name: string;
createdAt: string;
totalMembers: number;
totalContributions: number;
roundNumber: number;
currentPool: number;
}
/**
* Export contributions to CSV format
*/
export function exportToCSV(contributions: ContributionRecord[], filename: string): void {
const headers = ["Date", "Member", "Amount", "Status"];
const rows = contributions.map(c => [
c.date,
c.member,
c.amount.toString(),
c.status
]);
const csvContent = [
headers.join(","),
...rows.map(row => row.join(","))
].join("\n");
downloadFile(csvContent, `${filename}.csv`, "text/csv");
}
/**
* Export group summary to PDF (simplified - actual PDF requires library like jsPDF)
*/
export async function exportToPDF(summary: GroupSummary, contributions: ContributionRecord[]): Promise<void> {
// Build HTML content for PDF
const html = buildPDFHTML(summary, contributions);
// For now, download as HTML that can be printed to PDF
downloadFile(html, `${summary.name}_report.html`, "text/html");
console.log("PDF export: HTML downloaded. Use browser print to save as PDF.");
}
/**
* Build HTML content for PDF export
*/
function buildPDFHTML(summary: GroupSummary, contributions: ContributionRecord[]): string {
const contributionsRows = contributions.map(c => `
<tr>
<td>${c.date}</td>
<td>${c.member}</td>
<td>${c.amount}</td>
<td>${c.status}</td>
</tr>
`).join("");
return `
<!DOCTYPE html>
<html>
<head>
<title>Group Report - ${summary.name}</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; }
h1 { color: #333; }
table { width: 100%; border-collapse: collapse; margin-top: 20px; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background: #f5f5f5; }
.summary { display: flex; gap: 20px; margin: 20px 0; }
.summary-item { background: #f9f9f9; padding: 15px; border-radius: 8px; }
.summary-item h3 { margin: 0 0 5px 0; font-size: 14px; color: #666; }
.summary-item p { margin: 0; font-size: 24px; font-weight: bold; }
@media print { button { display: none; } }
</style>
</head>
<body>
<h1>Group Savings Report</h1>
<p>Generated: ${new Date().toLocaleDateString()}</p>
<div class="summary">
<div class="summary-item">
<h3>Total Members</h3>
<p>${summary.totalMembers}</p>
</div>
<div class="summary-item">
<h3>Total Contributions</h3>
<p>${summary.totalContributions}</p>
</div>
<div class="summary-item">
<h3>Current Pool</h3>
<p>${summary.currentPool}</p>
</div>
</div>
<h2>Contribution History</h2>
<table>
<thead>
<tr>
<th>Date</th>
<th>Member</th>
<th>Amount</th>
<th>Status</th>
</tr>
</thead>
<tbody>
${contributionsRows}
</tbody>
</table>
<button onclick="window.print()" style="margin-top: 20px; padding: 10px 20px; cursor: pointer;">
Print / Save as PDF
</button>
</body>
</html>
`.trim();
}
/**
* Download file helper
*/
function downloadFile(content: string, filename: string, mimeType: string): void {
const blob = new Blob([content], { type: mimeType });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
}
/**
* Fetch and export group data
*/
export async function exportGroupData(groupId: string, format: "csv" | "pdf"): Promise<void> {
try {
// Get group info
const groupInfo = await sorosaveClient.getGroupInfo(groupId);
// Build summary
const summary: GroupSummary = {
name: groupInfo.name || "Group",
createdAt: new Date().toISOString(),
totalMembers: groupInfo.members?.length || 0,
totalContributions: groupInfo.totalContributions || 0,
roundNumber: groupInfo.currentRound || 1,
currentPool: groupInfo.poolAmount || 0
};
// Mock contribution data (replace with actual API call)
const contributions: ContributionRecord[] = [
{ date: "2026-01-15", member: "Alice", amount: 100, status: "completed" },
{ date: "2026-01-22", member: "Bob", amount: 100, status: "completed" },
{ date: "2026-01-29", member: "Charlie", amount: 100, status: "completed" }
];
if (format === "csv") {
exportToCSV(contributions, `${summary.name}_contributions`);
} else {
await exportToPDF(summary, contributions);
}
console.log(`Exported ${format.toUpperCase()} for group ${groupId}`);
} catch (error) {
console.error("Export failed:", error);
throw error;
}
}