📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
[untested] added endpoint to create alerts
Blue Mousef2abb333ae41
diff --git a/src/alert/alert.controller.ts b/src/alert/alert.controller.tsindex b1089f5e8..a65ead066 100644--- a/src/alert/alert.controller.ts+++ b/src/alert/alert.controller.ts@@ -1,5 +1,5 @@import {- Controller, Get, Put, Delete, UseGuards, Query, Param, Body, NotFoundException,+ Controller, Get, Put, Delete, UseGuards, Query, Param, Body, NotFoundException, Post, ForbiddenException, BadRequestException,} from '@nestjs/common'import { In } from 'typeorm'import { ApiTags, ApiResponse } from '@nestjs/swagger'@@ -9,6 +9,7 @@ import { RolesGuard } from '../common/guards/roles.guard'import * as _isEmpty from 'lodash/isEmpty'import * as _map from 'lodash/map'import * as _omit from 'lodash/omit'+import * as _size from 'lodash/size'import { UserService } from 'src/user/user.service'import { ProjectService } from 'src/project/project.service'@@ -17,7 +18,10 @@ import { CurrentUserId } from '../common/decorators/current-user-id.decorator'import { Roles } from '../common/decorators/roles.decorator'import { UserType } from 'src/user/entities/user.entity'import { Alert } from './entity/alert.entity'-import { AlertDTO } from './dto/alert.dto'+import { AlertDTO, CreateAlertDTO } from './dto/alert.dto'+import { ACCOUNT_PLANS, PlanCode } from 'src/user/entities/user.entity'++const ALERTS_MAXIMUM = ACCOUNT_PLANS[PlanCode.free].maxAlerts@ApiTags('Alert')@Controller('alert')@@ -52,6 +56,63 @@ export class AlertController {return this.alertService.paginate({ take, skip }, { project: In(pids) })}+ @Post('/')+ @UseGuards(SelfhostedGuard)+ @UseGuards(RolesGuard)+ @Roles(UserType.ADMIN, UserType.CUSTOMER)+ @ApiResponse({ status: 201, type: Alert })+ async createAlert(+ @Body() alertDTO: CreateAlertDTO,+ @CurrentUserId() uid: string,+ ) {+ this.logger.log({ uid }, 'POST /alert')++ const user = await this.userService.findOneWithRelations(uid, [+ 'projects',+ ])++ const maxAlerts = ACCOUNT_PLANS[user.planCode]?.maxAlerts++ if (!user.isActive) {+ throw new ForbiddenException('Please, verify your email address first')+ }++ if (_size(user.projects) >= (maxAlerts || ALERTS_MAXIMUM)) {+ throw new ForbiddenException(+ `You cannot create more than ${maxAlerts} alerts on your account plan. Please upgrade to be able to create more alerts.`,+ )+ }++ const project = await this.projectService.findOneWhere({+ id: alertDTO.pid,+ }, {+ relations: ['alerts'],+ })++ if (_isEmpty(project)) {+ throw new NotFoundException('Project not found')+ }++ this.projectService.allowedToManage(project, uid, user.roles, 'You are not allowed to manage this project')++ try {+ let alert = new Alert()+ Object.assign(alert, alertDTO)+ alert = _omit(alert, ['pid'])++ const newAlert = await this.alertService.create(alert)++ project.alerts.push(newAlert)++ await this.projectService.create(project)++ return newAlert+ } catch (e) {+ throw new BadRequestException(e)+ }+ }++@Put('/:id')@UseGuards(SelfhostedGuard)@UseGuards(RolesGuard)diff --git a/src/alert/dto/alert.dto.ts b/src/alert/dto/alert.dto.tsindex 6461370e3..1574d266c 100644--- a/src/alert/dto/alert.dto.ts+++ b/src/alert/dto/alert.dto.ts@@ -34,4 +34,27 @@ export class AlertDTO {@ApiProperty()queryTime?: QueryTime | null-}\ No newline at end of file++ @ApiProperty()+ active?: boolean+}++export class CreateAlertDTO {+ @ApiProperty()+ pid: string++ @ApiProperty()+ name: string++ @ApiProperty()+ queryMetric: QueryMetric | null++ @ApiProperty()+ queryCondition: QueryCondition | null++ @ApiProperty()+ queryValue: number | null++ @ApiProperty()+ queryTime: QueryTime | null+}diff --git a/src/user/entities/user.entity.ts b/src/user/entities/user.entity.tsindex c785e22aa..8741185bf 100644--- a/src/user/entities/user.entity.ts+++ b/src/user/entities/user.entity.ts@@ -26,6 +26,7 @@ export const ACCOUNT_PLANS = {displayName: 'Free plan',monthlyUsageLimit: 5000,maxProjects: 10,+ maxAlerts: 1,},[PlanCode.freelancer]: {id: PlanCode.freelancer,@@ -34,6 +35,7 @@ export const ACCOUNT_PLANS = {maxProjects: 20,pid: '752316', // Plan IDypid: '776469', // Plan ID - Yearly billing+ maxAlerts: 10,},[PlanCode.startup]: {id: PlanCode.startup,@@ -42,6 +44,7 @@ export const ACCOUNT_PLANS = {maxProjects: 20,pid: '752317',ypid: '776470',+ maxAlerts: 50,},[PlanCode.enterprise]: {id: PlanCode.enterprise,@@ -50,6 +53,7 @@ export const ACCOUNT_PLANS = {maxProjects: 30,pid: '752318',ypid: '776471',+ maxAlerts: 100,},}