Files
pichunt/server/api/home.get.ts
T
2025-08-20 23:18:40 +02:00

110 lines
1.9 KiB
TypeScript

import prisma from '~~/lib/prisma';
async function getOwnHunts(userId: number | undefined) {
if (!userId) {
return null;
}
return prisma.hunt.findMany({
select: {
id: true,
name_de: true,
name_en: true,
description_de: true,
description_en: true,
picture: {
select: {
url: true
}
},
createdAt: true,
start: true,
end: true,
_count: {
select: {
teams: true,
quests: true
}
},
creator: {
select: {
name: true,
id: true
}
}
},
where: {
deletedAt: null,
teams: {
some: {
OR: [
{
ownerId: userId
},
{
members: {
some: {
memberId: userId
}
}
}
]
}
}
}
});
}
export default defineEventHandler(async (event) => {
const user = await getUserSession(event);
const userId = user.user?.id;
const own = await getOwnHunts(userId);
const others = await prisma.hunt.findMany({
select: {
id: true,
name_de: true,
name_en: true,
description_de: true,
description_en: true,
picture: {
select: {
url: true
}
},
createdAt: true,
start: true,
end: true,
_count: {
select: {
teams: true,
quests: true
}
},
creator: {
select: {
name: true,
id: true
}
}
},
where: {
deletedAt: null,
OR: [
{
start: {
gt: new Date()
}
},
{
start: null
}
],
allowJoin: true,
id: {
notIn: own?.map((h) => h.id) ?? []
}
}
});
return { own, others };
});