2024-04-02 10:23:17 -04:00
|
|
|
import { BadRequestException, Inject, Injectable } from '@nestjs/common';
|
|
|
|
|
import { BulkIdResponseDto, BulkIdsDto } from 'src/dtos/asset-ids.response.dto';
|
|
|
|
|
import { AuthDto } from 'src/dtos/auth.dto';
|
|
|
|
|
import { MemoryCreateDto, MemoryResponseDto, MemoryUpdateDto, mapMemory } from 'src/dtos/memory.dto';
|
|
|
|
|
import { AssetEntity } from 'src/entities/asset.entity';
|
2024-08-15 06:57:01 -04:00
|
|
|
import { Permission } from 'src/enum';
|
2024-04-02 10:23:17 -04:00
|
|
|
import { IAccessRepository } from 'src/interfaces/access.interface';
|
|
|
|
|
import { IMemoryRepository } from 'src/interfaces/memory.interface';
|
2024-08-20 07:49:56 -04:00
|
|
|
import { checkAccess, requireAccess } from 'src/utils/access';
|
2024-04-02 10:23:17 -04:00
|
|
|
import { addAssets, removeAssets } from 'src/utils/asset.util';
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class MemoryService {
|
|
|
|
|
constructor(
|
2024-08-20 07:49:56 -04:00
|
|
|
@Inject(IAccessRepository) private access: IAccessRepository,
|
2024-04-02 10:23:17 -04:00
|
|
|
@Inject(IMemoryRepository) private repository: IMemoryRepository,
|
2024-08-20 07:49:56 -04:00
|
|
|
) {}
|
2024-04-02 10:23:17 -04:00
|
|
|
|
|
|
|
|
async search(auth: AuthDto) {
|
|
|
|
|
const memories = await this.repository.search(auth.user.id);
|
|
|
|
|
return memories.map((memory) => mapMemory(memory));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async get(auth: AuthDto, id: string): Promise<MemoryResponseDto> {
|
2024-08-20 07:49:56 -04:00
|
|
|
await requireAccess(this.access, { auth, permission: Permission.MEMORY_READ, ids: [id] });
|
2024-04-02 10:23:17 -04:00
|
|
|
const memory = await this.findOrFail(id);
|
|
|
|
|
return mapMemory(memory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async create(auth: AuthDto, dto: MemoryCreateDto) {
|
|
|
|
|
// TODO validate type/data combination
|
|
|
|
|
|
|
|
|
|
const assetIds = dto.assetIds || [];
|
2024-08-20 07:49:56 -04:00
|
|
|
const allowedAssetIds = await checkAccess(this.access, {
|
|
|
|
|
auth,
|
|
|
|
|
permission: Permission.ASSET_SHARE,
|
|
|
|
|
ids: assetIds,
|
|
|
|
|
});
|
2024-04-02 10:23:17 -04:00
|
|
|
const memory = await this.repository.create({
|
|
|
|
|
ownerId: auth.user.id,
|
|
|
|
|
type: dto.type,
|
|
|
|
|
data: dto.data,
|
|
|
|
|
isSaved: dto.isSaved,
|
|
|
|
|
memoryAt: dto.memoryAt,
|
|
|
|
|
seenAt: dto.seenAt,
|
|
|
|
|
assets: [...allowedAssetIds].map((id) => ({ id }) as AssetEntity),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return mapMemory(memory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async update(auth: AuthDto, id: string, dto: MemoryUpdateDto): Promise<MemoryResponseDto> {
|
2024-08-20 07:49:56 -04:00
|
|
|
await requireAccess(this.access, { auth, permission: Permission.MEMORY_UPDATE, ids: [id] });
|
2024-04-02 10:23:17 -04:00
|
|
|
|
|
|
|
|
const memory = await this.repository.update({
|
|
|
|
|
id,
|
|
|
|
|
isSaved: dto.isSaved,
|
|
|
|
|
memoryAt: dto.memoryAt,
|
|
|
|
|
seenAt: dto.seenAt,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return mapMemory(memory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async remove(auth: AuthDto, id: string): Promise<void> {
|
2024-08-20 07:49:56 -04:00
|
|
|
await requireAccess(this.access, { auth, permission: Permission.MEMORY_DELETE, ids: [id] });
|
2024-04-02 10:23:17 -04:00
|
|
|
await this.repository.delete(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async addAssets(auth: AuthDto, id: string, dto: BulkIdsDto): Promise<BulkIdResponseDto[]> {
|
2024-08-20 07:49:56 -04:00
|
|
|
await requireAccess(this.access, { auth, permission: Permission.MEMORY_READ, ids: [id] });
|
2024-04-02 10:23:17 -04:00
|
|
|
|
2024-08-20 07:49:56 -04:00
|
|
|
const repos = { access: this.access, bulk: this.repository };
|
2024-06-29 00:17:58 -04:00
|
|
|
const results = await addAssets(auth, repos, { parentId: id, assetIds: dto.ids });
|
2024-04-02 10:23:17 -04:00
|
|
|
|
|
|
|
|
const hasSuccess = results.find(({ success }) => success);
|
|
|
|
|
if (hasSuccess) {
|
|
|
|
|
await this.repository.update({ id, updatedAt: new Date() });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async removeAssets(auth: AuthDto, id: string, dto: BulkIdsDto): Promise<BulkIdResponseDto[]> {
|
2024-08-20 07:49:56 -04:00
|
|
|
await requireAccess(this.access, { auth, permission: Permission.MEMORY_UPDATE, ids: [id] });
|
2024-04-02 10:23:17 -04:00
|
|
|
|
2024-08-20 07:49:56 -04:00
|
|
|
const repos = { access: this.access, bulk: this.repository };
|
2024-06-29 00:17:58 -04:00
|
|
|
const results = await removeAssets(auth, repos, {
|
|
|
|
|
parentId: id,
|
|
|
|
|
assetIds: dto.ids,
|
|
|
|
|
canAlwaysRemove: Permission.MEMORY_DELETE,
|
|
|
|
|
});
|
2024-04-02 10:23:17 -04:00
|
|
|
|
|
|
|
|
const hasSuccess = results.find(({ success }) => success);
|
|
|
|
|
if (hasSuccess) {
|
|
|
|
|
await this.repository.update({ id, updatedAt: new Date() });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async findOrFail(id: string) {
|
|
|
|
|
const memory = await this.repository.get(id);
|
|
|
|
|
if (!memory) {
|
|
|
|
|
throw new BadRequestException('Memory not found');
|
|
|
|
|
}
|
|
|
|
|
return memory;
|
|
|
|
|
}
|
|
|
|
|
}
|