Skip to content

Commit

Permalink
Export v1
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Dec 13, 2023
1 parent 1e265e1 commit 95e99b8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 40 deletions.
4 changes: 2 additions & 2 deletions apps/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@midday/supabase": "workspace:*",
"@midday/ui": "workspace:*",
"@novu/headless": "^0.22.0",
"@trigger.dev/nextjs": "^2.3.4",
"@trigger.dev/nextjs": "^2.3.5",
"@vercel/edge-config": "^0.4.1",
"@vercel/speed-insights": "^1.0.1",
"@vercel/toolbar": "^0.1.6",
Expand All @@ -39,7 +39,7 @@
"react": "18.2.0",
"react-dom": "18.2.0",
"react-dropzone": "^14.2.3",
"react-hook-form": "^7.49.1",
"react-hook-form": "^7.49.2",
"react-pin-field": "^3.1.4",
"recharts": "^2.10.3",
"sharp": "^0.33.0",
Expand Down
1 change: 1 addition & 0 deletions apps/dashboard/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export async function middleware(request: NextRequest) {

// Enrolled for mfa but not verified
if (
mfaData &&
mfaData.nextLevel === "aal2" &&
mfaData.nextLevel !== mfaData.currentLevel &&
request.nextUrl.pathname !== "/mfa/verify"
Expand Down
Binary file modified bun.lockb
Binary file not shown.
6 changes: 3 additions & 3 deletions packages/jobs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"check:types": "tsc --noEmit"
},
"dependencies": {
"@trigger.dev/react": "^2.3.4",
"@trigger.dev/sdk": "^2.3.4",
"@trigger.dev/supabase": "^2.3.4"
"@trigger.dev/react": "^2.3.5",
"@trigger.dev/sdk": "^2.3.5",
"@trigger.dev/supabase": "^2.3.5"
}
}

61 changes: 26 additions & 35 deletions packages/jobs/src/transactions/export.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { format } from "@fast-csv/format";
import { writeToString } from "@fast-csv/format";
import { getTransactionsQuery } from "@midday/supabase/queries";
import { download } from "@midday/supabase/storage";
import { eventTrigger } from "@trigger.dev/sdk";
import { BlobReader, BlobWriter, ZipWriter } from "@zip.js/zip.js";
import formatDate from "date-fns/format";
import { BlobReader, BlobWriter, TextReader, ZipWriter } from "@zip.js/zip.js";
import { format } from "date-fns";
import { revalidateTag } from "next/cache";
import { z } from "zod";
import { client, supabase } from "../client";
Expand All @@ -23,10 +23,17 @@ client.defineJob({
}),
integrations: { supabase },
run: async (payload, io) => {
const client = await io.supabase.client;
const locale = "en-us";
const { from, to, teamId } = payload;

const client = await io.supabase.client;
const filePath = `export-${format(new Date(from), "y-M-d")}-${format(
new Date(to),
"y-M-d"
)}`;

const path = `${teamId}/exports`;
const fileName = `${filePath}.zip`;

const generateExport = await io.createStatus("generate-export-start", {
label: "Generating export",
Expand Down Expand Up @@ -102,28 +109,22 @@ client.defineJob({
},
});

const csvStream = format({
const rows = data.map((transaction, idx) => [
idx + 1,
transaction.date,
transaction.name,
Intl.NumberFormat(locale, {
style: "currency",
currency: transaction.currency,
}).format(transaction.amount),
transaction?.attachments?.length > 0 ? "✔️" : "❌",
transaction?.note ?? "",
]);

const csv = await writeToString(rows, {
headers: ["ID", "Date", "Description", "Amount", "Attachment", "Note"],
});

csvStream.pipe(process.stdout).on("end", () => process.exit());

csvStream.write(
data.map((transaction, idx) => [
idx + 1,
transaction.date,
transaction.name,
Intl.NumberFormat(locale, {
style: "currency",
currency: transaction.currency,
}).format(transaction.amount),
transaction?.attachments?.length > 0 ? "✔️" : "❌",
transaction?.note ?? "",
])
);

csvStream.end();

await generateExport.update("generate-export-csv-end", {
state: "loading",
data: {
Expand All @@ -141,19 +142,12 @@ client.defineJob({
const zipFileWriter = new BlobWriter("application/zip");
const zipWriter = new ZipWriter(zipFileWriter);

// zipWriter.add("transactions.csv", new TextWriter('csvStream'));

// zipWriter.add("transactions.csv", new BlobReader(attachment.value.blob));

const filePath = `export-${formatDate(
new Date(from),
"y-M-d"
)}-${formatDate(new Date(to), "y-M-d")}`;
zipWriter.add("transactions.csv", new TextReader(csv));

attachments.map((attachment) => {
if (attachment?.value.blob) {
zipWriter.add(
`${filePath}/${attachment.value.name}`,
attachment.value.name,
new BlobReader(attachment.value.blob)
);
}
Expand All @@ -168,9 +162,6 @@ client.defineJob({
},
});

const path = `${teamId}/exports`;
const fileName = `${filePath}.zip`;

await client.storage
.from("vault")
.upload(`${path}/${fileName}`, await zip.arrayBuffer(), {
Expand Down

0 comments on commit 95e99b8

Please sign in to comment.