Skip to content

Commit

Permalink
N21-1967 fix amqp filestorage (#5066)
Browse files Browse the repository at this point in the history
* fixes copying of files
  • Loading branch information
arnegns authored Jun 17, 2024
1 parent 3141757 commit 583b8a0
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 46 deletions.
4 changes: 3 additions & 1 deletion apps/server/src/infra/rabbitmq/exchange/files-storage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { StorageLocation } from '@modules/files-storage/entity';
import { EntityId } from '@shared/domain/types';

export enum FilesStorageEvents {
Expand Down Expand Up @@ -35,7 +36,8 @@ export interface CopyFilesOfParentParams {
}

export interface FileRecordParams {
schoolId: EntityId;
storageLocationId: EntityId;
storageLocation: StorageLocation;
parentId: EntityId;
parentType: FileRecordParentType;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createMock } from '@golevelup/ts-jest';
import { ObjectId } from '@mikro-orm/mongodb';
import { StorageLocation } from '@modules/files-storage/entity';
import { FileRecordParentType } from '@src/infra/rabbitmq';
import { FilesStorageClientAdapterService } from '@src/modules/files-storage-client';
import { BoardNodeCopyContext } from './board-node-copy-context';
Expand All @@ -8,8 +9,10 @@ describe(BoardNodeCopyContext.name, () => {
describe('copyFilesOfParent', () => {
const setup = () => {
const contextProps = {
sourceSchoolId: new ObjectId().toHexString(),
targetSchoolId: new ObjectId().toHexString(),
sourceStorageLocationId: new ObjectId().toHexString(),
targetStorageLocationId: new ObjectId().toHexString(),
sourceStorageLocation: StorageLocation.SCHOOL,
targetStorageLocation: StorageLocation.SCHOOL,
userId: new ObjectId().toHexString(),
filesStorageClientAdapterService: createMock<FilesStorageClientAdapterService>(),
};
Expand All @@ -31,12 +34,14 @@ describe(BoardNodeCopyContext.name, () => {
source: {
parentId: sourceParentId,
parentType: FileRecordParentType.BoardNode,
schoolId: contextProps.sourceSchoolId,
storageLocationId: contextProps.sourceStorageLocationId,
storageLocation: contextProps.sourceStorageLocation,
},
target: {
parentId: targetParentId,
parentType: FileRecordParentType.BoardNode,
schoolId: contextProps.targetSchoolId,
storageLocationId: contextProps.targetStorageLocationId,
storageLocation: contextProps.targetStorageLocation,
},
userId: contextProps.userId,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { FilesStorageClientAdapterService } from '@modules/files-storage-client';
import { CopyFileDto } from '@modules/files-storage-client/dto';
import { StorageLocation } from '@modules/files-storage/entity';
import { EntityId } from '@shared/domain/types';
import { FileRecordParentType } from '@src/infra/rabbitmq';
import { CopyContext } from './board-node-copy.service';

export type BoardNodeCopyContextProps = {
sourceSchoolId: EntityId;
targetSchoolId: EntityId;
sourceStorageLocationId: EntityId;
targetStorageLocationId: EntityId;
userId: EntityId;
sourceStorageLocation: StorageLocation;
targetStorageLocation: StorageLocation;
filesStorageClientAdapterService: FilesStorageClientAdapterService;
};

Expand All @@ -19,12 +22,14 @@ export class BoardNodeCopyContext implements CopyContext {
source: {
parentId: sourceParentId,
parentType: FileRecordParentType.BoardNode,
schoolId: this.props.sourceSchoolId,
storageLocationId: this.props.sourceStorageLocationId,
storageLocation: this.props.sourceStorageLocation,
},
target: {
parentId: targetParentId,
parentType: FileRecordParentType.BoardNode,
schoolId: this.props.targetSchoolId,
storageLocationId: this.props.targetStorageLocationId,
storageLocation: this.props.targetStorageLocation,
},
userId: this.props.userId,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createMock } from '@golevelup/ts-jest';
import { ObjectId } from '@mikro-orm/mongodb';
import { CopyElementType, CopyHelperService, CopyStatus, CopyStatusEnum } from '@modules/copy-helper';
import { StorageLocation } from '@modules/files-storage/entity';
import { ContextExternalToolService } from '@modules/tool/context-external-tool/service';
import { IToolFeatures, ToolFeatures } from '@modules/tool/tool-config';
import { Test, TestingModule } from '@nestjs/testing';
Expand Down Expand Up @@ -63,8 +64,10 @@ describe(BoardNodeCopyService.name, () => {

const setup = () => {
const contextProps: BoardNodeCopyContextProps = {
sourceSchoolId: new ObjectId().toHexString(),
targetSchoolId: new ObjectId().toHexString(),
sourceStorageLocationId: new ObjectId().toHexString(),
sourceStorageLocation: StorageLocation.SCHOOL,
targetStorageLocationId: new ObjectId().toHexString(),
targetStorageLocation: StorageLocation.SCHOOL,
userId: new ObjectId().toHexString(),
filesStorageClientAdapterService: createMock<FilesStorageClientAdapterService>(),
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { ObjectId } from '@mikro-orm/mongodb';
import { CopyElementType, CopyHelperService, CopyStatus, CopyStatusEnum } from '@modules/copy-helper';
import { StorageLocation } from '@modules/files-storage/entity';
import { ContextExternalToolService } from '@modules/tool/context-external-tool/service';
import { IToolFeatures, ToolFeatures } from '@modules/tool/tool-config';
import { Test, TestingModule } from '@nestjs/testing';
Expand Down Expand Up @@ -89,8 +90,10 @@ describe(BoardNodeCopyService.name, () => {

const setupContext = () => {
const contextProps = {
sourceSchoolId: new ObjectId().toHexString(),
targetSchoolId: new ObjectId().toHexString(),
sourceStorageLocationId: new ObjectId().toHexString(),
sourceStorageLocation: StorageLocation.SCHOOL,
targetStorageLocationId: new ObjectId().toHexString(),
targetStorageLocation: StorageLocation.SCHOOL,
userId: new ObjectId().toHexString(),
filesStorageClientAdapterService: createMock<FilesStorageClientAdapterService>(),
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { CopyStatus } from '@modules/copy-helper';
import { FilesStorageClientAdapterService } from '@modules/files-storage-client';
import { StorageLocation } from '@modules/files-storage/entity';
import { UserService } from '@modules/user';
import { Injectable, InternalServerErrorException, NotImplementedException } from '@nestjs/common';
import { EntityId } from '@shared/domain/types';
import { CourseRepo } from '@shared/repo';
import { FilesStorageClientAdapterService } from '@modules/files-storage-client';
import { BoardExternalReference, BoardExternalReferenceType, ColumnBoard, isColumnBoard } from '../../domain';
import { BoardNodeService } from '../board-node.service';
import { BoardNodeCopyContext } from './board-node-copy-context';
import { BoardNodeCopyService } from './board-node-copy.service';
import { BoardNodeService } from '../board-node.service';
import { ColumnBoardTitleService } from './column-board-title.service';

@Injectable()
Expand Down Expand Up @@ -37,8 +38,10 @@ export class ColumnBoardCopyService {
const course = await this.courseRepo.findById(originalBoard.context.id); // TODO: get rid of this

const copyContext = new BoardNodeCopyContext({
sourceSchoolId: course.school.id,
targetSchoolId: user.schoolId,
sourceStorageLocationId: course.school.id,
targetStorageLocationId: user.schoolId,
sourceStorageLocation: StorageLocation.SCHOOL,
targetStorageLocation: StorageLocation.SCHOOL,
userId: props.userId,
filesStorageClientAdapterService: this.filesStorageClientAdapterService,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { FileRecordParentType } from '@infra/rabbitmq';
import { StorageLocation } from '@modules/files-storage/entity';
import { EntityId } from '@shared/domain/types';

export interface FileRequestInfo {
schoolId: EntityId;
storageLocationId: EntityId;
storageLocation: StorageLocation;
parentType: FileRecordParentType;
parentId: EntityId;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ObjectId } from '@mikro-orm/mongodb';
import { FileRecordParentType } from '@infra/rabbitmq';
import { ObjectId } from '@mikro-orm/mongodb';
import { StorageLocation } from '@modules/files-storage/entity';
import { lessonFactory, setupEntities, taskFactory } from '@shared/testing';
import { CopyFilesOfParentParamBuilder } from './copy-files-of-parent-param.builder';
import { FileParamBuilder } from './files-storage-param.builder';
Expand All @@ -14,22 +15,24 @@ describe('CopyFilesOfParentParamBuilder', () => {
const sourceEntity = taskFactory.buildWithId({});
const targetEntity = taskFactory.buildWithId();

const source = FileParamBuilder.build(sourceEntity.getSchoolId(), sourceEntity);
const target = FileParamBuilder.build(targetEntity.getSchoolId(), targetEntity);
const source = FileParamBuilder.build(sourceEntity.getSchoolId(), sourceEntity, StorageLocation.SCHOOL);
const target = FileParamBuilder.build(targetEntity.getSchoolId(), targetEntity, StorageLocation.SCHOOL);

const result = CopyFilesOfParentParamBuilder.build(userId, source, target);

const expectedResult = {
userId,
source: {
parentType: FileRecordParentType.Task,
schoolId: sourceEntity.getSchoolId(),
parentId: sourceEntity.id,
storageLocationId: sourceEntity.getSchoolId(),
storageLocation: StorageLocation.SCHOOL,
},
target: {
parentType: FileRecordParentType.Task,
schoolId: targetEntity.getSchoolId(),
parentId: targetEntity.id,
storageLocationId: targetEntity.getSchoolId(),
storageLocation: StorageLocation.SCHOOL,
},
};

Expand All @@ -41,22 +44,24 @@ describe('CopyFilesOfParentParamBuilder', () => {
const sourceEntity = lessonFactory.buildWithId({});
const targetEntity = lessonFactory.buildWithId();

const source = FileParamBuilder.build(sourceEntity.getSchoolId(), sourceEntity);
const target = FileParamBuilder.build(targetEntity.getSchoolId(), targetEntity);
const source = FileParamBuilder.build(sourceEntity.getSchoolId(), sourceEntity, StorageLocation.SCHOOL);
const target = FileParamBuilder.build(targetEntity.getSchoolId(), targetEntity, StorageLocation.SCHOOL);

const result = CopyFilesOfParentParamBuilder.build(userId, source, target);

const expectedResult = {
userId,
source: {
parentType: FileRecordParentType.Lesson,
schoolId: sourceEntity.getSchoolId(),
parentId: sourceEntity.id,
storageLocationId: sourceEntity.getSchoolId(),
storageLocation: StorageLocation.SCHOOL,
},
target: {
parentType: FileRecordParentType.Lesson,
schoolId: targetEntity.getSchoolId(),
parentId: targetEntity.id,
storageLocationId: targetEntity.getSchoolId(),
storageLocation: StorageLocation.SCHOOL,
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FileRecordParentType } from '@infra/rabbitmq';
import { StorageLocation } from '@modules/files-storage/entity';
import { lessonFactory, setupEntities, taskFactory } from '@shared/testing';
import { FileParamBuilder } from './files-storage-param.builder';

Expand All @@ -10,23 +11,23 @@ describe('FileParamBuilder', () => {
it('Should throw for not supported parent type', () => {
const schoolId = '123';
const parentType = 'abc';
const parentId = '123';

// @ts-expect-error: Test case
expect(() => FileParamBuilder.build(schoolId, parentType, parentId)).toThrowError();
expect(() => FileParamBuilder.build(schoolId, parentType, StorageLocation.SCHOOL)).toThrowError();
});

it('should build valid file request infos for task over shorthand task', () => {
const schoolId = '123';
const parentType = FileRecordParentType.Task;
const task = taskFactory.buildWithId();

const result = FileParamBuilder.build(schoolId, task);
const result = FileParamBuilder.build(schoolId, task, StorageLocation.SCHOOL);

const expectedResult = {
schoolId,
storageLocationId: schoolId,
parentType,
parentId: task.id,
storageLocation: StorageLocation.SCHOOL,
};

expect(result).toStrictEqual(expectedResult);
Expand All @@ -37,10 +38,11 @@ describe('FileParamBuilder', () => {
const parentType = FileRecordParentType.Lesson;
const lesson = lessonFactory.buildWithId();

const result = FileParamBuilder.build(schoolId, lesson);
const result = FileParamBuilder.build(schoolId, lesson, StorageLocation.SCHOOL);

const expectedResult = {
schoolId,
storageLocationId: schoolId,
storageLocation: StorageLocation.SCHOOL,
parentType,
parentId: lesson.id,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { StorageLocation } from '@modules/files-storage/entity';
import { EntityId } from '@shared/domain/types';
import { EntitiesWithFiles, FileRequestInfo } from '../interfaces';
import { FilesStorageClientMapper } from './files-storage-client.mapper';

export class FileParamBuilder {
static build(schoolId: EntityId, parent: EntitiesWithFiles): FileRequestInfo {
static build(
storageLocationId: EntityId,
parent: EntitiesWithFiles,
storageLocation: StorageLocation = StorageLocation.SCHOOL
): FileRequestInfo {
const parentType = FilesStorageClientMapper.mapEntityToParentType(parent);
const fileRequestInfo = {
parentType,
schoolId,
storageLocationId,
storageLocation,
parentId: parent.id,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CopyElementType, CopyHelperService, CopyStatus, CopyStatusEnum } from '@modules/copy-helper';
import { StorageLocation } from '@modules/files-storage/entity';
import { Injectable } from '@nestjs/common';
import { EntityId } from '@shared/domain/types';
import { CopyFileDto } from '../dto';
Expand All @@ -23,8 +24,8 @@ export class CopyFilesService {
fileUrlReplacements: FileUrlReplacement[];
fileCopyStatus: CopyStatus;
}> {
const source = FileParamBuilder.build(originalEntity.getSchoolId(), originalEntity);
const target = FileParamBuilder.build(copyEntity.getSchoolId(), copyEntity);
const source = FileParamBuilder.build(originalEntity.getSchoolId(), originalEntity, StorageLocation.SCHOOL);
const target = FileParamBuilder.build(copyEntity.getSchoolId(), copyEntity, StorageLocation.SCHOOL);
const copyFilesOfParentParams = CopyFilesOfParentParamBuilder.build(userId, source, target);

const fileDtos = await this.filesStorageClientAdapterService.copyFilesOfParent(copyFilesOfParentParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
OperationType,
} from '@modules/deletion';
import { deletionRequestFactory } from '@modules/deletion/domain/testing';
import { StorageLocation } from '@modules/files-storage/entity';
import { EventBus } from '@nestjs/cqrs';
import { Test, TestingModule } from '@nestjs/testing';
import { schoolEntityFactory, setupEntities, taskFactory } from '@shared/testing';
Expand Down Expand Up @@ -74,8 +75,8 @@ describe('FilesStorageClientAdapterService', () => {
const sourceEntity = taskFactory.buildWithId({ school });
const targetEntity = taskFactory.buildWithId({ school });

const source = FileParamBuilder.build(sourceEntity.getSchoolId(), sourceEntity);
const target = FileParamBuilder.build(targetEntity.getSchoolId(), targetEntity);
const source = FileParamBuilder.build(sourceEntity.getSchoolId(), sourceEntity, StorageLocation.SCHOOL);
const target = FileParamBuilder.build(targetEntity.getSchoolId(), targetEntity, StorageLocation.SCHOOL);

const param = CopyFilesOfParentParamBuilder.build(userId, source, target);

Expand All @@ -98,8 +99,8 @@ describe('FilesStorageClientAdapterService', () => {
const sourceEntity = taskFactory.buildWithId({ school });
const targetEntity = taskFactory.buildWithId({ school });

const source = FileParamBuilder.build(sourceEntity.getSchoolId(), sourceEntity);
const target = FileParamBuilder.build(targetEntity.getSchoolId(), targetEntity);
const source = FileParamBuilder.build(sourceEntity.getSchoolId(), sourceEntity, StorageLocation.SCHOOL);
const target = FileParamBuilder.build(targetEntity.getSchoolId(), targetEntity, StorageLocation.SCHOOL);

const param = CopyFilesOfParentParamBuilder.build(userId, source, target);

Expand Down
Loading

0 comments on commit 583b8a0

Please sign in to comment.