1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-23 00:22:19 +01:00

chore: stop using deprecated method (#6764)

`storeUserEvent` from event-service was deprecated. We stop using it and
remove the method completely
This commit is contained in:
Gastón Fournier 2024-04-03 09:55:09 +02:00 committed by GitHub
parent 994bc5b1f1
commit 0a0f5a73ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 8 additions and 64 deletions

View File

@ -1,20 +0,0 @@
import { ADMIN_TOKEN_USER, type IApiUser } from '../../types';
import { createTestConfig } from '../../../test/config/test-config';
import { createFakeEventsService } from '..';
import { ApiTokenType } from '../../types/models/api-token';
test('when using an admin token should get the username of the token and the id from internalAdminTokenUserId', async () => {
const adminToken: IApiUser = {
projects: ['*'],
environment: '*',
type: ApiTokenType.ADMIN,
secret: '',
username: 'admin-token-username',
permissions: [],
internalAdminTokenUserId: ADMIN_TOKEN_USER.id,
};
const eventService = createFakeEventsService(createTestConfig());
const userDetails = eventService.getUserDetails(adminToken);
expect(userDetails.createdBy).toBe('admin-token-username');
expect(userDetails.createdByUserId).toBe(ADMIN_TOKEN_USER.id);
});

View File

@ -2,15 +2,11 @@ import type { IUnleashConfig } from '../../types/option';
import type { IFeatureTagStore, IUnleashStores } from '../../types/stores';
import type { Logger } from '../../logger';
import type { IEventStore } from '../../types/stores/event-store';
import type { IBaseEvent, IEventList, IUserEvent } from '../../types/events';
import type { IBaseEvent, IEventList } from '../../types/events';
import type { SearchEventsSchema } from '../../openapi/spec/search-events-schema';
import type EventEmitter from 'events';
import type { IApiUser, ITag, IUser } from '../../types';
import { ApiTokenType } from '../../types/models/api-token';
import {
extractUserIdFromUser,
extractUsernameFromUser,
} from '../../util/extract-user';
import { EVENTS_CREATED_BY_PROCESSED } from '../../metric-events';
export default class EventService {
@ -94,16 +90,6 @@ export default class EventService {
return (user as IApiUser)?.type === ApiTokenType.ADMIN;
}
getUserDetails(user: IUser | IApiUser): {
createdBy: string;
createdByUserId: number;
} {
return {
createdBy: extractUsernameFromUser(user),
createdByUserId: extractUserIdFromUser(user),
};
}
async storeEvent(event: IBaseEvent): Promise<void> {
return this.storeEvents([event]);
}
@ -116,31 +102,6 @@ export default class EventService {
return this.eventStore.batchStore(enhancedEvents);
}
/**
* @deprecated this is tech debt, we should migrate to storeEvents and send the right
* userId and username parameters in IBaseEvent
*/
async storeUserEvent(event: IUserEvent): Promise<void> {
return this.storeUserEvents([event]);
}
/**
* @deprecated this is tech debt, we should migrate to storeEvents and send the right
* userId and username parameters in IBaseEvent
*/
async storeUserEvents(events: IUserEvent[]): Promise<void> {
let enhancedEvents = events.map(({ byUser, ...event }) => {
return {
...event,
...this.getUserDetails(byUser),
};
});
for (const enhancer of [this.enhanceEventsWithTags.bind(this)]) {
enhancedEvents = await enhancer(enhancedEvents);
}
return this.eventStore.batchStore(enhancedEvents);
}
async setEventCreatedByUserId(): Promise<void> {
const updated = await this.eventStore.setCreatedByUserId(100);
if (updated !== undefined) {

View File

@ -10,6 +10,7 @@ import { OperationDeniedError } from '../error/operation-denied-error';
import { PAT_LIMIT } from '../util/constants';
import type EventService from '../features/events/event-service';
import type { CreatePatSchema, PatSchema } from '../openapi';
import { extractUserIdFromUser, extractUsernameFromUser } from '../util';
export default class PatService {
private config: IUnleashConfig;
@ -41,9 +42,10 @@ export default class PatService {
const secret = this.generateSecretKey();
const newPat = await this.patStore.create(pat, secret, forUserId);
await this.eventService.storeUserEvent({
await this.eventService.storeEvent({
type: PAT_CREATED,
byUser,
createdBy: extractUsernameFromUser(byUser),
createdByUserId: extractUserIdFromUser(byUser),
data: { ...pat, secret: '***' },
});
@ -61,9 +63,10 @@ export default class PatService {
): Promise<void> {
const pat = await this.patStore.get(id);
await this.eventService.storeUserEvent({
await this.eventService.storeEvent({
type: PAT_DELETED,
byUser,
createdBy: extractUsernameFromUser(byUser),
createdByUserId: extractUserIdFromUser(byUser),
data: { ...pat, secret: '***' },
});