- 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>
110 lines
3.9 KiB
TypeScript
110 lines
3.9 KiB
TypeScript
import type { PrismaClient } from '@generated/prisma/client';
|
|
import type { CountryCode, Plan, PlanCatalogDto, PlanLimits } from '@gruperly/shared';
|
|
import { COUNTRY_CURRENCY, countryCodeSchema, PLAN_LIMITS, PRO_PRICE_BY_COUNTRY } from '@gruperly/shared';
|
|
import prisma from '@/lib/prisma';
|
|
import { logger } from '@/logger';
|
|
|
|
/** Limits and prices are read often (every plan guard) and edited rarely. */
|
|
const CACHE_TTL_MS = 30 * 1000;
|
|
|
|
export type PlanCatalogSnapshot = {
|
|
limits: Record<Plan, PlanLimits>;
|
|
/** Monthly price of Pro, in the currency of each country. */
|
|
proPrices: Record<CountryCode, number>;
|
|
};
|
|
|
|
export function defaultPlanCatalog(): PlanCatalogSnapshot {
|
|
return {
|
|
limits: { FREE: { ...PLAN_LIMITS.FREE }, PRO: { ...PLAN_LIMITS.PRO } },
|
|
proPrices: { ...PRO_PRICE_BY_COUNTRY },
|
|
};
|
|
}
|
|
|
|
type PlanCatalogDeps = {
|
|
db?: Pick<PrismaClient, 'planConfig' | 'planPrice'>;
|
|
ttlMs?: number;
|
|
now?: () => number;
|
|
};
|
|
|
|
/**
|
|
* What each plan allows and what Pro costs, as edited from the backoffice
|
|
* (`plan_configs`, `plan_prices`). A missing row falls back to the defaults in
|
|
* shared (`PLAN_LIMITS`, `PRO_PRICE_BY_COUNTRY`), which also seed the tables.
|
|
*
|
|
* Cached in memory for a few seconds; the backoffice invalidates it on save, so
|
|
* the instance that saved sees the change at once and the others within the TTL.
|
|
*/
|
|
export class PlanCatalog {
|
|
private cached: { snapshot: PlanCatalogSnapshot; at: number } | null = null;
|
|
|
|
constructor(private readonly deps: PlanCatalogDeps = {}) {}
|
|
|
|
invalidate(): void {
|
|
this.cached = null;
|
|
}
|
|
|
|
async snapshot(): Promise<PlanCatalogSnapshot> {
|
|
const now = this.deps.now?.() ?? Date.now();
|
|
const ttl = this.deps.ttlMs ?? CACHE_TTL_MS;
|
|
if (this.cached && now - this.cached.at < ttl) return this.cached.snapshot;
|
|
|
|
const snapshot = defaultPlanCatalog();
|
|
try {
|
|
const db = this.deps.db ?? prisma;
|
|
const [configs, prices] = await Promise.all([db.planConfig.findMany(), db.planPrice.findMany()]);
|
|
for (const config of configs) {
|
|
snapshot.limits[config.plan] = {
|
|
maxGroups: config.maxGroups,
|
|
maxAttendees: config.maxAttendees,
|
|
whatsappAutomations: config.whatsappAutomations,
|
|
mpCharges: config.mpCharges,
|
|
kidsGroups: config.kidsGroups,
|
|
};
|
|
}
|
|
for (const price of prices) {
|
|
const country = countryCodeSchema.safeParse(price.country);
|
|
if (country.success) snapshot.proPrices[country.data] = Number(price.amount.toString());
|
|
}
|
|
} catch (error) {
|
|
// The defaults keep the guards working; not cached, so the next call retries.
|
|
logger.warn({ err: error }, 'Could not read the plan catalog, using the defaults');
|
|
return snapshot;
|
|
}
|
|
|
|
this.cached = { snapshot, at: now };
|
|
return snapshot;
|
|
}
|
|
|
|
async getLimits(plan: Plan): Promise<PlanLimits> {
|
|
return (await this.snapshot()).limits[plan];
|
|
}
|
|
|
|
async getProPrice(country: CountryCode): Promise<number> {
|
|
return (await this.snapshot()).proPrices[country];
|
|
}
|
|
}
|
|
|
|
/** The catalog every use-case shares, so an invalidation reaches all of them. */
|
|
export const planCatalog = new PlanCatalog();
|
|
|
|
/** A fixed catalog, for tests and callers that already hold a snapshot. */
|
|
export function staticPlanCatalog(snapshot: PlanCatalogSnapshot = defaultPlanCatalog()): PlanCatalogReader {
|
|
return {
|
|
snapshot: async () => snapshot,
|
|
getLimits: async (plan) => snapshot.limits[plan],
|
|
getProPrice: async (country) => snapshot.proPrices[country],
|
|
};
|
|
}
|
|
|
|
export type PlanCatalogReader = Pick<PlanCatalog, 'snapshot' | 'getLimits' | 'getProPrice'>;
|
|
|
|
export function toPlanCatalogDto(snapshot: PlanCatalogSnapshot): PlanCatalogDto {
|
|
const proPrices = Object.fromEntries(
|
|
countryCodeSchema.options.map((country) => [
|
|
country,
|
|
{ amount: snapshot.proPrices[country], currency: COUNTRY_CURRENCY[country] },
|
|
]),
|
|
) as PlanCatalogDto['proPrices'];
|
|
return { limits: snapshot.limits, proPrices };
|
|
}
|