Skip to content

Commit

Permalink
refactor: use new add/remove asset utility
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasm91 committed Mar 29, 2024
1 parent 66db64a commit 80848da
Showing 1 changed file with 12 additions and 49 deletions.
61 changes: 12 additions & 49 deletions server/src/services/memory.service.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { BadRequestException, Inject, Injectable } from '@nestjs/common';
import { AccessCore, Permission } from 'src/cores/access.core';
import { BulkIdErrorReason, BulkIdResponseDto, BulkIdsDto } from 'src/dtos/asset-ids.response.dto';
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';
import { IAccessRepository } from 'src/interfaces/access.interface';
import { IMemoryRepository } from 'src/interfaces/memory.interface';
import { addAssets, removeAssets } from 'src/utils/asset.util';

@Injectable()
export class MemoryService {
private access: AccessCore;

constructor(
@Inject(IAccessRepository) accessRepository: IAccessRepository,
@Inject(IAccessRepository) private accessRepository: IAccessRepository,
@Inject(IMemoryRepository) private repository: IMemoryRepository,
) {
this.access = AccessCore.create(accessRepository);
Expand Down Expand Up @@ -68,31 +69,11 @@ export class MemoryService {
async addAssets(auth: AuthDto, id: string, dto: BulkIdsDto): Promise<BulkIdResponseDto[]> {
await this.access.requirePermission(auth, Permission.MEMORY_READ, id);

// TODO move this to AssetCore
const existingAssetIds = await this.repository.getAssetIds(id, dto.ids);
const notPresentAssetIds = dto.ids.filter((id) => !existingAssetIds.has(id));
const allowedAssetIds = await this.access.checkAccess(auth, Permission.ASSET_SHARE, notPresentAssetIds);

const results: BulkIdResponseDto[] = [];
for (const assetId of dto.ids) {
const hasAsset = existingAssetIds.has(assetId);
if (hasAsset) {
results.push({ id: assetId, success: false, error: BulkIdErrorReason.DUPLICATE });
continue;
}

const hasAccess = allowedAssetIds.has(assetId);
if (!hasAccess) {
results.push({ id: assetId, success: false, error: BulkIdErrorReason.NO_PERMISSION });
continue;
}

results.push({ id: assetId, success: true });
}
const repos = { accessRepository: this.accessRepository, repository: this.repository };
const results = await addAssets(auth, repos, { id, assetIds: dto.ids });

const newAssetIds = results.filter(({ success }) => success).map(({ id }) => id);
if (newAssetIds.length > 0) {
await this.repository.addAssetIds(id, newAssetIds);
const hasSuccess = results.find(({ success }) => success);
if (hasSuccess) {
await this.repository.update({ id, updatedAt: new Date() });
}

Expand All @@ -102,30 +83,12 @@ export class MemoryService {
async removeAssets(auth: AuthDto, id: string, dto: BulkIdsDto): Promise<BulkIdResponseDto[]> {
await this.access.requirePermission(auth, Permission.MEMORY_WRITE, id);

const existingAssetIds = await this.repository.getAssetIds(id, dto.ids);
const presentAssetIds = new Set(dto.ids.filter((id) => existingAssetIds.has(id)));
const allowedAssetIds = await this.access.checkAccess(auth, Permission.ASSET_SHARE, presentAssetIds);

const results: BulkIdResponseDto[] = [];
for (const assetId of dto.ids) {
const hasAsset = existingAssetIds.has(assetId);
if (!hasAsset) {
results.push({ id: assetId, success: false, error: BulkIdErrorReason.NOT_FOUND });
continue;
}

const hasAccess = allowedAssetIds.has(assetId);
if (!hasAccess) {
results.push({ id: assetId, success: false, error: BulkIdErrorReason.NO_PERMISSION });
continue;
}

results.push({ id: assetId, success: true });
}
const repos = { accessRepository: this.accessRepository, repository: this.repository };
const permissions = [Permission.ASSET_SHARE];
const results = await removeAssets(auth, repos, { id, assetIds: dto.ids, permissions });

const removedIds = results.filter(({ success }) => success).map(({ id }) => id);
if (removedIds.length > 0) {
await this.repository.removeAssetIds(id, removedIds);
const hasSuccess = results.find(({ success }) => success);
if (hasSuccess) {
await this.repository.update({ id, updatedAt: new Date() });
}

Expand Down

0 comments on commit 80848da

Please sign in to comment.