Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PB-3097, PB-2977] Filter files in trash - Delete files from deleted folders #422

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion src/modules/file/file.repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Test, TestingModule } from '@nestjs/testing';
import { getModelToken } from '@nestjs/sequelize';
import { createMock } from '@golevelup/ts-jest';
import { newUser, newWorkspace } from '../../../test/fixtures';
import { FileStatus } from './file.domain';
import { File, FileStatus } from './file.domain';
import { FileModel } from './file.model';
import { FileRepository, SequelizeFileRepository } from './file.repository';
import { Op } from 'sequelize';
Expand Down Expand Up @@ -131,4 +131,56 @@ describe('FileRepository', () => {
});
});
});

describe('updateManyByFileIdAndUserId', () => {
it('When params are passed, then it should update files', async () => {
const fileIds = [v4(), v4()];
const updateData: Partial<File> = {
deleted: false,
deletedAt: null,
status: FileStatus.EXISTS,
};

await repository.updateManyByFileIdAndUserId(
fileIds,
user.id,
updateData,
);

expect(fileModel.update).toHaveBeenCalledWith(updateData, {
where: {
userId: user.id,
fileId: {
[Op.in]: fileIds,
},
},
});
});
});

describe('trashFilesByUserAndFolderUuids', () => {
it('When params are passed, then it should update files', async () => {
const folderUuids = [v4(), v4()];

await repository.trashFilesByUserAndFolderUuids(user, folderUuids);

expect(fileModel.update).toHaveBeenCalledWith(
{
deleted: true,
deletedAt: new Date(),
status: FileStatus.TRASHED,
updatedAt: new Date(),
},
{
where: {
userId: user.id,
removed: false,
folderUuid: {
[Op.in]: folderUuids,
},
},
},
);
});
});
});
31 changes: 29 additions & 2 deletions src/modules/file/file.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export interface FileRepository {
userId: FileAttributes['userId'],
update: Partial<File>,
): Promise<void>;
updateManyByFieldIdAndUserId(
updateManyByFileIdAndUserId(
fileIds: FileAttributes['fileId'][],
userId: FileAttributes['userId'],
update: Partial<File>,
Expand All @@ -93,6 +93,10 @@ export interface FileRepository {
userId: User['id'],
fileIds: FileAttributes['fileId'][],
): Promise<File[]>;
trashFilesByUserAndFolderUuids(
user: User,
folderUuids: FileAttributes['folderUuid'][],
): Promise<void>;
}

@Injectable()
Expand Down Expand Up @@ -602,7 +606,7 @@ export class SequelizeFileRepository implements FileRepository {
return this.toDomain(file);
}

async updateManyByFieldIdAndUserId(
async updateManyByFileIdAndUserId(
fileIds: FileAttributes['fileId'][],
userId: FileAttributes['userId'],
update: Partial<File>,
Expand Down Expand Up @@ -706,6 +710,29 @@ export class SequelizeFileRepository implements FileRepository {
);
}

async trashFilesByUserAndFolderUuids(
user: User,
folderUuids: Folder['uuid'][],
): Promise<void> {
await this.fileModel.update(
{
deleted: true,
deletedAt: new Date(),
status: FileStatus.TRASHED,
updatedAt: new Date(),
},
{
where: {
userId: user.id,
removed: false,
folderUuid: {
[Op.in]: folderUuids,
},
},
},
);
}

async deleteFilesByUser(user: User, files: File[]): Promise<void> {
await this.fileModel.update(
{
Expand Down
91 changes: 91 additions & 0 deletions src/modules/file/file.usecase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { SharingService } from '../sharing/sharing.service';
import { SharingItemType } from '../sharing/sharing.domain';
import { CreateFileDto } from './dto/create-file.dto';
import { UpdateFileMetaDto } from './dto/update-file-meta.dto';
import { Op } from 'sequelize';

const fileId = '6295c99a241bb000083f1c6a';
const userId = 1;
Expand Down Expand Up @@ -1091,4 +1092,94 @@ describe('FileUseCases', () => {
expect(result).toBeNull();
});
});

describe('trashFilesByUserAndFolderUuids', () => {
const user = newUser();

it('When called, should update the status of files in the specified folder to TRASHED', async () => {
const folder = newFolder({ owner: user });

jest
.spyOn(fileRepository, 'trashFilesByUserAndFolderUuids')
.mockResolvedValueOnce();

await expect(
service.trashFilesByUserAndFolderUuids(user, [folder.uuid]),
).resolves.not.toThrow();

expect(
fileRepository.trashFilesByUserAndFolderUuids,
).toHaveBeenCalledWith(user, [folder.uuid]);
});
});

describe('updateManyByFileIdAndUserId', () => {
const user = newUser();
const fileIds = [v4(), v4()];
const updateData: Partial<File> = {
status: FileStatus.TRASHED,
};

it('When called, should update the specified files', async () => {
jest
.spyOn(fileRepository, 'updateManyByFileIdAndUserId')
.mockResolvedValueOnce(undefined);

await expect(
service.updateManyByFileIdAndUserId(fileIds, user.id, updateData),
).resolves.not.toThrow();

expect(fileRepository.updateManyByFileIdAndUserId).toHaveBeenCalledWith(
fileIds,
user.id,
updateData,
);
});
});

describe('filterFilesWithNonDeletedFolders', () => {
const user = newUser();

it('should filter out files belonging to deleted folders', async () => {
const folderDeleted = newFolder({
owner: user,
attributes: { deleted: true },
});
const folderNotDeleted = newFolder({ owner: user });
const extractFolderUuids = [folderDeleted.uuid, folderNotDeleted.uuid];

const fileWithFolderDeleted = newFile({
owner: user,
folder: folderDeleted,
});
const fileWithFolderNotDeleted = newFile({
owner: user,
folder: folderNotDeleted,
});

const files = [fileWithFolderDeleted, fileWithFolderNotDeleted];
const mockGetFolderResolved = [folderDeleted];
const mockResolved = [fileWithFolderNotDeleted];

jest
.spyOn(folderUseCases, 'getFolders')
.mockResolvedValue(mockGetFolderResolved);

const result = await service.filterFilesWithNonDeletedFolders(
user,
files,
);

expect(folderUseCases.getFolders).toHaveBeenCalledWith(
user.id,
{
deleted: true,
removed: false,
uuid: { [Op.in]: extractFolderUuids },
},
{ limit: 50, offset: 0 },
);
expect(result).toEqual(mockResolved);
});
});
});
46 changes: 46 additions & 0 deletions src/modules/file/file.usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { WorkspaceAttributes } from '../workspaces/attributes/workspace.attribut
import { Folder } from '../folder/folder.domain';
import { getPathFileData } from '../../lib/path';
import { isStringEmpty } from '../../lib/validators';
import { Op } from 'sequelize';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you try to use this in the repository? You can find the reason in this comment: #428 (comment)


export type SortParamsFile = Array<[SortableFileAttributes, 'ASC' | 'DESC']>;

Expand Down Expand Up @@ -734,4 +735,49 @@ export class FileUseCases {
);
return file;
}

async trashFilesByUserAndFolderUuids(
user: User,
folderUuids: FileAttributes['folderUuid'][],
) {
return this.fileRepository.trashFilesByUserAndFolderUuids(
user,
folderUuids,
);
}

async updateManyByFileIdAndUserId(
fileIds: FileAttributes['fileId'][],
userId: FileAttributes['userId'],
update: Partial<File>,
) {
return this.fileRepository.updateManyByFileIdAndUserId(
fileIds,
userId,
update,
);
}

async filterFilesWithNonDeletedFolders(
user: User,
files: File[],
): Promise<File[]> {
const extractFolderUuids = files
.filter(
(item, index, self) =>
index === self.findIndex((t) => t.folderUuid === item.folderUuid),
)
.map((item) => item.folderUuid);

const deletedFolders = await this.folderUsecases.getFolders(
user.id,
{ deleted: true, removed: false, uuid: { [Op.in]: extractFolderUuids } },
{ limit: 50, offset: 0 },
);

const deletedFoldersUuids = deletedFolders.map((f) => f.uuid);
return files.filter(
(file) => !deletedFoldersUuids.includes(file.folderUuid),
);
}
}
40 changes: 39 additions & 1 deletion src/modules/folder/folder.usecase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { SharingService } from '../sharing/sharing.service';
import { InvalidParentFolderException } from './exception/invalid-parent-folder';
import { UpdateFolderMetaDto } from './dto/update-folder-meta.dto';
import { FileUseCases } from '../file/file.usecase';
import { FileStatus } from '../file/file.domain';
import { File, FileStatus } from '../file/file.domain';

const folderId = 4;
const user = newUser();
Expand Down Expand Up @@ -148,6 +148,11 @@ describe('FolderUseCases', () => {
[mockFolder.uuid],
);

expect(fileUsecases.trashFilesByUserAndFolderUuids).toHaveBeenCalledWith(
user,
[mockBackupFolder.uuid, mockFolder.uuid],
);

expect(folderRepository.updateManyByFolderId).toHaveBeenCalledTimes(2);
expect(folderRepository.updateManyByFolderId).toHaveBeenCalledWith(
[mockFolder.id],
Expand Down Expand Up @@ -185,6 +190,10 @@ describe('FolderUseCases', () => {
expect(service.getFoldersByIds).toHaveBeenCalledWith(user, [
mockFolder.id,
]);
expect(fileUsecases.trashFilesByUserAndFolderUuids).toHaveBeenCalledWith(
user,
[mockFolder.uuid],
);
});
});

Expand Down Expand Up @@ -576,6 +585,26 @@ describe('FolderUseCases', () => {
attributes: { userId: userMocked.id, removed: false },
});

const mockTrashFiles = [
newFile({
owner: userMocked,
folder,
attributes: { status: FileStatus.TRASHED },
}),
newFile({
owner: userMocked,
folder,
attributes: { status: FileStatus.TRASHED },
}),
];
const mockTrashFilesIds = mockTrashFiles.map((f) => f.fileId);

const updateTrashFiles: Partial<File> = {
deleted: false,
deletedAt: null,
status: FileStatus.EXISTS,
};

jest.spyOn(folderRepository, 'findOne').mockResolvedValueOnce(folder);
jest
.spyOn(folderRepository, 'findById')
Expand All @@ -600,6 +629,10 @@ describe('FolderUseCases', () => {
.spyOn(folderRepository, 'updateByFolderId')
.mockResolvedValueOnce(expectedFolder);

jest
.spyOn(fileUsecases, 'getFilesByFolderUuid')
.mockResolvedValueOnce(mockTrashFiles);

const result = await service.moveFolder(
userMocked,
folder.uuid,
Expand All @@ -618,6 +651,11 @@ describe('FolderUseCases', () => {
deletedAt: null,
},
);
expect(fileUsecases.updateManyByFileIdAndUserId).toHaveBeenCalledWith(
mockTrashFilesIds,
userMocked.id,
updateTrashFiles,
);
});

it('When folder is moved but it is removed, then an error is thrown', async () => {
Expand Down
Loading
Loading