- 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.
37 lines
1.1 KiB
TypeScript
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);
|
|
} |