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!

cleaning up user's data after deletion of their project / account

Blue Mousea9e0ea70a7ac
diff --git a/src/project/project.controller.ts b/src/project/project.controller.ts
index 42cf6b37b..ff790e408 100644
--- a/src/project/project.controller.ts
+++ b/src/project/project.controller.ts
@@ -19,7 +19,7 @@ import { UserService } from '../user/user.service'
import { ProjectDTO } from './dto/project.dto'
import { AppLoggerService } from '../logger/logger.service'
import {
- redis, isValidPID, getRedisProjectKey, redisProjectCacheTimeout,
+ redis, isValidPID, getRedisProjectKey, redisProjectCacheTimeout, clickhouse,
} from '../common/constants'
@ApiTags('Project')
@@ -146,18 +146,27 @@ export class ProjectController {
this.logger.log({ userId, id }, 'DELETE /project/:id')
if (!isValidPID(id)) throw new BadRequestException('The provided Project ID (pid) is incorrect')
- const project = await this.projectService.findOneWithRelations(id)
+ const project = await this.projectService.findOneWhere({ id }, {
+ relations: ['admin'],
+ select: ['id'],
+ })
if (_isEmpty(project)) {
throw new NotFoundException(`Project with ID ${id} does not exist`)
}
await this.projectService.allowedToManage(project, userId)
+ const query1 = `ALTER table analytics DELETE WHERE pid='${id}'`
+ const query2 = `ALTER table customEV DELETE WHERE pid='${id}'`
+
try {
await this.projectService.delete(id)
+ await clickhouse.query(query1).toPromise()
+ await clickhouse.query(query2).toPromise()
return 'Project deleted successfully'
} catch(e) {
- return e
+ this.logger.error(e)
+ return 'Error while deleting your project'
}
}
}
diff --git a/src/project/project.service.ts b/src/project/project.service.ts
index 2c3128a23..0ecb4a0ad 100644
--- a/src/project/project.service.ts
+++ b/src/project/project.service.ts
@@ -52,6 +52,13 @@ export class ProjectService {
return this.projectsRepository.delete(id)
}
+ async deleteMultiple(pids: string[]): Promise<any> {
+ return this.projectsRepository.createQueryBuilder()
+ .delete()
+ .where(`id IN (${pids})`)
+ .execute()
+ }
+
findOneWithRelations(id: string): Promise<Project | null> {
return this.projectsRepository.findOne(id, { relations: ['admin'] })
}
@@ -68,8 +75,8 @@ export class ProjectService {
return this.projectsRepository.find(params)
}
- findOneWhere(where: Record<string, unknown>): Promise<Project> {
- return this.projectsRepository.findOne({ where })
+ findOneWhere(where: Record<string, unknown>, params: object = {}): Promise<Project> {
+ return this.projectsRepository.findOne({ where, ...params })
}
ifAllowedToManage(userId: string, project: Project): boolean {
diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts
index 0af438f69..a32e10c5c 100644
--- a/src/user/user.controller.ts
+++ b/src/user/user.controller.ts
@@ -8,13 +8,14 @@ import * as utc from 'dayjs/plugin/utc'
import * as _map from 'lodash/map'
import * as _join from 'lodash/join'
import * as _isNull from 'lodash/isNull'
+import * as _isEmpty from 'lodash/isEmpty'
import { UserService } from './user.service'
import { ProjectService } from '../project/project.service'
import { User, UserType, MAX_EMAIL_REQUESTS } from './entities/user.entity'
import { Roles } from '../common/decorators/roles.decorator'
import { Pagination } from '../common/pagination/pagination'
-import { GDPR_EXPORT_TIMEFRAME } from '../common/constants'
+import { GDPR_EXPORT_TIMEFRAME, clickhouse } from '../common/constants'
import { RolesGuard } from 'src/common/guards/roles.guard'
import { UpdateUserProfileDTO } from './dto/update-user.dto'
import { CurrentUserId } from 'src/common/decorators/current-user-id.decorator'
@@ -86,25 +87,59 @@ export class UserController {
@Roles(UserType.ADMIN)
async delete(@Param('id') id: string, @CurrentUserId() uid: string): Promise<any> {
this.logger.log({ id, uid }, 'DELETE /user/:id')
- const userToDelete = await this.userService.findOneWhere({ id })
+ const user = await this.userService.findOne(id, {
+ relations: ['projects'],
+ select: ['id'],
+ })
- if (!userToDelete) {
+ if (_isEmpty(user)) {
throw new BadRequestException(`User with id ${id} does not exist`)
}
-
- await this.userService.delete(id)
- return
+
+ const pids = _join(_map(user.projects, el => `'${el.id}'`), ',')
+ const query1 = `ALTER table analytics DELETE WHERE pid IN (${pids})`
+ const query2 = `ALTER table customEV DELETE WHERE pid IN (${pids})`
+
+ try {
+ await this.projectService.deleteMultiple(pids)
+ await clickhouse.query(query1).toPromise()
+ await clickhouse.query(query2).toPromise()
+ await this.userService.delete(id)
+
+ return 'Account has been deleted'
+ } catch(e) {
+ this.logger.error(e)
+ return 'Error while deleting user account'
+ }
}
@Delete('/')
@HttpCode(204)
@UseGuards(RolesGuard)
@Roles(UserType.CUSTOMER, UserType.ADMIN)
- async deleteSelf(@CurrentUserId() uid: string): Promise<any> {
- this.logger.log({ uid }, 'DELETE /user')
- // TODO: Delete all users projects
- await this.userService.delete(uid)
- return
+ async deleteSelf(@CurrentUserId() id: string): Promise<any> {
+ this.logger.log({ id }, 'DELETE /user')
+
+ const user = await this.userService.findOne(id, {
+ relations: ['projects'],
+ select: ['id'],
+ })
+
+ const pids = _join(_map(user.projects, el => `'${el.id}'`), ',')
+ const query1 = `ALTER table analytics DELETE WHERE pid IN (${pids})`
+ const query2 = `ALTER table customEV DELETE WHERE pid IN (${pids})`
+
+ try {
+ await this.projectService.deleteMultiple(pids)
+ await clickhouse.query(query1).toPromise()
+ await clickhouse.query(query2).toPromise()
+ await this.userService.delete(id)
+
+ return 'Account has been deleted'
+ } catch(e) {
+ this.logger.error(e)
+ return 'Error while deleting your account'
+ }
}
@Post('/confirm_email')
diff --git a/src/user/user.service.ts b/src/user/user.service.ts
index a99672130..cf6d138a1 100644
--- a/src/user/user.service.ts
+++ b/src/user/user.service.ts
@@ -42,8 +42,10 @@ export class UserService {
return this.usersRepository.findOne({ where, relations })
}
- findOne(id: string): Promise<User> {
- return this.findOneWhere({ id })
+ findOne(id: string, params: object = {}): Promise<User> {
+ return this.usersRepository.findOne({
+ where: { id }, ...params,
+ })
}
findOneWithRelations(id: string, relations: string[]): Promise<User> {