mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01: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:
		
							parent
							
								
									7c28d247d8
								
							
						
					
					
						commit
						c70c023143
					
				| @ -326,4 +326,12 @@ export class ApiTokenStore implements IApiTokenStore { | |||||||
|             activeLegacyTokens, |             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); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -38,17 +38,7 @@ export class ProjectStatusService { | |||||||
|         ] = await Promise.all([ |         ] = await Promise.all([ | ||||||
|             this.projectStore.getConnectedEnvironmentCountForProject(projectId), |             this.projectStore.getConnectedEnvironmentCountForProject(projectId), | ||||||
|             this.projectStore.getMembersCountByProject(projectId), |             this.projectStore.getMembersCountByProject(projectId), | ||||||
|             this.apiTokenStore |             this.apiTokenStore.countProjectTokens(projectId), | ||||||
|                 .getAll() |  | ||||||
|                 .then( |  | ||||||
|                     (tokens) => |  | ||||||
|                         tokens.filter( |  | ||||||
|                             (token) => |  | ||||||
|                                 token.project === projectId || |  | ||||||
|                                 token.projects.includes(projectId), |  | ||||||
|                         ).length, |  | ||||||
|                 ), |  | ||||||
| 
 |  | ||||||
|             this.segmentStore.getProjectSegmentCount(projectId), |             this.segmentStore.getProjectSegmentCount(projectId), | ||||||
|             this.eventStore.getProjectRecentEventActivity(projectId), |             this.eventStore.getProjectRecentEventActivity(projectId), | ||||||
|         ]); |         ]); | ||||||
|  | |||||||
| @ -14,4 +14,5 @@ export interface IApiTokenStore extends Store<IApiToken, string> { | |||||||
|         legacyTokens: number; |         legacyTokens: number; | ||||||
|         activeLegacyTokens: number; |         activeLegacyTokens: number; | ||||||
|     }>; |     }>; | ||||||
|  |     countProjectTokens(projectId: string): Promise<number>; | ||||||
| } | } | ||||||
|  | |||||||
| @ -2,6 +2,7 @@ import dbInit, { type ITestDb } from '../helpers/database-init'; | |||||||
| import getLogger from '../../fixtures/no-logger'; | import getLogger from '../../fixtures/no-logger'; | ||||||
| import type { IUnleashStores } from '../../../lib/types'; | import type { IUnleashStores } from '../../../lib/types'; | ||||||
| import { ApiTokenType } from '../../../lib/types/models/api-token'; | import { ApiTokenType } from '../../../lib/types/models/api-token'; | ||||||
|  | import { randomId } from '../../../lib/util'; | ||||||
| 
 | 
 | ||||||
| let stores: IUnleashStores; | let stores: IUnleashStores; | ||||||
| let db: ITestDb; | 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); | ||||||
|  |     }); | ||||||
|  | }); | ||||||
|  | |||||||
							
								
								
									
										4
									
								
								src/test/fixtures/fake-api-token-store.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								src/test/fixtures/fake-api-token-store.ts
									
									
									
									
										vendored
									
									
								
							| @ -92,4 +92,8 @@ export default class FakeApiTokenStore | |||||||
|             activeLegacyTokens: 0, |             activeLegacyTokens: 0, | ||||||
|         }; |         }; | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     async countProjectTokens(): Promise<number> { | ||||||
|  |         return 0; | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user