Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion packages/api/src/routers/api/__tests__/me.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// against a shared server fixture. The 300-line guideline targets source
// modules, not test suites.
import type { OnboardingTaskId } from '@hyperdx/common-utils/dist/types';
import { AlertThresholdType } from '@hyperdx/common-utils/dist/types';
import {
AlertThresholdType,
MeTeamSchema,
} from '@hyperdx/common-utils/dist/types';

import {
getAgent,
Expand All @@ -13,6 +16,7 @@ import {
randomMongoId,
} from '@/fixtures';
import type { ObjectId } from '@/models';
import Team from '@/models/team';
import User from '@/models/user';
import Webhook, { WebhookService } from '@/models/webhook';

Expand Down Expand Up @@ -43,6 +47,23 @@ describe('me router', () => {
expect(resp.body.team.id).toEqual(team._id.toString());
});

it('returns only the team fields in MeTeamSchema', async () => {
const { agent, team } = await getLoggedInAgent(server);
await Team.updateOne({ _id: team._id }, { queryTimeout: 42 });

const resp = await agent.get('/me').expect(200);

expect(resp.body.team.queryTimeout).toBe(42);
expect(resp.body.team.name).toEqual(team.name);
expect(resp.body.team).not.toHaveProperty('apiKey');
expect(resp.body.team).not.toHaveProperty('hookId');
expect(
Object.keys(resp.body.team).filter(
k => !['_id', ...MeTeamSchema.keyof().options].includes(k),
),
).toEqual([]);
});

it('defaults onboardingData for users created before the field existed', async () => {
const { agent, user } = await getLoggedInAgent(server);
// Simulate a legacy document with no onboardingData subdocument.
Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/routers/api/me.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
import {
CompleteOnboardingTaskApiBodySchema,
DismissOnboardingApiBodySchema,
MeTeamSchema,
OnboardingDataSchema,
} from '@hyperdx/common-utils/dist/types';
import express from 'express';
Expand Down Expand Up @@ -39,7 +40,7 @@ router.get('/', async (req, res: express.Response<MeApiResponse>, next) => {
team: teamId,
} = req.user;

const team = await getTeam(teamId);
const team = await getTeam(teamId, MeTeamSchema.keyof().options);
if (team == null) {
throw new Api404Error(`Team not found for user ${id}`);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ interface MeResponse {
email: string;
id: string;
name: string;
team: MeTeam & { apiKey: string };
team: MeTeam;
/**
* All teams the user belongs to. Present on multi-team deployments
* (HyperDX Cloud / EE). Absent on OSS, where the user always belongs
Expand Down
12 changes: 6 additions & 6 deletions packages/common-utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3525,19 +3525,19 @@ export const OnboardingDataSchema = z.object({
export type OnboardingData = z.infer<typeof OnboardingDataSchema>;

// Me
// GET /me loads only these Team fields, so a new Team secret can't reach every member.
export const MeTeamSchema = TeamSchema.pick({ id: true, name: true }).merge(
TeamClickHouseSettingsSchema,
);

export const MeApiResponseSchema = z.object({
accessKey: z.string(),
createdAt: z.string(),
email: z.string(),
id: z.string(),
name: z.string(),
onboardingData: OnboardingDataSchema,
team: TeamSchema.pick({
id: true,
name: true,
allowedAuthMethods: true,
apiKey: true,
}).merge(TeamClickHouseSettingsSchema),
team: MeTeamSchema,
usageStatsEnabled: z.boolean(),
aiAssistantEnabled: z.boolean(),
});
Expand Down
Loading