Skip to content

Commit

Permalink
feat: removed functions
Browse files Browse the repository at this point in the history
  • Loading branch information
apsantiso committed Sep 12, 2024
1 parent 96a6111 commit ba2dda7
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 120 deletions.
56 changes: 5 additions & 51 deletions src/modules/file/file.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export interface FileRepository {
where: Partial<Omit<FileAttributes, 'name' | 'plainName'>>,
nameFilter: Pick<FileAttributes, 'name' | 'plainName'>,
): Promise<File | null>;
findFileByFolderUuid(
folderUuid: Folder['uuid'],
findFilesInFolderByName(
folderId: Folder['uuid'],
searchBy: { plainName: File['plainName']; type?: File['type'] }[],
): Promise<File[]>;
findByNameAndFolderUuid(
Expand Down Expand Up @@ -73,16 +73,6 @@ export interface FileRepository {
userId: FileAttributes['userId'],
update: Partial<File>,
): Promise<void>;
findFilesWithPagination(
folderUuid: Folder['uuid'],
limit: number,
page: number,
): Promise<{
files: File[];
totalPages: number;
currentPage: number;
nextPage: number | null;
}>;
getFilesWhoseFolderIdDoesNotExist(userId: File['userId']): Promise<number>;
getFilesCountWhere(where: Partial<File>): Promise<number>;
updateFilesStatusToTrashed(
Expand Down Expand Up @@ -546,12 +536,12 @@ export class SequelizeFileRepository implements FileRepository {
return file ? this.toDomain(file) : null;
}

async findFileByFolderUuid(
folderUuid: Folder['uuid'],
async findFilesInFolderByName(
folderId: Folder['uuid'],
searchFilter: { plainName: File['plainName']; type?: File['type'] }[],
): Promise<File[]> {
const where: WhereOptions<File> = {
folderUuid,
folderUuid: folderId,
status: FileStatus.EXISTS,
};

Expand All @@ -569,42 +559,6 @@ export class SequelizeFileRepository implements FileRepository {
return files.map(this.toDomain.bind(this));
}

async findFilesWithPagination(
folderUuid: Folder['uuid'],
limit: number,
page: number,
): Promise<{
files: File[];
totalPages: number;
currentPage: number;
nextPage: number | null;
}> {
const offset = (page - 1) * limit;

const result = await this.fileModel.findAndCountAll({
where: {
folderUuid,
status: FileStatus.EXISTS,
},
limit,
offset,
order: [['id', 'ASC']],
});

const { count, rows } = result;

const totalPages = Math.ceil(count / limit);

const nextPage = page < totalPages ? page + 1 : null;

return {
files: rows.map(this.toDomain.bind(this)),
totalPages,
currentPage: page,
nextPage,
};
}

async updateByFieldIdAndUserId(
fileId: FileAttributes['fileId'],
userId: FileAttributes['userId'],
Expand Down
65 changes: 4 additions & 61 deletions src/modules/file/file.usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,67 +148,10 @@ export class FileUseCases {
folder: Folder,
searchFilter: { plainName: File['plainName']; type?: File['type'] }[],
): Promise<File[]> {
return this.fileRepository.findFileByFolderUuid(folder.uuid, searchFilter);
}

async findFilesWithPagination(
folderUuid: Folder['uuid'],
limit: number,
page: number,
) {
return this.fileRepository.findFilesWithPagination(folderUuid, limit, page);
}

async checkMultipleFilesExistence(
folderUuid: Folder['uuid'],
dtoFiles: { plainName: string; type?: string }[],
): Promise<File[]> {
const limit = 1000;
let page = 1;
const allExistentFiles: File[] = [];
let nextPage: number | null = null;

let remainingFiles = [...dtoFiles];

do {
const { files, nextPage: newNextPage } =
await this.findFilesWithPagination(folderUuid, limit, page);

const matchingFiles = files.filter((file) =>
remainingFiles.some(
(dtoFile) =>
file.plainName === dtoFile.plainName &&
(!dtoFile.type || file.type === dtoFile.type),
),
);

allExistentFiles.push(...matchingFiles);

remainingFiles = remainingFiles.filter(
(dtoFile) =>
!matchingFiles.some(
(file) =>
file.plainName === dtoFile.plainName &&
(!dtoFile.type || file.type === dtoFile.type),
),
);

if (remainingFiles.length === 0) {
break;
}

nextPage = newNextPage;
page++;
} while (nextPage);

return allExistentFiles;
}

async getFilesInFolder(
folder: Folder,
searchFilter: { plainName: File['plainName']; type?: File['type'] }[],
): Promise<File[]> {
return this.fileRepository.findFileByFolderUuid(folder.uuid, searchFilter);
return this.fileRepository.findFilesInFolderByName(
folder.uuid,
searchFilter,
);
}

async updateFileMetaData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('CheckFileExistenceInFolderDto', () => {
});

it('When files array exceeds max size, then it should fail', async () => {
const files = Array.from({ length: 1991 }, (_, i) => ({
const files = Array.from({ length: 201 }, (_, i) => ({
plainName: `file${i + 1}`,
type: 'txt',
}));
Expand Down
2 changes: 1 addition & 1 deletion src/modules/folder/dto/files-existence-in-folder.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class CheckFileExistenceInFolderDto {
description: 'Array of files with names and types',
})
@ArrayMinSize(1)
@ArrayMaxSize(1000)
@ArrayMaxSize(200)
@ValidateNested()
@Type(() => FilesNameAndType)
files: FilesNameAndType[];
Expand Down
6 changes: 2 additions & 4 deletions src/modules/folder/folder.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ describe('FolderController', () => {
.spyOn(folderUseCases, 'getFolderByUuidAndUser')
.mockResolvedValue(parentFolder);
jest
.spyOn(fileUseCases, 'checkMultipleFilesExistence')
.spyOn(fileUseCases, 'searchFilesInFolder')
.mockResolvedValue(mockFiles);

const result = await folderController.checkFilesExistenceInFolder(
Expand All @@ -449,9 +449,7 @@ describe('FolderController', () => {
jest
.spyOn(folderUseCases, 'getFolderByUuidAndUser')
.mockResolvedValue(parentFolder);
jest
.spyOn(fileUseCases, 'checkMultipleFilesExistence')
.mockResolvedValue([]);
jest.spyOn(fileUseCases, 'searchFilesInFolder').mockResolvedValue([]);

const result = await folderController.checkFilesExistenceInFolder(
user,
Expand Down
4 changes: 2 additions & 2 deletions src/modules/folder/folder.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,8 @@ export class FolderController {
throw new InvalidParentFolderException('Parent folder not valid!');
}

const files = await this.fileUseCases.checkMultipleFilesExistence(
parentFolder.uuid,
const files = await this.fileUseCases.searchFilesInFolder(
parentFolder,
query.files,
);

Expand Down

0 comments on commit ba2dda7

Please sign in to comment.