-
-
Notifications
You must be signed in to change notification settings - Fork 736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: user settings #8556
Closed
Closed
feat: user settings #8556
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 11 additions & 0 deletions
11
src/lib/features/user-settings/createUserSettingsService.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 { IUnleashConfig, IUnleashStores } from '../../types'; | ||
import type EventService from '../events/event-service'; | ||
import { UserSettingsService } from './user-settings-service'; | ||
|
||
export const createUserSettingsService = ( | ||
stores: Pick<IUnleashStores, 'userStore'>, | ||
config: Pick<IUnleashConfig, 'getLogger'>, | ||
eventService: EventService, | ||
): UserSettingsService => { | ||
return new UserSettingsService(stores, config, eventService); | ||
}; |
97 changes: 97 additions & 0 deletions
97
src/lib/features/user-settings/user-settings-controller.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,97 @@ | ||
import Controller from '../../routes/controller'; | ||
import type { OpenApiService } from '../../services'; | ||
import type { UserSettingsService } from './user-settings-service'; | ||
import type { | ||
IFlagResolver, | ||
IUnleashConfig, | ||
IUnleashServices, | ||
} from '../../types'; | ||
import { | ||
createRequestSchema, | ||
createResponseSchema, | ||
getStandardResponses, | ||
} from '../../openapi'; | ||
import { ForbiddenError } from '../../error'; | ||
|
||
export default class UserSettingsController extends Controller { | ||
private userSettingsService: UserSettingsService; | ||
|
||
private flagResolver: IFlagResolver; | ||
|
||
private openApiService: OpenApiService; | ||
|
||
constructor(config: IUnleashConfig, services: IUnleashServices) { | ||
super(config); | ||
this.userSettingsService = services.userSettingsService; | ||
this.openApiService = services.openApiService; | ||
this.flagResolver = config.flagResolver; | ||
|
||
this.route({ | ||
method: 'get', | ||
path: '', | ||
handler: this.getUserSettings, | ||
permission: 'user', | ||
middleware: [ | ||
this.openApiService.validPath({ | ||
tags: ['Unstable'], // TODO: Remove this tag when the endpoint is stable | ||
operationId: 'getUserSettings', | ||
summary: 'Get user settings', | ||
description: | ||
'Get the settings for the currently authenticated user.', | ||
responses: { | ||
200: createResponseSchema('userSettingsSchema'), | ||
...getStandardResponses(401, 403, 404), | ||
}, | ||
}), | ||
], | ||
}); | ||
|
||
this.route({ | ||
method: 'put', | ||
path: '', | ||
handler: this.updateUserSettings, | ||
permission: 'user', | ||
middleware: [ | ||
this.openApiService.validPath({ | ||
tags: ['Unstable'], // TODO: Update/remove when endpoint stabilizes | ||
operationId: 'updateUserSettings', | ||
summary: 'Update user settings', | ||
description: 'Update a specific user setting by key.', | ||
requestBody: createRequestSchema('setUserSettingSchema'), | ||
responses: { | ||
204: { description: 'Setting updated successfully' }, | ||
...getStandardResponses(400, 401, 403, 409, 415), | ||
}, | ||
}), | ||
], | ||
}); | ||
} | ||
|
||
async getUserSettings(req, res) { | ||
if (!this.flagResolver.isEnabled('userSettings')) { | ||
throw new ForbiddenError('User settings feature is not enabled'); | ||
} | ||
const { user } = req; | ||
const settings = await this.userSettingsService.getAll(user.id); | ||
res.json(settings); | ||
} | ||
|
||
async updateUserSettings(req, res) { | ||
if (!this.flagResolver.isEnabled('userSettings')) { | ||
throw new ForbiddenError('User settings feature is not enabled'); | ||
} | ||
const { user } = req; | ||
const { key, value } = req.body; | ||
const allowedSettings = ['productivity-insights-email']; | ||
|
||
if (!allowedSettings.includes(key)) { | ||
res.status(400).json({ | ||
message: `Invalid setting key`, | ||
}); | ||
return; | ||
} | ||
|
||
await this.userSettingsService.set(user.id, key, value, user); | ||
res.status(204).end(); | ||
} | ||
} |
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,48 @@ | ||
import type { IUnleashStores } from '../../types/stores'; | ||
|
||
import type { Logger } from '../../logger'; | ||
import type { IUnleashConfig } from '../../types/option'; | ||
import type EventService from '../events/event-service'; | ||
import { | ||
type IAuditUser, | ||
UserSettingsUpdatedEvent, | ||
type IUserStore, | ||
} from '../../types'; | ||
import type { UserSettingsSchema } from '../../openapi/spec/user-settings-schema'; | ||
|
||
export class UserSettingsService { | ||
private userStore: IUserStore; | ||
|
||
private eventService: EventService; | ||
|
||
private logger: Logger; | ||
|
||
constructor( | ||
{ userStore }: Pick<IUnleashStores, 'userStore'>, | ||
{ getLogger }: Pick<IUnleashConfig, 'getLogger'>, | ||
eventService: EventService, | ||
) { | ||
this.userStore = userStore; | ||
this.eventService = eventService; | ||
this.logger = getLogger('services/user-settings-service.js'); | ||
} | ||
|
||
async getAll(userId: number): Promise<UserSettingsSchema['settings']> { | ||
return this.userStore.getSettings(userId); | ||
} | ||
|
||
async set( | ||
userId: number, | ||
param: string, | ||
value: string, | ||
auditUser: IAuditUser, | ||
) { | ||
await this.userStore.setSettings(userId, { [param]: value }); | ||
await this.eventService.storeEvent( | ||
new UserSettingsUpdatedEvent({ | ||
auditUser, | ||
data: { userId, param, value }, | ||
}), | ||
); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
src/lib/features/user-settings/user-settings.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,107 @@ | ||
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'; | ||
import type { IUserStore } from '../../types'; | ||
|
||
let app: IUnleashTest; | ||
let db: ITestDb; | ||
let userStore: IUserStore; | ||
|
||
const loginUser = (email: string) => { | ||
return app.request | ||
.post(`/auth/demo/login`) | ||
.send({ | ||
email, | ||
}) | ||
.expect(200); | ||
}; | ||
|
||
beforeAll(async () => { | ||
db = await dbInit('user_settings', getLogger); | ||
app = await setupAppWithAuth( | ||
db.stores, | ||
{ | ||
experimental: { | ||
flags: { | ||
userSettings: true, | ||
}, | ||
}, | ||
}, | ||
db.rawDatabase, | ||
); | ||
userStore = db.stores.userStore; | ||
}); | ||
|
||
afterAll(async () => { | ||
getLogger.setMuteError(false); | ||
await app.destroy(); | ||
await db.destroy(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await db.stores.userStore.deleteAll(); | ||
await db.stores.eventStore.deleteAll(); | ||
}); | ||
|
||
describe('UserSettingsController', () => { | ||
test('should return user settings', async () => { | ||
const { body: user } = await loginUser('[email protected]'); | ||
// console.log({user}) | ||
// await db.stores.userStore.setSettings(1, { | ||
// 'productivity-insights-email': 'true', | ||
// }); | ||
// const { body } = await app.request | ||
// .put(`/api/admin/user/settings`) | ||
// .send({ | ||
// key: 'productivity-insights-email', | ||
// value: 'new_value', | ||
// }) | ||
// .expect(204); | ||
|
||
const res = await app.request.get('/api/admin/user').expect(200); | ||
|
||
// expect(res.body).toEqual({ 'productivity-insights-email': 'true' }); | ||
}); | ||
|
||
// test('should return empty object if no settings are available', async () => { | ||
// const res = await app.request | ||
// .get('/api/admin/user/settings') | ||
// // .set('Authorization', `Bearer ${userId}`) | ||
// .expect(200); | ||
|
||
// expect(res.body).toEqual({}); | ||
// }); | ||
|
||
// describe('PUT /settings/:key', () => { | ||
// const allowedKey = 'productivity-insights-email'; | ||
|
||
// test('should update user setting if key is valid', async () => { | ||
// const res = await app.request | ||
// .put(`/api/admin/user/settings/${allowedKey}`) | ||
// // .set('Authorization', `Bearer ${userId}`) | ||
// .send({ value: 'new_value' }) | ||
// .expect(204); | ||
|
||
// expect(res.body).toEqual({}); | ||
|
||
// const updatedSetting = | ||
// await db.stores.userStore.getSettings(userId); | ||
// expect(updatedSetting.value).toEqual('new_value'); | ||
// }); | ||
|
||
// test('should return 400 for invalid setting key', async () => { | ||
// const res = await app.request | ||
// .put(`/api/admin/user/settings/invalid-key`) | ||
// // .set('Authorization', `Bearer ${userId}`) | ||
// .send({ value: 'some_value' }) | ||
// .expect(400); | ||
|
||
// expect(res.body).toEqual({ | ||
// message: 'Invalid setting key', | ||
// }); | ||
// }); | ||
// }); | ||
}); |
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,23 @@ | ||
import type { FromSchema } from 'json-schema-to-ts'; | ||
|
||
export const setUserSettingSchema = { | ||
$id: '#/components/schemas/setUserSettingSchema', | ||
type: 'object', | ||
description: 'Schema for setting a user-specific value', | ||
required: ['key', 'value'], | ||
properties: { | ||
key: { | ||
type: 'string', | ||
description: 'Setting key', | ||
example: 'email', | ||
}, | ||
value: { | ||
type: 'string', | ||
description: 'The setting value for the user', | ||
example: 'optOut', | ||
}, | ||
}, | ||
components: {}, | ||
} as const; | ||
|
||
export type SetUserSettingSchema = FromSchema<typeof setUserSettingSchema>; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.