59 lines
2.0 KiB
TypeScript
Executable File
59 lines
2.0 KiB
TypeScript
Executable File
import { handleApi } from "@/lib/api";
|
|
import { requireUser, updateUserProfile } from "@/lib/services/user";
|
|
import { db, spiritualConfig } from "@/lib/db";
|
|
import { eq } from "drizzle-orm";
|
|
import { normalizeSettingsTheme } from "@/lib/services/theme-migration";
|
|
import { migrateUserThemeInDb } from "@/lib/services/theme-migration";
|
|
import { seedPromptTemplatesForUser } from "@/lib/services/ai-templates";
|
|
import { getSettingsForUser, upsertSetting } from "@/lib/repositories/settings.repository";
|
|
import { parseJsonBody, settingsPatchSchema } from "@/lib/validation";
|
|
|
|
export async function GET() {
|
|
return handleApi(async () => {
|
|
const { user } = await requireUser();
|
|
const allSettings = await getSettingsForUser(user.id);
|
|
const settingsMap = Object.fromEntries(allSettings.map((s) => [s.key, s.value]));
|
|
const migratedTheme = await migrateUserThemeInDb(
|
|
user.id,
|
|
settingsMap.theme as string | undefined
|
|
);
|
|
settingsMap.theme = migratedTheme;
|
|
const [spiritual] = await db
|
|
.select()
|
|
.from(spiritualConfig)
|
|
.where(eq(spiritualConfig.userId, user.id));
|
|
await seedPromptTemplatesForUser(user.id);
|
|
return {
|
|
user,
|
|
settings: normalizeSettingsTheme(settingsMap),
|
|
spiritual,
|
|
};
|
|
});
|
|
}
|
|
|
|
export async function PATCH(request: Request) {
|
|
return handleApi(async () => {
|
|
const body = await parseJsonBody(request, settingsPatchSchema);
|
|
const { user } = await requireUser();
|
|
|
|
if (body.profile) {
|
|
await updateUserProfile(user.id, body.profile);
|
|
}
|
|
if (body.settings) {
|
|
for (const [key, value] of Object.entries(body.settings)) {
|
|
await upsertSetting(user.id, key, value);
|
|
}
|
|
}
|
|
if (body.spiritual) {
|
|
await db
|
|
.update(spiritualConfig)
|
|
.set({
|
|
prayerLabels: body.spiritual.prayerLabels,
|
|
litanyLabels: body.spiritual.litanyLabels,
|
|
})
|
|
.where(eq(spiritualConfig.userId, user.id));
|
|
}
|
|
return { ok: true };
|
|
});
|
|
}
|