import { Controller, UseGuards, Patch, Body } from '@nestjs/common'; import { Crud, CrudController, Override, ParsedRequest, CrudRequest, } from '@nestjsx/crud'; import { Category } from '../entities/category.entity'; import { CategoryDto } from './category.dto'; import { JwtAuthGuard } from '../auth/jwt-auth.guard'; import { ApiTags, ApiHeader, ApiBearerAuth } from '@nestjs/swagger'; import { CategoriesService } from './categories.service'; import { cleanupDtoList } from '../cleanup-dto-list'; import { FilterDeletedItemsInterceptor } from '../filter-deleted-items.interceptor'; @ApiTags('Categories') @ApiHeader({ name: 'Authorization', allowEmptyValue: false, description: '"Bearer Token"', example: 'Bearer ', }) @ApiBearerAuth() @Crud({ model: { type: Category, }, routes: { exclude: ['replaceOneBase'], getManyBase: { interceptors: [FilterDeletedItemsInterceptor], }, getOneBase: { interceptors: [FilterDeletedItemsInterceptor], }, }, dto: { create: CategoryDto, update: CategoryDto, }, query: { filter: { deletedAt: { $eq: null, }, }, sort: [ { field: 'createdAt', order: 'ASC', }, ], join: { products: { exclude: ['createdAt', 'updatedAt', 'version'], }, }, }, serialize: { create: CategoryDto, createMany: CategoryDto, get: CategoryDto, getMany: CategoryDto, update: CategoryDto, replace: CategoryDto, }, }) @UseGuards(JwtAuthGuard) @Controller('categories') export class CategoriesController implements CrudController { constructor(public service: CategoriesService) {} get base(): CrudController { return this; } @Override() async deleteOne(@ParsedRequest() req: CrudRequest) { const id = req.parsed.paramsFilter.find( f => f.field === 'id' && f.operator === '$eq', ).value; const res = await this.service.markDeleted(id); return res; } @Patch('bulk') async bulkUpdate( @Body() categories: Array>, ) { if (categories instanceof Array) { const cs = cleanupDtoList(CategoryDto, categories); await this.service.bulkUpdate(cs); } else { this.service.throwBadRequestException('send data as array'); } } }