Skip to content

📈 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!

Implement functionality for archiving project and disable statistic gathering once archived

Yehor Dremliuhae81783c86abd
diff --git a/apps/production/src/analytics/analytics.controller.ts b/apps/production/src/analytics/analytics.controller.ts
index 3e0713726..c0772f1cf 100644
--- a/apps/production/src/analytics/analytics.controller.ts
+++ b/apps/production/src/analytics/analytics.controller.ts
@@ -28,8 +28,9 @@ import {
ForbiddenException,
Response,
Header,
+ ConflictException,
} from '@nestjs/common'
-import { ApiTags } from '@nestjs/swagger'
+import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'
import * as UAParser from 'ua-parser-js'
import * as isbot from 'isbot'
@@ -89,6 +90,7 @@ import { ErrorDTO } from './dto/error.dto'
import { GetErrorsDto } from './dto/get-errors.dto'
import { GetErrorDTO } from './dto/get-error.dto'
import { PatchStatusDTO } from './dto/patch-status.dto'
+import { ProjectService } from '../project/project.service'
// eslint-disable-next-line @typescript-eslint/no-var-requires
const mysql = require('mysql2')
@@ -366,6 +368,7 @@ export class AnalyticsController {
constructor(
private readonly analyticsService: AnalyticsService,
private readonly logger: AppLoggerService,
+ private readonly projectService: ProjectService,
) {}
@Get()
@@ -1215,6 +1218,7 @@ export class AnalyticsController {
}
// Log error event
+ @ApiBearerAuth()
@Post('error')
@UseGuards(BotDetectionGuard)
@BotDetection()
@@ -1224,6 +1228,15 @@ export class AnalyticsController {
@Headers() headers,
@Ip() reqIP,
): Promise<any> {
+ const project = await this.projectService.getProjectById(errorDTO.pid)
+
+ if (!project || project.isArchived) {
+ const message = !project
+ ? 'Project is not found.'
+ : 'Project is archived.'
+ throw new ConflictException(message)
+ }
+
const { 'user-agent': userAgent, origin } = headers
const ip = getIPFromHeaders(headers, true) || reqIP || ''
@@ -1298,6 +1311,7 @@ export class AnalyticsController {
}
// Log custom event
+ @ApiBearerAuth()
@Post('custom')
@UseGuards(BotDetectionGuard)
@BotDetection()
@@ -1307,6 +1321,15 @@ export class AnalyticsController {
@Headers() headers,
@Ip() reqIP,
): Promise<any> {
+ const project = await this.projectService.getProjectById(eventsDTO.pid)
+
+ if (!project || project.isArchived) {
+ const message = !project
+ ? 'Project is not found.'
+ : 'Project is archived.'
+ throw new ConflictException(message)
+ }
+
const { 'user-agent': userAgent, origin } = headers
const ip = getIPFromHeaders(headers, true) || reqIP || ''
@@ -1378,6 +1401,7 @@ export class AnalyticsController {
}
}
+ @ApiBearerAuth()
@Post('hb')
@UseGuards(BotDetectionGuard)
@BotDetection()
@@ -1387,6 +1411,15 @@ export class AnalyticsController {
@Headers() headers,
@Ip() reqIP,
): Promise<any> {
+ const project = await this.projectService.getProjectById(logDTO.pid)
+
+ if (!project || project.isArchived) {
+ const message = !project
+ ? 'Project is not found.'
+ : 'Project is archived.'
+ throw new ConflictException(message)
+ }
+
const { 'user-agent': userAgent } = headers
const { pid } = logDTO
const ip = getIPFromHeaders(headers, true) || reqIP || ''
@@ -1407,6 +1440,7 @@ export class AnalyticsController {
}
// Log pageview event
+ @ApiBearerAuth()
@Post()
@UseGuards(BotDetectionGuard)
@BotDetection()
@@ -1416,6 +1450,15 @@ export class AnalyticsController {
@Headers() headers,
@Ip() reqIP,
): Promise<any> {
+ const project = await this.projectService.getProjectById(logDTO.pid)
+
+ if (!project || project.isArchived) {
+ const message = !project
+ ? 'Project is not found.'
+ : 'Project is archived.'
+ throw new ConflictException(message)
+ }
+
const { 'user-agent': userAgent, origin } = headers
const ip = getIPFromHeaders(headers, true) || reqIP || ''
diff --git a/apps/production/src/project/dto/project-id.dto.ts b/apps/production/src/project/dto/project-id.dto.ts
deleted file mode 100644
index 4c33ab2c7..000000000
--- a/apps/production/src/project/dto/project-id.dto.ts
+++ /dev/null
@@ -1,11 +0,0 @@
-import { ApiProperty } from '@nestjs/swagger'
-import { Matches, IsNotEmpty } from 'class-validator'
-
-const PROJECT_ID_REGEX = /^(?!.*--)[a-zA-Z0-9-]{12}$/
-
-export class ProjectIdDto {
- @ApiProperty()
- @Matches(PROJECT_ID_REGEX)
- @IsNotEmpty()
- readonly projectId: string
-}
diff --git a/apps/production/src/project/dto/update-project.dto.ts b/apps/production/src/project/dto/update-project.dto.ts
index f08ac2a40..1489a5880 100644
--- a/apps/production/src/project/dto/update-project.dto.ts
+++ b/apps/production/src/project/dto/update-project.dto.ts
@@ -1,8 +1,8 @@
+import { IsBoolean, IsOptional } from 'class-validator'
import { IntersectionType, PartialType } from '@nestjs/mapped-types'
import { ApiProperty } from '@nestjs/swagger'
import { ProjectDTO } from './project.dto'
import { ProjectPasswordDto } from './project-password.dto'
-import { IsBoolean, IsOptional } from 'class-validator'
export class UpdateProjectDto extends IntersectionType(
PartialType(ProjectDTO),
@@ -24,10 +24,11 @@ export class UpdateProjectDto extends IntersectionType(
@ApiProperty({
required: false,
- description: "When true, the incoming data will not be gathered for this project"})
-
+ description:
+ 'When true, the incoming data will not be gathered for this project',
+ default: false,
+ })
@IsBoolean()
@IsOptional()
isArchived?: boolean
-
}
diff --git a/apps/production/src/project/project.controller.ts b/apps/production/src/project/project.controller.ts
index 6e6336c24..c142ba644 100644
--- a/apps/production/src/project/project.controller.ts
+++ b/apps/production/src/project/project.controller.ts
@@ -22,15 +22,7 @@ import {
UnauthorizedException,
} from '@nestjs/common'
import { Response } from 'express'
-import {
- ApiTags,
- ApiQuery,
- ApiResponse,
- ApiBearerAuth,
- ApiOkResponse,
- ApiOperation,
- ApiNoContentResponse,
-} from '@nestjs/swagger'
+import { ApiTags, ApiQuery, ApiResponse, ApiBearerAuth } from '@nestjs/swagger'
import { ILike } from 'typeorm'
import * as _isEmpty from 'lodash/isEmpty'
import * as _map from 'lodash/map'
@@ -1714,6 +1706,7 @@ export class ProjectController {
res.send(html)
}
+ @ApiBearerAuth()
@Put('/:id')
@HttpCode(200)
@Auth([], true)
@@ -1752,7 +1745,7 @@ export class ProjectController {
project.active = projectDTO.active
}
- if (projectDTO.isArchived) {
+ if (_isBoolean(projectDTO.isArchived)) {
project.isArchived = projectDTO.isArchived
}
@@ -1876,5 +1869,4 @@ export class ProjectController {
isErrorDataExists,
}
}
-
}