feat(platform-service): add user plan update to auth module for Stripe webhooks

- Added auth repository updatePlan(userId, productId, plan) helper
- Stripe webhook handlers now sync plan changes into auth users container
- Handles checkout completion and cancellation downgrade paths
- Keeps existing backend sync behavior intact
- Verified: tsc --noEmit clean, 19 test files / 178 tests passing
This commit is contained in:
saravanakumardb1 2026-02-15 14:47:26 -08:00
parent a264538c5e
commit 84681cbf75
2 changed files with 27 additions and 0 deletions

View File

@ -37,6 +37,28 @@ export async function create(user: UserDoc): Promise<UserDoc> {
return resource as UserDoc;
}
export async function updatePlan(
id: string,
productId: string,
plan: UserDoc['plan']
): Promise<UserDoc | null> {
try {
const { resource } = await container().item(id, id).read<UserDoc>();
if (!resource || resource.productId !== productId) return null;
const { resource: updated } = await container()
.item(id, id)
.replace<UserDoc>({
...resource,
plan,
updatedAt: new Date().toISOString(),
});
if (!updated) return null;
return updated;
} catch {
return null;
}
}
export async function updateLastLogin(id: string): Promise<void> {
try {
const { resource } = await container().item(id, id).read<UserDoc>();

View File

@ -16,6 +16,7 @@ import { DEFAULT_PRODUCT_ID } from '../../lib/product-config.js';
import { getRequestProductId } from '../../lib/request-context.js';
import { getStripeForProduct, getPriceIds } from '../../lib/stripe.js';
import { BadRequestError } from '../../lib/errors.js';
import * as authRepo from '../auth/repository.js';
import * as subRepo from '../subscriptions/repository.js';
import type { SubscriptionDoc } from '../subscriptions/types.js';
@ -134,6 +135,7 @@ export async function stripeRoutes(app: FastifyInstance) {
currentPeriodStart: now.toISOString(),
currentPeriodEnd: periodEnd.toISOString(),
});
await authRepo.updatePlan(userId, eventProductId, plan as SubscriptionDoc['plan']);
} else {
await subRepo.createSubscription({
id: `sub_${userId}_${Date.now()}`,
@ -152,6 +154,7 @@ export async function stripeRoutes(app: FastifyInstance) {
createdAt: now.toISOString(),
updatedAt: now.toISOString(),
});
await authRepo.updatePlan(userId, eventProductId, plan as SubscriptionDoc['plan']);
}
// Sync plan to backend users container
@ -190,6 +193,7 @@ export async function stripeRoutes(app: FastifyInstance) {
cancelAtPeriodEnd: sub.cancel_at_period_end,
currentPeriodEnd: new Date(sub.current_period_end * 1000).toISOString(),
});
await authRepo.updatePlan(existing.userId, eventProductId, existing.plan);
// Sync plan to backend users container
await syncUserPlan(existing.userId, existing.plan, app.log);
}
@ -206,6 +210,7 @@ export async function stripeRoutes(app: FastifyInstance) {
plan: 'free',
cancelAtPeriodEnd: false,
});
await authRepo.updatePlan(existing.userId, eventProductId, 'free');
// Sync plan downgrade to backend users container
await syncUserPlan(existing.userId, 'free', app.log);
}