2023-06-06 22:18:38 +02:00
|
|
|
import { IAccessRepository } from '@app/domain';
|
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
|
|
import { Repository } from 'typeorm';
|
2023-06-16 21:01:34 +02:00
|
|
|
import { AlbumEntity, AssetEntity, PartnerEntity, SharedLinkEntity } from '../entities';
|
2023-06-06 22:18:38 +02:00
|
|
|
|
|
|
|
export class AccessRepository implements IAccessRepository {
|
2023-06-07 06:34:42 +02:00
|
|
|
constructor(
|
2023-06-16 21:01:34 +02:00
|
|
|
@InjectRepository(AssetEntity) private assetRepository: Repository<AssetEntity>,
|
|
|
|
@InjectRepository(AlbumEntity) private albumRepository: Repository<AlbumEntity>,
|
2023-06-07 06:34:42 +02:00
|
|
|
@InjectRepository(PartnerEntity) private partnerRepository: Repository<PartnerEntity>,
|
|
|
|
@InjectRepository(SharedLinkEntity) private sharedLinkRepository: Repository<SharedLinkEntity>,
|
|
|
|
) {}
|
2023-06-06 22:18:38 +02:00
|
|
|
|
|
|
|
hasPartnerAccess(userId: string, partnerId: string): Promise<boolean> {
|
|
|
|
return this.partnerRepository.exist({
|
|
|
|
where: {
|
|
|
|
sharedWithId: userId,
|
|
|
|
sharedById: partnerId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-16 21:01:34 +02:00
|
|
|
hasAlbumAssetAccess(userId: string, assetId: string): Promise<boolean> {
|
|
|
|
return this.albumRepository.exist({
|
|
|
|
where: [
|
|
|
|
{
|
|
|
|
ownerId: userId,
|
|
|
|
assets: {
|
|
|
|
id: assetId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sharedUsers: {
|
|
|
|
id: userId,
|
|
|
|
},
|
|
|
|
assets: {
|
|
|
|
id: assetId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
hasOwnerAssetAccess(userId: string, assetId: string): Promise<boolean> {
|
|
|
|
return this.assetRepository.exist({
|
|
|
|
where: {
|
|
|
|
id: assetId,
|
|
|
|
ownerId: userId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-06 22:18:38 +02:00
|
|
|
hasPartnerAssetAccess(userId: string, assetId: string): Promise<boolean> {
|
|
|
|
return this.partnerRepository.exist({
|
|
|
|
where: {
|
|
|
|
sharedWith: {
|
|
|
|
id: userId,
|
|
|
|
},
|
|
|
|
sharedBy: {
|
|
|
|
assets: {
|
|
|
|
id: assetId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
relations: {
|
|
|
|
sharedWith: true,
|
|
|
|
sharedBy: {
|
|
|
|
assets: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2023-06-07 06:34:42 +02:00
|
|
|
|
|
|
|
async hasSharedLinkAssetAccess(sharedLinkId: string, assetId: string): Promise<boolean> {
|
|
|
|
return (
|
|
|
|
// album asset
|
|
|
|
(await this.sharedLinkRepository.exist({
|
|
|
|
where: {
|
|
|
|
id: sharedLinkId,
|
|
|
|
album: {
|
|
|
|
assets: {
|
|
|
|
id: assetId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})) ||
|
|
|
|
// individual asset
|
|
|
|
(await this.sharedLinkRepository.exist({
|
|
|
|
where: {
|
|
|
|
id: sharedLinkId,
|
|
|
|
assets: {
|
|
|
|
id: assetId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
}
|
2023-06-06 22:18:38 +02:00
|
|
|
}
|