28 lines
796 B
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {
getActiveStreak,
resetStreak,
updateStreakSavings,
} from "../../db/streak";
/** PATCH /api/streak reset oder avg_monthly_savings aktualisieren */
export default defineEventHandler(async (event) => {
const user = await requireUser(event);
const body = await readBody(event);
const current = await getActiveStreak(user.id);
if (!current)
throw createError({ statusCode: 404, message: "Kein aktiver Streak" });
if (body.reset === true) {
const longest = Math.max(current.longestDays, current.currentDays);
const reason = body.reason ?? "manual";
return resetStreak(current.id, longest, user.id, reason);
}
if (body.avgMonthlySavings !== undefined) {
return updateStreakSavings(current.id, body.avgMonthlySavings);
}
return current;
});