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 customevents fir views

Yehor Dremliuha94964f83ac09
diff --git a/apps/production/src/analytics/analytics.controller.ts b/apps/production/src/analytics/analytics.controller.ts
index 12a83fb37..6c32c4af2 100644
--- a/apps/production/src/analytics/analytics.controller.ts
+++ b/apps/production/src/analytics/analytics.controller.ts
@@ -408,16 +408,13 @@ export class AnalyticsController {
await this.analyticsService.checkBillingAccess(pid)
// TODO make it smarter
- if (
- viewId &&
- (period || timeBucket || from || to || filters || timezone || mode)
- ) {
+ if (viewId && filters) {
throw new ConflictException('Cannot specify both viewId and filters.')
}
- const view = await this.projectsViewsRepository.findView(viewId)
+ const view = await this.projectsViewsRepository.findProjectView(pid, viewId)
- if (pid !== view.projectId) {
+ if (!view) {
throw new NotFoundException('View not found.')
}
diff --git a/apps/production/src/project/dto/project-view-ids.dto.ts b/apps/production/src/project/dto/project-view-ids.dto.ts
new file mode 100644
index 000000000..c2ddf0c7b
--- /dev/null
+++ b/apps/production/src/project/dto/project-view-ids.dto.ts
@@ -0,0 +1,8 @@
+import { IntersectionType } from '@nestjs/swagger'
+import { ProjectIdDto } from './project-id.dto'
+import { ProjectViewIdDto } from './project-view-id.dto'
+
+export class ProjectViewIdsDto extends IntersectionType(
+ ProjectIdDto,
+ ProjectViewIdDto,
+) {}
diff --git a/apps/production/src/project/entity/project-view-custom-event.entity.ts b/apps/production/src/project/entity/project-view-custom-event.entity.ts
index 67b9e69fc..8185f58fe 100644
--- a/apps/production/src/project/entity/project-view-custom-event.entity.ts
+++ b/apps/production/src/project/entity/project-view-custom-event.entity.ts
@@ -52,8 +52,6 @@ export class ProjectViewCustomEventEntity {
@UpdateDateColumn()
updatedAt: Date
- @ManyToOne(() => ProjectViewEntity, projectView => projectView.id, {
- onDelete: 'CASCADE',
- })
+ @ManyToOne(() => ProjectViewEntity, projectView => projectView.customEvents)
view: ProjectViewEntity
}
diff --git a/apps/production/src/project/entity/project-view.entity.ts b/apps/production/src/project/entity/project-view.entity.ts
index 342edc6f6..19cfb6f00 100644
--- a/apps/production/src/project/entity/project-view.entity.ts
+++ b/apps/production/src/project/entity/project-view.entity.ts
@@ -98,9 +98,10 @@ export class ProjectViewEntity {
@ManyToOne(() => Project, project => project.views, { onDelete: 'CASCADE' })
project: Project
+ @ApiProperty({ type: ProjectViewCustomEventEntity, isArray: true })
@OneToMany(
() => ProjectViewCustomEventEntity,
- projectViewCustomEvent => projectViewCustomEvent.id,
+ projectViewCustomEvent => projectViewCustomEvent.view,
)
customEvents: ProjectViewCustomEventEntity[]
}
diff --git a/apps/production/src/project/project.controller.ts b/apps/production/src/project/project.controller.ts
index e94039e89..e5c6d71ed 100644
--- a/apps/production/src/project/project.controller.ts
+++ b/apps/production/src/project/project.controller.ts
@@ -106,7 +106,7 @@ 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'
+import { ProjectViewIdsDto } from './dto/project-view-ids.dto'
const PROJECTS_MAXIMUM = 50
@@ -1974,7 +1974,7 @@ export class ProjectController {
@UseGuards(JwtAccessTokenGuard, RolesGuard)
@Roles(UserType.CUSTOMER, UserType.ADMIN)
async updateProjectView(
- @Param() params: ProjectIdDto & ProjectViewIdDto,
+ @Param() params: ProjectViewIdsDto,
@Body() body: UpdateProjectViewDto,
@CurrentUserId() userId: string,
) {
@@ -2017,7 +2017,7 @@ export class ProjectController {
@UseGuards(JwtAccessTokenGuard, RolesGuard)
@Roles(UserType.CUSTOMER, UserType.ADMIN)
async deleteProjectView(
- @Param() params: ProjectIdDto & ProjectViewIdDto,
+ @Param() params: ProjectViewIdsDto,
@CurrentUserId() userId: string,
) {
const project = await this.projectService.findProject(params.projectId, [
diff --git a/apps/production/src/project/repositories/projects-views.repository.ts b/apps/production/src/project/repositories/projects-views.repository.ts
index 733cd8f6f..8f955b698 100644
--- a/apps/production/src/project/repositories/projects-views.repository.ts
+++ b/apps/production/src/project/repositories/projects-views.repository.ts
@@ -4,27 +4,47 @@ 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'
+import { ProjectViewCustomEventEntity } from '../entity/project-view-custom-event.entity'
@Injectable()
export class ProjectsViewsRepository {
constructor(
@InjectRepository(ProjectViewEntity)
private viewsRepository: Repository<ProjectViewEntity>,
+ @InjectRepository(ProjectViewCustomEventEntity)
+ private projectViewCustomEventsRepository: Repository<ProjectViewCustomEventEntity>,
) {}
async findViews(projectId: string) {
- return this.viewsRepository.find({ projectId })
+ return this.viewsRepository.find({
+ where: { projectId },
+ relations: ['customEvents'],
+ })
}
async createProjectView(projectId: string, data: CreateProjectViewDto) {
- return this.viewsRepository.save({
+ const view = await this.viewsRepository.save({
project: { id: projectId },
...data,
+ customEvents: data.customEvents,
})
+
+ const customEventPromises = data.customEvents.map(customEvent =>
+ this.projectViewCustomEventsRepository.save({
+ viewId: view.id,
+ ...customEvent,
+ }),
+ )
+ await Promise.all(customEventPromises)
+
+ return view
}
async findView(id: string) {
- return this.viewsRepository.findOne({ where: { id } })
+ return this.viewsRepository.findOne({
+ where: { id },
+ relations: ['customEvents'],
+ })
}
async findProjectView(projectId: string, viewId: string) {