feat: update multiple balances
This commit is contained in:
@@ -23,8 +23,8 @@ import { getHono } from "./hono";
|
|||||||
import { Configuration, CountryCode, PlaidApi, PlaidEnvironments, Products } from "plaid";
|
import { Configuration, CountryCode, PlaidApi, PlaidEnvironments, Products } from "plaid";
|
||||||
import { randomUUID } from "crypto";
|
import { randomUUID } from "crypto";
|
||||||
import { db } from "./db";
|
import { db } from "./db";
|
||||||
import { plaidAccessTokens, plaidLink, transaction } from "@money/shared/db";
|
import { balance, plaidAccessTokens, plaidLink, transaction } from "@money/shared/db";
|
||||||
import { eq } from "drizzle-orm";
|
import { asc, desc, eq, sql } from "drizzle-orm";
|
||||||
|
|
||||||
|
|
||||||
const configuration = new Configuration({
|
const configuration = new Configuration({
|
||||||
@@ -118,11 +118,39 @@ const createMutators = (authData: AuthData | null) => {
|
|||||||
id: randomUUID(),
|
id: randomUUID(),
|
||||||
user_id: authData.user.id,
|
user_id: authData.user.id,
|
||||||
name: t.name,
|
name: t.name,
|
||||||
amount: t.amount,
|
amount: t.amount.toString(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
},
|
||||||
|
async updateBalences() {
|
||||||
|
isLoggedIn(authData);
|
||||||
|
const accounts = await db.query.plaidAccessTokens.findMany({
|
||||||
|
where: eq(plaidAccessTokens.userId, authData.user.id),
|
||||||
|
});
|
||||||
|
if (accounts.length == 0) {
|
||||||
|
console.error("No accounts");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const account of accounts) {
|
||||||
|
const { data } = await plaidClient.accountsBalanceGet({
|
||||||
|
access_token: account.token
|
||||||
|
});
|
||||||
|
await db.insert(balance).values(data.accounts.map(bal => ({
|
||||||
|
id: randomUUID(),
|
||||||
|
user_id: authData.user.id,
|
||||||
|
plaid_id: bal.account_id,
|
||||||
|
avaliable: bal.balances.available as any,
|
||||||
|
current: bal.balances.current as any,
|
||||||
|
name: bal.name,
|
||||||
|
}))).onConflictDoUpdate({
|
||||||
|
target: balance.plaid_id,
|
||||||
|
set: { current: sql.raw(`excluded.${balance.current.name}`), avaliable: sql.raw(`excluded.${balance.avaliable.name}`) }
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
},
|
||||||
}
|
}
|
||||||
} as const satisfies Mutators;
|
} as const satisfies Mutators;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { SafeAreaView } from 'react-native-safe-area-context';
|
|
||||||
import { authClient } from '@/lib/auth-client';
|
import { authClient } from '@/lib/auth-client';
|
||||||
import { Button, Linking, ScrollView, Text, View } from 'react-native';
|
import { Button, Linking, ScrollView, Text, View } from 'react-native';
|
||||||
import { useQuery, useZero } from "@rocicorp/zero/react";
|
import { useQuery, useZero } from "@rocicorp/zero/react";
|
||||||
@@ -11,6 +10,7 @@ export default function HomeScreen() {
|
|||||||
const z = useZero<Schema, Mutators>();
|
const z = useZero<Schema, Mutators>();
|
||||||
const [plaidLink] = useQuery(queries.getPlaidLink(session));
|
const [plaidLink] = useQuery(queries.getPlaidLink(session));
|
||||||
const [transactions] = useQuery(queries.allTransactions(session));
|
const [transactions] = useQuery(queries.allTransactions(session));
|
||||||
|
const [balances] = useQuery(queries.getBalances(session));
|
||||||
|
|
||||||
const [idx, setIdx] = useState(0);
|
const [idx, setIdx] = useState(0);
|
||||||
|
|
||||||
@@ -28,7 +28,6 @@ export default function HomeScreen() {
|
|||||||
|
|
||||||
window.addEventListener("keydown", handleKeyDown);
|
window.addEventListener("keydown", handleKeyDown);
|
||||||
|
|
||||||
// Cleanup listener on unmount
|
|
||||||
return () => {
|
return () => {
|
||||||
window.removeEventListener("keydown", handleKeyDown);
|
window.removeEventListener("keydown", handleKeyDown);
|
||||||
};
|
};
|
||||||
@@ -39,6 +38,16 @@ export default function HomeScreen() {
|
|||||||
{plaidLink && <Button onPress={() => {
|
{plaidLink && <Button onPress={() => {
|
||||||
z.mutate.link.updateTransactions();
|
z.mutate.link.updateTransactions();
|
||||||
}} title="Update transactions" />}
|
}} title="Update transactions" />}
|
||||||
|
{plaidLink && <Button onPress={() => {
|
||||||
|
z.mutate.link.updateBalences();
|
||||||
|
}} title="Update bal" />}
|
||||||
|
|
||||||
|
<View style={{ backgroundColor: 'red' }}>
|
||||||
|
{balances.map(bal => <View>
|
||||||
|
<Text style={{ fontFamily: 'mono', color: 'white' }}>{bal.name}: {bal.current} ({bal.avaliable})</Text>
|
||||||
|
</View>)}
|
||||||
|
</View>
|
||||||
|
|
||||||
{transactions.map((t, i) => <View style={{ backgroundColor: i == idx ? 'black' : undefined }} key={t.id}>
|
{transactions.map((t, i) => <View style={{ backgroundColor: i == idx ? 'black' : undefined }} key={t.id}>
|
||||||
<Text style={{ fontFamily: 'mono', color: i == idx ? 'white' : undefined }}>{t.name} {t.amount}</Text>
|
<Text style={{ fontFamily: 'mono', color: i == idx ? 'white' : undefined }}>{t.name} {t.amount}</Text>
|
||||||
</View>)}
|
</View>)}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { definePermissions } from "@rocicorp/zero";
|
||||||
import { pgTable, text, boolean, timestamp, uniqueIndex, decimal } from "drizzle-orm/pg-core";
|
import { pgTable, text, boolean, timestamp, uniqueIndex, decimal } from "drizzle-orm/pg-core";
|
||||||
|
|
||||||
export const users = pgTable(
|
export const users = pgTable(
|
||||||
@@ -29,3 +30,13 @@ export const plaidLink = pgTable("plaidLink", {
|
|||||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const balance = pgTable("balance", {
|
||||||
|
id: text("id").primaryKey(),
|
||||||
|
user_id: text("userId").notNull(),
|
||||||
|
plaid_id: text("plaidId").notNull().unique(),
|
||||||
|
avaliable: decimal("avaliable").notNull(),
|
||||||
|
current: decimal("current").notNull(),
|
||||||
|
name: text("name").notNull(),
|
||||||
|
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||||
|
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
||||||
|
})
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ export function createMutators(authData: AuthData | null) {
|
|||||||
async create() {},
|
async create() {},
|
||||||
async get(tx: Tx, { link_token }: { link_token: string }) {},
|
async get(tx: Tx, { link_token }: { link_token: string }) {},
|
||||||
async updateTransactions() {},
|
async updateTransactions() {},
|
||||||
|
async updateBalences() {},
|
||||||
}
|
}
|
||||||
} as const;
|
} as const;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,4 +25,9 @@ export const queries = {
|
|||||||
.orderBy('createdAt', 'desc')
|
.orderBy('createdAt', 'desc')
|
||||||
.one();
|
.one();
|
||||||
}),
|
}),
|
||||||
|
getBalances: syncedQueryWithContext('getBalances', z.tuple([]), (authData: AuthData | null) => {
|
||||||
|
isLoggedIn(authData);
|
||||||
|
return builder.balance
|
||||||
|
.where('user_id', '=', authData.user.id);
|
||||||
|
})
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -21,6 +21,88 @@ type ZeroSchema = DrizzleToZeroSchema<typeof drizzleSchema>;
|
|||||||
*/
|
*/
|
||||||
export const schema = {
|
export const schema = {
|
||||||
tables: {
|
tables: {
|
||||||
|
balance: {
|
||||||
|
name: "balance",
|
||||||
|
columns: {
|
||||||
|
id: {
|
||||||
|
type: "string",
|
||||||
|
optional: false,
|
||||||
|
customType: null as unknown as ZeroCustomType<
|
||||||
|
ZeroSchema,
|
||||||
|
"balance",
|
||||||
|
"id"
|
||||||
|
>,
|
||||||
|
},
|
||||||
|
user_id: {
|
||||||
|
type: "string",
|
||||||
|
optional: false,
|
||||||
|
customType: null as unknown as ZeroCustomType<
|
||||||
|
ZeroSchema,
|
||||||
|
"balance",
|
||||||
|
"user_id"
|
||||||
|
>,
|
||||||
|
serverName: "userId",
|
||||||
|
},
|
||||||
|
plaid_id: {
|
||||||
|
type: "string",
|
||||||
|
optional: false,
|
||||||
|
customType: null as unknown as ZeroCustomType<
|
||||||
|
ZeroSchema,
|
||||||
|
"balance",
|
||||||
|
"plaid_id"
|
||||||
|
>,
|
||||||
|
serverName: "plaidId",
|
||||||
|
},
|
||||||
|
avaliable: {
|
||||||
|
type: "number",
|
||||||
|
optional: false,
|
||||||
|
customType: null as unknown as ZeroCustomType<
|
||||||
|
ZeroSchema,
|
||||||
|
"balance",
|
||||||
|
"avaliable"
|
||||||
|
>,
|
||||||
|
},
|
||||||
|
current: {
|
||||||
|
type: "number",
|
||||||
|
optional: false,
|
||||||
|
customType: null as unknown as ZeroCustomType<
|
||||||
|
ZeroSchema,
|
||||||
|
"balance",
|
||||||
|
"current"
|
||||||
|
>,
|
||||||
|
},
|
||||||
|
name: {
|
||||||
|
type: "string",
|
||||||
|
optional: false,
|
||||||
|
customType: null as unknown as ZeroCustomType<
|
||||||
|
ZeroSchema,
|
||||||
|
"balance",
|
||||||
|
"name"
|
||||||
|
>,
|
||||||
|
},
|
||||||
|
createdAt: {
|
||||||
|
type: "number",
|
||||||
|
optional: true,
|
||||||
|
customType: null as unknown as ZeroCustomType<
|
||||||
|
ZeroSchema,
|
||||||
|
"balance",
|
||||||
|
"createdAt"
|
||||||
|
>,
|
||||||
|
serverName: "created_at",
|
||||||
|
},
|
||||||
|
updatedAt: {
|
||||||
|
type: "number",
|
||||||
|
optional: true,
|
||||||
|
customType: null as unknown as ZeroCustomType<
|
||||||
|
ZeroSchema,
|
||||||
|
"balance",
|
||||||
|
"updatedAt"
|
||||||
|
>,
|
||||||
|
serverName: "updated_at",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
primaryKey: ["id"],
|
||||||
|
},
|
||||||
plaidLink: {
|
plaidLink: {
|
||||||
name: "plaidLink",
|
name: "plaidLink",
|
||||||
columns: {
|
columns: {
|
||||||
@@ -199,6 +281,11 @@ export const schema = {
|
|||||||
* This type is auto-generated from your Drizzle schema definition.
|
* This type is auto-generated from your Drizzle schema definition.
|
||||||
*/
|
*/
|
||||||
export type Schema = typeof schema;
|
export type Schema = typeof schema;
|
||||||
|
/**
|
||||||
|
* Represents a row from the "balance" table.
|
||||||
|
* This type is auto-generated from your Drizzle schema definition.
|
||||||
|
*/
|
||||||
|
export type Balance = Row<Schema["tables"]["balance"]>;
|
||||||
/**
|
/**
|
||||||
* Represents a row from the "plaidLink" table.
|
* Represents a row from the "plaidLink" table.
|
||||||
* This type is auto-generated from your Drizzle schema definition.
|
* This type is auto-generated from your Drizzle schema definition.
|
||||||
|
|||||||
Reference in New Issue
Block a user