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!

Add endpoint to update views

Yehor Dremliuha8519c4747e9c
diff --git a/apps/production/src/project/dto/project-view-id.dto.ts b/apps/production/src/project/dto/project-view-id.dto.ts
new file mode 100644
index 000000000..6d9085dfb
--- /dev/null
+++ b/apps/production/src/project/dto/project-view-id.dto.ts
@@ -0,0 +1,9 @@
+import { ApiProperty } from '@nestjs/swagger'
+import { IsNotEmpty, IsUUID } from 'class-validator'
+
+export class ProjectViewIdDto {
+ @ApiProperty()
+ @IsUUID('4')
+ @IsNotEmpty()
+ readonly viewId: string
+}
diff --git a/apps/production/src/project/dto/update-project-view.dto.ts b/apps/production/src/project/dto/update-project-view.dto.ts
new file mode 100644
index 000000000..cbf5ae42d
--- /dev/null
+++ b/apps/production/src/project/dto/update-project-view.dto.ts
@@ -0,0 +1,6 @@
+import { PartialType, OmitType } from '@nestjs/swagger'
+import { CreateProjectViewDto } from './create-project-view.dto'
+
+export class UpdateProjectViewDto extends PartialType(
+ OmitType(CreateProjectViewDto, ['type', 'customEvents'] as const),
+) {}
diff --git a/apps/production/src/project/project.controller.ts b/apps/production/src/project/project.controller.ts
index 9a104d3c9..fdf925e92 100644
--- a/apps/production/src/project/project.controller.ts
+++ b/apps/production/src/project/project.controller.ts
@@ -104,6 +104,8 @@ import { ProjectsViewsRepository } from './repositories/projects-views.repositor
import { ProjectViewEntity } from './entity/project-view.entity'
import { ProjectIdDto } from './dto/project-id.dto'
import { CreateProjectViewDto } from './dto/create-project-view.dto'
+import { UpdateProjectViewDto } from './dto/update-project-view.dto'
+import { ProjectViewIdDto } from './dto/project-view-id.dto'
const PROJECTS_MAXIMUM = 50
@@ -1963,4 +1965,46 @@ export class ProjectController {
return this.projectsViewsRepository.findViews(params.projectId)
}
+
+ @ApiOperation({ summary: 'Update project view' })
+ @ApiOkResponse({ type: ProjectViewEntity })
+ @ApiBearerAuth()
+ @Patch(':projectId/views/:viewId')
+ @UseGuards(JwtAccessTokenGuard, RolesGuard)
+ @Roles(UserType.CUSTOMER, UserType.ADMIN)
+ async updateProjectView(
+ @Param() params: ProjectIdDto & ProjectViewIdDto,
+ @Body() body: UpdateProjectViewDto,
+ @CurrentUserId() userId: string,
+ ) {
+ const project = await this.projectService.findProject(params.projectId, [
+ 'admin',
+ 'share',
+ ])
+
+ if (!project) {
+ throw new NotFoundException('Project not found.')
+ }
+
+ const user = await this.userService.findUserV2(userId, ['roles'])
+
+ if (!user) {
+ throw new NotFoundException('User not found.')
+ }
+
+ this.projectService.allowedToManage(project, userId, user.roles)
+
+ const view = await this.projectsViewsRepository.findProjectView(
+ params.projectId,
+ params.viewId,
+ )
+
+ if (!view) {
+ throw new NotFoundException('View not found.')
+ }
+
+ await this.projectsViewsRepository.updateProjectView(params.viewId, body)
+
+ return this.projectsViewsRepository.findView(params.viewId)
+ }
}
diff --git a/apps/production/src/project/repositories/projects-views.repository.ts b/apps/production/src/project/repositories/projects-views.repository.ts
index 5de30ec9b..cca32252c 100644
--- a/apps/production/src/project/repositories/projects-views.repository.ts
+++ b/apps/production/src/project/repositories/projects-views.repository.ts
@@ -3,6 +3,7 @@ import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import { ProjectViewEntity } from '../entity/project-view.entity'
import { CreateProjectViewDto } from '../dto/create-project-view.dto'
+import { UpdateProjectViewDto } from '../dto/update-project-view.dto'
@Injectable()
export class ProjectsViewsRepository {
@@ -23,6 +24,14 @@ export class ProjectsViewsRepository {
}
async findView(id: string) {
- return this.viewsRepository.findOne({ id })
+ return this.viewsRepository.findOne({ where: { id } })
+ }
+
+ async findProjectView(projectId: string, viewId: string) {
+ return this.viewsRepository.findOne({ where: { id: viewId, projectId } })
+ }
+
+ async updateProjectView(id: string, data: UpdateProjectViewDto) {
+ await this.viewsRepository.update({ id }, data)
}
}