feat: un miembro puede pertenecer a más de un grupo
- Nuevo modelo `Person` (`people`): contacto y notas por profe, con teléfono único (índice parcial `people_owner_phone_key`). `Attendee` pasa a ser la membresía (`group_id` + `person_id`, único por grupo). - La cuenta de la app de alumnos se vincula a la persona (`student_account_people`): una sola cuenta ve todos sus grupos. - Migración unifica attendees del mismo profe y teléfono, y fusiona sus cuentas de alumno (conserva la usada más recientemente). - `POST /groups/:gid/attendees/:aid/add-to-group` y `GET /people/lookup` para detectar personas existentes al cargar; el alta responde `409 person_exists` y acepta `personId`. Bulk, lista de espera y preinscripciones vinculan solas. - El límite del plan cuenta personas con al menos un grupo activo. - Web: "Agregar a otro grupo", sección "También está en" en la ficha y aviso "Ya tenés a esta persona" en el alta individual. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -63,7 +63,10 @@ describe.skipIf(!url)('payment review (real database)', () => {
|
||||
],
|
||||
});
|
||||
await db.group.create({ data: { id: ids.group, name: 'Natación', createdById: ids.teacher } });
|
||||
await db.attendee.create({ data: { id: ids.ana, groupId: ids.group, fullName: 'Ana', createdAt: new Date('2026-01-01T00:00:00Z') } });
|
||||
await db.person.create({ data: { id: `${ids.ana}-person`, createdById: ids.teacher, fullName: 'Ana' } });
|
||||
await db.attendee.create({
|
||||
data: { id: ids.ana, groupId: ids.group, personId: `${ids.ana}-person`, createdAt: new Date('2026-01-01T00:00:00Z') },
|
||||
});
|
||||
await db.studentAccount.create({
|
||||
data: {
|
||||
id: ids.account,
|
||||
@@ -71,7 +74,7 @@ describe.skipIf(!url)('payment review (real database)', () => {
|
||||
createdById: ids.teacher,
|
||||
recoveryEmail: 'ana@test.local',
|
||||
recoveryEmailVerifiedAt: NOW,
|
||||
attendees: { create: { attendeeId: ids.ana } },
|
||||
people: { create: { personId: `${ids.ana}-person` } },
|
||||
},
|
||||
});
|
||||
await db.payment.create({
|
||||
|
||||
@@ -19,7 +19,14 @@ describe.skipIf(!url)('student access (real database)', () => {
|
||||
// Same contract as UnitOfWork.executeResult, built on this file's own client
|
||||
// (other test files mock '@/lib/prisma').
|
||||
let unitOfWork: { executeResult: unknown };
|
||||
const ids = { user: `test-user-${suffix}`, group: `test-group-${suffix}`, attendee: `test-att-${suffix}` };
|
||||
const ids = {
|
||||
user: `test-user-${suffix}`,
|
||||
group: `test-group-${suffix}`,
|
||||
group2: `test-group2-${suffix}`,
|
||||
person: `test-person-${suffix}`,
|
||||
attendee: `test-att-${suffix}`,
|
||||
attendee2: `test-att2-${suffix}`,
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
db = new PrismaClient({ adapter: new PrismaPg({ connectionString: url as string }) });
|
||||
@@ -42,7 +49,11 @@ describe.skipIf(!url)('student access (real database)', () => {
|
||||
};
|
||||
await db.user.create({ data: { id: ids.user, name: 'Profe Test', email: `${ids.user}@test.local` } });
|
||||
await db.group.create({ data: { id: ids.group, name: 'Natación', createdById: ids.user } });
|
||||
await db.attendee.create({ data: { id: ids.attendee, groupId: ids.group, fullName: 'Ana Pérez' } });
|
||||
await db.group.create({ data: { id: ids.group2, name: 'Yoga', createdById: ids.user } });
|
||||
// One person in two groups of the teacher.
|
||||
await db.person.create({ data: { id: ids.person, createdById: ids.user, fullName: 'Ana Pérez' } });
|
||||
await db.attendee.create({ data: { id: ids.attendee, groupId: ids.group, personId: ids.person } });
|
||||
await db.attendee.create({ data: { id: ids.attendee2, groupId: ids.group2, personId: ids.person } });
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
@@ -96,7 +107,7 @@ describe.skipIf(!url)('student access (real database)', () => {
|
||||
if (!loser.ok) expect(loser.error.status).toBe(410);
|
||||
}
|
||||
|
||||
const account = await db.studentAccountAttendee.findFirstOrThrow({ where: { attendeeId: ids.attendee } });
|
||||
const account = await db.studentAccountPerson.findFirstOrThrow({ where: { personId: ids.person } });
|
||||
const sessions = await db.studentSession.findMany({ where: { accountId: account.accountId, revokedAt: null } });
|
||||
expect(sessions).toHaveLength(1);
|
||||
expect(sessions[0]?.device).toBe('iPhone · Safari');
|
||||
@@ -122,7 +133,7 @@ describe.skipIf(!url)('student access (real database)', () => {
|
||||
if (!first.ok) throw new Error('invite failed');
|
||||
const consumed = await consume(tokenOf(first.value.url));
|
||||
expect(consumed.ok).toBe(true);
|
||||
const linked = await db.studentAccountAttendee.findFirstOrThrow({ where: { attendeeId: ids.attendee } });
|
||||
const linked = await db.studentAccountPerson.findFirstOrThrow({ where: { personId: ids.person } });
|
||||
await db.studentPasskey.create({
|
||||
data: { accountId: linked.accountId, credentialId: `cred-${suffix}`, publicKey: new Uint8Array([1]), device: 'iPhone · Safari' },
|
||||
});
|
||||
@@ -141,9 +152,25 @@ describe.skipIf(!url)('student access (real database)', () => {
|
||||
expect((await consume(tokenOf(pendingOld.value.url))).ok).toBe(false);
|
||||
expect((await consume(tokenOf(reinvite.value.url))).ok).toBe(true);
|
||||
|
||||
const account = await db.studentAccountAttendee.findFirstOrThrow({ where: { attendeeId: ids.attendee } });
|
||||
const account = await db.studentAccountPerson.findFirstOrThrow({ where: { personId: ids.person } });
|
||||
expect(await db.studentSession.count({ where: { accountId: account.accountId, revokedAt: null } })).toBe(1);
|
||||
const after = await new GetStudentAccess({ db }).execute(ids.group, ids.attendee, ids.user);
|
||||
expect(after).toMatchObject({ ok: true, value: { status: 'active' } });
|
||||
});
|
||||
|
||||
it('una sola cuenta por persona: invitar desde otro grupo la reutiliza', async () => {
|
||||
const fromFirst = await invite();
|
||||
if (!fromFirst.ok) throw new Error('invite failed');
|
||||
const fromSecond = await new InviteStudent({ db, unitOfWork: unitOfWork as never }).execute(
|
||||
ids.group2,
|
||||
ids.attendee2,
|
||||
ids.user,
|
||||
);
|
||||
if (!fromSecond.ok) throw new Error('invite failed');
|
||||
|
||||
expect(await db.studentAccountPerson.count({ where: { personId: ids.person } })).toBe(1);
|
||||
// Same state from both groups: it is the person's access.
|
||||
const fromGroup2 = await new GetStudentAccess({ db }).execute(ids.group2, ids.attendee2, ids.user);
|
||||
expect(fromGroup2).toMatchObject({ ok: true, value: { status: 'invited' } });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -8,6 +8,7 @@ import type { FileStorage } from '@/lib/storage';
|
||||
import { CancelStudentAbsence, NotifyStudentAbsences } from '@/modules/student-portal/features/absences/use-case';
|
||||
import { GetStudentClasses } from '@/modules/student-portal/features/classes/use-case';
|
||||
import { GetStudentFees } from '@/modules/student-portal/features/fees/use-case';
|
||||
import { GetStudentMe } from '@/modules/student-portal/features/me/use-case';
|
||||
import { ReportStudentPayment } from '@/modules/student-portal/features/payment-reports/use-case';
|
||||
|
||||
const url = process.env.DB_TESTS === '1' ? process.env.DATABASE_URL : undefined;
|
||||
@@ -30,7 +31,12 @@ describe.skipIf(!url)('student portal (real database)', () => {
|
||||
const ids = {
|
||||
user: `t-user-${suffix}`,
|
||||
group: `t-group-${suffix}`,
|
||||
yoga: `t-yoga-${suffix}`,
|
||||
personAna: `t-p-ana-${suffix}`,
|
||||
personBeto: `t-p-beto-${suffix}`,
|
||||
ana: `t-ana-${suffix}`,
|
||||
anaYoga: `t-ana-yoga-${suffix}`,
|
||||
betoYoga: `t-beto-yoga-${suffix}`,
|
||||
beto: `t-beto-${suffix}`,
|
||||
accountAna: `t-acc-ana-${suffix}`,
|
||||
accountBeto: `t-acc-beto-${suffix}`,
|
||||
@@ -50,17 +56,29 @@ describe.skipIf(!url)('student portal (real database)', () => {
|
||||
await db.group.create({
|
||||
data: { id: ids.group, name: 'Natación', createdById: ids.user, days: ['MONDAY', 'WEDNESDAY', 'FRIDAY'], time: '18:00' },
|
||||
});
|
||||
await db.group.create({
|
||||
data: { id: ids.yoga, name: 'Yoga', createdById: ids.user, days: ['TUESDAY'], time: '10:00' },
|
||||
});
|
||||
await db.person.createMany({
|
||||
data: [
|
||||
{ id: ids.personAna, createdById: ids.user, fullName: 'Ana' },
|
||||
{ id: ids.personBeto, createdById: ids.user, fullName: 'Beto' },
|
||||
],
|
||||
});
|
||||
// Ana and Beto are in both groups.
|
||||
await db.attendee.createMany({
|
||||
data: [
|
||||
{ id: ids.ana, groupId: ids.group, fullName: 'Ana' },
|
||||
{ id: ids.beto, groupId: ids.group, fullName: 'Beto' },
|
||||
{ id: ids.ana, groupId: ids.group, personId: ids.personAna },
|
||||
{ id: ids.beto, groupId: ids.group, personId: ids.personBeto },
|
||||
{ id: ids.anaYoga, groupId: ids.yoga, personId: ids.personAna },
|
||||
{ id: ids.betoYoga, groupId: ids.yoga, personId: ids.personBeto },
|
||||
],
|
||||
});
|
||||
await db.studentAccount.create({
|
||||
data: { id: ids.accountAna, name: 'Ana', createdById: ids.user, attendees: { create: { attendeeId: ids.ana } } },
|
||||
data: { id: ids.accountAna, name: 'Ana', createdById: ids.user, people: { create: { personId: ids.personAna } } },
|
||||
});
|
||||
await db.studentAccount.create({
|
||||
data: { id: ids.accountBeto, name: 'Beto', createdById: ids.user, attendees: { create: { attendeeId: ids.beto } } },
|
||||
data: { id: ids.accountBeto, name: 'Beto', createdById: ids.user, people: { create: { personId: ids.personBeto } } },
|
||||
});
|
||||
await db.payment.createMany({
|
||||
data: [
|
||||
@@ -117,6 +135,21 @@ describe.skipIf(!url)('student portal (real database)', () => {
|
||||
expect(!invented.ok && invented.error.status).toBe(400);
|
||||
});
|
||||
|
||||
it('la cuenta de la persona ve todos sus grupos, y solo los suyos', async () => {
|
||||
const me = await new GetStudentMe({ db }).execute(ids.accountAna);
|
||||
if (!me.ok) throw new Error('me failed');
|
||||
expect(me.value.students.map((student) => [student.attendeeId, student.groupName])).toEqual([
|
||||
[ids.ana, 'Natación'],
|
||||
[ids.anaYoga, 'Yoga'],
|
||||
]);
|
||||
|
||||
// Her membership in the second group is reachable; Beto's in that group is not.
|
||||
expect((await new GetStudentFees({ db }).execute(ids.accountAna, ids.anaYoga)).ok).toBe(true);
|
||||
const foreign = await new GetStudentFees({ db }).execute(ids.accountAna, ids.betoYoga);
|
||||
expect(foreign.ok).toBe(false);
|
||||
if (!foreign.ok) expect(foreign.error.status).toBe(404);
|
||||
});
|
||||
|
||||
it('IDOR: una cuenta no ve ni toca datos de otro alumno', async () => {
|
||||
expect((await new GetStudentClasses({ db, now: NOW }).execute(ids.accountBeto, ids.ana)).ok).toBe(false);
|
||||
expect((await new GetStudentFees({ db, now: NOW }).execute(ids.accountBeto, ids.ana)).ok).toBe(false);
|
||||
|
||||
Reference in New Issue
Block a user