Skip to content

Commit

Permalink
Merge pull request #395 from internxt/feat/allow-search-by-plainname-…
Browse files Browse the repository at this point in the history
…folder

feat: allow existent folder by plainName
  • Loading branch information
apsantiso authored Sep 17, 2024
2 parents 1fb495b + 7fa042b commit 7f7063a
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 38 deletions.
16 changes: 8 additions & 8 deletions src/modules/folder/dto/folder-existence-in-folder.dto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CheckFoldersExistenceDto } from './folder-existence-in-folder.dto';
describe('CheckFoldersExistenceDto', () => {
it('When valid data is passed, then no errors should be returned', async () => {
const dto = plainToInstance(CheckFoldersExistenceDto, {
plainName: ['folder1', 'folder2'],
plainNames: ['folder1', 'folder2'],
});

const errors = await validate(dto);
Expand All @@ -14,17 +14,17 @@ describe('CheckFoldersExistenceDto', () => {

it('When a single string is passed, then it should be transformed into an array and validate successfully', async () => {
const dto = plainToInstance(CheckFoldersExistenceDto, {
plainName: 'folder1',
plainNames: 'folder1',
});

const errors = await validate(dto);
expect(errors.length).toBe(0);
expect(dto.plainName).toEqual(['folder1']);
expect(dto.plainNames).toEqual(['folder1']);
});

it('When plainName array exceeds max size, then it should fail', async () => {
const plainName = Array.from({ length: 51 }, (_, i) => `folder${i + 1}`);
const dto = plainToInstance(CheckFoldersExistenceDto, { plainName });
const plainNames = Array.from({ length: 201 }, (_, i) => `folder${i + 1}`);
const dto = plainToInstance(CheckFoldersExistenceDto, { plainNames });

const errors = await validate(dto);
expect(errors.length).toBeGreaterThan(0);
Expand All @@ -33,7 +33,7 @@ describe('CheckFoldersExistenceDto', () => {

it('When plainName contains non-string values, then it should fail', async () => {
const dto = plainToInstance(CheckFoldersExistenceDto, {
plainName: [1, 2, 3],
plainNames: [1, 2, 3],
});

const errors = await validate(dto);
Expand All @@ -47,8 +47,8 @@ describe('CheckFoldersExistenceDto', () => {
expect(errors.length).toBeGreaterThan(0);
});

it('When plainName is an empty array, then it should validate successfully', async () => {
const dto = plainToInstance(CheckFoldersExistenceDto, { plainName: [] });
it('When plainName is an empty array, then it should faild', async () => {
const dto = plainToInstance(CheckFoldersExistenceDto, { plainNames: [] });

const errors = await validate(dto);
expect(errors.length).toBe(0);
Expand Down
10 changes: 5 additions & 5 deletions src/modules/folder/dto/folder-existence-in-folder.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { IsString, ArrayMaxSize, IsArray } from 'class-validator';

export class CheckFoldersExistenceDto {
@ApiProperty({
description: 'Plain name of folder',
example: 'my folder',
description: 'Plain name of folder or array of plain names',
example: ['My folder'],
})
@IsArray()
@ArrayMaxSize(50, {
message: 'Names parameter cannot contain more than 50 names',
@ArrayMaxSize(200, {
message: 'Names parameter cannot contain more than 200 names',
})
@IsString({ each: true })
@Transform(({ value }) => {
Expand All @@ -18,5 +18,5 @@ export class CheckFoldersExistenceDto {
}
return value;
})
plainName: string[];
plainNames: string[];
}
4 changes: 2 additions & 2 deletions src/modules/folder/folder.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ describe('FolderController', () => {
const result = await folderController.checkFoldersExistenceInFolder(
user,
folderUuid,
{ plainName: plainNames },
{ plainNames: plainNames },
);

expect(result).toEqual({ existentFolders: mockFolders });
Expand All @@ -401,7 +401,7 @@ describe('FolderController', () => {
const result = await folderController.checkFoldersExistenceInFolder(
user,
folderUuid,
{ plainName: plainNames },
{ plainNames: plainNames },
);

expect(result).toEqual({ existentFolders: [] });
Expand Down
8 changes: 4 additions & 4 deletions src/modules/folder/folder.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export class FolderController {
};
}

@Get('/content/:uuid/folders/existence')
@Post('/content/:uuid/folders/existence')
@GetDataFromRequest([
{
sourceKey: 'params',
Expand All @@ -368,15 +368,15 @@ export class FolderController {
async checkFoldersExistenceInFolder(
@UserDecorator() user: User,
@Param('uuid') folderUuid: string,
@Query() query: CheckFoldersExistenceDto,
@Body() query: CheckFoldersExistenceDto,
) {
const { plainName } = query;
const { plainNames } = query;

const folders = await this.folderUseCases.searchFoldersInFolder(
user,
folderUuid,
{
plainNames: plainName,
plainNames,
},
);

Expand Down
11 changes: 5 additions & 6 deletions src/modules/folder/folder.repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { FolderModel } from './folder.model';
import { Folder } from './folder.domain';
import { newFolder } from '../../../test/fixtures';
import { FileStatus } from '../file/file.domain';
import { v4 } from 'uuid';
import { Op } from 'sequelize';

jest.mock('./folder.model', () => ({
Expand Down Expand Up @@ -111,19 +110,19 @@ describe('SequelizeFolderRepository', () => {
});

describe('findByParentUuid', () => {
const parentUuid = v4();
const parentId = 1;
const plainNames = ['Document', 'Image'];

it('When folders are searched with names, then it should handle the call with names', async () => {
await repository.findByParentUuid(parentUuid, {
await repository.findByParent(parentId, {
plainName: plainNames,
deleted: false,
removed: false,
});

expect(folderModel.findAll).toHaveBeenCalledWith({
where: {
parentUuid: parentUuid,
parentId,
plainName: { [Op.in]: plainNames },
deleted: false,
removed: false,
Expand All @@ -132,15 +131,15 @@ describe('SequelizeFolderRepository', () => {
});

it('When called without specific criteria, then it should handle the call', async () => {
await repository.findByParentUuid(parentUuid, {
await repository.findByParent(parentId, {
plainName: [],
deleted: false,
removed: false,
});

expect(folderModel.findAll).toHaveBeenCalledWith({
where: {
parentUuid: parentUuid,
parentId,
deleted: false,
removed: false,
},
Expand Down
10 changes: 5 additions & 5 deletions src/modules/folder/folder.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export interface FolderRepository {
folderUuid: FolderAttributes['uuid'],
deleted: FolderAttributes['deleted'],
): Promise<Folder | null>;
findByParentUuid(
parentUuid: Folder['uuid'],
findByParent(
parentId: Folder['id'],
searchBy: {
plainName: Folder['plainName'][];
deleted: boolean;
Expand Down Expand Up @@ -132,16 +132,16 @@ export class SequelizeFolderRepository implements FolderRepository {
return newOrder;
}

async findByParentUuid(
parentUuid: Folder['uuid'],
async findByParent(
parentId: Folder['id'],
searchBy: {
plainName: Folder['plainName'][];
deleted: boolean;
removed: boolean;
},
): Promise<Folder[]> {
const where: WhereOptions<Folder> = {
parentUuid,
parentId,
removed: searchBy.removed,
deleted: searchBy.deleted,
};
Expand Down
12 changes: 6 additions & 6 deletions src/modules/folder/folder.usecase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1030,16 +1030,16 @@ describe('FolderUseCases', () => {
.spyOn(folderRepository, 'findOne')
.mockResolvedValue(mockParentFolder);
jest
.spyOn(folderRepository, 'findByParentUuid')
.spyOn(folderRepository, 'findByParent')
.mockResolvedValue(mockFolders);

const result = await service.searchFoldersInFolder(user, folderUuid, {
plainNames,
});

expect(result).toEqual(mockFolders);
expect(folderRepository.findByParentUuid).toHaveBeenCalledWith(
mockParentFolder.uuid,
expect(folderRepository.findByParent).toHaveBeenCalledWith(
mockParentFolder.id,
{
plainName: plainNames,
removed: false,
Expand All @@ -1056,15 +1056,15 @@ describe('FolderUseCases', () => {
jest
.spyOn(folderRepository, 'findOne')
.mockResolvedValue(mockParentFolder);
jest.spyOn(folderRepository, 'findByParentUuid').mockResolvedValue([]);
jest.spyOn(folderRepository, 'findByParent').mockResolvedValue([]);

const result = await service.searchFoldersInFolder(user, folderUuid, {
plainNames,
});

expect(result).toEqual([]);
expect(folderRepository.findByParentUuid).toHaveBeenCalledWith(
mockParentFolder.uuid,
expect(folderRepository.findByParent).toHaveBeenCalledWith(
mockParentFolder.id,
{
plainName: plainNames,
removed: false,
Expand Down
4 changes: 2 additions & 2 deletions src/modules/folder/folder.usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,10 +544,10 @@ export class FolderUseCases {
});

if (!parentFolder) {
throw new BadRequestException('Folder not valid!');
throw new BadRequestException('Parent folder not valid');
}

return this.folderRepository.findByParentUuid(parentFolder.uuid, {
return this.folderRepository.findByParent(parentFolder.id, {
plainName: plainNames,
deleted: false,
removed: false,
Expand Down

0 comments on commit 7f7063a

Please sign in to comment.