import { Controller, UseGuards, Patch, Body } from '@nestjs/common'; import { Crud, CrudController, Override, ParsedRequest, CrudRequest, } from '@nestjsx/crud'; import { Unit } from '../entities/unit.entity'; import { UnitDto } from './unit.dto'; import { JwtAuthGuard } from '../auth/jwt-auth.guard'; import { ApiTags, ApiHeader, ApiBearerAuth } from '@nestjs/swagger'; import { UnitsService } from './units.service'; import { cleanupDtoList } from '../cleanup-dto-list'; @ApiTags('Units') @ApiHeader({ name: 'Authorization', allowEmptyValue: false, description: '"Bearer Token"', example: 'Bearer ', }) @ApiBearerAuth() @Crud({ model: { type: Unit, }, routes: { exclude: ['replaceOneBase'], }, dto: { create: UnitDto, update: UnitDto, }, query: { filter: { deletedAt: { $eq: null, }, }, sort: [ { field: 'createdAt', order: 'ASC', }, ], }, serialize: { create: UnitDto, createMany: UnitDto, get: UnitDto, getMany: UnitDto, update: UnitDto, replace: UnitDto, }, }) @UseGuards(JwtAuthGuard) @Controller('units') export class UnitsController implements CrudController { constructor(public service: UnitsService) {} 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() units: Array>, ) { if (units instanceof Array) { const us = cleanupDtoList(UnitDto, units); await this.service.bulkUpdate(us); } else { this.service.throwBadRequestException('send data as array'); } } }