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:
Jose Selesan
2026-11-03 09:46:37 -03:00
parent f1829fb5a9
commit d21e1c3730
84 changed files with 1981 additions and 423 deletions

View File

@@ -2,7 +2,7 @@ import { useEffect, useRef, useState } from 'react'
import type { RefObject } from 'react'
import type { GroupListItem } from '@gruperly/shared'
import type { LucideIcon } from 'lucide-react'
import { CalendarX, History, Loader2, MoveRight, Wallet } from 'lucide-react'
import { CalendarCheck, CalendarX, History, Loader2, MoveRight, UserPlus, Wallet } from 'lucide-react'
import { Button, Modal } from '../../components/ui'
import { useAttendeeDetail } from './AttendeeDetailProvider'
@@ -12,6 +12,9 @@ function isFull(group: GroupListItem): boolean {
return group.capacity != null && group.attendeesCount >= group.capacity
}
/* "Cambiar de grupo" (sale de este y entra al otro) y "Agregar a otro grupo"
(queda en los dos) comparten el selector de destino; cambian el texto, las
consecuencias y la acción. */
export function ChangeGroupDialog({
returnFocusRef,
}: {
@@ -24,18 +27,22 @@ export function ChangeGroupDialog({
groupName,
groups,
groupsPending,
isChangeGroupDialogOpen,
closeChangeGroupDialog,
groupDialogMode,
closeGroupDialog,
moveToGroup,
isMovingAttendee,
addToGroup,
isAddingToGroup,
} = useAttendeeDetail()
const isOpen = groupDialogMode !== null
const isAdd = groupDialogMode === 'add'
const [targetGroupId, setTargetGroupId] = useState('')
const wasOpen = useRef(false)
// Cada apertura arranca sin destino elegido; al cerrar, el foco vuelve al "⋯".
useEffect(() => {
if (isChangeGroupDialogOpen) {
if (isOpen) {
wasOpen.current = true
setTargetGroupId('')
return
@@ -43,36 +50,62 @@ export function ChangeGroupDialog({
if (!wasOpen.current) return
wasOpen.current = false
returnFocusRef.current?.focus()
}, [isChangeGroupDialogOpen, returnFocusRef])
}, [isOpen, returnFocusRef])
if (!attendee) return null
const otherGroups = groups.filter((group) => group.id !== groupId)
// Fuera el grupo actual y los grupos donde ya está. Al agregar, una baja
// anterior (DROPPED) se puede reactivar; al mover, no (sería otra membresía).
const blocked = new Set(
attendee.otherGroups
.filter((membership) => !isAdd || membership.status !== 'DROPPED')
.map((membership) => membership.groupId),
)
const otherGroups = groups.filter((group) => group.id !== groupId && !blocked.has(group.id))
const currentName = groupName ?? 'el grupo actual'
const isSubmitting = isAdd ? isAddingToGroup : isMovingAttendee
const consequences: Consequence[] = [
{
key: 'historial',
icon: History,
text: 'Conserva su historial, sus cuotas y su acceso a la app de alumnos.',
},
{
key: 'cuotas',
icon: Wallet,
text: `Las cuotas pendientes de ${currentName} siguen vigentes.`,
},
{
key: 'faltas',
icon: CalendarX,
text: `Se cancelan las faltas avisadas para clases futuras de ${currentName}.`,
},
]
const consequences: Consequence[] = isAdd
? [
{
key: 'sigue',
icon: CalendarCheck,
text: `Sigue en ${currentName}: va a estar en los dos grupos.`,
},
{
key: 'datos',
icon: History,
text: 'Comparte el contacto, las notas y el acceso a la app de alumnos.',
},
{
key: 'cuotas',
icon: Wallet,
text: 'La asistencia y las cuotas se llevan por separado en cada grupo.',
},
]
: [
{
key: 'historial',
icon: History,
text: 'Conserva su historial, sus cuotas y su acceso a la app de alumnos.',
},
{
key: 'cuotas',
icon: Wallet,
text: `Las cuotas pendientes de ${currentName} siguen vigentes.`,
},
{
key: 'faltas',
icon: CalendarX,
text: `Se cancelan las faltas avisadas para clases futuras de ${currentName}.`,
},
]
return (
<Modal
isOpen={isChangeGroupDialogOpen}
onClose={closeChangeGroupDialog}
title="Cambiar de grupo"
isOpen={isOpen}
onClose={closeGroupDialog}
title={isAdd ? 'Agregar a otro grupo' : 'Cambiar de grupo'}
description={`${attendee.fullName}${groupName ? ` · ${groupName}` : ''}`}
maxWidth="sm"
>
@@ -83,11 +116,15 @@ export function ChangeGroupDialog({
</div>
) : otherGroups.length === 0 ? (
<p className="rounded-xl border border-border bg-primary-soft/30 px-4 py-3 text-sm text-foreground/70">
No tenés otros grupos. Creá uno para poder mover a este miembro.
{groups.length > 1
? 'Ya es miembro de todos tus otros grupos.'
: `No tenés otros grupos. Creá uno para poder ${isAdd ? 'agregar' : 'mover'} a este miembro.`}
</p>
) : (
<label className="block">
<span className="mb-1.5 block text-sm font-medium text-primary">Mover a</span>
<span className="mb-1.5 block text-sm font-medium text-primary">
{isAdd ? 'Agregar a' : 'Mover a'}
</span>
<select
autoFocus
value={targetGroupId}
@@ -120,21 +157,23 @@ export function ChangeGroupDialog({
</ul>
<div className="flex flex-col-reverse gap-2.5 border-t border-border pt-3 sm:flex-row sm:justify-end">
<Button variant="ghost" onClick={closeChangeGroupDialog}>
<Button variant="ghost" onClick={closeGroupDialog}>
Cancelar
</Button>
<Button
variant="primary"
onClick={() => moveToGroup(targetGroupId)}
disabled={!targetGroupId || isMovingAttendee}
onClick={() => (isAdd ? addToGroup(targetGroupId) : moveToGroup(targetGroupId))}
disabled={!targetGroupId || isSubmitting}
className="gap-2"
>
{isMovingAttendee ? (
{isSubmitting ? (
<Loader2 className="size-4 animate-spin" />
) : isAdd ? (
<UserPlus className="size-4" />
) : (
<MoveRight className="size-4" />
)}
<span>Cambiar de grupo</span>
<span>{isAdd ? 'Agregar' : 'Cambiar de grupo'}</span>
</Button>
</div>
</div>