Skip to content

Commit

Permalink
Merge pull request #6 from midday-ai/feature/export
Browse files Browse the repository at this point in the history
Export
  • Loading branch information
pontusab committed Nov 23, 2023
2 parents 9d37379 + 40abdcf commit 93dd159
Show file tree
Hide file tree
Showing 28 changed files with 120 additions and 56 deletions.
1 change: 0 additions & 1 deletion .env-example
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
NEXTAUTH_URL=http://localhost:3000
NEXT_PUBLIC_SUPABASE_URL=
NEXT_PUBLIC_SUPABASE_ANON_KEY=
RESEND_API_KEY=
Expand Down
14 changes: 7 additions & 7 deletions apps/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"build": "NODE_ENV=production next build",
"clean": "git clean -xdf .next .turbo node_modules",
"jobs": "bunx @trigger.dev/cli@latest dev --port 3001",
"jobs": "bunx @trigger.dev/cli@latest dev --port 3001 --client-id=midday-CpkS",
"dev": "next dev -p 3001",
"lint": "next lint",
"format": "biome format --write .",
Expand All @@ -31,23 +31,23 @@
"framer-motion": "^10.16.5",
"next": "14.0.4-canary.4",
"next-international": "^1.1.4",
"next-safe-action": "^5.0.3",
"next-safe-action": "^5.1.2",
"next-themes": "^0.2.1",
"next-usequerystate": "^1.12.2",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-dropzone": "^14.2.3",
"react-hook-form": "^7.48.2",
"recharts": "^2.9.3",
"recharts": "^2.10.1",
"sharp": "^0.32.6",
"zod": "^3.22.4"
},
"devDependencies": {
"@midday/tsconfig": "workspace:*",
"@t3-oss/env-nextjs": "^0.7.1",
"@types/node": "^20.9.1",
"@types/react": "^18.2.37",
"@types/react-dom": "^18.2.15",
"typescript": "^5.2.2"
"@types/node": "^20.9.4",
"@types/react": "^18.2.38",
"@types/react-dom": "^18.2.17",
"typescript": "^5.3.2"
}
}
2 changes: 1 addition & 1 deletion apps/dashboard/src/actions/export-transactions-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ export const exportTransactionsAction = action(
},
});

console.log(event);
return event;
}
);
4 changes: 2 additions & 2 deletions apps/dashboard/src/actions/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ export const createAttachmentsSchema = z.array(
export const deleteAttachmentSchema = z.string();

export const exportTransactionsSchema = z.object({
from: z.string(),
to: z.string(),
from: z.coerce.date(),
to: z.coerce.date(),
});
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ export default async function Transactions({
fallback={<Loading collapsed={Boolean(transactionId)} />}
key={page}
>
<Table filter={filter} page={page} sort={sort} noAccounts={empty} />
<Table
filter={filter}
page={page}
sort={sort}
noAccounts={empty}
initialTransactionId={searchParams.id}
/>
</Suspense>
</div>

Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/src/app/api/download/document/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function GET(req, res) {
const path = requestUrl.searchParams.get("path");
const filename = requestUrl.searchParams.get("filename");

const { data } = await supabase.storage.from("files").download(path);
const { data } = await supabase.storage.from("vault").download(path);
const responseHeaders = new Headers(res.headers);

responseHeaders.set(
Expand Down
6 changes: 3 additions & 3 deletions apps/dashboard/src/components/attachments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function Attachments({ id, data }) {
const uploadedFiles = await Promise.all(
acceptedFiles.map(async (acceptedFile) => {
const { path } = await uploadFile({
bucket: "files",
bucket: "vault",
path: `${userData?.team_id}/transactions/${id}`,
file: acceptedFile,
});
Expand All @@ -99,7 +99,7 @@ export function Attachments({ id, data }) {
transaction_id: id,
type: acceptedFile.type,
};
}),
})
);

const { data: newFiles } = await createAttachmentsAction(uploadedFiles);
Expand All @@ -120,7 +120,7 @@ export function Attachments({ id, data }) {
<div
className={cn(
"w-full h-[120px] border-dotted border-2 border-border rounded-xl text-center flex flex-col justify-center space-y-1 transition-colors text-[#606060]",
isDragActive && "bg-secondary text-white",
isDragActive && "bg-secondary text-white"
)}
{...getRootProps()}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,40 @@ import {
DialogHeader,
DialogTitle,
} from "@midday/ui/dialog";
import { useToast } from "@midday/ui/use-toast";
import { Loader2 } from "lucide-react";
import { useAction } from "next-safe-action/hook";
import { useSearchParams } from "next/navigation";
import { useEffect } from "react";

export function ExportTransactionsModal({ isOpen, setOpen }) {
const searchParams = useSearchParams();
const { execute, status } = useAction(exportTransactionsAction);
const { execute, status, result } = useAction(exportTransactionsAction);
const filter = searchParams.get("filter");
const date = filter ? JSON.parse(filter)?.date : null;
const { toast } = useToast();

useEffect(() => {
if (status === "hasSucceeded" && isOpen) {
setOpen(false);
}
}, [status]);

useEffect(() => {
if (result.data) {
toast({
duration: 6000,
title: "Exporting...",
description: "Your export is ready base on 46 transactions.",
// action: (
// <ToastAction altText="Yes" onClick={handleUpdateSimilar}>
// Yes
// </ToastAction>
// ),
});
}
}, [result]);

return (
<Dialog open={isOpen} onOpenChange={setOpen}>
<DialogContent>
Expand Down
4 changes: 2 additions & 2 deletions apps/dashboard/src/components/notification-setting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function NotificationSetting({
...state,
enabled: !state.enabled,
};
},
}
);

const onChange = () => {
Expand All @@ -35,7 +35,7 @@ export function NotificationSetting({
};

return (
<div className="flex items-center space-x-2">
<div className="flex items-center space-x-2 mb-3">
<Checkbox
id={id}
checked={optimisticData.enabled}
Expand Down
7 changes: 7 additions & 0 deletions apps/dashboard/src/components/notification-settings.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { updateSubscriberPreferenceAction } from "@/actions/update-subscriber-preference-action";
import { getSubscriberPreferences } from "@midday/notification";
import { getUser } from "@midday/supabase/cached-queries";
import { Skeleton } from "@midday/ui/skeleton";
import { NotificationSetting } from "./notification-setting";

export function NotificationSettingsSkeleton() {
return [...Array(2)].map((_, index) => (
<Skeleton key={index.toString()} className="h-4 w-[25%] mb-3" />
));
}

export async function NotificationSettings() {
const { data: userData } = await getUser();
const { data: subscriberPreferences } = await getSubscriberPreferences({
Expand Down
8 changes: 5 additions & 3 deletions apps/dashboard/src/components/notifications-settings-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import {
CardHeader,
CardTitle,
} from "@midday/ui/card";
import { Skeleton } from "@midday/ui/skeleton";
import { Suspense } from "react";
import { NotificationSettings } from "./notification-settings";
import {
NotificationSettings,
NotificationSettingsSkeleton,
} from "./notification-settings";

export async function NotificationsSettingsList() {
return (
Expand All @@ -20,7 +22,7 @@ export async function NotificationsSettingsList() {
</CardHeader>

<CardContent>
<Suspense fallback={<Skeleton className="h-4 w-[25%]" />}>
<Suspense fallback={<NotificationSettingsSkeleton />}>
<NotificationSettings />
</Suspense>
</CardContent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ type Item = {
type ItemsProps = {
data: Item[];
teamId?: string;
initialTransactionId: string;
};

export function DataTable({ data, teamId }: ItemsProps) {
export function DataTable({ data, teamId, initialTransactionId }: ItemsProps) {
const supabase = createClient();
const router = useRouter();
const [transactionId, setTransactionId] = useQueryState("id", {
defaultValue: initialTransactionId,
shallow: false, // TODO: Fix without this (redirect after mutation)
});

Expand Down Expand Up @@ -80,7 +82,7 @@ export function DataTable({ data, teamId }: ItemsProps) {
.on(
"postgres_changes",
{
event: "*",
event: "INSERT",
schema: "public",
table: "transactions",
filter: `team_id=eq.${teamId}`,
Expand Down
14 changes: 12 additions & 2 deletions apps/dashboard/src/components/tables/transactions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import { Loading } from "./loading";

const pageSize = 50;

export async function Table({ filter, page, sort, noAccounts }) {
export async function Table({
filter,
page,
sort,
noAccounts,
initialTransactionId,
}) {
const hasFilters = Object.keys(filter).length > 0;
const { to, from } = getPagination(page, pageSize);
const { data: userData } = await getUser();
Expand All @@ -30,7 +36,11 @@ export async function Table({ filter, page, sort, noAccounts }) {

return (
<>
<DataTable data={data} teamId={userData.team_id} />
<DataTable
data={data}
teamId={userData.team_id}
initialTransactionId={initialTransactionId}
/>
{hasFilters ? (
<div className="h-10" />
) : (
Expand Down
43 changes: 34 additions & 9 deletions apps/dashboard/src/jobs/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TransactionsEmail } from "@midday/email/emails/transactions";
import { getI18n } from "@midday/email/locales";
import { getTransactions } from "@midday/gocardless";
import { TriggerEvents, triggerBulk } from "@midday/notification";
import { getTransactionsQuery } from "@midday/supabase/queries";
import { Database } from "@midday/supabase/src/types";
import { renderAsync } from "@react-email/components";
import { eventTrigger } from "@trigger.dev/sdk";
Expand Down Expand Up @@ -71,7 +72,7 @@ client.defineJob({
table: "bank_accounts",
}),
run: async (payload, io) => {
await io.sendEvent("Schedule Transactions", {
await io.sendEvent("Transactions Initial Sync", {
id: payload.record.id,
name: "transactions.initial.sync",
payload: {
Expand All @@ -86,7 +87,7 @@ client.defineJob({
await dynamicSchedule.register(payload.record.id, {
type: "interval",
options: {
seconds: 3600 * 4, // every 4h
seconds: 3600, // every 1h
},
});
},
Expand Down Expand Up @@ -236,7 +237,7 @@ client.defineJob({

client.defineJob({
id: "transactions-initial-sync",
name: "Transactions - Initial",
name: "Transactions - Initial Sync",
version: "1.0.0",
trigger: eventTrigger({
name: "transactions.initial.sync",
Expand All @@ -252,6 +253,14 @@ client.defineJob({

const { transactions } = await getTransactions(accountId);

// Update bank account last_accessed
await io.supabase.client
.from("bank_accounts")
.update({
last_accessed: new Date(),
})
.eq("id", recordId);

if (!transactions?.booked.length) {
await io.logger.info("No transactions found");
}
Expand All @@ -260,7 +269,7 @@ client.defineJob({
.from("transactions")
.insert(
transformTransactions(transactions?.booked, {
accountId: recordId,
accountId: recordId, // Bank account record id
teamId: teamId,
})
)
Expand All @@ -287,23 +296,39 @@ client.defineJob({
trigger: eventTrigger({
name: "transactions.export",
schema: z.object({
from: z.string().datetime(),
to: z.string().datetime(),
from: z.coerce.date(),
to: z.coerce.date(),
teamId: z.string(),
}),
}),
integrations: { supabase },
run: async (payload, io) => {
const { from, to, teamId } = payload;

const generateExport = await io.createStatus("generate-export", {
label: "Generating memes",
const client = await io.supabase.client;

const generateExport = await io.createStatus("generate-export-start", {
label: "Generating export",
state: "loading",
});

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

await generateExport.update("generate-export", {
const data = await getTransactionsQuery(client, {
teamId,
from: 0,
to: 100000,
filter: {
date: {
from: from.toDateString(),
to: to.toDateString(),
},
},
});

await io.logger.info(`Transactions: ${JSON.stringify(data, null, 2)}`);

await generateExport.update("generate-export-done", {
state: "success",
data: {
url: "",
Expand Down
6 changes: 3 additions & 3 deletions apps/website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
},
"devDependencies": {
"@midday/tsconfig": "workspace:*",
"@types/node": "^20.9.1",
"@types/react": "^18.2.37",
"@types/react-dom": "^18.2.15"
"@types/node": "^20.9.4",
"@types/react": "^18.2.38",
"@types/react-dom": "^18.2.17"
}
}
2 changes: 1 addition & 1 deletion apps/website/src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default {
email: "Enter your email",
open: "Open source",
live: "Live profit/loss",
document: "Files",
document: "Vault",
reciept: "Receipt linking",
time: "Time tracking",
ai: "AI-enhanced filter & search",
Expand Down
Binary file modified bun.lockb
Binary file not shown.
Loading

0 comments on commit 93dd159

Please sign in to comment.