-
Notifications
You must be signed in to change notification settings - Fork 595
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #337 from midday-ai/feature/jobs
Feature/jobs
- Loading branch information
Showing
20 changed files
with
320 additions
and
211 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { client } from "@midday/engine/client"; | ||
import { createClient } from "@midday/supabase/job"; | ||
import { logger, schedules } from "@trigger.dev/sdk/v3"; | ||
import { processBatch } from "jobs/utils/process-batch"; | ||
|
||
export const ratesScheduler = schedules.task({ | ||
id: "rates-scheduler", | ||
cron: "0 0,12 * * *", | ||
run: async () => { | ||
// Only run in production (Set in Trigger.dev) | ||
if (process.env.TRIGGER_ENVIRONMENT !== "production") return; | ||
|
||
const supabase = createClient(); | ||
|
||
const ratesResponse = await client.rates.$get(); | ||
|
||
if (!ratesResponse.ok) { | ||
logger.error("Failed to get rates"); | ||
throw new Error("Failed to get rates"); | ||
} | ||
|
||
const { data: ratesData } = await ratesResponse.json(); | ||
|
||
const data = ratesData.flatMap((rate) => { | ||
return Object.entries(rate.rates).map(([target, value]) => ({ | ||
base: rate.source, | ||
target: target, | ||
rate: value, | ||
updated_at: rate.date, | ||
})); | ||
}); | ||
|
||
await processBatch(data, 500, async (batch) => { | ||
await supabase.from("exchange_rates").upsert(batch, { | ||
onConflict: "base, target", | ||
ignoreDuplicates: false, | ||
}); | ||
|
||
return batch; | ||
}); | ||
}, | ||
}); |
Empty file.
92 changes: 92 additions & 0 deletions
92
apps/dashboard/jobs/tasks/transactions/update-account-base-currency.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { createClient } from "@midday/supabase/job"; | ||
import { logger, schemaTask } from "@trigger.dev/sdk/v3"; | ||
import { | ||
getAccountBalance, | ||
getTransactionAmount, | ||
} from "jobs/utils/base-currency"; | ||
import { processBatch } from "jobs/utils/process-batch"; | ||
import { z } from "zod"; | ||
|
||
const BATCH_LIMIT = 500; | ||
|
||
export const updateAccountBaseCurrency = schemaTask({ | ||
id: "update-account-base-currency", | ||
schema: z.object({ | ||
accountId: z.string().uuid(), | ||
currency: z.string(), | ||
balance: z.number(), | ||
baseCurrency: z.string(), | ||
}), | ||
maxDuration: 300, | ||
queue: { | ||
concurrencyLimit: 10, | ||
}, | ||
run: async ({ accountId, currency, balance, baseCurrency }) => { | ||
const supabase = createClient(); | ||
|
||
const { data: exchangeRate } = await supabase | ||
.from("exchange_rates") | ||
.select("rate") | ||
.eq("base", currency) | ||
.eq("target", baseCurrency) | ||
.single(); | ||
|
||
if (!exchangeRate) { | ||
logger.info("No exchange rate found", { | ||
currency, | ||
baseCurrency, | ||
}); | ||
|
||
return; | ||
} | ||
|
||
// Update account base balance and base currency | ||
// based on the new currency exchange rate | ||
await supabase | ||
.from("bank_accounts") | ||
.update({ | ||
base_balance: getAccountBalance({ | ||
currency: currency, | ||
balance, | ||
baseCurrency, | ||
rate: exchangeRate.rate, | ||
}), | ||
base_currency: baseCurrency, | ||
}) | ||
.eq("id", accountId); | ||
|
||
const { data: transactionsData } = await supabase.rpc( | ||
"get_all_transactions_by_account", | ||
{ | ||
account_id: accountId, | ||
}, | ||
); | ||
|
||
const formattedTransactions = transactionsData?.map( | ||
// Exclude fts_vector from the transaction object because it's a generated column | ||
({ fts_vector, ...transaction }) => ({ | ||
...transaction, | ||
base_amount: getTransactionAmount({ | ||
amount: transaction.amount, | ||
currency: transaction.currency, | ||
baseCurrency, | ||
rate: exchangeRate?.rate, | ||
}), | ||
base_currency: baseCurrency, | ||
}), | ||
); | ||
|
||
await processBatch( | ||
formattedTransactions ?? [], | ||
BATCH_LIMIT, | ||
async (batch) => { | ||
await supabase.from("transactions").upsert(batch, { | ||
onConflict: "internal_id", | ||
ignoreDuplicates: false, | ||
}); | ||
|
||
return batch; | ||
}, | ||
); | ||
}, | ||
}); |
45 changes: 45 additions & 0 deletions
45
apps/dashboard/jobs/tasks/transactions/update-base-currency.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { createClient } from "@midday/supabase/job"; | ||
import { schemaTask } from "@trigger.dev/sdk/v3"; | ||
import { revalidateCache } from "jobs/utils/revalidate-cache"; | ||
import { triggerSequenceAndWait } from "jobs/utils/trigger-sequence"; | ||
import { z } from "zod"; | ||
import { updateAccountBaseCurrency } from "./update-account-base-currency"; | ||
|
||
export const updateBaseCurrency = schemaTask({ | ||
id: "update-base-currency", | ||
schema: z.object({ | ||
teamId: z.string().uuid(), | ||
baseCurrency: z.string(), | ||
}), | ||
maxDuration: 300, | ||
queue: { | ||
concurrencyLimit: 10, | ||
}, | ||
run: async ({ teamId, baseCurrency }) => { | ||
const supabase = createClient(); | ||
|
||
// Get all enabled accounts | ||
const { data: accountsData } = await supabase | ||
.from("bank_accounts") | ||
.select("id, currency, balance") | ||
.eq("team_id", teamId) | ||
.eq("enabled", true); | ||
|
||
if (!accountsData) { | ||
return; | ||
} | ||
|
||
const formattedAccounts = accountsData.map((account) => ({ | ||
accountId: account.id, | ||
currency: account.currency, | ||
balance: account.balance, | ||
baseCurrency, | ||
})); | ||
|
||
await triggerSequenceAndWait(formattedAccounts, updateAccountBaseCurrency, { | ||
delayMinutes: 0, | ||
}); | ||
|
||
await revalidateCache({ tag: "bank", id: teamId }); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
type GetAccountBalanceParams = { | ||
currency: string; | ||
balance: number; | ||
baseCurrency: string; | ||
rate: number | null; | ||
}; | ||
|
||
export function getAccountBalance({ | ||
currency, | ||
balance, | ||
baseCurrency, | ||
rate, | ||
}: GetAccountBalanceParams) { | ||
if (currency === baseCurrency) { | ||
return balance; | ||
} | ||
|
||
return +(balance * (rate ?? 1)).toFixed(2); | ||
} | ||
|
||
type GetTransactionAmountParams = { | ||
amount: number; | ||
currency: string; | ||
baseCurrency: string; | ||
rate: number | null; | ||
}; | ||
|
||
export function getTransactionAmount({ | ||
amount, | ||
currency, | ||
baseCurrency, | ||
rate, | ||
}: GetTransactionAmountParams) { | ||
if (currency === baseCurrency) { | ||
return amount; | ||
} | ||
|
||
return +(amount * (rate ?? 1)).toFixed(2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.