-
-
Notifications
You must be signed in to change notification settings - Fork 744
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: project flag creators api (#7302)
- Loading branch information
Showing
12 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/lib/features/project/fake-project-flag-creators-read-model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { IProjectFlagCreatorsReadModel } from './project-flag-creators-read-model.type'; | ||
|
||
export class FakeProjectFlagCreatorsReadModel | ||
implements IProjectFlagCreatorsReadModel | ||
{ | ||
async getFlagCreators( | ||
project: string, | ||
): Promise<{ id: number; name: string }[]> { | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/lib/features/project/project-flag-creators-read-model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { Db } from '../../db/db'; | ||
import type { IProjectFlagCreatorsReadModel } from './project-flag-creators-read-model.type'; | ||
|
||
export class ProjectFlagCreatorsReadModel | ||
implements IProjectFlagCreatorsReadModel | ||
{ | ||
private db: Db; | ||
|
||
constructor(db: Db) { | ||
this.db = db; | ||
} | ||
|
||
async getFlagCreators( | ||
project: string, | ||
): Promise<Array<{ id: number; name: string }>> { | ||
const result = await this.db('users') | ||
.distinct('users.id') | ||
.join('features', 'users.id', '=', 'features.created_by_user_id') | ||
.where('features.project', project) | ||
.select([ | ||
'users.id', | ||
'users.name', | ||
'users.username', | ||
'users.email', | ||
]); | ||
return result.map((row) => ({ | ||
id: Number(row.id), | ||
name: String(row.name || row.username || row.email), | ||
})); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/lib/features/project/project-flag-creators-read-model.type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface IProjectFlagCreatorsReadModel { | ||
getFlagCreators( | ||
project: string, | ||
): Promise<Array<{ id: number; name: string }>>; | ||
} |
61 changes: 61 additions & 0 deletions
61
src/lib/features/project/project-flag-creators.e2e.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import dbInit, { type ITestDb } from '../../../test/e2e/helpers/database-init'; | ||
import { | ||
type IUnleashTest, | ||
setupAppWithAuth, | ||
} from '../../../test/e2e/helpers/test-helper'; | ||
import getLogger from '../../../test/fixtures/no-logger'; | ||
|
||
let app: IUnleashTest; | ||
let db: ITestDb; | ||
|
||
beforeAll(async () => { | ||
db = await dbInit('project_flag_creators', getLogger); | ||
app = await setupAppWithAuth( | ||
db.stores, | ||
{ | ||
experimental: { | ||
flags: { | ||
strictSchemaValidation: true, | ||
}, | ||
}, | ||
}, | ||
db.rawDatabase, | ||
); | ||
}); | ||
|
||
afterEach(async () => { | ||
await db.stores.featureToggleStore.deleteAll(); | ||
await db.stores.userStore.deleteAll(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app.destroy(); | ||
await db.destroy(); | ||
}); | ||
|
||
test('should return flag creators', async () => { | ||
await app.request | ||
.post(`/auth/demo/login`) | ||
.send({ | ||
email: '[email protected]', | ||
}) | ||
.expect(200); | ||
await app.createFeature('flag-name-1'); | ||
await app.request | ||
.post(`/auth/demo/login`) | ||
.send({ | ||
email: '[email protected]', | ||
}) | ||
.expect(200); | ||
await app.createFeature('flag-name-2'); | ||
|
||
const { body } = await app.request | ||
.get('/api/admin/projects/default/flag-creators') | ||
.expect('Content-Type', /json/) | ||
.expect(200); | ||
|
||
expect(body).toEqual([ | ||
{ id: 1, name: '[email protected]' }, | ||
{ id: 2, name: '[email protected]' }, | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { FromSchema } from 'json-schema-to-ts'; | ||
|
||
export const projectFlagCreatorsSchema = { | ||
$id: '#/components/schemas/projectFlagCreatorsSchema', | ||
type: 'array', | ||
description: 'A list of project flag creators', | ||
items: { | ||
type: 'object', | ||
additionalProperties: false, | ||
required: ['id', 'name'], | ||
properties: { | ||
id: { | ||
type: 'integer', | ||
example: 50, | ||
description: 'The user id.', | ||
}, | ||
name: { | ||
description: | ||
"Name of the user. If the user has no set name, the API falls back to using the user's username (if they have one) or email (if neither name or username is set).", | ||
type: 'string', | ||
example: 'User', | ||
}, | ||
}, | ||
}, | ||
|
||
components: { | ||
schemas: {}, | ||
}, | ||
} as const; | ||
|
||
export type ProjectFlagCreatorsSchema = FromSchema< | ||
typeof projectFlagCreatorsSchema | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters