feat: add scoped shortcuts
This commit is contained in:
@@ -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,
|
||||
});
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user