habits.andr33v.ru/server/api/user/visit.post.ts

31 lines
1.1 KiB
TypeScript

import { getUserIdFromSession } from '../../utils/auth';
import { calculateDailyStreak } from '../../utils/streak';
import prisma from '../../utils/prisma';
/**
* Registers a user's daily visit and calculates their new streak.
* This endpoint is idempotent. Calling it multiple times on the same day
* will not increment the streak further.
*/
export default defineEventHandler(async (event) => {
const userId = await getUserIdFromSession(event);
// Calculate the streak and create today's visit record
const updatedUser = await calculateDailyStreak(prisma, userId);
// The consumer of this endpoint needs the most up-to-date user info,
// including the newly calculated streak.
return {
id: updatedUser.id,
email: updatedUser.email,
nickname: updatedUser.nickname,
avatar: updatedUser.avatar,
coins: updatedUser.coins,
exp: updatedUser.exp,
dailyStreak: updatedUser.dailyStreak,
soundOn: updatedUser.soundOn,
confettiOn: updatedUser.confettiOn,
createdAt: updatedUser.createdAt,
updatedAt: updatedUser.updatedAt,
}
});