Files
gruperly/apps/backend/prisma/migrations/20261111150000_person_guardians/migration.sql
Jose Selesan 58c776e18c 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>
2026-11-11 13:27:13 -03:00

53 lines
2.0 KiB
SQL

-- CreateTable
CREATE TABLE "person_guardians" (
"child_id" TEXT NOT NULL,
"guardian_id" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "person_guardians_pkey" PRIMARY KEY ("child_id","guardian_id")
);
-- CreateIndex
CREATE INDEX "person_guardians_guardian_id_idx" ON "person_guardians"("guardian_id");
-- AddForeignKey
ALTER TABLE "person_guardians" ADD CONSTRAINT "person_guardians_child_id_fkey" FOREIGN KEY ("child_id") REFERENCES "people"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "person_guardians" ADD CONSTRAINT "person_guardians_guardian_id_fkey" FOREIGN KEY ("guardian_id") REFERENCES "people"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- The old guardian_name / guardian_phone become an adult of their own: an
-- existing person of the same teacher with that phone, or a new one (one per
-- teacher and phone, so siblings end up sharing it).
INSERT INTO "people" ("id", "created_by_id", "full_name", "phone", "created_at", "updated_at")
SELECT DISTINCT ON (k."created_by_id", k."guardian_phone")
gen_random_uuid()::text,
k."created_by_id",
COALESCE(NULLIF(TRIM(k."guardian_name"), ''), 'Adulto a cargo'),
k."guardian_phone",
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP
FROM (
SELECT "created_by_id", TRIM("guardian_phone") AS "guardian_phone", "guardian_name", "created_at"
FROM "people"
WHERE NULLIF(TRIM("guardian_phone"), '') IS NOT NULL
) k
WHERE NOT EXISTS (
SELECT 1 FROM "people" p
WHERE p."created_by_id" = k."created_by_id" AND p."phone" = k."guardian_phone"
)
ORDER BY k."created_by_id", k."guardian_phone", k."created_at";
INSERT INTO "person_guardians" ("child_id", "guardian_id")
SELECT c."id", g."id"
FROM "people" c
JOIN "people" g
ON g."created_by_id" = c."created_by_id"
AND g."phone" = TRIM(c."guardian_phone")
AND g."id" <> c."id"
WHERE NULLIF(TRIM(c."guardian_phone"), '') IS NOT NULL;
-- AlterTable
ALTER TABLE "people" DROP COLUMN "guardian_name",
DROP COLUMN "guardian_phone";