Files
pichunt/server/api/admin/hunt/[huntId]/track/[userId].get.ts
T
Pascal 91d9bc4cbb
Docker Image CI / test (push) Successful in 1m2s
Tests / unit (push) Successful in 1m2s
Tests / integration (push) Successful in 1m39s
Docker Image CI / deploy (push) Successful in 1m24s
style(prettier): Fix formatting
2026-09-27 21:00:09 +02:00

56 lines
1.4 KiB
TypeScript

import type { UserTrail } from '#shared/types/map';
import { useValidatedParams, z, zh } from 'h3-zod';
import prisma from '~~/lib/prisma';
/**
* Full TrackPoint trail for a single user in a hunt, fetched lazily when an
* organizer clicks a user on the live map.
*
* Gated by HuntMember membership AND `hunt.trackDistance` (404 otherwise).
* Plain JSON: `recordedAt` as ISO strings, coordinates coerced to numbers.
*/
export default defineEventHandler(async (event): Promise<UserTrail> => {
const user = await requireUserSession(event);
const callerId = user.user.id;
const { huntId, userId } = await useValidatedParams(
event,
z.object({
huntId: zh.numAsString,
userId: zh.numAsString
})
);
const hunt = await prisma.hunt.findUnique({
where: {
id: huntId,
trackDistance: true,
members: {
some: {
memberId: callerId
}
}
},
select: { id: true }
});
if (!hunt) {
throw createError({ status: 404, statusText: 'Not found!' });
}
const points = await prisma.trackPoint.findMany({
where: { huntId, userId },
orderBy: { recordedAt: 'asc' },
select: { lat: true, lon: true, accuracy: true, recordedAt: true }
});
return {
points: points.map((p) => ({
lat: Number(p.lat),
lon: Number(p.lon),
accuracy: p.accuracy,
recordedAt: p.recordedAt.toISOString()
}))
};
});