initial commit
This commit is contained in:
72
convex/settings.ts
Normal file
72
convex/settings.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { query, mutation, internalQuery } from "./_generated/server";
|
||||
import { v } from "convex/values";
|
||||
import { requireUserId } from "./lib/helpers";
|
||||
|
||||
const settingsValidator = v.object({
|
||||
_id: v.id("appSettings"),
|
||||
_creationTime: v.number(),
|
||||
userId: v.id("users"),
|
||||
ownNames: v.array(v.string()),
|
||||
monthBasis: v.union(v.literal("effective"), v.literal("booking")),
|
||||
salaryShift: v.object({
|
||||
enabled: v.boolean(),
|
||||
categoryNames: v.array(v.string()),
|
||||
dayThreshold: v.number(),
|
||||
}),
|
||||
});
|
||||
|
||||
export const get = query({
|
||||
args: {},
|
||||
returns: v.union(settingsValidator, v.null()),
|
||||
handler: async (ctx) => {
|
||||
const userId = await requireUserId(ctx);
|
||||
return await ctx.db
|
||||
.query("appSettings")
|
||||
.withIndex("by_user", (q) => q.eq("userId", userId))
|
||||
.unique();
|
||||
},
|
||||
});
|
||||
|
||||
export const update = mutation({
|
||||
args: {
|
||||
ownNames: v.optional(v.array(v.string())),
|
||||
monthBasis: v.optional(v.union(v.literal("effective"), v.literal("booking"))),
|
||||
salaryShift: v.optional(
|
||||
v.object({
|
||||
enabled: v.boolean(),
|
||||
categoryNames: v.array(v.string()),
|
||||
dayThreshold: v.number(),
|
||||
}),
|
||||
),
|
||||
},
|
||||
returns: v.null(),
|
||||
handler: async (ctx, args) => {
|
||||
const userId = await requireUserId(ctx);
|
||||
const settings = await ctx.db
|
||||
.query("appSettings")
|
||||
.withIndex("by_user", (q) => q.eq("userId", userId))
|
||||
.unique();
|
||||
if (!settings) throw new Error("Einstellungen nicht gefunden");
|
||||
|
||||
const patch: Record<string, unknown> = {};
|
||||
if (args.ownNames !== undefined) patch.ownNames = args.ownNames;
|
||||
if (args.monthBasis !== undefined) patch.monthBasis = args.monthBasis;
|
||||
if (args.salaryShift !== undefined) patch.salaryShift = args.salaryShift;
|
||||
|
||||
if (Object.keys(patch).length > 0) {
|
||||
await ctx.db.patch(settings._id, patch);
|
||||
}
|
||||
return null;
|
||||
},
|
||||
});
|
||||
|
||||
export const getInternal = internalQuery({
|
||||
args: { userId: v.id("users") },
|
||||
returns: v.union(settingsValidator, v.null()),
|
||||
handler: async (ctx, args) => {
|
||||
return await ctx.db
|
||||
.query("appSettings")
|
||||
.withIndex("by_user", (q) => q.eq("userId", args.userId))
|
||||
.unique();
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user