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!

Fix broken view update / delete if there's a custom metric defined in it

Blue Mousee59490cc46a5
diff --git a/apps/production/src/project/project.controller.ts b/apps/production/src/project/project.controller.ts
index ca3629777..8c400eeb1 100644
--- a/apps/production/src/project/project.controller.ts
+++ b/apps/production/src/project/project.controller.ts
@@ -2003,7 +2003,9 @@ export class ProjectController {
const createdProjectView =
await this.projectsViewsRepository.createProjectView(params.projectId, {
- ...body,
+ type: body.type,
+ customEvents: body.customEvents,
+ name: body.name,
filters: JSON.stringify(
this.projectService.filterUnsupportedColumns(body.filters),
),
@@ -2081,7 +2083,8 @@ export class ProjectController {
}
await this.projectsViewsRepository.updateProjectView(params.viewId, {
- ...body,
+ name: body.name,
+ customEvents: body.customEvents,
filters: JSON.stringify(
this.projectService.filterUnsupportedColumns(body.filters),
),
diff --git a/apps/production/src/project/repositories/projects-views.repository.ts b/apps/production/src/project/repositories/projects-views.repository.ts
index 8e1e87fb4..a31cabc35 100644
--- a/apps/production/src/project/repositories/projects-views.repository.ts
+++ b/apps/production/src/project/repositories/projects-views.repository.ts
@@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import * as _map from 'lodash/map'
+import * as _isEmpty from 'lodash/isEmpty'
import { ProjectViewEntity } from '../entity/project-view.entity'
import { CreateProjectViewDto, Filter } from '../dto/create-project-view.dto'
import { UpdateProjectViewDto } from '../dto/update-project-view.dto'
@@ -38,12 +39,13 @@ export class ProjectsViewsRepository {
if (data.customEvents) {
const customEventPromises = data.customEvents.map(customEvent => {
- // @ts-expect-error - mass assignment
- delete customEvent.id
-
return this.projectViewCustomEventsRepository.save({
viewId: view.id,
- ...customEvent,
+ customEventName: customEvent.customEventName,
+ metaKey: customEvent.metaKey,
+ metaValue: customEvent.metaValue,
+ metaValueType: customEvent.metaValueType,
+ metricKey: customEvent.metricKey,
})
})
await Promise.all(customEventPromises)
@@ -98,10 +100,35 @@ export class ProjectsViewsRepository {
id: string,
data: Omit<UpdateProjectViewDto, 'filters'> & { filters: string },
) {
- await this.viewsRepository.update({ id }, data)
+ // doing this instead of calling .update() method to prevent
+ // "Error: Cannot query across one-to-many for property customEvents"
+ const entity = await this.viewsRepository.findOne({
+ where: { id },
+ relations: ['customEvents'],
+ })
+
+ Object.assign(entity, data)
+
+ await this.viewsRepository.save(entity)
+
+ if (!_isEmpty(entity.customEvents)) {
+ const customEventPromises = entity.customEvents.map(customEvent => {
+ return this.projectViewCustomEventsRepository.save({
+ id: customEvent.id,
+ viewId: id,
+ customEventName: customEvent.customEventName,
+ metaKey: customEvent.metaKey,
+ metaValue: customEvent.metaValue,
+ metaValueType: customEvent.metaValueType,
+ metricKey: customEvent.metricKey,
+ })
+ })
+ await Promise.all(customEventPromises)
+ }
}
async deleteProjectView(id: string) {
+ await this.projectViewCustomEventsRepository.delete({ viewId: id })
await this.viewsRepository.delete({ id })
}
}