Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@Post()
async create(@Body() dto: InPermissionDto) {
try {
return plainToClass(
OutPermissionDto,
await this.service.create({
item: plainToClass(Permission, dto)
})
);
} catch (error) {
throw error;
}
}
@Roles('isSuperuser')
@Permissions('change_permission')
@HttpCode(HttpStatus.OK)
@ApiResponse({
status: HttpStatus.OK,
type: OutPermissionDto,
description: 'The record has been successfully updated.'
})
@ApiResponse({ status: HttpStatus.FORBIDDEN, description: 'Forbidden.' })
@ApiImplicitParam({ name: 'id', type: Number })
@Put(':id')
async update(@Param('id', new ParseIntPipe()) id, @Body() dto: InPermissionDto) {
try {
return plainToClass(
OutPermissionDto,
await this.service.update({
id,
item: plainToClass(Permission, dto)
})
import { Body, Controller, Headers, HttpCode, Post } from '@nestjs/common';
import { WebhooksService } from './webhooks.service';
@Controller('webhooks')
export class WebhooksController {
constructor(private readonly whService: WebhooksService) {}
@Post('/heroku')
@HttpCode(204)
herokuWebhook(
@Headers('heroku-webhook-hmac-sha256') header: string,
@Body() body: any
) {
this.whService.herokuWebhook(header, body);
}
}
import { join } from 'path';
import { Observable, of } from 'rxjs';
@Controller()
export class GrpcController {
@Client({
transport: Transport.GRPC,
options: {
package: 'math',
protoPath: join(__dirname, 'math.proto'),
},
})
client: ClientGrpc;
@Post()
@HttpCode(200)
call(@Body() data: number[]): Observable {
const svc = this.client.getService('Math');
return svc.sum({ data });
}
@GrpcMethod('Math')
async sum({ data }: { data: number[] }): Promise {
return of({
result: data.reduce((a, b) => a + b),
});
}
}
import { OutUserDto } from '../dto/out-user.dto';
import { OutUsersDto } from '../dto/out-users.dto';
import { User } from '../entities/user.entity';
import { AccessGuard } from '../guards/access.guard';
import { ParseIntWithDefaultPipe } from '../pipes/parse-int-with-default.pipe';
import { UsersService } from '../services/users.service';
@ApiUseTags('users')
@ApiBearerAuth()
@Controller('/api/users')
@UseGuards(AccessGuard)
export class UsersController {
constructor(private readonly service: UsersService) {}
@Roles('isSuperuser')
@Permissions('add_user')
@HttpCode(HttpStatus.CREATED)
@ApiResponse({
status: HttpStatus.CREATED,
type: OutUserDto,
description: 'The record has been successfully created.'
})
@ApiResponse({ status: HttpStatus.FORBIDDEN, description: 'Forbidden.' })
@Post()
async create(@Body() dto: InCreateUserDto) {
try {
return plainToClass(
OutUserDto,
await this.service.create({
item: await plainToClass(User, dto).setPassword(dto.password)
})
);
} catch (error) {
}
@ApiOperation({ title: 'Update an existing record' })
@ApiCreatedResponse({ description: 'The record has been successfully edited.' })
@ApiNotFoundResponse({ description: 'Record not found' })
@ApiBadRequestResponse({ description: 'Invalid input, The response body may contain clues as to what went wrong' })
@HttpCode(HttpStatus.CREATED)
@Put(':id')
public async update(@Param('id') id: string, @Body() entity: Partial>): Promise> {
return this.crudService.update(id, entity);
}
@ApiOperation({ title: 'Delete record' })
@ApiResponse({ status: HttpStatus.NO_CONTENT, description: 'The record has been successfully deleted' })
@ApiNotFoundResponse({ description: 'Record not found' })
@HttpCode(HttpStatus.NO_CONTENT)
@Delete(':id')
public async delete(@Param('id') id: string): Promise>> {
return this.crudService.delete(id);
}
}
async findById(@Param('id', new ParseIntPipe()) id) {
try {
return plainToClass(
OutGroupDto,
await this.service.findById({
id
})
);
} catch (error) {
throw error;
}
}
@Roles('isSuperuser')
@Permissions('read_group')
@HttpCode(HttpStatus.OK)
@ApiResponse({
status: HttpStatus.OK,
type: OutGroupsDto,
description: ''
})
@ApiResponse({ status: HttpStatus.FORBIDDEN, description: 'Forbidden.' })
@ApiImplicitQuery({
name: 'q',
required: false,
type: String,
description: 'Text for search (default: empty)'
})
@ApiImplicitQuery({
name: 'sort',
required: false,
type: String,
async update(@Param('id', new ParseIntPipe()) id, @Body() dto: InUserDto) {
try {
return plainToClass(
OutUserDto,
await this.service.update({
id,
item: await plainToClass(User, dto).setPassword(dto.password)
})
);
} catch (error) {
throw error;
}
}
@Roles('isSuperuser')
@Permissions('delete_user')
@HttpCode(HttpStatus.NO_CONTENT)
@ApiResponse({
status: HttpStatus.NO_CONTENT,
description: 'The record has been successfully deleted.'
})
@ApiResponse({ status: HttpStatus.FORBIDDEN, description: 'Forbidden.' })
@ApiImplicitParam({ name: 'id', type: Number })
@Delete(':id')
async delete(@Param('id', new ParseIntPipe()) id) {
try {
return plainToClass(
OutUserDto,
await this.service.delete({
id
})
);
} catch (error) {
@Post()
@HttpCode(200)
call(@Query('command') cmd, @Body() data: number[]) {
return this.client.send({ cmd }, data);
}
@Post('stream')
@HttpCode(200)
stream(@Body() data: number[]): Observable {
return this.client
.send({ cmd: 'streaming' }, data)
.pipe(scan((a, b) => a + b));
}
@Post('concurrent')
@HttpCode(200)
concurrent(@Body() data: number[][]): Promise {
const send = async (tab: number[]) => {
const expected = tab.reduce((a, b) => a + b);
const result = await this.client
.send({ cmd: 'sum' }, tab)
.toPromise();
return result === expected;
};
return data
.map(async tab => send(tab))
.reduce(async (a, b) => (await a) && b);
}
@MessagePattern({ cmd: 'sum' })
sum(data: number[]): number {
client: ClientProxy;
constructor() {
this.client = ClientProxyFactory.create({
transport: Transport.RMQ,
options: {
urls: [`amqp://localhost:5672`],
queue: 'test',
queueOptions: { durable: false },
socketOptions: { noDelay: true },
},
});
}
@Post()
@HttpCode(200)
call(@Query('command') cmd, @Body() data: number[]) {
return this.client.send({ cmd }, data);
}
@Post('stream')
@HttpCode(200)
stream(@Body() data: number[]): Observable {
return this.client
.send({ cmd: 'streaming' }, data)
.pipe(scan((a, b) => a + b));
}
@Post('concurrent')
@HttpCode(200)
concurrent(@Body() data: number[][]): Promise {
const send = async (tab: number[]) => {
@ApiUseTags('period-events')
@Controller('api/period-events')
@UseGuards(AccessGuard)
export class PeriodEventController {
constructor(
private readonly periodEventService: PeriodEventService,
) {
}
@ApiBearerAuth()
@ApiCreatedResponse({ description: 'Successfully created query and query values.', type: OutPeriodEventDto, isArray: true })
@ApiResponse({ status: HttpStatus.FORBIDDEN, description: 'Forbidden.' })
@ApiBadRequestResponse({description: 'Invalid query.' })
@HttpCode(HttpStatus.CREATED)
@Post('query')
async find(
@Request() req,
@Body(new ValidationPipe()) createPeriodEventQuery: CreatePeriodEventQueryDto,
): Promise {
Logger.log(`${req.user?req.user.id:''} try to`,PeriodEventService.name+':query');
const entities = await this.periodEventService.find(createPeriodEventQuery);
return plainToClass(OutPeriodEventDto,entities);
}
@ApiBearerAuth()
@ApiCreatedResponse({ description: 'Successfully created query and count values.', type: Number })
@ApiResponse({ status: HttpStatus.FORBIDDEN, description: 'Forbidden.' })
@ApiBadRequestResponse({description: 'Invalid query.' })
@HttpCode(HttpStatus.CREATED)
@Post('count')