feat: add scoped shortcuts

This commit is contained in:
Max Koon
2025-12-12 18:44:18 -05:00
parent 27f6e627d4
commit c6dd174376
13 changed files with 451 additions and 138 deletions

View File

@@ -1,6 +1,6 @@
import type { Transaction } from "@rocicorp/zero";
import { authDataSchema, type AuthData } from "./auth";
import { type Schema } from "./zero-schema.gen";
import { type Category, type Schema } from "./zero-schema.gen";
import { isLoggedIn } from "./zql";
type Tx = Transaction<Schema>;
@@ -8,10 +8,10 @@ type Tx = Transaction<Schema>;
export function createMutators(authData: AuthData | null) {
return {
link: {
async create() { },
async get(tx: Tx, { link_token }: { link_token: string }) { },
async updateTransactions() { },
async updateBalences() { },
async create() {},
async get(tx: Tx, { link_token }: { link_token: string }) {},
async updateTransactions() {},
async updateBalences() {},
async deleteAccounts(tx: Tx, { accountIds }: { accountIds: string[] }) {
isLoggedIn(authData);
for (const id of accountIds) {
@@ -74,15 +74,29 @@ export function createMutators(authData: AuthData | null) {
id,
budgetId,
order,
}: { id: string; budgetId: string; order?: number },
}: { id: string; budgetId: string; order: number },
) {
isLoggedIn(authData);
if (order != undefined) {
const after = await tx.query.category
.where("budgetId", "=", budgetId)
.where("order", ">", order);
after.forEach((item) => {
tx.mutate.category.update({
id: item.id,
order: item.order + 1,
});
});
}
tx.mutate.category.insert({
id,
budgetId,
amount: 0,
every: "week",
order: order || 0,
order: order + 1,
label: "My category",
color: "#f06",
createdBy: authData.user.id,
@@ -90,20 +104,46 @@ export function createMutators(authData: AuthData | null) {
},
async deleteCategory(tx: Tx, { id }: { id: string }) {
isLoggedIn(authData);
const item = await tx.query.category.where("id", "=", id).one();
if (!item) throw Error("Item does not exist");
tx.mutate.category.update({
id,
removedAt: new Date().getTime(),
removedBy: authData.user.id,
});
const after = await tx.query.category
.where("budgetId", "=", item.budgetId)
.where("order", ">", item.order)
.run();
for (const item of after) {
tx.mutate.category.update({ id: item.id, order: item.order - 1 });
}
// after.forEach((item) => {
// });
},
async updateCategory(
tx: Tx,
{ id, label }: { id: string; label: string },
{
id,
label,
order,
amount,
every,
}: {
id: string;
label?: string;
order?: number;
amount?: number;
every?: Category["every"];
},
) {
isLoggedIn(authData);
tx.mutate.category.update({
id,
label,
order,
amount,
every,
});
},
},

View File

@@ -67,7 +67,7 @@ export const queries = {
isLoggedIn(authData);
return builder.budget
.related("categories", (q) =>
q.where("removedAt", "IS", null).orderBy("order", "desc"),
q.where("removedAt", "IS", null).orderBy("order", "asc"),
)
.limit(10);
},