feat(groups): kids groups with an adult in charge
- 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>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import type { PrismaClient } from '@generated/prisma/client';
|
||||
import type { CreateAttendee, CreateAttendeeResult, ProblemDetails, Result } from '@gruperly/shared';
|
||||
import type { CreateAttendee, CreateAttendeeResult, GuardianInput, ProblemDetails, Result } from '@gruperly/shared';
|
||||
import { err, ok } from '@gruperly/shared';
|
||||
import {
|
||||
alreadyWaitlistedProblem,
|
||||
@@ -11,9 +11,13 @@ import prisma from '@/lib/prisma';
|
||||
import { PlanGuard } from '@/modules/billing/lib/entitlements';
|
||||
import {
|
||||
alreadyInGroupProblem,
|
||||
contactPhone,
|
||||
findPersonByPhone,
|
||||
guardianRequiredProblem,
|
||||
guardianSelect,
|
||||
isUniqueViolation,
|
||||
personExistsProblem,
|
||||
type WithGuardians,
|
||||
} from '@/modules/people/lib/people';
|
||||
import { attendeeWithPerson, normalizePhone, toAttendeeDto } from '../../lib/helpers';
|
||||
|
||||
@@ -54,27 +58,51 @@ export class CreateAttendeeUseCase {
|
||||
return err(noGroupAccessProblem());
|
||||
}
|
||||
|
||||
const phone = normalizePhone(payload.phone);
|
||||
const isKidsGroup = group.audience === 'KIDS';
|
||||
if (isKidsGroup && !payload.personId && !payload.guardian) {
|
||||
return err(guardianRequiredProblem());
|
||||
}
|
||||
// A kid may have no phone of their own: the adult in charge is the contact.
|
||||
const phone = payload.phone ? normalizePhone(payload.phone) : null;
|
||||
|
||||
/* The person: the one the form picked (`personId`, after the "ya existe"
|
||||
hint) or a new one. A phone the teacher already has is never duplicated. */
|
||||
let person: { id: string; fullName: string } | null = null;
|
||||
let person: ({ id: string; fullName: string; phone: string | null } & WithGuardians) | null = null;
|
||||
// Kids groups: the adult in charge, already one of the teacher's people or new.
|
||||
let guardian: { id: string } | null = null;
|
||||
if (payload.personId) {
|
||||
person = await db.person.findFirst({
|
||||
where: { id: payload.personId, createdById: group.createdById },
|
||||
select: { id: true, fullName: true },
|
||||
select: { id: true, fullName: true, phone: true, ...guardianSelect },
|
||||
});
|
||||
if (!person) {
|
||||
return err(notFoundResourceProblem('Person', payload.personId));
|
||||
}
|
||||
} else {
|
||||
const samePhone = await findPersonByPhone(db, group.createdById, payload.phone);
|
||||
if (samePhone) {
|
||||
return err(
|
||||
samePhone.attendees.some((membership) => membership.groupId === groupId)
|
||||
? alreadyInGroupProblem()
|
||||
: personExistsProblem(),
|
||||
);
|
||||
if (payload.phone) {
|
||||
// The raw phone: its variants also match older rows stored as typed.
|
||||
const samePhone = await findPersonByPhone(db, group.createdById, payload.phone);
|
||||
if (samePhone) {
|
||||
return err(
|
||||
samePhone.attendees.some((membership) => membership.groupId === groupId)
|
||||
? alreadyInGroupProblem()
|
||||
: personExistsProblem(),
|
||||
);
|
||||
}
|
||||
}
|
||||
if (payload.guardian) {
|
||||
guardian = await findPersonByPhone(db, group.createdById, payload.guardian.phone);
|
||||
// The same kid of the same adult (same name) is that kid, not a twin.
|
||||
if (guardian) {
|
||||
person = await db.person.findFirst({
|
||||
where: {
|
||||
createdById: group.createdById,
|
||||
fullName: { equals: kidFullName(payload), mode: 'insensitive' },
|
||||
guardians: { some: { guardianId: guardian.id } },
|
||||
},
|
||||
select: { id: true, fullName: true, phone: true, ...guardianSelect },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,7 +114,12 @@ export class CreateAttendeeUseCase {
|
||||
return err(alreadyInGroupProblem());
|
||||
}
|
||||
|
||||
const fullName = person?.fullName ?? `${payload.firstName.trim()} ${payload.lastName.trim()}`.trim();
|
||||
const fullName = person?.fullName ?? kidFullName(payload);
|
||||
// Who the waitlist row (and later the messages) reaches.
|
||||
const reachPhone =
|
||||
(person ? contactPhone(person) : null) ??
|
||||
phone ??
|
||||
(payload.guardian ? normalizePhone(payload.guardian.phone) : null);
|
||||
|
||||
if (group.capacity !== null) {
|
||||
const currentCount = await db.attendee.count({
|
||||
@@ -99,9 +132,9 @@ export class CreateAttendeeUseCase {
|
||||
}
|
||||
|
||||
if (!isOwner) {
|
||||
const existingWaitlistEntry = await db.groupWaitlistEntry.findFirst({
|
||||
where: { groupId, phone },
|
||||
});
|
||||
const existingWaitlistEntry = reachPhone
|
||||
? await db.groupWaitlistEntry.findFirst({ where: { groupId, phone: reachPhone } })
|
||||
: null;
|
||||
|
||||
if (existingWaitlistEntry) {
|
||||
return err(alreadyWaitlistedProblem());
|
||||
@@ -111,7 +144,7 @@ export class CreateAttendeeUseCase {
|
||||
data: {
|
||||
groupId,
|
||||
fullName,
|
||||
phone,
|
||||
phone: reachPhone ?? '',
|
||||
email: payload.email?.trim() || null,
|
||||
notes: payload.notes?.trim() || null,
|
||||
},
|
||||
@@ -157,6 +190,7 @@ export class CreateAttendeeUseCase {
|
||||
phone,
|
||||
email: payload.email?.trim() || null,
|
||||
notes: payload.notes?.trim() || null,
|
||||
...(payload.guardian ? { guardians: { create: guardianLink(group.createdById, payload.guardian, guardian) } } : {}),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -173,3 +207,24 @@ export class CreateAttendeeUseCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function kidFullName(payload: CreateAttendee): string {
|
||||
return `${payload.firstName.trim()} ${payload.lastName.trim()}`.trim();
|
||||
}
|
||||
|
||||
/* Nested create of the kid's link to the adult in charge: the existing adult,
|
||||
or a new person with the adult's data. */
|
||||
function guardianLink(ownerId: string, input: GuardianInput, existing: { id: string } | null) {
|
||||
return {
|
||||
guardian: existing
|
||||
? { connect: { id: existing.id } }
|
||||
: {
|
||||
create: {
|
||||
createdById: ownerId,
|
||||
fullName: input.fullName.trim(),
|
||||
phone: normalizePhone(input.phone),
|
||||
email: input.email?.trim() || null,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user