Skip to content

Commit

Permalink
Merge pull request #397 from internxt/feat/trash-sorting
Browse files Browse the repository at this point in the history
[PB-2946]: feat/update trash controller to support sorting
  • Loading branch information
apsantiso authored Sep 23, 2024
2 parents 4de1781 + 21f164e commit d4b72be
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/modules/trash/trash.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ import {
DeleteItemsDto,
DeleteItemType,
} from './dto/controllers/delete-item.dto';
import { Folder } from '../folder/folder.domain';
import { File, FileStatus } from '../file/file.domain';
import { Folder, SortableFolderAttributes } from '../folder/folder.domain';
import { File, FileStatus, SortableFileAttributes } from '../file/file.domain';
import logger from '../../externals/logger';
import { v4 } from 'uuid';
import { Response } from 'express';
import { HttpExceptionFilter } from '../../lib/http/http-exception.filter';
import {
WorkspaceResourcesAction,
WorkspacesInBehalfGuard,
} from '../workspaces/guards/workspaces-resources-in-behalf.decorator';
import { GetDataFromRequest } from '../../common/extract-data-from-request';
import { StorageNotificationService } from '../../externals/notifications/storage.notifications.service';
import { BasicPaginationDto } from '../../common/dto/basic-pagination.dto';

@ApiTags('Trash')
@Controller('storage/trash')
Expand All @@ -68,20 +68,20 @@ export class TrashController {
@ApiOkResponse({ description: 'Files on trash for a given folder' })
async getTrashedFilesPaginated(
@UserDecorator() user: User,
@Query('limit') limit: number,
@Query('offset') offset: number,
@Query() pagination: BasicPaginationDto,
@Query('type') type: 'files' | 'folders',
@Res({ passthrough: true }) res: Response,
@Query('sort') sort?: SortableFolderAttributes | SortableFileAttributes,
@Query('order') order?: 'ASC' | 'DESC',
) {
if (!limit || offset === undefined || !type) {
if (!pagination.limit || pagination.offset === undefined || !type) {
throw new BadRequestException();
}

if (type !== 'files' && type !== 'folders') {
throw new BadRequestException();
}

if (limit < 1 || limit > 50) {
if (pagination.limit < 1 || pagination.limit > 50) {
throw new BadRequestException('Limit should be between 1 and 50');
}

Expand All @@ -92,13 +92,21 @@ export class TrashController {
result = await this.fileUseCases.getFiles(
user.id,
{ status: FileStatus.TRASHED },
{ limit, offset },
{
limit: pagination.limit,
offset: pagination.offset,
sort: sort && order && [[sort, order]],
},
);
} else {
result = await this.folderUseCases.getFolders(
user.id,
{ deleted: true, removed: false },
{ limit, offset },
{
limit: pagination.limit,
offset: pagination.offset,
sort: sort && order && [[sort as SortableFolderAttributes, order]],
},
);
}

Expand All @@ -113,9 +121,8 @@ export class TrashController {
uuid,
})} STACK: ${(error as Error).stack}`,
);
res.status(HttpStatus.INTERNAL_SERVER_ERROR);

return { error: 'Internal Server Error' };
throw new InternalServerErrorException();
}
}

Expand Down

0 comments on commit d4b72be

Please sign in to comment.