Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Dec 26, 2023
1 parent db9dadb commit f532b36
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 82 deletions.
2 changes: 1 addition & 1 deletion apps/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"check:types": "tsc --noEmit"
},
"dependencies": {
"@hookform/resolvers": "^3.3.2",
"@hookform/resolvers": "^3.3.3",
"@midday/gocardless": "workspace:*",
"@midday/jobs": "workspace:*",
"@midday/kv": "workspace:*",
Expand Down
26 changes: 24 additions & 2 deletions apps/dashboard/src/actions/connect-bank-account-action.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
"use server";

import { Events, client } from "@midday/jobs";
import { getUser } from "@midday/supabase/cached-queries";
import { createBankAccounts } from "@midday/supabase/mutations";
import { createClient } from "@midday/supabase/server";
import { revalidateTag } from "next/cache";
import { action } from "./safe-action";
import { connectBankAccountSchema } from "./schema";

export const connectBankAccountAction = action(
connectBankAccountSchema,
async (value) => {
async ({ accounts }) => {
const user = await getUser();
const supabase = createClient();
const teamId = user.data.team_id;

return value;
const { data } = await createBankAccounts(supabase, accounts);

// const event = await client.sendEvent({
// name: Events.TRANSACTIONS_INITIAL_SYNC,
// payload: {
// id,
// accountId,
// teamId,
// },
// });

revalidateTag(`bank_connections_${teamId}`);

return {
event,
data,
};
}
);
15 changes: 0 additions & 15 deletions apps/dashboard/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
import { env } from "@/env.mjs";
import { getUser } from "@midday/supabase/cached-queries";
import {
createBankAccounts,
createEnrichmentTransaction,
updateSimilarTransactions,
updateTransaction,
} from "@midday/supabase/mutations";
import { createClient } from "@midday/supabase/server";
import { revalidateTag } from "next/cache";
import { invalidateCacheAction } from "./invalidate-cache-action";

const baseUrl = "https://api.resend.com";
Expand Down Expand Up @@ -58,19 +56,6 @@ export async function subscribeEmail(formData: FormData, userGroup: string) {
return json;
}

export async function createBankAccountsAction(accounts) {
const supabase = await createClient();
const { data } = await createBankAccounts(supabase, accounts);
const teamId = data.at(0).team_id;

// TODO: Send event to trigger.dev
// Kick off initial fetch and setup scheduler

revalidateTag(`bank_connections_${teamId}`);

return data;
}

export async function updateTransactionAction(id: string, payload: any) {
const supabase = await createClient();
const { data } = await updateTransaction(supabase, id, payload);
Expand Down
5 changes: 5 additions & 0 deletions apps/dashboard/src/actions/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,8 @@ export const voteSchema = z.object({
revalidatePath: z.string(),
id: z.string(),
});

export const connectBankAccountSchema = z.object({
accountId: z.string(),
id: z.string(),
});
19 changes: 16 additions & 3 deletions apps/dashboard/src/components/modals/select-account-modal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { createBankAccountsAction } from "@/actions";
import { connectBankAccountAction } from "@/actions/connect-bank-account-action";
import { zodResolver } from "@hookform/resolvers/zod";
import { getAccounts } from "@midday/gocardless";
import { Avatar, AvatarImage } from "@midday/ui/avatar";
Expand All @@ -21,8 +21,10 @@ import {
FormLabel,
} from "@midday/ui/form";
import { Skeleton } from "@midday/ui/skeleton";
import { useToast } from "@midday/ui/use-toast";
import { capitalCase } from "change-case";
import { Loader2 } from "lucide-react";
import { useAction } from "next-safe-action/hook";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { useEffect, useState } from "react";
import { useForm } from "react-hook-form";
Expand Down Expand Up @@ -54,6 +56,8 @@ function RowsSkeleton() {
}

export function SelectAccountModal({ countryCode }) {
const { toast } = useToast();

const searchParams = useSearchParams();
const router = useRouter();
const [accounts, setAccounts] = useState([]);
Expand All @@ -62,6 +66,16 @@ export function SelectAccountModal({ countryCode }) {
const isOpen =
searchParams.get("step") === "account" && !searchParams.has("error");

const action = useAction(connectBankAccountAction, {
onError: () => {
toast({
duration: 3500,
variant: "error",
title: "Something went wrong pleaase try again.",
});
},
});

const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
Expand Down Expand Up @@ -89,8 +103,7 @@ export function SelectAccountModal({ countryCode }) {
},
}));

// Change to use safe-action and listen to loading and disable modal close and button
await createBankAccountsAction(accountsWithDetails);
action.execute(accountsWithDetails);
}

useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/src/components/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { TeamMenu } from "./team-menu";
export function Sidebar() {
return (
<aside className="h-screen flex-shrink-0 flex-col justify-between flex sticky top-0 ml-4 pb-4 items-center">
<div className="flex flex-col items-center justify-center xl:items-start xl:justify-start">
<div className="flex flex-col items-center justify-center">
<div className="mt-6">
<Link href="/">
<Icons.LogoSmall />
Expand Down
4 changes: 2 additions & 2 deletions apps/dashboard/src/components/team-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export async function TeamMenu() {
</DialogFooter>
</div>
</DialogContent>
<DropdownMenuItem>Members</DropdownMenuItem>
<DropdownMenuItem>Settings</DropdownMenuItem>
{/* <DropdownMenuItem>Members</DropdownMenuItem>
<DropdownMenuItem>Settings</DropdownMenuItem> */}
</Dialog>
</DropdownMenuContent>
</DropdownMenu>
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/src/styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ body {
}

:root {
--sidebar-width: 662px;
--sidebar-width: 642px;

/* Counter */
--transition: 1;
Expand Down
Binary file modified bun.lockb
Binary file not shown.
122 changes: 66 additions & 56 deletions packages/jobs/src/transactions/initial.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,74 @@
import { getTransactions } from "@midday/gocardless";
import { eventTrigger } from "@trigger.dev/sdk";
import { revalidateTag } from "next/cache";
import { z } from "zod";
import { client, supabase } from "../client";
import { Events, Jobs } from "../constants";
import { transformTransactions } from "../utils";
// import { getTransactions } from "@midday/gocardless";
// import { eventTrigger } from "@trigger.dev/sdk";
// import { revalidateTag } from "next/cache";
// import { z } from "zod";
// import { client, supabase } from "../client";
// import { Events, Jobs } from "../constants";
// import { transformTransactions } from "../utils";
// import { scheduler } from "./scheduler";

client.defineJob({
id: Jobs.TRANSACTIONS_INITIAL_SYNC,
name: "🔂 Transactions - Initial Sync",
version: "1.0.2",
trigger: eventTrigger({
name: Events.TRANSACTIONS_INITIAL_SYNC,
schema: z.object({
accountId: z.string(),
teamId: z.string(),
recordId: z.string(),
}),
}),
integrations: { supabase },
run: async (payload, io) => {
const { accountId, teamId, recordId } = payload;
// client.defineJob({
// id: Jobs.TRANSACTIONS_INITIAL_SYNC,
// name: "🔂 Transactions - Initial Sync",
// version: "1.0.2",
// trigger: eventTrigger({
// name: Events.TRANSACTIONS_INITIAL_SYNC,
// schema: z.object({
// accountId: z.string(),
// teamId: z.string(),
// id: z.string(),
// }),
// }),
// integrations: { supabase },
// run: async (payload, io) => {
// const { accountId, teamId, id } = payload;

const { transactions } = await getTransactions(accountId);
// const { transactions } = await getTransactions(accountId);

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

const { data: transactionsData, error } = await io.supabase.client
.from("transactions")
.insert(
transformTransactions(transactions?.booked, {
accountId: recordId, // Bank account record id
teamId,
})
)
.select();
// const { data: transactionsData, error } = await io.supabase.client
// .from("transactions")
// .insert(
// transformTransactions(transactions?.booked, {
// accountId: id, // Bank account record id
// teamId,
// })
// )
// .select();

if (transactionsData?.length && transactionsData.length > 0) {
revalidateTag(`transactions_${teamId}`);
revalidateTag(`spending_${teamId}`);
revalidateTag(`metrics_${teamId}`);
// if (transactionsData?.length && transactionsData.length > 0) {
// revalidateTag(`transactions_${teamId}`);
// revalidateTag(`spending_${teamId}`);
// revalidateTag(`metrics_${teamId}`);

await io.sendEvent("💅 Enrich Transactions", {
name: Events.TRANSACTIONS_ENCRICHMENT,
payload: {
teamId,
},
});
}
// await io.sendEvent("💅 Enrich Transactions", {
// name: Events.TRANSACTIONS_ENCRICHMENT,
// payload: {
// teamId,
// },
// });
// }

if (error) {
await io.logger.error(JSON.stringify(error, null, 2));
}
// if (error) {
// await io.logger.error(JSON.stringify(error, null, 2));
// }

await io.logger.info(`Transactions Created: ${transactionsData?.length}`);
},
});
// await io.logger.info(`Transactions Created: ${transactionsData?.length}`);

// // use the bank account row id as the id for the DynamicSchedule
// // so it comes through to run() in the context source.id
// await scheduler.register(id, {
// type: "interval",
// options: {
// seconds: 3600, // every 1h
// },
// });
// },
// });
2 changes: 1 addition & 1 deletion packages/supabase/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"@supabase/ssr": "^0.0.10",
"@supabase/supabase-js": "^2.39.1",
"supabase": "^1.123.4"
"supabase": "^1.127.3"
},
"devDependencies": {
"typescript": "^5.3.3"
Expand Down

0 comments on commit f532b36

Please sign in to comment.