Files
gruperly/packages/shared/src/schemas/onboarding.ts
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

89 lines
3.9 KiB
TypeScript

import { z } from 'zod';
import { planSchema, subscriptionStatusSchema } from './billing.js';
import { countryCodeSchema, DEFAULT_COUNTRY } from './country.js';
import { billingTypeSchema, groupAudienceSchema, weekdaySchema } from './enums.js';
import { GroupDtoSchema } from './groups.js';
export { countryCodeSchema } from './country.js';
export type { BillingType, GroupAudience, WeekDay } from './enums.js';
export { billingTypeSchema, groupAudienceSchema, weekdaySchema } from './enums.js';
// Step 4: create the teacher's first group. Linking Mercado Pago is step 2 and
// is skippable, so nothing here may require a linked account. The country is
// stored on the user and drives the currency of the payment links later. It
// defaults to Argentina because the group form (also used outside onboarding)
// has no country field: the country lives on the user, not on the group.
export const CreateFirstGroupSchema = z.strictObject({
name: z
.string()
.trim()
.min(3, 'group.nameMin')
.max(60, 'group.nameMax'),
days: z.array(weekdaySchema).min(1, 'group.daysMin'),
time: z
.string()
.regex(/^([01]?\d|2[0-3]):[0-5]\d$/, 'group.timeInvalid'),
capacity: z
.number({ invalid_type_error: 'group.capacityInvalid' })
.int({ message: 'group.capacityInt' })
.min(1, 'group.capacityMin'),
price: z
.number({ invalid_type_error: 'group.priceInvalid' })
.min(0, 'group.priceMin'),
billingType: billingTypeSchema,
dueDay: z
.number({ invalid_type_error: 'group.dueDayInvalid' })
.int({ message: 'group.dueDayInt' })
.min(1, 'group.dueDayRange')
.max(28, 'group.dueDayRange'),
country: countryCodeSchema.default(DEFAULT_COUNTRY),
// A KIDS group needs the `kidsGroups` plan feature.
audience: groupAudienceSchema.default('ADULTS'),
});
export type CreateFirstGroup = z.output<typeof CreateFirstGroupSchema>;
// "Editar grupo" (`PATCH /groups/:id`): the same fields as the group form, any
// subset. The country lives on the user, not on the group. Turning the group
// into KIDS needs the `kidsGroups` plan feature; going back to ADULTS is blocked
// while it has kids with no phone of their own.
export const UpdateGroupSchema = CreateFirstGroupSchema.omit({ country: true, audience: true })
.extend({ audience: groupAudienceSchema })
.partial()
.refine((value) => Object.keys(value).length > 0, 'update.empty');
export type UpdateGroup = z.output<typeof UpdateGroupSchema>;
export const OnboardingGroupDtoSchema = GroupDtoSchema.extend({
days: z.array(weekdaySchema).nullable(),
time: z.string().nullable(),
capacity: z.number().int().min(1).nullable(),
price: z.number().min(0).nullable(),
billingType: billingTypeSchema.nullable(),
dueDay: z.number().int().min(1).max(28).nullable(),
});
export type OnboardingGroupDto = z.output<typeof OnboardingGroupDtoSchema>;
export const CreateFirstGroupResultSchema = z.object({
group: OnboardingGroupDtoSchema,
onboardingCompleted: z.boolean(),
});
export type CreateFirstGroupResult = z.output<typeof CreateFirstGroupResultSchema>;
// Onboarding state, to resume the flow if it was interrupted.
export const onboardingStepSchema = z.enum(['NOT_STARTED', 'PAYMENT_CONNECTED', 'COMPLETED']);
export type OnboardingStep = z.output<typeof onboardingStepSchema>;
export const OnboardingStatusDtoSchema = z.object({
step: onboardingStepSchema,
paymentConnected: z.boolean(),
// WhatsApp is its own skippable step and does not move `step`.
whatsappConnected: z.boolean(),
completed: z.boolean(),
// The "Plan" step is mandatory: these survive the round trip to Mercado Pago
// and a reload, so the flow resumes where it was.
planSelected: z.boolean(),
// PRO as soon as the professor chose it, even while the subscription is PENDING.
selectedPlan: planSchema.nullable(),
subscriptionStatus: subscriptionStatusSchema.nullable(),
country: countryCodeSchema.nullable(),
});
export type OnboardingStatusDto = z.output<typeof OnboardingStatusDtoSchema>;