habits.andr33v.ru/server/api/admin/village/complete-clearing.post.ts

52 lines
1.4 KiB
TypeScript

// server/api/admin/village/complete-clearing.post.ts
import { getUserIdFromSession } from '../../../utils/auth';
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
export default defineEventHandler(async (event) => {
const userId = await getUserIdFromSession(event);
// Simple admin check
if (userId !== 1) {
throw createError({ statusCode: 403, statusMessage: 'Forbidden' });
}
const village = await prisma.village.findUniqueOrThrow({ where: { userId } });
const tilesToComplete = await prisma.villageTile.findMany({
where: {
villageId: village.id,
terrainState: 'CLEARING',
},
});
if (tilesToComplete.length === 0) {
return { success: true, message: 'No clearing tasks to complete.' };
}
await prisma.$transaction([
// Complete the tiles
prisma.villageTile.updateMany({
where: {
id: { in: tilesToComplete.map(t => t.id) },
},
data: {
terrainState: 'IDLE',
terrainType: 'EMPTY',
clearingStartedAt: null,
},
}),
// Give the user the rewards (1 coin and 1 exp per tile)
prisma.user.update({
where: { id: userId },
data: {
coins: { increment: tilesToComplete.length },
exp: { increment: tilesToComplete.length },
},
}),
]);
return { success: true, message: `Completed ${tilesToComplete.length} clearing tasks.` };
});