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

[_]: feat/remove-encrypted-name-from-search #429

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
56 changes: 8 additions & 48 deletions src/modules/file/file.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,15 @@ export interface FileRepository {
userId: FileAttributes['userId'],
where: FindOptions<FileAttributes>,
): Promise<File | null>;
findFileByName(
where: Partial<Omit<FileAttributes, 'name' | 'plainName'>>,
nameFilter: Pick<FileAttributes, 'name' | 'plainName'>,
): Promise<File | null>;
findFilesInFolderByName(
folderId: Folder['uuid'],
searchBy: { plainName: File['plainName']; type?: File['type'] }[],
): Promise<File[]>;
findByNameAndFolderUuid(
name: FileAttributes['name'],
type: FileAttributes['type'],
folderUuid: FileAttributes['folderUuid'],
status: FileAttributes['status'],
): Promise<File | null>;
findByPlainNameAndFolderUuid(
findByPlainNameAndFolderId(
userId: File['userId'],
plainName: FileAttributes['plainName'],
type: FileAttributes['type'],
folderUuid: FileAttributes['folderUuid'],
folderId: FileAttributes['folderId'],
status: FileAttributes['status'],
): Promise<File | null>;
getSumSizeOfFilesInWorkspaceByStatuses(
Expand Down Expand Up @@ -185,34 +176,19 @@ export class SequelizeFileRepository implements FileRepository {
return file ? this.toDomain(file) : null;
}

async findByNameAndFolderUuid(
name: FileAttributes['name'],
type: FileAttributes['type'],
folderUuid: FileAttributes['folderUuid'],
status: FileAttributes['status'],
): Promise<File | null> {
const file = await this.fileModel.findOne({
where: {
name: { [Op.eq]: name },
type: { [Op.eq]: type },
folderUuid: { [Op.eq]: folderUuid },
status: { [Op.eq]: status },
},
});
return file ? this.toDomain(file) : null;
}

async findByPlainNameAndFolderUuid(
async findByPlainNameAndFolderId(
userId: FileAttributes['userId'],
plainName: FileAttributes['plainName'],
type: FileAttributes['type'],
folderUuid: FileAttributes['folderUuid'],
folderId: FileAttributes['folderId'],
status: FileAttributes['status'],
): Promise<File | null> {
const file = await this.fileModel.findOne({
where: {
userId: { [Op.eq]: userId },
plainName: { [Op.eq]: plainName },
type: { [Op.eq]: type },
folderUuid: { [Op.eq]: folderUuid },
folderId: { [Op.eq]: folderId },
status: { [Op.eq]: status },
},
});
Expand Down Expand Up @@ -543,22 +519,6 @@ export class SequelizeFileRepository implements FileRepository {
return file ? this.toDomain(file) : null;
}

async findFileByName(
where: Partial<Omit<FileAttributes, 'name' | 'plainName'>>,
nameFilter: Pick<FileAttributes, 'name' | 'plainName'>,
): Promise<File | null> {
const file = await this.fileModel.findOne({
where: {
...where,
[Op.or]: [
{ name: nameFilter.name },
{ plainName: nameFilter.plainName },
],
},
});
return file ? this.toDomain(file) : null;
}

async findFilesInFolderByName(
folderId: Folder['uuid'],
searchFilter: { plainName: File['plainName']; type?: File['type'] }[],
Expand Down
62 changes: 27 additions & 35 deletions src/modules/file/file.usecase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,16 +517,12 @@ describe('FileUseCases', () => {
.spyOn(folderUseCases, 'getFolderByUuidAndUser')
.mockResolvedValueOnce(destinationFolder);

jest
.spyOn(cryptoService, 'decryptName')
.mockReturnValueOnce(file.plainName);

jest
.spyOn(cryptoService, 'encryptName')
.mockReturnValueOnce(expectedFile.name);

jest
.spyOn(fileRepository, 'findByNameAndFolderUuid')
.spyOn(fileRepository, 'findByPlainNameAndFolderId')
.mockResolvedValueOnce(null);

jest
Expand Down Expand Up @@ -602,12 +598,9 @@ describe('FileUseCases', () => {
jest
.spyOn(folderRepository, 'findByUuid')
.mockResolvedValueOnce(destinationFolder);
jest
.spyOn(cryptoService, 'decryptName')
.mockReturnValueOnce(file.plainName);
jest.spyOn(cryptoService, 'encryptName').mockReturnValueOnce(file.name);
jest
.spyOn(fileRepository, 'findByNameAndFolderUuid')
.spyOn(fileRepository, 'findByPlainNameAndFolderId')
.mockResolvedValueOnce(file);

expect(
Expand All @@ -626,12 +619,9 @@ describe('FileUseCases', () => {
jest
.spyOn(folderRepository, 'findByUuid')
.mockResolvedValueOnce(destinationFolder);
jest
.spyOn(cryptoService, 'decryptName')
.mockReturnValueOnce(file.plainName);
jest.spyOn(cryptoService, 'encryptName').mockReturnValueOnce(file.name);
jest
.spyOn(fileRepository, 'findByNameAndFolderUuid')
.spyOn(fileRepository, 'findByPlainNameAndFolderId')
.mockResolvedValueOnce(conflictFile);

expect(
Expand Down Expand Up @@ -695,7 +685,9 @@ describe('FileUseCases', () => {
const folder = newFolder({ attributes: { userId: userMocked.id } });

jest.spyOn(folderUseCases, 'getByUuid').mockResolvedValueOnce(folder);
jest.spyOn(fileRepository, 'findOneBy').mockResolvedValueOnce(null);
jest
.spyOn(fileRepository, 'findByPlainNameAndFolderId')
.mockResolvedValueOnce(null);

const createdFile = newFile({
attributes: {
Expand Down Expand Up @@ -729,7 +721,7 @@ describe('FileUseCases', () => {
jest.spyOn(fileRepository, 'findOneBy').mockResolvedValueOnce(mockFile);

jest
.spyOn(fileRepository, 'findFileByName')
.spyOn(fileRepository, 'findByPlainNameAndFolderId')
.mockResolvedValueOnce(fileWithSameName);

try {
Expand Down Expand Up @@ -832,7 +824,9 @@ describe('FileUseCases', () => {
});

jest.spyOn(fileRepository, 'findOneBy').mockResolvedValueOnce(mockFile);
jest.spyOn(fileRepository, 'findFileByName').mockResolvedValueOnce(null);
jest
.spyOn(fileRepository, 'findByPlainNameAndFolderId')
.mockResolvedValueOnce(null);
jest.spyOn(cryptoService, 'encryptName').mockReturnValue(encryptedName);

const result = await service.updateFileMetaData(
Expand All @@ -846,13 +840,12 @@ describe('FileUseCases', () => {
userId: mockFile.userId,
status: FileStatus.EXISTS,
});
expect(fileRepository.findFileByName).toHaveBeenCalledWith(
{
folderId: mockFile.folderId,
type: mockFile.type,
status: FileStatus.EXISTS,
},
{ name: encryptedName, plainName: newFileMeta.plainName },
expect(fileRepository.findByPlainNameAndFolderId).toHaveBeenCalledWith(
mockFile.userId,
newFileMeta.plainName,
mockFile.type,
mockFile.folderId,
FileStatus.EXISTS,
);
expect(fileRepository.updateByUuidAndUserId).toHaveBeenCalledWith(
mockFile.uuid,
Expand Down Expand Up @@ -892,7 +885,9 @@ describe('FileUseCases', () => {
});

jest.spyOn(fileRepository, 'findOneBy').mockResolvedValueOnce(mockFile);
jest.spyOn(fileRepository, 'findFileByName').mockResolvedValueOnce(null);
jest
.spyOn(fileRepository, 'findByPlainNameAndFolderId')
.mockResolvedValueOnce(null);
jest.spyOn(cryptoService, 'encryptName').mockReturnValue(mockFile.name);

const result = await service.updateFileMetaData(
Expand All @@ -906,13 +901,12 @@ describe('FileUseCases', () => {
userId: mockFile.userId,
status: FileStatus.EXISTS,
});
expect(fileRepository.findFileByName).toHaveBeenCalledWith(
{
folderId: mockFile.folderId,
type: newTypeFileMeta.type,
status: FileStatus.EXISTS,
},
{ name: mockFile.name, plainName: mockFile.plainName },
expect(fileRepository.findByPlainNameAndFolderId).toHaveBeenCalledWith(
mockFile.userId,
mockFile.plainName,
newTypeFileMeta.type,
mockFile.folderId,
FileStatus.EXISTS,
);
expect(fileRepository.updateByUuidAndUserId).toHaveBeenCalledWith(
mockFile.uuid,
Expand Down Expand Up @@ -977,7 +971,7 @@ describe('FileUseCases', () => {
.spyOn(folderUseCases, 'getFolderMetadataByPath')
.mockResolvedValue(parentFolderFile);
jest
.spyOn(service, 'findByPlainNameAndFolderUuid')
.spyOn(service, 'findByPlainNameAndFolderId')
.mockResolvedValue(expectedFile);

const result = await service.getFileMetadataByPath(userMocked, filePath);
Expand Down Expand Up @@ -1018,9 +1012,7 @@ describe('FileUseCases', () => {
jest
.spyOn(folderUseCases, 'getFolderMetadataByPath')
.mockResolvedValue(parentFolderFile);
jest
.spyOn(service, 'findByPlainNameAndFolderUuid')
.mockResolvedValue(null);
jest.spyOn(service, 'findByPlainNameAndFolderId').mockResolvedValue(null);

const result = await service.getFileMetadataByPath(userMocked, filePath);
expect(result).toBeNull();
Expand Down
82 changes: 31 additions & 51 deletions src/modules/file/file.usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,14 @@ export class FileUseCases {
folder.id,
);

const maybeAlreadyExistentFile = await this.fileRepository.findOneBy({
name: cryptoFileName,
plainName: newFileDto.plainName,
folderId: folder.id,
...(newFileDto.type ? { type: newFileDto.type } : null),
userId: user.id,
status: FileStatus.EXISTS,
});

const fileAlreadyExists = !!maybeAlreadyExistentFile;

if (fileAlreadyExists) {
const exists = await this.fileRepository.findByPlainNameAndFolderId(
user.id,
newFileDto.plainName,
newFileDto.type,
folder.id,
FileStatus.EXISTS,
);
if (exists) {
throw new ConflictException('File already exists');
}

Expand Down Expand Up @@ -197,15 +193,12 @@ export class FileUseCases {
type,
});

const fileWithSameNameExists = await this.fileRepository.findFileByName(
{
folderId: updatedFile.folderId,
type: updatedFile.type,
status: FileStatus.EXISTS,
},
{ name: updatedFile.name, plainName: updatedFile.plainName },
const fileWithSameNameExists = await this.findByPlainNameAndFolderId(
updatedFile.userId,
updatedFile.plainName,
updatedFile.type,
updatedFile.folderId,
);

if (fileWithSameNameExists) {
throw new ConflictException(
'A file with this name already exists in this location',
Expand Down Expand Up @@ -589,19 +582,11 @@ export class FileUseCases {
);
}

const originalPlainName = this.cryptoService.decryptName(
file.name,
file.folderId,
);
const destinationEncryptedName = this.cryptoService.encryptName(
originalPlainName,
destinationFolder.id,
);

const exists = await this.fileRepository.findByNameAndFolderUuid(
destinationEncryptedName,
const exists = await this.fileRepository.findByPlainNameAndFolderId(
file.userId,
file.plainName,
file.type,
destinationFolder.uuid,
destinationFolder.id,
FileStatus.EXISTS,
);
if (exists) {
Expand All @@ -615,6 +600,11 @@ export class FileUseCases {
);
}

const destinationEncryptedName = this.cryptoService.encryptName(
file.plainName,
destinationFolder.id,
);

const updateData: Partial<File> = {
folderId: destinationFolder.id,
folderUuid: destinationFolder.uuid,
Expand Down Expand Up @@ -686,28 +676,17 @@ export class FileUseCases {
});
}

async findByNameAndFolderUuid(
name: FileAttributes['name'],
type: FileAttributes['type'],
folderUuid: FileAttributes['folderUuid'],
): Promise<File | null> {
return this.fileRepository.findByNameAndFolderUuid(
name,
type,
folderUuid,
FileStatus.EXISTS,
);
}

async findByPlainNameAndFolderUuid(
async findByPlainNameAndFolderId(
userId: FileAttributes['userId'],
plainName: FileAttributes['plainName'],
type: FileAttributes['type'],
folderUuid: FileAttributes['folderUuid'],
folderId: FileAttributes['folderId'],
): Promise<File | null> {
return this.fileRepository.findByPlainNameAndFolderUuid(
return this.fileRepository.findByPlainNameAndFolderId(
userId,
plainName,
type,
folderUuid,
folderId,
FileStatus.EXISTS,
);
}
Expand All @@ -726,10 +705,11 @@ export class FileUseCases {
throw new NotFoundException('Parent folders not found');
}

const file = await this.findByPlainNameAndFolderUuid(
const file = await this.findByPlainNameAndFolderId(
user.id,
path.fileName,
path.fileType,
folder.uuid,
folder.id,
);
return file;
}
Expand Down
Loading