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!

a little bit of improvements

Blue Mousee7d7667f3f7b
diff --git a/src/project/project.controller.ts b/src/project/project.controller.ts
index 28c11e715..84d565ff9 100644
--- a/src/project/project.controller.ts
+++ b/src/project/project.controller.ts
@@ -21,7 +21,7 @@ import * as _size from 'lodash/size'
import * as _values from 'lodash/values'
import { ProjectService } from './project.service'
-import { UserType, ACCOUNT_PLANS } from '../user/entities/user.entity'
+import { UserType, ACCOUNT_PLANS, PlanCode } from '../user/entities/user.entity'
import { Roles } from '../common/decorators/roles.decorator'
import { SelfhostedGuard } from 'src/common/guards/selfhosted.guard'
import { RolesGuard } from '../common/guards/roles.guard'
@@ -46,6 +46,8 @@ import {
deleteProjectClickhouse,
} from '../common/utils'
+const PROJECTS_MAXIMUM = ACCOUNT_PLANS[PlanCode.free].maxProjects
+
@ApiTags('Project')
@Controller('project')
export class ProjectController {
@@ -107,7 +109,7 @@ export class ProjectController {
@Query('take') take: number | undefined,
@Query('skip') skip: number | undefined,
): Promise<Project | object> {
- this.logger.log({ take, skip }, 'GET all')
+ this.logger.log({ take, skip }, 'GET /all')
const where = Object()
return await this.projectService.paginate({ take, skip }, where)
@@ -147,10 +149,11 @@ export class ProjectController {
@CurrentUserId() uid: string,
): Promise<Project | object> {
this.logger.log({ id }, 'GET /project/:id')
- if (!isValidPID(id))
+ if (!isValidPID(id)) {
throw new BadRequestException(
'The provided Project ID (pid) is incorrect',
)
+ }
let project
@@ -160,8 +163,9 @@ export class ProjectController {
project = await this.projectService.findOne(id)
}
- if (_isEmpty(project))
+ if (_isEmpty(project)) {
throw new NotFoundException('Project was not found in the database')
+ }
this.projectService.allowedToView(project, uid)
@@ -181,7 +185,7 @@ export class ProjectController {
@Param('id') userId: string,
@Body() projectDTO: ProjectDTO,
): Promise<Project> {
- this.logger.log({ userId, projectDTO }, 'POST /project/admin')
+ this.logger.log({ userId, projectDTO }, 'POST /project/admin/:id')
const user = await this.userService.findOneWithRelations(userId, [
'projects',
@@ -189,12 +193,12 @@ export class ProjectController {
const maxProjects = ACCOUNT_PLANS[user.planCode]?.maxProjects
if (!user.isActive) {
- throw new ForbiddenException('Please, verify your email address first')
+ throw new ForbiddenException('User\'s email address has to be verified first')
}
- if (_size(user.projects) >= (maxProjects || 5)) {
+ if (_size(user.projects) >= (maxProjects || PROJECTS_MAXIMUM)) {
throw new ForbiddenException(
- `You cannot create more than ${maxProjects} projects on your account plan. Please upgrade to be able to create more projects.`,
+ `The user's plan supports maximum of ${maxProjects} projects`,
)
}
@@ -260,7 +264,7 @@ export class ProjectController {
throw new ForbiddenException('Please, verify your email address first')
}
- if (_size(user.projects) >= (maxProjects || 5)) {
+ if (_size(user.projects) >= (maxProjects || PROJECTS_MAXIMUM)) {
throw new ForbiddenException(
`You cannot create more than ${maxProjects} projects on your account plan. Please upgrade to be able to create more projects.`,
)
@@ -332,7 +336,6 @@ export class ProjectController {
}
this.projectService.allowedToManage(project, uid, user.roles)
-
project.active = projectDTO.active
project.origins = _map(projectDTO.origins, _trim)
@@ -368,10 +371,11 @@ export class ProjectController {
@CurrentUserId() uid: string,
): Promise<any> {
this.logger.log({ uid, id }, 'DELETE /project/:id')
- if (!isValidPID(id))
+ if (!isValidPID(id)) {
throw new BadRequestException(
'The provided Project ID (pid) is incorrect',
)
+ }
if (isSelfhosted) {
await deleteProjectClickhouse(id)
@@ -400,9 +404,8 @@ export class ProjectController {
if (_isEmpty(project)) {
throw new NotFoundException(`Project with ID ${id} does not exist`)
}
-
+
this.projectService.allowedToManage(project, uid, user.roles)
-
const query1 = `ALTER table analytics DELETE WHERE pid='${id}'`
const query2 = `ALTER table customEV DELETE WHERE pid='${id}'`
diff --git a/src/project/project.service.ts b/src/project/project.service.ts
index 66585b05f..f5ab43bcc 100644
--- a/src/project/project.service.ts
+++ b/src/project/project.service.ts
@@ -1,4 +1,3 @@
-import { UserType } from './../user/entities/user.entity';
import { ForbiddenException, Injectable, BadRequestException, UnprocessableEntityException, InternalServerErrorException } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
@@ -17,6 +16,7 @@ import * as _includes from 'lodash/includes'
import { Pagination, PaginationOptionsInterface } from '../common/pagination'
import { Project } from './entity/project.entity'
import { ProjectDTO } from './dto/project.dto'
+import { UserType } from './../user/entities/user.entity'
import {
isValidPID, redisProjectCountCacheTimeout, getRedisUserCountKey, redis, clickhouse, isSelfhosted,
} from '../common/constants'
@@ -97,7 +97,7 @@ export class ProjectService {
}
}
- allowedToManage(project: Project, uid: string, roles: Array<string>): void {
+ allowedToManage(project: Project, uid: string, roles: Array<UserType>): void {
if (uid === project.admin.id || _includes(roles, UserType.ADMIN)) {
return
} else {