Files
gruperly/apps/backend/src/modules/analytics/lib/access.ts
Jose Selesan 2e184845e1 feat: add absence notification and analytics features
- Implemented AbsenceNotifyView for notifying absence with session details.
- Created AnalyticsView for displaying group analytics and managing students at risk.
- Added ClassAttendanceView for marking attendance in classes.
- Defined new schemas for analytics and attendance in shared package.
2026-09-23 16:24:45 -03:00

37 lines
1.1 KiB
TypeScript

import type { PrismaClient } from '@generated/prisma/client';
import type { ProblemDetails, Result } from '@gruperly/shared';
import { err, ok } from '@gruperly/shared';
import { noGroupAccessProblem, notFoundResourceProblem } from '@/http/problem-builders';
export type AnalyticsAccessDb = Pick<PrismaClient, 'group'>;
type GroupWithAccess = {
id: string;
name: string;
createdById: string;
members: { userId: string }[];
};
// Verifica que el usuario sea owner o miembro del grupo. Devuelve el grupo si tiene acceso.
export async function findGroupForUser(
db: AnalyticsAccessDb,
groupId: string,
userId: string,
): Promise<Result<GroupWithAccess, ProblemDetails>> {
const group = await db.group.findUnique({
where: { id: groupId },
include: { members: { where: { userId } } },
});
if (!group) {
return err(notFoundResourceProblem('Group', groupId));
}
const isOwner = group.createdById === userId;
const isMember = group.members.length > 0;
if (!isOwner && !isMember) {
return err(noGroupAccessProblem());
}
return ok(group);
}