- Add the `kidsGroups` plan feature (Pro) and `Group.audience` (ADULTS | KIDS); creating or switching a group to KIDS requires the feature, existing KIDS groups keep working after a downgrade. - Model the adult in charge as a Person linked through `person_guardians`; migrate the old guardian_name/guardian_phone columns and drop them. - Kids quick add: kid + guardian form, guardian lookup by phone to pick an existing kid or add a sibling; backend rejects KIDS adds without guardian. - Attendee page: "Adulto a cargo" section with siblings, editable via PATCH /groups/:gid/attendees/:aid/guardian. - Contact phone (WhatsApp jobs, attention, student access invite, students at risk, attendee actions) prefers the guardian's phone. - Edit group screen (PATCH /groups/:gid, owner only); switching back to ADULTS is blocked while kids have no phone of their own. - Hide bulk import in KIDS groups until it carries guardian data. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
107 lines
4.1 KiB
TypeScript
107 lines
4.1 KiB
TypeScript
import type { PrismaClient } from '@generated/prisma/client';
|
|
import type { ProblemDetails, Result, StudentInvitationDto } from '@gruperly/shared';
|
|
import { type CountryCode, err, INVITATION_TTL_HOURS, ok, toWhatsappE164 } from '@gruperly/shared';
|
|
import { notFoundResourceProblem } from '@/http/problem-builders';
|
|
import prisma, { UnitOfWork } from '@/lib/prisma';
|
|
import { findGroupForUser } from '@/modules/group-waitlist/lib/helpers';
|
|
import { contactPhone, guardianSelect } from '@/modules/people/lib/people';
|
|
import { generateAccessToken, hashToken, studentLinkUrl } from '@/modules/student-portal/lib';
|
|
import { computeStudentAccess } from '../../lib/status';
|
|
|
|
type InviteStudentDeps = {
|
|
db?: Pick<PrismaClient, 'group' | 'attendee' | 'studentAccountPerson'>;
|
|
unitOfWork?: Pick<UnitOfWork, 'executeResult'>;
|
|
now?: Date;
|
|
};
|
|
|
|
/* "Invitar" / "Reinvitar": reinviting restarts the access. It revokes every
|
|
pending link and every session and removes the passkeys (a lost phone must not
|
|
keep a way in), then issues a fresh 72 h single-use link. The welcome screen
|
|
shows again on the next sign-in so the student can add a passkey back. The
|
|
raw token only leaves here, inside the URL: the database keeps its hash. */
|
|
export class InviteStudent {
|
|
constructor(private readonly deps: InviteStudentDeps = {}) {}
|
|
|
|
async execute(
|
|
groupId: string,
|
|
attendeeId: string,
|
|
userId: string,
|
|
): Promise<Result<StudentInvitationDto, ProblemDetails>> {
|
|
const db = this.deps.db ?? prisma;
|
|
const now = this.deps.now ?? new Date();
|
|
|
|
const access = await findGroupForUser(db, groupId, userId);
|
|
if (!access.ok) return access;
|
|
|
|
const attendee = await db.attendee.findFirst({
|
|
where: { id: attendeeId, groupId },
|
|
select: {
|
|
id: true,
|
|
personId: true,
|
|
person: { select: { fullName: true, phone: true, ...guardianSelect } },
|
|
group: { select: { owner: { select: { country: true } } } },
|
|
},
|
|
});
|
|
if (!attendee) return err(notFoundResourceProblem('Attendee', attendeeId));
|
|
|
|
const token = generateAccessToken();
|
|
const expiresAt = new Date(now.getTime() + INVITATION_TTL_HOURS * 60 * 60 * 1000);
|
|
const unitOfWork = this.deps.unitOfWork ?? new UnitOfWork(prisma as PrismaClient);
|
|
|
|
const issued = await unitOfWork.executeResult(async (tx) => {
|
|
// One account per person: inviting from any of their groups reuses it.
|
|
const link = await tx.studentAccountPerson.findFirst({
|
|
where: { personId: attendee.personId },
|
|
orderBy: { createdAt: 'asc' },
|
|
select: { accountId: true },
|
|
});
|
|
const accountId =
|
|
link?.accountId
|
|
?? (
|
|
await tx.studentAccount.create({
|
|
data: {
|
|
name: attendee.person.fullName,
|
|
createdById: userId,
|
|
people: { create: { personId: attendee.personId } },
|
|
},
|
|
select: { id: true },
|
|
})
|
|
).id;
|
|
|
|
await tx.studentAccessToken.updateMany({
|
|
where: { accountId, usedAt: null, revokedAt: null },
|
|
data: { revokedAt: now },
|
|
});
|
|
await tx.studentSession.updateMany({
|
|
where: { accountId, revokedAt: null },
|
|
data: { revokedAt: now },
|
|
});
|
|
await tx.studentPasskey.deleteMany({ where: { accountId } });
|
|
await tx.studentAccount.update({ where: { id: accountId }, data: { welcomedAt: null } });
|
|
await tx.studentAccessToken.create({
|
|
data: {
|
|
accountId,
|
|
kind: 'INVITATION',
|
|
tokenHash: hashToken(token),
|
|
expiresAt,
|
|
createdById: userId,
|
|
createdAt: now,
|
|
},
|
|
});
|
|
return ok(accountId);
|
|
});
|
|
if (!issued.ok) return issued;
|
|
|
|
return ok({
|
|
url: studentLinkUrl(token),
|
|
expiresAt: expiresAt.toISOString(),
|
|
whatsappPhone: toWhatsappPhone(contactPhone(attendee.person), attendee.group.owner.country),
|
|
access: await computeStudentAccess(db, attendee.personId, now),
|
|
});
|
|
}
|
|
}
|
|
|
|
function toWhatsappPhone(phone: string | null, country: string | null): string | null {
|
|
return phone ? toWhatsappE164(phone, country as CountryCode | null) : null;
|
|
}
|