Skip to content

Commit

Permalink
implements classes for business logic
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Antonelli committed Jun 28, 2024
1 parent 8304224 commit dc40354
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 26 deletions.
1 change: 1 addition & 0 deletions .env.local.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
STRIPE_API_TEST_KEY=
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ yarn-error.log*
*.tsbuildinfo
next-env.d.ts

tasks.json
.vscode/tasks.json
54 changes: 37 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@
"lint": "next lint"
},
"dependencies": {
"csv-writer": "^1.6.0",
"next": "14.2.4",
"react": "^18",
"react-dom": "^18",
"next": "14.2.4"
"stripe": "^15.12.0"
},
"devDependencies": {
"typescript": "^5",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"eslint": "^8",
"eslint-config-next": "14.2.4",
"postcss": "^8",
"tailwindcss": "^3.4.1",
"eslint": "^8",
"eslint-config-next": "14.2.4"
"typescript": "^5"
}
}
69 changes: 69 additions & 0 deletions src/app/api/stripe/report/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {createObjectCsvWriter} from "csv-writer";
import fs from "fs";
import path from "path";
import Stripe from "stripe";
import {NextResponse} from "next/server";
import {secrets} from "@/config/secrets";
import {StripeService} from "@/services/api/stripe/StripeService";

async function generateCSVReport() {
const stripeService = new StripeService();
const transactions = await stripeService.fetchBalanceTransactions();
const fees = await stripeService.fetchApplicationFees();
const refunds = await stripeService.fetchRefunds();

const csvWriter = createObjectCsvWriter({
path: "stripe_report.csv",
header: [
{id: "type", title: "TYPE"},
{id: "amount", title: "AMOUNT"},
{id: "currency", title: "CURRENCY"},
{id: "description", title: "DESCRIPTION"},
{id: "created", title: "CREATED"},
],
});

const data = [
...transactions.map((t) => ({
type: "Transaction",
amount: t.amount,
currency: t.currency,
description: t.description,
created: new Date(t.created * 1000).toISOString(),
})),
...fees.map((f) => ({
type: "Application Fee",
amount: f.amount,
currency: f.currency,
description: f.description,
created: new Date(f.created * 1000).toISOString(),
})),
...refunds.map((r) => ({
type: "Refund",
amount: r.amount,
currency: r.currency,
description: r.description,
created: new Date(r.created * 1000).toISOString(),
})),
];

await csvWriter.writeRecords(data);
return path.resolve("stripe_report.csv");
}

export async function GET() {
try {
const filePath = await generateCSVReport();
const fileBuffer = fs.readFileSync(filePath);
fs.unlinkSync(filePath); // delete file after sending
return new NextResponse(fileBuffer, {
headers: {
"Content-Type": "text/csv",
"Content-Disposition": 'attachment; filename="stripe_report.csv"',
},
});
} catch (error) {
console.error("Error generating report:", error);
return new NextResponse("Error generating report", {status: 500});
}
}
32 changes: 28 additions & 4 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
"use client"
"use client";

export default function Home() {
const handleDownload = () => {
fetch("/api/stripe/report")
.then((response) => {
if (response.ok) {
return response.blob();
}
throw new Error("Network response was not ok.");
})
.then((blob) => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.style.display = "none";
a.href = url;
a.download = "stripe_report.csv";
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
})
.catch((error) => console.error("Download error:", error));
};
return (
<main className="flex min-h-screen flex-col items-center justify-center p-24">
<button className="rounded-full bg-blue-600 p-3" onClick={() => console.log("Button clicked")}>
Authenticate
<h2>Stripe financial report:</h2>
<button
type="button"
onClick={handleDownload}
className="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800">
Download Report
</button>
<p></p>
<div></div>
</main>
);
}
10 changes: 10 additions & 0 deletions src/config/secrets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class EnvConfig {
get stripeKey(): string {
if (!process.env.STRIPE_KEY) {
throw new Error("STRIPE_KEY environment not set");
}
return process.env.STRIPE_KEY;
}
}

export const secrets = new EnvConfig();
Loading

0 comments on commit dc40354

Please sign in to comment.