fix: Resolve "Can't delete an account" issue (#9232) (#9238)

### Summary

This pull request addresses the issue described in #9232, where
attempting to delete a user account results in a `TypeError: Cannot read
properties of undefined (reading 'dataSourceService')`.

### Changes Made

- Fixed the `this` context issue in the `deleteUserFromWorkspace` method
by ensuring it is correctly bound.
- Updated the `deleteUser` method to use a bound function when calling
`deleteUserFromWorkspace`.

### Linked Issue

This pull request fixes #9232.

### Additional Notes

- Please review the changes carefully to ensure no unintended side
effects in the user or workspace deletion process.
- Suggestions for further improvement are welcome.

---------

Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com>
Co-authored-by: Weiko <corentin@twenty.com>
This commit is contained in:
Melvin Jariwala
2025-01-02 18:44:28 +05:30
committed by GitHub
parent 2290ed8f87
commit 23499735b4

View File

@ -96,14 +96,14 @@ export class UserService extends TypeOrmQueryService<User> {
assert(workspaceMember, 'WorkspaceMember not found');
if (workspaceMembers.length === 1) {
await this.workspaceService.deleteWorkspace(workspaceId);
}
await workspaceDataSource?.query(
`DELETE FROM ${dataSourceMetadata.schema}."workspaceMember" WHERE "userId" = '${userId}'`,
);
if (workspaceMembers.length === 1) {
await this.workspaceService.deleteWorkspace(workspaceId);
}
const objectMetadata = await this.objectMetadataRepository.findOneOrFail({
where: {
nameSingular: 'workspaceMember',
@ -136,7 +136,9 @@ export class UserService extends TypeOrmQueryService<User> {
userValidator.assertIsDefinedOrThrow(user);
await Promise.all(user.workspaces.map(this.deleteUserFromWorkspace));
await Promise.all(
user.workspaces.map(this.deleteUserFromWorkspace.bind(this)),
);
return user;
}