Skip to content

Commit

Permalink
Merge pull request #164 from apurvaubade/DeleteUser
Browse files Browse the repository at this point in the history
Task:PS-372 User: Delete user API- delete user from keycloak,user & related tables
  • Loading branch information
vaivk369 authored May 16, 2024
2 parents b94ee72 + 08a2b5c commit bef8dcc
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/adapters/hasura/user.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -706,4 +706,7 @@ export class HasuraUserService implements IServicelocator {
return e;
}
}

public async deleteUserById(userId){}

}
60 changes: 60 additions & 0 deletions src/adapters/postgres/user-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,66 @@ export class PostgresUserService {
};
}

public async deleteUserById(userId){
const { KEYCLOAK, KEYCLOAK_ADMIN } = process.env;
// Validate userId format
if (!isUUID(userId)) {
return new ErrorResponseTypeOrm({
statusCode: HttpStatus.BAD_REQUEST,
errorMessage: "Please enter a valid UUID for userId",
});
}

try {
// Check if user exists in usersRepository
const user = await this.usersRepository.findOne({ where :{userId:userId}});
if (!user) {
return new ErrorResponseTypeOrm({
statusCode: HttpStatus.NOT_FOUND,
errorMessage: "User not found in user table.",
});
}


// Delete from User table
const userResult = await this.usersRepository.delete(userId);

// Delete from CohortMembers table
const cohortMembersResult = await this.cohortMemberRepository.delete({ userId: userId });

// Delete from UserTenantMapping table
const userTenantMappingResult = await this.userTenantMappingRepository.delete({ userId: userId });

// Delete from UserRoleMapping table
const userRoleMappingResult = await this.userRoleMappingRepository.delete({ userId: userId });

// Delete from FieldValues table where ItemId matches userId
const fieldValuesResult = await this.fieldsValueRepository.delete({ itemId: userId });

const keycloakResponse = await getKeycloakAdminToken();
const token = keycloakResponse.data.access_token;

await this.axios.delete(`${KEYCLOAK}${KEYCLOAK_ADMIN}/${userId}`, {
headers: {
'Authorization': `Bearer ${token}`
}});


return new SuccessResponse({
statusCode: HttpStatus.OK,
message: "User and related entries deleted Successfully.",
data: {
user: userResult
},
});
} catch (e) {
return new ErrorResponseTypeOrm({
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
errorMessage: e,
});
}
}

}


Expand Down
2 changes: 2 additions & 0 deletions src/adapters/userservicelocator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ export interface IServicelocator {
userSearchDto: UserSearchDto
);
resetUserPassword(request: any, username: string, newPassword: string);
deleteUserById(userId: string): Promise<any>;

}
24 changes: 24 additions & 0 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
UsePipes,
ValidationPipe,
HttpStatus,
Delete,
} from "@nestjs/common";

import { Request } from "@nestjs/common";
Expand Down Expand Up @@ -161,4 +162,27 @@ export class UserController {
.buildUserAdapter()
.resetUserPassword(request, reqBody.username, reqBody.newPassword);
}

//delete
@Delete("/:userId")

@ApiBasicAuth("access-token")
@ApiOkResponse({ description: "User deleted successfully" })
@ApiNotFoundResponse({ description: "Data not found" })
@SerializeOptions({
strategy: "excludeAll",
})

public async deleteUserById(
@Headers() headers,
@Param("userId") userId: string,
@Req() request: Request,
@Res() response: Response
) {

const result = await this.userAdapter
.buildUserAdapter()
.deleteUserById(userId);
return response.status(result.statusCode).json(result);
}
}

0 comments on commit bef8dcc

Please sign in to comment.