import { IAccessRepository } from '@app/domain'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { AlbumEntity, AssetEntity, PartnerEntity, SharedLinkEntity } from '../entities'; export class AccessRepository implements IAccessRepository { constructor( @InjectRepository(AssetEntity) private assetRepository: Repository, @InjectRepository(AlbumEntity) private albumRepository: Repository, @InjectRepository(PartnerEntity) private partnerRepository: Repository, @InjectRepository(SharedLinkEntity) private sharedLinkRepository: Repository, ) {} hasPartnerAccess(userId: string, partnerId: string): Promise { return this.partnerRepository.exist({ where: { sharedWithId: userId, sharedById: partnerId, }, }); } hasAlbumAssetAccess(userId: string, assetId: string): Promise { return this.albumRepository.exist({ where: [ { ownerId: userId, assets: { id: assetId, }, }, { sharedUsers: { id: userId, }, assets: { id: assetId, }, }, ], }); } hasOwnerAssetAccess(userId: string, assetId: string): Promise { return this.assetRepository.exist({ where: { id: assetId, ownerId: userId, }, }); } hasPartnerAssetAccess(userId: string, assetId: string): Promise { return this.partnerRepository.exist({ where: { sharedWith: { id: userId, }, sharedBy: { assets: { id: assetId, }, }, }, relations: { sharedWith: true, sharedBy: { assets: true, }, }, }); } async hasSharedLinkAssetAccess(sharedLinkId: string, assetId: string): Promise { 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, }, }, })) ); } hasAlbumOwnerAccess(userId: string, albumId: string): Promise { return this.albumRepository.exist({ where: { id: albumId, ownerId: userId, }, }); } }