fix: handle deleted users
This commit is contained in:
@@ -339,10 +339,7 @@ describe('/album', () => {
|
|||||||
.set('Authorization', `Bearer ${user1.accessToken}`);
|
.set('Authorization', `Bearer ${user1.accessToken}`);
|
||||||
|
|
||||||
expect(status).toBe(200);
|
expect(status).toBe(200);
|
||||||
expect(body).toEqual({
|
expect(body).toMatchObject({ id: user2Albums[0].id });
|
||||||
...user2Albums[0],
|
|
||||||
assets: [expect.objectContaining({ id: user2Albums[0].assets[0].id })],
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return album info for shared album (viewer)', async () => {
|
it('should return album info for shared album (viewer)', async () => {
|
||||||
@@ -351,11 +348,7 @@ describe('/album', () => {
|
|||||||
.set('Authorization', `Bearer ${user2.accessToken}`);
|
.set('Authorization', `Bearer ${user2.accessToken}`);
|
||||||
|
|
||||||
expect(status).toBe(200);
|
expect(status).toBe(200);
|
||||||
console.log(body);
|
expect(body).toMatchObject({ id: user1Albums[3].id });
|
||||||
expect(body).toEqual({
|
|
||||||
...user1Albums[3],
|
|
||||||
assets: [expect.objectContaining({ id: user2Albums[0].assets[0].id })],
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return album info with assets when withoutAssets is undefined', async () => {
|
it('should return album info with assets when withoutAssets is undefined', async () => {
|
||||||
|
|||||||
@@ -10,6 +10,13 @@ import { Instrumentation } from 'src/utils/instrumentation';
|
|||||||
import { setUnion } from 'src/utils/set';
|
import { setUnion } from 'src/utils/set';
|
||||||
import { DataSource, FindOptionsOrder, FindOptionsRelations, In, IsNull, Not, Repository } from 'typeorm';
|
import { DataSource, FindOptionsOrder, FindOptionsRelations, In, IsNull, Not, Repository } from 'typeorm';
|
||||||
|
|
||||||
|
const withoutDeletedUsers = <T extends AlbumEntity | null>(album: T) => {
|
||||||
|
if (album) {
|
||||||
|
album.albumUsers = album.albumUsers.filter((albumUser) => albumUser.user && !albumUser.user.deletedAt);
|
||||||
|
}
|
||||||
|
return album;
|
||||||
|
};
|
||||||
|
|
||||||
@Instrumentation()
|
@Instrumentation()
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AlbumRepository implements IAlbumRepository {
|
export class AlbumRepository implements IAlbumRepository {
|
||||||
@@ -20,7 +27,7 @@ export class AlbumRepository implements IAlbumRepository {
|
|||||||
) {}
|
) {}
|
||||||
|
|
||||||
@GenerateSql({ params: [DummyValue.UUID, {}] })
|
@GenerateSql({ params: [DummyValue.UUID, {}] })
|
||||||
getById(id: string, options: AlbumInfoOptions): Promise<AlbumEntity | null> {
|
async getById(id: string, options: AlbumInfoOptions): Promise<AlbumEntity | null> {
|
||||||
const relations: FindOptionsRelations<AlbumEntity> = {
|
const relations: FindOptionsRelations<AlbumEntity> = {
|
||||||
owner: true,
|
owner: true,
|
||||||
albumUsers: { user: 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]] })
|
@GenerateSql({ params: [[DummyValue.UUID]] })
|
||||||
@ChunkedArray()
|
@ChunkedArray()
|
||||||
getByIds(ids: string[]): Promise<AlbumEntity[]> {
|
async getByIds(ids: string[]): Promise<AlbumEntity[]> {
|
||||||
return this.repository.find({
|
const albums = await this.repository.find({
|
||||||
where: {
|
where: {
|
||||||
id: In(ids),
|
id: In(ids),
|
||||||
},
|
},
|
||||||
@@ -55,11 +63,13 @@ export class AlbumRepository implements IAlbumRepository {
|
|||||||
albumUsers: { user: true },
|
albumUsers: { user: true },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return albums.map((album) => withoutDeletedUsers(album));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID] })
|
@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID] })
|
||||||
getByAssetId(ownerId: string, assetId: string): Promise<AlbumEntity[]> {
|
async getByAssetId(ownerId: string, assetId: string): Promise<AlbumEntity[]> {
|
||||||
return this.repository.find({
|
const albums = await this.repository.find({
|
||||||
where: [
|
where: [
|
||||||
{ ownerId, assets: { id: assetId } },
|
{ ownerId, assets: { id: assetId } },
|
||||||
{ albumUsers: { userId: 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 } },
|
relations: { owner: true, albumUsers: { user: true } },
|
||||||
order: { createdAt: 'DESC' },
|
order: { createdAt: 'DESC' },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return albums.map((album) => withoutDeletedUsers(album));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GenerateSql({ params: [[DummyValue.UUID]] })
|
@GenerateSql({ params: [[DummyValue.UUID]] })
|
||||||
@@ -127,20 +139,22 @@ export class AlbumRepository implements IAlbumRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GenerateSql({ params: [DummyValue.UUID] })
|
@GenerateSql({ params: [DummyValue.UUID] })
|
||||||
getOwned(ownerId: string): Promise<AlbumEntity[]> {
|
async getOwned(ownerId: string): Promise<AlbumEntity[]> {
|
||||||
return this.repository.find({
|
const albums = await this.repository.find({
|
||||||
relations: { albumUsers: { user: true }, sharedLinks: true, owner: true },
|
relations: { albumUsers: { user: true }, sharedLinks: true, owner: true },
|
||||||
where: { ownerId },
|
where: { ownerId },
|
||||||
order: { createdAt: 'DESC' },
|
order: { createdAt: 'DESC' },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return albums.map((album) => withoutDeletedUsers(album));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get albums shared with and shared by owner.
|
* Get albums shared with and shared by owner.
|
||||||
*/
|
*/
|
||||||
@GenerateSql({ params: [DummyValue.UUID] })
|
@GenerateSql({ params: [DummyValue.UUID] })
|
||||||
getShared(ownerId: string): Promise<AlbumEntity[]> {
|
async getShared(ownerId: string): Promise<AlbumEntity[]> {
|
||||||
return this.repository.find({
|
const albums = await this.repository.find({
|
||||||
relations: { albumUsers: { user: true }, sharedLinks: true, owner: true },
|
relations: { albumUsers: { user: true }, sharedLinks: true, owner: true },
|
||||||
where: [
|
where: [
|
||||||
{ albumUsers: { userId: ownerId } },
|
{ albumUsers: { userId: ownerId } },
|
||||||
@@ -149,18 +163,22 @@ export class AlbumRepository implements IAlbumRepository {
|
|||||||
],
|
],
|
||||||
order: { createdAt: 'DESC' },
|
order: { createdAt: 'DESC' },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return albums.map((album) => withoutDeletedUsers(album));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get albums of owner that are _not_ shared
|
* Get albums of owner that are _not_ shared
|
||||||
*/
|
*/
|
||||||
@GenerateSql({ params: [DummyValue.UUID] })
|
@GenerateSql({ params: [DummyValue.UUID] })
|
||||||
getNotShared(ownerId: string): Promise<AlbumEntity[]> {
|
async getNotShared(ownerId: string): Promise<AlbumEntity[]> {
|
||||||
return this.repository.find({
|
const albums = await this.repository.find({
|
||||||
relations: { albumUsers: true, sharedLinks: true, owner: true },
|
relations: { albumUsers: true, sharedLinks: true, owner: true },
|
||||||
where: { ownerId, albumUsers: { user: IsNull() }, sharedLinks: { id: IsNull() } },
|
where: { ownerId, albumUsers: { user: IsNull() }, sharedLinks: { id: IsNull() } },
|
||||||
order: { createdAt: 'DESC' },
|
order: { createdAt: 'DESC' },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return albums.map((album) => withoutDeletedUsers(album));
|
||||||
}
|
}
|
||||||
|
|
||||||
async restoreAll(userId: string): Promise<void> {
|
async restoreAll(userId: string): Promise<void> {
|
||||||
|
|||||||
Reference in New Issue
Block a user