From e512e8de84a0a51c808c94afd9303bbc66cac89a Mon Sep 17 00:00:00 2001 From: Jason Rasmussen Date: Tue, 23 Apr 2024 10:49:46 -0400 Subject: [PATCH] fix: handle deleted users --- e2e/src/api/specs/album.e2e-spec.ts | 11 +----- server/src/repositories/album.repository.ts | 42 +++++++++++++++------ 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/e2e/src/api/specs/album.e2e-spec.ts b/e2e/src/api/specs/album.e2e-spec.ts index c5ad2d8615..5240cdb0d2 100644 --- a/e2e/src/api/specs/album.e2e-spec.ts +++ b/e2e/src/api/specs/album.e2e-spec.ts @@ -339,10 +339,7 @@ describe('/album', () => { .set('Authorization', `Bearer ${user1.accessToken}`); expect(status).toBe(200); - expect(body).toEqual({ - ...user2Albums[0], - assets: [expect.objectContaining({ id: user2Albums[0].assets[0].id })], - }); + expect(body).toMatchObject({ id: user2Albums[0].id }); }); it('should return album info for shared album (viewer)', async () => { @@ -351,11 +348,7 @@ describe('/album', () => { .set('Authorization', `Bearer ${user2.accessToken}`); expect(status).toBe(200); - console.log(body); - expect(body).toEqual({ - ...user1Albums[3], - assets: [expect.objectContaining({ id: user2Albums[0].assets[0].id })], - }); + expect(body).toMatchObject({ id: user1Albums[3].id }); }); it('should return album info with assets when withoutAssets is undefined', async () => { diff --git a/server/src/repositories/album.repository.ts b/server/src/repositories/album.repository.ts index 9fa4f20135..536c9b666d 100644 --- a/server/src/repositories/album.repository.ts +++ b/server/src/repositories/album.repository.ts @@ -10,6 +10,13 @@ import { Instrumentation } from 'src/utils/instrumentation'; import { setUnion } from 'src/utils/set'; import { DataSource, FindOptionsOrder, FindOptionsRelations, In, IsNull, Not, Repository } from 'typeorm'; +const withoutDeletedUsers = (album: T) => { + if (album) { + album.albumUsers = album.albumUsers.filter((albumUser) => albumUser.user && !albumUser.user.deletedAt); + } + return album; +}; + @Instrumentation() @Injectable() export class AlbumRepository implements IAlbumRepository { @@ -20,7 +27,7 @@ export class AlbumRepository implements IAlbumRepository { ) {} @GenerateSql({ params: [DummyValue.UUID, {}] }) - getById(id: string, options: AlbumInfoOptions): Promise { + async getById(id: string, options: AlbumInfoOptions): Promise { const relations: FindOptionsRelations = { owner: true, albumUsers: { user: true }, @@ -40,13 +47,14 @@ export class AlbumRepository implements IAlbumRepository { }; } - return this.repository.findOne({ where: { id }, relations, order }); + const album = await this.repository.findOne({ where: { id }, relations, order }); + return withoutDeletedUsers(album); } @GenerateSql({ params: [[DummyValue.UUID]] }) @ChunkedArray() - getByIds(ids: string[]): Promise { - return this.repository.find({ + async getByIds(ids: string[]): Promise { + const albums = await this.repository.find({ where: { id: In(ids), }, @@ -55,11 +63,13 @@ export class AlbumRepository implements IAlbumRepository { albumUsers: { user: true }, }, }); + + return albums.map((album) => withoutDeletedUsers(album)); } @GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID] }) - getByAssetId(ownerId: string, assetId: string): Promise { - return this.repository.find({ + async getByAssetId(ownerId: string, assetId: string): Promise { + const albums = await this.repository.find({ where: [ { ownerId, assets: { id: assetId } }, { albumUsers: { userId: ownerId }, assets: { id: assetId } }, @@ -67,6 +77,8 @@ export class AlbumRepository implements IAlbumRepository { relations: { owner: true, albumUsers: { user: true } }, order: { createdAt: 'DESC' }, }); + + return albums.map((album) => withoutDeletedUsers(album)); } @GenerateSql({ params: [[DummyValue.UUID]] }) @@ -127,20 +139,22 @@ export class AlbumRepository implements IAlbumRepository { } @GenerateSql({ params: [DummyValue.UUID] }) - getOwned(ownerId: string): Promise { - return this.repository.find({ + async getOwned(ownerId: string): Promise { + const albums = await this.repository.find({ relations: { albumUsers: { user: true }, sharedLinks: true, owner: true }, where: { ownerId }, order: { createdAt: 'DESC' }, }); + + return albums.map((album) => withoutDeletedUsers(album)); } /** * Get albums shared with and shared by owner. */ @GenerateSql({ params: [DummyValue.UUID] }) - getShared(ownerId: string): Promise { - return this.repository.find({ + async getShared(ownerId: string): Promise { + const albums = await this.repository.find({ relations: { albumUsers: { user: true }, sharedLinks: true, owner: true }, where: [ { albumUsers: { userId: ownerId } }, @@ -149,18 +163,22 @@ export class AlbumRepository implements IAlbumRepository { ], order: { createdAt: 'DESC' }, }); + + return albums.map((album) => withoutDeletedUsers(album)); } /** * Get albums of owner that are _not_ shared */ @GenerateSql({ params: [DummyValue.UUID] }) - getNotShared(ownerId: string): Promise { - return this.repository.find({ + async getNotShared(ownerId: string): Promise { + const albums = await this.repository.find({ relations: { albumUsers: true, sharedLinks: true, owner: true }, where: { ownerId, albumUsers: { user: IsNull() }, sharedLinks: { id: IsNull() } }, order: { createdAt: 'DESC' }, }); + + return albums.map((album) => withoutDeletedUsers(album)); } async restoreAll(userId: string): Promise {