Skip to content

Commit

Permalink
fix: event creators, distinct on two users with same id (#7824)
Browse files Browse the repository at this point in the history
Previously distinct was not working properly, because we were joining
users table.
You needed to do distinctOn. Now fixed.
  • Loading branch information
sjaanus authored Aug 9, 2024
1 parent 1db222a commit 3fbb645
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/lib/features/events/event-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ class EventStore implements IEventStore {

async getEventCreators(): Promise<Array<{ id: number; name: string }>> {
const query = this.db('events')
.distinct('events.created_by_user_id')
.distinctOn('events.created_by_user_id')
.leftJoin('users', 'users.id', '=', 'events.created_by_user_id')
.select([
'events.created_by_user_id as id',
Expand All @@ -399,8 +399,8 @@ class EventStore implements IEventStore {

const result = await query;
return result
.filter((row) => row.name || row.username || row.email)
.map((row) => ({
.filter((row: any) => row.name || row.username || row.email)
.map((row: any) => ({
id: Number(row.id),
name: String(row.name || row.username || row.email),
}));
Expand Down
42 changes: 35 additions & 7 deletions src/test/e2e/api/admin/event.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,27 +158,20 @@ test('event creators - if system user, return system name, else should return na
{
type: FEATURE_CREATED,
project: randomId(),
data: { id: randomId() },
tags: [],
createdBy: 'should-not-use-this-name',
createdByUserId: SYSTEM_USER.id,
ip: '127.0.0.1',
},
{
type: FEATURE_CREATED,
project: randomId(),
data: { id: randomId() },
tags: [],
createdBy: 'test-user1',
createdByUserId: user.id,
ip: '127.0.0.1',
},
{
type: FEATURE_CREATED,
project: randomId(),
data: { id: randomId() },
preData: { id: randomId() },
tags: [{ type: 'simple', value: randomId() }],
createdBy: 'test-user2',
createdByUserId: 2,
ip: '127.0.0.1',
Expand Down Expand Up @@ -209,3 +202,38 @@ test('event creators - if system user, return system name, else should return na
},
]);
});

test('event creators - takes single distinct username, if 2 users have same id', async () => {
const events: IBaseEvent[] = [
{
type: FEATURE_CREATED,
project: randomId(),
createdBy: 'test-user4',
createdByUserId: 2,
ip: '127.0.0.1',
},
{
type: FEATURE_CREATED,
project: randomId(),
createdBy: 'test-user2',
createdByUserId: 2,
ip: '127.0.0.1',
},
];

await Promise.all(
events.map((event) => {
return eventService.storeEvent(event);
}),
);

const { body } = await app.request
.get('/api/admin/event-creators')
.expect(200);
expect(body).toMatchObject([
{
id: 2,
name: 'test-user4',
},
]);
});

0 comments on commit 3fbb645

Please sign in to comment.