import { ISharedLinkRepository } from '@app/domain'; import { Injectable, Logger } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { SharedLinkEntity } from '../entities'; @Injectable() export class SharedLinkRepository implements ISharedLinkRepository { readonly logger = new Logger(SharedLinkRepository.name); constructor( @InjectRepository(SharedLinkEntity) private readonly repository: Repository, ) {} get(userId: string, id: string): Promise { return this.repository.findOne({ where: { id, userId, }, relations: { assets: { exifInfo: true, }, album: { assets: { exifInfo: true, }, owner: true, }, }, order: { createdAt: 'DESC', assets: { fileCreatedAt: 'ASC', }, album: { assets: { fileCreatedAt: 'ASC', }, }, }, }); } getAll(userId: string): Promise { return this.repository.find({ where: { userId, }, relations: { assets: true, album: { owner: true, }, }, order: { createdAt: 'DESC', }, }); } async getByKey(key: Buffer): Promise { return await this.repository.findOne({ where: { key, }, relations: { assets: true, album: { assets: true, }, user: true, }, order: { createdAt: 'DESC', }, }); } create(entity: Omit): Promise { return this.repository.save(entity); } async remove(entity: SharedLinkEntity): Promise { await this.repository.remove(entity); } async save(entity: SharedLinkEntity): Promise { await this.repository.save(entity); return this.repository.findOneOrFail({ where: { id: entity.id } }); } async hasAssetAccess(id: string, assetId: string): Promise { const count1 = await this.repository.count({ where: { id, assets: { id: assetId, }, }, }); const count2 = await this.repository.count({ where: { id, album: { assets: { id: assetId, }, }, }, }); return Boolean(count1 + count2); } }