53 lines
1.2 KiB
TypeScript
53 lines
1.2 KiB
TypeScript
import { mutation, query } from './_generated/server.js';
|
|
import { requireOwner } from './lib.js';
|
|
import {
|
|
dashboardRangeValidator,
|
|
editorModeValidator,
|
|
moodValidator,
|
|
themePreferenceValidator
|
|
} from './validators.js';
|
|
|
|
const defaults = {
|
|
theme: 'system' as const,
|
|
defaultMood: 'neutral' as const,
|
|
editorMode: 'split' as const,
|
|
dashboardRange: '30d' as const
|
|
};
|
|
|
|
export const get = query({
|
|
args: {},
|
|
handler: async (ctx) => {
|
|
const owner = await requireOwner(ctx);
|
|
const settings = await ctx.db
|
|
.query('settings')
|
|
.withIndex('by_owner', (q) => q.eq('owner', owner))
|
|
.unique();
|
|
|
|
return settings ?? defaults;
|
|
}
|
|
});
|
|
|
|
export const save = mutation({
|
|
args: {
|
|
theme: themePreferenceValidator,
|
|
defaultMood: moodValidator,
|
|
editorMode: editorModeValidator,
|
|
dashboardRange: dashboardRangeValidator
|
|
},
|
|
handler: async (ctx, args) => {
|
|
const owner = await requireOwner(ctx);
|
|
const existing = await ctx.db
|
|
.query('settings')
|
|
.withIndex('by_owner', (q) => q.eq('owner', owner))
|
|
.unique();
|
|
const next = { ...args, owner, updatedAt: Date.now() };
|
|
|
|
if (existing) {
|
|
await ctx.db.patch(existing._id, next);
|
|
return existing._id;
|
|
}
|
|
|
|
return await ctx.db.insert('settings', next);
|
|
}
|
|
});
|