Skip to content

Commit

Permalink
Fix query
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Dec 29, 2023
1 parent e527a02 commit d94302a
Show file tree
Hide file tree
Showing 5 changed files with 481 additions and 402 deletions.
21 changes: 9 additions & 12 deletions apps/dashboard/src/actions/connect-bank-account-action.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use server";

import { getTransactions, transformTransactions } from "@midday/gocardless";
import { Events, scheduler } from "@midday/jobs";
import { scheduler } from "@midday/jobs";
import { getUser } from "@midday/supabase/cached-queries";
import { createBankAccounts } from "@midday/supabase/mutations";
import { createClient } from "@midday/supabase/server";
Expand All @@ -18,7 +18,7 @@ export const connectBankAccountAction = action(

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

const promisses = data?.map(async (account) => {
const promises = data?.map(async (account) => {
// Fetch transactions for each account
const { transactions } = await getTransactions(account.account_id);

Expand All @@ -31,15 +31,12 @@ export const connectBankAccountAction = action(
.eq("id", account.id);

// Create transactions
await supabase
.from("transactions")
.insert(
transformTransactions(transactions?.booked, {
accountId: account.id, // Bank account row id
teamId,
})
)
.select();
await supabase.from("transactions").insert(
transformTransactions(transactions?.booked, {
accountId: account.id, // Bank account row id
teamId,
})
);

// Schedule sync for each account
await scheduler.register(account.id, {
Expand All @@ -52,7 +49,7 @@ export const connectBankAccountAction = action(
return;
});

await Promise.all(promisses);
await Promise.all(promises);

revalidateTag(`bank_connections_${teamId}`);
revalidateTag(`transactions_${teamId}`);
Expand Down
1 change: 0 additions & 1 deletion packages/gocardless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ export const transformTransactions = (transactions, { teamId, accountId }) => {
return {
transaction_id: data.transactionId,
reference: data.entryReference,
booking_date: data.bookingDate,
date: data.valueDate,
name: capitalCase(data.additionalInformation),
original: data.additionalInformation,
Expand Down
2 changes: 1 addition & 1 deletion packages/supabase/src/queries/cached-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export const getTeams = async () => {
["teams", userId],
{
tags: [`teams_${userId}`],
revalidate: 10,
revalidate: 180,
}
)();
};
43 changes: 16 additions & 27 deletions packages/supabase/src/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ export async function getSpendingQuery(
`
currency,
category,
amount,
enrichment:enrichment_id(category)
amount
`
)
.order("order", { ascending: false })
Expand All @@ -157,9 +156,9 @@ export async function getSpendingQuery(
const combinedValues = {};

for (const item of data) {
const { category, amount, currency, enrichment } = item;
const { category, amount, currency } = item;

const key = category || enrichment?.category || "uncategorized";
const key = category || "uncategorized";

if (combinedValues[key]) {
combinedValues[key].amount += amount;
Expand All @@ -175,11 +174,9 @@ export async function getSpendingQuery(
currency: data?.at(0)?.currency,
},
data: Object.entries(combinedValues)
.map(([category, { amount, currency, enrichment }]) => {
.map(([category, { amount, currency }]) => {
return {
category: !category
? "uncategorized"
: category || enrichment?.category,
category: category || "uncategorized",
currency,
amount: +Math.abs(amount).toFixed(2),
};
Expand Down Expand Up @@ -229,7 +226,6 @@ export async function getTransactionsQuery(
`
*,
assigned:assigned_id(*),
enrichment:transaction_enrichments(category),
attachments(*)
`,
{ count: "exact" }
Expand Down Expand Up @@ -272,22 +268,20 @@ export async function getTransactionsQuery(
`
*,
assigned:assigned_id(*),
enrichment:transaction_enrichments!inner(category),
attachments(*)
`
);

const matchCategory = categories
.map((category) => `category.eq.${category}`)
.map((category) => {
if (category === "uncategorized") {
return "category.is.null";
}
return `category.eq.${category}`;
})
.join(",");

query
// .or(matchCategory)
.or(matchCategory, { referencedTable: "enrichment" });
}

if (categories?.includes("uncategorized")) {
query.not("category", "is", null);
query.or(matchCategory);
}

if (type === "expense") {
Expand All @@ -296,8 +290,7 @@ export async function getTransactionsQuery(
}

if (type === "income") {
query.gt("amount", 0);
query.neq("category", "transfer");
query.eq("category", "income");
}

const { data, count } = await query.range(from, to).throwOnError();
Expand Down Expand Up @@ -327,10 +320,7 @@ export async function getTransactionsQuery(
},
data: data?.map((transaction) => ({
...transaction,
category:
transaction?.category ||
transaction?.enrichment?.category ||
"uncategorized",
category: transaction?.category || "uncategorized",
})),
};
}
Expand All @@ -342,7 +332,6 @@ export async function getTransactionQuery(supabase: Client, id: string) {
`
*,
assigned:assigned_id(*),
enrichment:enrichment_id(category),
attachments(*)
`
)
Expand All @@ -352,7 +341,7 @@ export async function getTransactionQuery(supabase: Client, id: string) {

return {
...data,
category: data?.category || data?.enrichment?.category || "uncategorized",
category: data?.category || "uncategorized",
};
}

Expand Down Expand Up @@ -409,7 +398,7 @@ export async function getMetricsQuery(
.neq("category", "transfer");

if (type === "income") {
query.gt("amount", 0);
query.eq("category", "income");
}

const { data } = await query.throwOnError();
Expand Down
Loading

0 comments on commit d94302a

Please sign in to comment.