OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
ralph(M15-129): Per-module fixes: packages/core/src/modules/user - R2 and the findings no rule covers
ralphb2f34f757b0b
diff --git a/packages/core/src/modules/user/user.rpc.test.ts b/packages/core/src/modules/user/user.rpc.test.tsindex 8a8e8a3b8..c843c3e87 100644--- a/packages/core/src/modules/user/user.rpc.test.ts+++ b/packages/core/src/modules/user/user.rpc.test.ts@@ -1,8 +1,7 @@// Only the "is anyone logged in" boundary is exercised here — no database.-// The mutation bodies ride on @openpanel/db (lazy-loaded, see-// user.service.ts's header); wiring this router end-to-end against a real-// Postgres is P6's (protectedProcedure) job, not this one's — see-// user.rpc.ts's header.+// Wiring this router end-to-end against a real Postgres is a separate+// concern from `protectedProcedure`'s auth guard, which is what these+// tests cover.import { expect, test } from 'bun:test';import { stubHttpCtx } from '../../../test/rpc-fixtures';@@ -50,10 +49,3 @@ test('update rejects an unauthenticated caller before touching the profile', asycaller.update({ firstName: 'Ralph', lastName: 'Wiggum' })).rejects.toMatchObject({ code: 'UNAUTHORIZED' });});--test('debugPostCookie rejects an unauthenticated caller', async () => {- const caller = await anonCaller();- await expect(- caller.debugPostCookie({ sameSite: 'lax', domain: '.openpanel.dev' })- ).rejects.toMatchObject({ code: 'UNAUTHORIZED' });-});diff --git a/packages/core/src/modules/user/user.rpc.ts b/packages/core/src/modules/user/user.rpc.tsindex 9f6fdca4d..73e29728e 100644--- a/packages/core/src/modules/user/user.rpc.ts+++ b/packages/core/src/modules/user/user.rpc.ts@@ -1,18 +1,15 @@-// Ported from packages/trpc/src/routers/user.ts (M6-001).+// Account deletion, profile updates and the deletion-blockers check.//-// M11-001: every procedure is on its V1 twin's builder.// `protectedProcedure` runs `enforceUserIsAuthed` + `enforceAccess` BEFORE-// the input parser, exactly as V1 does. The explicit checks in the handlers-// below stay: `enforceAccess` only sees a TOP-LEVEL `projectId` /-// `organizationId`, so anything resolved from another id needs its own-// (ADR-011).+// the input parser. The explicit checks in the handlers below stay:+// `enforceAccess` only sees a TOP-LEVEL `projectId` / `organizationId`, so+// anything resolved from another id needs its own (ADR-011).//// `ctx.services.user` carries this module's factory (M10-004); it has no// queue/cron of its own, same as `conversation`.import { z } from 'zod';import { createTRPCRouter, protectedProcedure } from '../../rpc/base';-import { deleteSessionTokenCookie } from '../auth/auth.service';export const userRouter = createTRPCRouter({deletionBlockers: protectedProcedure.query(async ({ ctx }) => {@@ -23,7 +20,7 @@ export const userRouter = createTRPCRouter({delete: protectedProcedure.mutation(async ({ ctx }) => {const userId = ctx.session.userId;await ctx.services.user.deleteUserAccount(userId);- deleteSessionTokenCookie(ctx.config, ctx.setCookie);+ ctx.services.auth.deleteSessionTokenCookie(ctx.setCookie);return true;}),@@ -38,38 +35,4 @@ export const userRouter = createTRPCRouter({const userId = ctx.session.userId;return ctx.services.user.updateUserProfile({ userId, ...input });}),-- debugPostCookie: protectedProcedure- .input(- z.object({- sameSite: z.enum(['lax', 'strict', 'none']),- domain: z.string(),- })- )- .mutation(async ({ ctx, input }) => {- ctx.setCookie('debugCookie', new Date().toISOString(), {- domain: input.domain,- sameSite: input.sameSite,- httpOnly: true,- secure: true,- path: '/',- });- }),-- debugGetCookie: protectedProcedure- .input(- z.object({- sameSite: z.enum(['lax', 'strict', 'none']),- domain: z.string(),- })- )- .query(async ({ ctx, input }) => {- ctx.setCookie('debugCookie', new Date().toISOString(), {- domain: input.domain,- sameSite: input.sameSite,- httpOnly: true,- secure: true,- path: '/',- });- }),});diff --git a/packages/core/src/modules/user/user.service.ts b/packages/core/src/modules/user/user.service.tsindex ce57b0a5c..5f70ede16 100644--- a/packages/core/src/modules/user/user.service.ts+++ b/packages/core/src/modules/user/user.service.ts@@ -1,7 +1,4 @@-// Moved from packages/db/src/services/user.service.ts (M6-001). packages/db-// keeps a re-export shim — packages/trpc's auth/onboarding routers (not yet-// ported) still reach `getUserById`/`getUserAccount`/`IServiceUser` through-// @openpanel/db's barrel, same shape as packages/db/src/gsc.ts since M5-002.+// Moved from packages/db/src/services/user.service.ts (M6-001).//// M10-004: every function takes `ServiceDeps` and reaches Postgres as// `deps.db`; the `loadDb()` lazy loader is gone. Every caller — including@@ -11,8 +8,6 @@ import type { User } from '@openpanel/db/src/prisma-client';import { TRPCBadRequestError } from '../../rpc/errors';import type { ServiceDeps, Services } from '../../services';-export type IServiceUser = Awaited<ReturnType<typeof getUserById>>;-// Explicit `Promise<User>` return type, not inferred: Prisma's// `findUniqueOrThrow` returns a chainable "fluent" client (PromiseLike, plus// relation-loading methods), which does not unify with the plain