Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Dec 13, 2023
1 parent f5e448f commit 1e265e1
Show file tree
Hide file tree
Showing 23 changed files with 140 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/preview-dashboard.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
uses: rlespinasse/github-slug-action@v4
- uses: oven-sh/setup-bun@v1
with:
bun-version: "1.0.16"
bun-version: "1.0.17"
- name: Install dependencies
run: bun install --frozen-lockfile
- name: 📤 Pull Vercel Environment Information
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/preview-website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
uses: rlespinasse/github-slug-action@v4
- uses: oven-sh/setup-bun@v1
with:
bun-version: "1.0.16"
bun-version: "1.0.17"
- name: Install dependencies
run: bun install --frozen-lockfile
- name: 📤 Pull Vercel Environment Information
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/production-dashboard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1
with:
bun-version: "1.0.16"
bun-version: "1.0.17"
- name: Install dependencies
run: bun install --frozen-lockfile
- name: 📤 Pull Vercel Environment Information
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/production-website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1
with:
bun-version: "1.0.16"
bun-version: "1.0.17"
- name: Install dependencies
run: bun install --frozen-lockfile
- name: 📤 Pull Vercel Environment Information
Expand Down
5 changes: 3 additions & 2 deletions apps/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@vercel/toolbar": "^0.1.6",
"@zip.js/zip.js": "2.7.32",
"change-case": "^5.3.0",
"fast-csv": "^4.3.6",
"framer-motion": "^10.16.16",
"ms": "^2.1.3",
"next": "14.0.4",
Expand All @@ -38,7 +39,7 @@
"react": "18.2.0",
"react-dom": "18.2.0",
"react-dropzone": "^14.2.3",
"react-hook-form": "^7.49.0",
"react-hook-form": "^7.49.1",
"react-pin-field": "^3.1.4",
"recharts": "^2.10.3",
"sharp": "^0.33.0",
Expand All @@ -50,7 +51,7 @@
"@midday/tsconfig": "workspace:*",
"@t3-oss/env-nextjs": "^0.7.1",
"@types/node": "^20.10.4",
"@types/react": "^18.2.43",
"@types/react": "^18.2.45",
"@types/react-dom": "^18.2.17",
"typescript": "^5.3.3"
}
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/src/app/api/download/file/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export async function GET(req, res) {

const { data } = await download(supabase, {
bucket: "vault",
path: `${user.data.team_id}/${path}/${filename}`,
path: `${user.data.team_id}/${path}`,
});

const responseHeaders = new Headers(res.headers);
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/src/components/attachments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const Item = ({ file, onDelete, id }) => {

<div className="flex flex-col space-y-0.5 w-80">
<a
href={`/api/download/file?path=transactions/${id}&filename=${file.name}`}
href={`/api/download/file?path=transactions/${id}/${file.name}&filename=${file.name}`}
download
className="truncate"
>
Expand Down
1 change: 1 addition & 0 deletions apps/dashboard/src/components/charts/bar-chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export function BarChart({ data }) {
formatAmount({
maximumFractionDigits: 0,
minimumFractionDigits: 0,
// TODO: Fix
currency: "SEK",
amount: value,
})
Expand Down
6 changes: 4 additions & 2 deletions apps/dashboard/src/components/export-status.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export function ExportStatus() {
});

const handleOnDownload = (id: string) => {
console.log("download");
dismiss(id);
};

Expand Down Expand Up @@ -117,7 +116,10 @@ export function ExportStatus() {
</DropdownMenuContent>
</DropdownMenu>

<a href={`/api/download/file?path=katt&filename=katt.zip`} download>
<a
href={`/api/download/file?path=exports/${status?.data?.fileName}&filename=${status?.data?.fileName}`}
download
>
<Button size="sm" onClick={() => handleOnDownload(id)}>
Download
</Button>
Expand Down
8 changes: 4 additions & 4 deletions apps/dashboard/src/components/tables/vault/data-table-row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,15 @@ export function DataTableRow({ data, deleteFile, createFolder, deleteFolder }) {
<DropdownMenuItem>
{data.isFolder ? (
<a
href={`/api/download/zip?path=${filepath}&filename=${data.name}`}
href={`/api/download/zip?path=${filepath}/${data.name}&filename=${data.name}`}
download
className="truncate"
>
Download
</a>
) : (
<a
href={`/api/download/file?path=${folderPath}&filename=${data.name}`}
href={`/api/download/file?path=${folderPath}/${data.name}&filename=${data.name}`}
download
className="truncate"
>
Expand Down Expand Up @@ -266,15 +266,15 @@ export function DataTableRow({ data, deleteFile, createFolder, deleteFolder }) {
<ContextMenuItem>
{data.isFolder ? (
<a
href={`/api/download/zip?path=${filepath}&filename=${data.name}`}
href={`/api/download/zip?path=${filepath}/${data.name}&filename=${data.name}`}
download
className="truncate"
>
Download
</a>
) : (
<a
href={`/api/download/file?path=${folderPath}&filename=${data.name}`}
href={`/api/download/file?path=${folderPath}/${data.name}&filename=${data.name}`}
download
className="truncate"
>
Expand Down
2 changes: 0 additions & 2 deletions apps/dashboard/src/utils/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ export async function resumableUpload(

const fullPath = decodeURIComponent([...path, file.name].join("/"));

console.log(fullPath);

return new Promise((resolve, reject) => {
const upload = new tus.Upload(file, {
endpoint: `https://${process.env.NEXT_PUBLIC_SUPABASE_ID}.supabase.co/storage/v1/upload/resumable`,
Expand Down
2 changes: 1 addition & 1 deletion apps/website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"devDependencies": {
"@midday/tsconfig": "workspace:*",
"@types/node": "^20.10.4",
"@types/react": "^18.2.43",
"@types/react": "^18.2.45",
"@types/react-dom": "^18.2.17"
}
}
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
"dependencies": {
"@biomejs/biome": "1.4.1",
"@manypkg/cli": "^0.21.1",
"turbo": "^1.11.1",
"turbo": "^1.11.2",
"typescript": "^5.3.3"
},
"devDependencies": {
"vercel": "32.7.0"
"vercel": "32.7.1"
}
}
4 changes: 2 additions & 2 deletions packages/jobs/src/transactions/created.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { scheduler } from "./scheduler";
client.defineJob({
id: Jobs.BANK_ACCOUNT_CREATED,
name: "🏦 Bank Account Created",
version: "1.0.1",
version: "1.0.2",
trigger: supabaseTriggers.onInserted({
table: "bank_accounts",
}),
Expand All @@ -25,7 +25,7 @@ client.defineJob({
await scheduler.register(payload.record.id, {
type: "interval",
options: {
seconds: 36000, // every 1h
seconds: 3600, // every 1h
},
});
},
Expand Down
2 changes: 1 addition & 1 deletion packages/jobs/src/transactions/encrichment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { processPromisesBatch } from "../utils";
client.defineJob({
id: Jobs.TRANSACTIONS_ENCRICHMENT,
name: "💅 Transactions - Enrichment",
version: "1.0.1",
version: "1.0.2",
trigger: eventTrigger({
name: Events.TRANSACTIONS_ENCRICHMENT,
schema: z.object({
Expand Down
119 changes: 110 additions & 9 deletions packages/jobs/src/transactions/export.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { format } 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 { revalidateTag } from "next/cache";
import { z } from "zod";
import { client, supabase } from "../client";
import { Events, Jobs } from "../constants";

client.defineJob({
id: Jobs.TRANSACTIONS_EXPORT,
name: "🗄️ Transactions - Export",
version: "1.0.1",
version: "1.0.2",
trigger: eventTrigger({
name: Events.TRANSACTIONS_EXPORT,
schema: z.object({
Expand All @@ -18,6 +23,7 @@ client.defineJob({
}),
integrations: { supabase },
run: async (payload, io) => {
const locale = "en-us";
const { from, to, teamId } = payload;

const client = await io.supabase.client;
Expand All @@ -30,9 +36,7 @@ client.defineJob({
},
});

await io.logger.info("Transactions Export");

const data = await getTransactionsQuery(client, {
const { data, meta } = await getTransactionsQuery(client, {
teamId,
from: 0,
to: 100000,
Expand All @@ -58,34 +62,131 @@ client.defineJob({
},
});

const attachments = await Promise.allSettled(
data.flatMap((transaction, idx) => {
const rowId = idx + 1;

return transaction?.attachments.map(
async (attachment, idx2: number) => {
const extension = attachment.name.split(".").pop();
const name =
idx2 > 0
? `${rowId}_${idx2}.${extension}`
: `${rowId}.${extension}`;

const { data } = await download(client, {
bucket: "vault",
path: attachment.path,
});

return {
name,
blob: data,
};
}
);
})
);

await generateExport.update("generate-export-attachments-end", {
state: "loading",
data: {
progress: 70,
},
});

await generateExport.update("generate-export-zip", {
await generateExport.update("generate-export-csv-start", {
state: "loading",
data: {
progress: 75,
},
});

const csvStream = format({
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: {
progress: 80,
},
});

await generateExport.update("generate-export-move", {
await generateExport.update("generate-export-zip", {
state: "loading",
data: {
progress: 85,
},
});

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")}`;

attachments.map((attachment) => {
if (attachment?.value.blob) {
zipWriter.add(
`${filePath}/${attachment.value.name}`,
new BlobReader(attachment.value.blob)
);
}
});

const zip = await zipWriter.close();

await generateExport.update("generate-export-upload", {
state: "loading",
data: {
progress: 90,
},
});

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

await client.storage
.from("vault")
.upload(`${path}/${fileName}`, await zip.arrayBuffer(), {
upsert: true,
contentType: "application/zip",
});

revalidateTag(`vault_${teamId}`);

await generateExport.update("generate-export-done", {
state: "success",
data: {
filePath: "exports/export-2022-01-01-2023-01-01.zip",
fileName: "export-2022-01-01-2023-01-01.zip",
filePath,
fileName,
progress: 100,
totalItems: data.meta.count,
totalItems: meta.count,
},
});
},
Expand Down
2 changes: 1 addition & 1 deletion packages/jobs/src/transactions/initial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { transformTransactions } from "../utils";
client.defineJob({
id: Jobs.TRANSACTIONS_INITIAL_SYNC,
name: "🔂 Transactions - Initial Sync",
version: "1.0.1",
version: "1.0.2",
trigger: eventTrigger({
name: Events.TRANSACTIONS_INITIAL_SYNC,
schema: z.object({
Expand Down
2 changes: 1 addition & 1 deletion packages/jobs/src/transactions/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Events, Jobs } from "../constants";
client.defineJob({
id: Jobs.TRANSACTIONS_NOTIFICATION,
name: "🔔 Transactions - Notification",
version: "1.0.1",
version: "1.0.2",
trigger: eventTrigger({
name: Events.TRANSACTIONS_NOTIFICATION,
schema: z.object({
Expand Down
Loading

0 comments on commit 1e265e1

Please sign in to comment.