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: User self delete does not work

Blue Mouseb5ab2f0c9b8f
diff --git a/backend/apps/cloud/src/project/project.service.ts b/backend/apps/cloud/src/project/project.service.ts
index 68d98e254..12caad5dd 100644
--- a/backend/apps/cloud/src/project/project.service.ts
+++ b/backend/apps/cloud/src/project/project.service.ts
@@ -17,6 +17,7 @@ import {
DeepPartial,
Brackets,
FindOptionsWhere,
+ In,
} from 'typeorm'
import handlebars from 'handlebars'
import crypto from 'crypto'
@@ -616,6 +617,42 @@ export class ProjectService implements OnModuleDestroy {
.execute()
}
+ async deleteProjectsForUser(userId: string) {
+ const projects = await this.projectsRepository.find({
+ where: { admin: { id: userId } },
+ select: ['id'],
+ })
+
+ if (_isEmpty(projects)) {
+ return
+ }
+
+ const projectIds = _map(projects, 'id')
+ const queries = [
+ 'ALTER TABLE analytics DELETE WHERE pid IN ({pids:Array(FixedString(12))})',
+ 'ALTER TABLE customEV DELETE WHERE pid IN ({pids:Array(FixedString(12))})',
+ 'ALTER TABLE performance DELETE WHERE pid IN ({pids:Array(FixedString(12))})',
+ 'ALTER TABLE errors DELETE WHERE pid IN ({pids:Array(FixedString(12))})',
+ 'ALTER TABLE error_statuses DELETE WHERE pid IN ({pids:Array(FixedString(12))})',
+ 'ALTER TABLE captcha DELETE WHERE pid IN ({pids:Array(FixedString(12))})',
+ ]
+
+ await Promise.all(
+ _map(queries, (query) =>
+ clickhouse.command({
+ query,
+ query_params: {
+ pids: projectIds,
+ },
+ }),
+ ),
+ )
+
+ await this.deleteMultipleShare({ project: { id: In(projectIds) } })
+ await this.deleteMultiple(projectIds)
+ await deleteProjectsRedis(projectIds)
+ }
+
async deleteMultipleShare(where: FindOptionsWhere<ProjectShare>) {
return this.projectShareRepository.delete(where)
}
diff --git a/backend/apps/cloud/src/user/user.controller.ts b/backend/apps/cloud/src/user/user.controller.ts
index 2e644bfdf..ad3c8bfd1 100644
--- a/backend/apps/cloud/src/user/user.controller.ts
+++ b/backend/apps/cloud/src/user/user.controller.ts
@@ -225,10 +225,7 @@ export class UserController {
try {
if (!_isEmpty(user.projects)) {
- await this.projectService.update(
- { admin: { id } },
- { isArchived: true },
- )
+ await this.projectService.deleteProjectsForUser(id)
}
await this.actionTokensService.deleteMultiple({ user: { id } })
await this.userService.deleteAllRefreshTokens(id)