Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

ralph(M15-108): Per-module fixes: packages/core/src/modules/email - R13 and the findings no rule covers

ralphbfba9ea1ee4f
diff --git a/packages/core/src/modules/group/group.rpc.ts b/packages/core/src/modules/group/group.rpc.ts
index 6d992a231..610165710 100644
--- a/packages/core/src/modules/group/group.rpc.ts
+++ b/packages/core/src/modules/group/group.rpc.ts
@@ -2,10 +2,10 @@
//
// 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, exactly as V1 does. Every procedure below carries a
+// top-level `projectId`, so `enforceAccess` already covers it (ADR-011) - the
+// explicit `requireProjectAccess` calls in the handlers are the tree-wide
+// M15-007 pattern (ADR-022 R10), not the resolved-from-another-id exception.
import { z } from 'zod';
import { createTRPCRouter, protectedProcedure } from '../../rpc/base';
diff --git a/packages/core/src/modules/group/group.service.ts b/packages/core/src/modules/group/group.service.ts
index c6be1d1e7..bc350e974 100644
--- a/packages/core/src/modules/group/group.service.ts
+++ b/packages/core/src/modules/group/group.service.ts
@@ -4,7 +4,7 @@
// tools, apps/api's insights controller) keep working while V1 runs
// (DELEGATE PATTERN); the trpc router delegates its handler bodies here.
//
-// Every query is a `sql` fragment (src/group.sql.ts), converted one at a time
+// Every query is a `sql` fragment (src/sql.ts), converted one at a time
// with a result-set proof each, per ADR-013.
//
// M10-005: every function takes `ServiceDeps` and reaches ClickHouse as
@@ -18,6 +18,7 @@ import type { ServiceDeps, Services } from '../../services';
import { getProfiles, type IServiceProfile } from '../profile/profile.service';
import { formatClickhouseDate, toNullIfDefaultMinDate } from './src/dates';
import {
+ GROUPS_TABLE,
groupActivityQuery,
groupByIdQuery,
groupEventMetricsQuery,
@@ -32,9 +33,8 @@ import {
groupsByIdsQuery,
groupTypesQuery,
groupUniqueProfilesQuery,
-} from './src/group.sql';
+} from './src/sql';
-const GROUPS_TABLE = 'groups';
const FIND_GROUPS_DEFAULT_LIMIT = 20;
const GROUP_MEMBERS_DEFAULT_LIMIT = 10;
@@ -112,6 +112,17 @@ async function writeGroupToCh(
});
}
+/** `upsertGroup`/`updateGroup` share this: existing properties, incoming ones on top. */
+function mergeGroupProperties(
+ existingProperties: Record<string, unknown> | undefined,
+ incomingProperties: Record<string, unknown> | undefined
+): Record<string, string> {
+ return toDots({
+ ...(existingProperties ?? {}),
+ ...(incomingProperties ?? {}),
+ });
+}
+
export async function upsertGroup(
deps: ServiceDeps,
input: IServiceUpsertGroup
@@ -122,10 +133,7 @@ export async function upsertGroup(
projectId: input.projectId,
type: input.type,
name: input.name,
- properties: toDots({
- ...(existing?.properties ?? {}),
- ...(input.properties ?? {}),
- }),
+ properties: mergeGroupProperties(existing?.properties, input.properties),
createdAt: existing?.createdAt,
});
}
@@ -215,19 +223,12 @@ export async function updateGroup(
if (!existing) {
throw new Error(`Group ${id} not found`);
}
- const mergedProperties = {
- ...(existing.properties ?? {}),
- ...(data.properties ?? {}),
- };
- const normalizedProperties = toDots(
- mergedProperties as Record<string, unknown>
- );
const updated = {
id,
projectId,
type: data.type ?? existing.type,
name: data.name ?? existing.name,
- properties: normalizedProperties,
+ properties: mergeGroupProperties(existing.properties, data.properties),
createdAt: existing.createdAt,
};
await writeGroupToCh(deps, updated);
@@ -521,9 +522,96 @@ export function createGroupService(
_services: () => Services
) {
return {
- byId: (id: string, projectId: string): Promise<IServiceGroup | null> =>
- getGroupById(deps, id, projectId),
- upsert: (input: IServiceUpsertGroup): Promise<void> =>
- upsertGroup(deps, input),
+ getGroupById: (
+ id: string,
+ projectId: string
+ ): ReturnType<typeof getGroupById> => getGroupById(deps, id, projectId),
+ upsertGroup: (
+ input: IServiceUpsertGroup
+ ): ReturnType<typeof upsertGroup> => upsertGroup(deps, input),
+ getGroupList: (
+ options: GetGroupListOptions
+ ): ReturnType<typeof getGroupList> => getGroupList(deps, options),
+ getGroupListCount: (options: {
+ projectId: string;
+ type?: string;
+ search?: string;
+ }): ReturnType<typeof getGroupListCount> =>
+ getGroupListCount(deps, options),
+ getGroupTypes: (projectId: string): ReturnType<typeof getGroupTypes> =>
+ getGroupTypes(deps, projectId),
+ createGroup: (
+ input: IServiceUpsertGroup
+ ): ReturnType<typeof createGroup> => createGroup(deps, input),
+ updateGroup: (
+ id: string,
+ projectId: string,
+ data: { type?: string; name?: string; properties?: Record<string, unknown> }
+ ): ReturnType<typeof updateGroup> =>
+ updateGroup(deps, id, projectId, data),
+ deleteGroup: (
+ id: string,
+ projectId: string
+ ): ReturnType<typeof deleteGroup> => deleteGroup(deps, id, projectId),
+ getGroupPropertyKeys: (
+ projectId: string
+ ): ReturnType<typeof getGroupPropertyKeys> =>
+ getGroupPropertyKeys(deps, projectId),
+ getGroupStats: (
+ projectId: string,
+ groupIds: string[]
+ ): ReturnType<typeof getGroupStats> =>
+ getGroupStats(deps, projectId, groupIds),
+ getGroupsByIds: (
+ projectId: string,
+ ids: string[]
+ ): ReturnType<typeof getGroupsByIds> =>
+ getGroupsByIds(deps, projectId, ids),
+ getGroupMemberProfiles: (
+ options: GetGroupMemberProfilesOptions
+ ): ReturnType<typeof getGroupMemberProfiles> =>
+ getGroupMemberProfiles(deps, options),
+ getGroupListPage: (
+ input: GetGroupListOptions
+ ): ReturnType<typeof getGroupListPage> => getGroupListPage(deps, input),
+ getGroupMetrics: (
+ id: string,
+ projectId: string
+ ): ReturnType<typeof getGroupMetrics> =>
+ getGroupMetrics(deps, id, projectId),
+ getGroupActivity: (
+ id: string,
+ projectId: string
+ ): ReturnType<typeof getGroupActivity> =>
+ getGroupActivity(deps, id, projectId),
+ getGroupMemberGrowth: (
+ id: string,
+ projectId: string
+ ): ReturnType<typeof getGroupMemberGrowth> =>
+ getGroupMemberGrowth(deps, id, projectId),
+ getGroupMostEvents: (
+ id: string,
+ projectId: string
+ ): ReturnType<typeof getGroupMostEvents> =>
+ getGroupMostEvents(deps, id, projectId),
+ getGroupPopularRoutes: (
+ id: string,
+ projectId: string
+ ): ReturnType<typeof getGroupPopularRoutes> =>
+ getGroupPopularRoutes(deps, id, projectId),
+ getGroupMemberProfilesPage: (
+ input: GetGroupMemberProfilesOptions
+ ): ReturnType<typeof getGroupMemberProfilesPage> =>
+ getGroupMemberProfilesPage(deps, input),
+ listGroupTypesCore: (
+ projectId: string
+ ): ReturnType<typeof listGroupTypesCore> =>
+ listGroupTypesCore(deps, projectId),
+ findGroupsCore: (
+ input: Parameters<typeof findGroupsCore>[1]
+ ): ReturnType<typeof findGroupsCore> => findGroupsCore(deps, input),
+ getGroupCore: (
+ input: Parameters<typeof getGroupCore>[1]
+ ): ReturnType<typeof getGroupCore> => getGroupCore(deps, input),
};
}
diff --git a/packages/core/src/modules/group/src/group.sql.proof.md b/packages/core/src/modules/group/src/sql.proof.md
similarity index 95%
rename from packages/core/src/modules/group/src/group.sql.proof.md
rename to packages/core/src/modules/group/src/sql.proof.md
index f03729f14..b75237c18 100644
--- a/packages/core/src/modules/group/src/group.sql.proof.md
+++ b/packages/core/src/modules/group/src/sql.proof.md
@@ -1,6 +1,6 @@
-# group.sql.ts — V1 → V2 result-set proof (M7-002)
+# sql.ts (formerly group.sql.ts) — V1 → V2 result-set proof (M7-002)
-Every query builder in `group.sql.ts` was executed twice against the same data — once as V1 (the `git HEAD` service/router code, `3300f2a0`) and once as V2 (core's service, which renders the builder) — through one patched ClickHouse client that captured each statement, its `query_params`, `clickhouse_settings`, wall time and the raw JSON response. The two responses were compared on `data` (every row, every column, as JSON) and `meta` (column names and types). Statements without an `ORDER BY` were compared as row sets, because ClickHouse's parallel aggregation returns them in a different order run to run — V1 against itself too.
+Every query builder in `sql.ts` was executed twice against the same data — once as V1 (the `git HEAD` service/router code, `3300f2a0`) and once as V2 (core's service, which renders the builder) — through one patched ClickHouse client that captured each statement, its `query_params`, `clickhouse_settings`, wall time and the raw JSON response. The two responses were compared on `data` (every row, every column, as JSON) and `meta` (column names and types). Statements without an `ORDER BY` were compared as row sets, because ClickHouse's parallel aggregation returns them in a different order run to run — V1 against itself too.
- **Date**: 2026-09-04. **Data**: local prod-copy `openpanel` (319,499,000 events, static), project `redcollege` unless noted; cases needing rows the prod-copy lacks (`cohort_members`, `events_bots` are empty there) also ran on the isolated `openpanel_test` database with 3,380 events copied from `redcollege` under project `m7-002-proof`, plus seeded cohort/bot rows (deleted afterwards).
- **Machine**: single-node ClickHouse 26.1.3.52 on a 4-vCPU box — timings are directional only; production is 2 shards × 2 replicas (`docs/ENVIRONMENT.md`). `rows_read` differences on JOIN statements are ClickHouse's per-run accounting of the right-hand side, not a plan difference: the SQL text is identical up to `{pN:Type}` binding.
diff --git a/packages/core/src/modules/group/src/group.sql.test.ts b/packages/core/src/modules/group/src/sql.test.ts
similarity index 99%
rename from packages/core/src/modules/group/src/group.sql.test.ts
rename to packages/core/src/modules/group/src/sql.test.ts
index 89e4866d5..192fe8ae8 100644
--- a/packages/core/src/modules/group/src/group.sql.test.ts
+++ b/packages/core/src/modules/group/src/sql.test.ts
@@ -19,7 +19,7 @@ import {
groupsByIdsQuery,
groupTypesQuery,
groupUniqueProfilesQuery,
-} from './group.sql';
+} from './sql';
const PROJECT_ID = 'proj-1';
const GROUP_ID = 'grp-1';
diff --git a/packages/core/src/modules/group/src/group.sql.ts b/packages/core/src/modules/group/src/sql.ts
similarity index 97%
rename from packages/core/src/modules/group/src/group.sql.ts
rename to packages/core/src/modules/group/src/sql.ts
index 99095e71c..02390257d 100644
--- a/packages/core/src/modules/group/src/group.sql.ts
+++ b/packages/core/src/modules/group/src/sql.ts
@@ -3,7 +3,7 @@
// packages/trpc/src/routers/group.ts (M7-002): the SQL text is V1's, with every
// value bound as a `{pN:Type}` parameter instead of an escaped literal. Each
// builder's result set was diffed against V1's on the local prod-copy; the
-// statements, params, row counts and timings are in group.sql.proof.md.
+// statements, params, row counts and timings are in sql.proof.md.
//
// Cluster note (docs/ENVIRONMENT.md): `groups`, `profiles` and `events` are
// Distributed on Cloud. No query here carries an `IN (subquery)`; the `IN`
@@ -17,6 +17,9 @@ const TABLE = {
events: 'events',
} as const;
+/** The one table name `group.service.ts` needs outside a query fragment. */
+export const GROUPS_TABLE = TABLE.groups;
+
const GROUP_COLUMNS = sql`project_id, id, type, name, properties, created_at, version`;
function optional(