1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-07-26 13:48:33 +02:00

refactor: introduce countProjectTokens method on ApiTokenStore (#8674)

This change introduces a new method `countProjectTokens` on the
`IApiTokenStore` interface. It also swaps out the manual filtering for
api tokens belonging to a project in the project status service.
This commit is contained in:
Thomas Heartman 2024-11-06 14:13:44 +01:00 committed by GitHub
parent 7c28d247d8
commit c70c023143
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 58 additions and 11 deletions

View File

@ -326,4 +326,12 @@ export class ApiTokenStore implements IApiTokenStore {
activeLegacyTokens,
};
}
async countProjectTokens(projectId: string): Promise<number> {
const count = await this.db(API_LINK_TABLE)
.where({ project: projectId })
.count()
.first();
return Number(count?.count ?? 0);
}
}

View File

@ -38,17 +38,7 @@ export class ProjectStatusService {
] = await Promise.all([
this.projectStore.getConnectedEnvironmentCountForProject(projectId),
this.projectStore.getMembersCountByProject(projectId),
this.apiTokenStore
.getAll()
.then(
(tokens) =>
tokens.filter(
(token) =>
token.project === projectId ||
token.projects.includes(projectId),
).length,
),
this.apiTokenStore.countProjectTokens(projectId),
this.segmentStore.getProjectSegmentCount(projectId),
this.eventStore.getProjectRecentEventActivity(projectId),
]);

View File

@ -14,4 +14,5 @@ export interface IApiTokenStore extends Store<IApiToken, string> {
legacyTokens: number;
activeLegacyTokens: number;
}>;
countProjectTokens(projectId: string): Promise<number>;
}

View File

@ -2,6 +2,7 @@ import dbInit, { type ITestDb } from '../helpers/database-init';
import getLogger from '../../fixtures/no-logger';
import type { IUnleashStores } from '../../../lib/types';
import { ApiTokenType } from '../../../lib/types/models/api-token';
import { randomId } from '../../../lib/util';
let stores: IUnleashStores;
let db: ITestDb;
@ -182,3 +183,46 @@ describe('count deprecated tokens', () => {
});
});
});
describe('count project tokens', () => {
test('counts only tokens belonging to the specified project', async () => {
const project = await stores.projectStore.create({
id: randomId(),
name: 'project A',
});
const store = stores.apiTokenStore;
await store.insert({
secret: `default:default.${randomId()}`,
environment: 'default',
type: ApiTokenType.CLIENT,
projects: ['default'],
tokenName: 'token1',
});
await store.insert({
secret: `*:*.${randomId()}`,
environment: 'default',
type: ApiTokenType.CLIENT,
projects: ['*'],
tokenName: 'token2',
});
await store.insert({
secret: `${project.id}:default.${randomId()}`,
environment: 'default',
type: ApiTokenType.CLIENT,
projects: [project.id],
tokenName: 'token3',
});
await store.insert({
secret: `[]:default.${randomId()}`,
environment: 'default',
type: ApiTokenType.CLIENT,
projects: [project.id, 'default'],
tokenName: 'token4',
});
expect(await store.countProjectTokens(project.id)).toBe(2);
});
});

View File

@ -92,4 +92,8 @@ export default class FakeApiTokenStore
activeLegacyTokens: 0,
};
}
async countProjectTokens(): Promise<number> {
return 0;
}
}