-
Notifications
You must be signed in to change notification settings - Fork 1
164 implement user management #172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Bikilaketema
wants to merge
38
commits into
release-02
Choose a base branch
from
164-implement-user-management
base: release-02
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 23 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
3e83779
refactor: improve role comparison logic in RoleGuard
Bikilaketema 7d96024
fix es-lint error
Bikilaketema 8dc7783
add Admin role to seed data
Bikilaketema e7693d8
refactor: remove unused User import in RoleGuard
Bikilaketema b3610a9
Merge branch 'bug/fixed-role-guard-problem' into feature/allow-admin-…
Bikilaketema 6bc26f6
feat: enhance admin access by adding RoleGuard and supporting ADMIN r…
Bikilaketema 43b9f7d
add isActive field to User model with default value true
Bikilaketema 5f0af5a
Merge branch 'feature/added-admin-role-to-seed.ts' into feature/add-g…
Bikilaketema 55c221f
Merge branch 'bug/fixed-role-guard-problem' into feature/add-get-and-…
Bikilaketema 7a8a033
Merge branch 'feature/allow-admin-to-manage-mentors-and-channels' int…
Bikilaketema 5366a8f
feat: add AdminProfile module with controller, service, and DTOs for …
Bikilaketema 418a3d5
refactor: clean up code formatting and improve readability in AdminPr…
Bikilaketema 53f3ae4
fix: improve role guard logic to support multiple user accounts
Bikilaketema 4bafee3
feat: enhance admin profile management with account-based user activa…
Bikilaketema 0cf541c
feat: update User entity to include optional fields and enhance role …
Bikilaketema ce94153
fix: remove debug logging from RoleGuard to clean up code
Bikilaketema bd8fba2
fix: change User entity id field from optional to required
Bikilaketema 918715b
fix: update User entity to remove optional sub field and adjust AuthG…
Bikilaketema b5f4937
feat: enhance RoleGuard to throw exceptions for inactive accounts and…
Bikilaketema 90bccfe
fix: remove debug logging from RoleGuard to improve code clarity
Bikilaketema 3805a22
fix: update route parameter for findAll method in AdminProfileController
Bikilaketema faa2e05
feat: add Role module with controller, service, and DTOs for role man…
Bikilaketema a7dcb2b
feat: implement AdminProfile module with controller, service, DTOs, a…
Bikilaketema 987fcb8
changed adminProfile to profile
Bikilaketema 4765c49
refactor: rename AdminDto to ProfileDto and update roleName to role
Bikilaketema 6d76a6c
fix es lint
Bikilaketema 14c82bb
refactor: remove unnecessary comment from ProfileDto file
Bikilaketema ef70806
refactor: update ProfileService to use ProfileDto and add RoleDto for…
Bikilaketema e0c87ce
refactor: format code in RoleService for consistency
Bikilaketema 7316949
refactor: rename RoleController to RolesController for consistency
Bikilaketema 5e5e9f1
refactor: enhance ProfileService to filter by Admin role in account u…
Bikilaketema b61149e
removed the profile module and moved all logics to the user in admin …
Bikilaketema 285b592
removed the admin profile
Bikilaketema 04a939d
added the owner to returned admins list
Bikilaketema ec8f1b5
change the some to filter on active acccounts
Bikilaketema 3324d52
fixed pr-comments
Bikilaketema 9700cfb
fixed es lint error
Bikilaketema 73fa9c8
dis-allwoed the owner from deactivating it's own account
Bikilaketema File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/modules/admin/adminProfile/admin-profile.controller.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { Test, TestingModule } from '@nestjs/testing'; | ||
| import { AdminProfileController } from './admin-profile.controller'; | ||
| import { AdminProfileService } from './admin-profile.service'; | ||
|
|
||
| describe('AdminProfileController', () => { | ||
| let controller: AdminProfileController; | ||
|
|
||
| beforeEach(async () => { | ||
| const module: TestingModule = await Test.createTestingModule({ | ||
| controllers: [AdminProfileController], | ||
| providers: [AdminProfileService], | ||
| }).compile(); | ||
|
|
||
| controller = module.get<AdminProfileController>(AdminProfileController); | ||
| }); | ||
|
|
||
| it('should be defined', () => { | ||
| expect(controller).toBeDefined(); | ||
| }); | ||
| }); |
49 changes: 49 additions & 0 deletions
49
src/modules/admin/adminProfile/admin-profile.controller.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { Controller, Get, Body, Patch, Param, UseGuards } from '@nestjs/common'; | ||
| import { AuthGuard } from 'src/modules/auth/guard/auth/auth.guard'; | ||
| import { AdminProfileService } from './admin-profile.service'; | ||
| import { UpdateAdminProfileDto } from './dto/update-admin-profile.dto'; | ||
| import { RoleGuard } from 'src/modules/auth/guard/role/role.guard'; | ||
| import { Roles } from '../../auth/auth.decorator'; | ||
|
|
||
| @Controller('admin/profile') | ||
| @UseGuards(AuthGuard) | ||
| export class AdminProfileController { | ||
| constructor(private readonly adminProfileService: AdminProfileService) {} | ||
|
|
||
| @Get(':accountId/:userId/') | ||
| @UseGuards(RoleGuard) | ||
| @Roles('OWNER', 'ADMIN') | ||
| findOne( | ||
| @Param('userId') userId: string, | ||
| @Param('accountId') accountId: string, | ||
| ) { | ||
| return this.adminProfileService.findOne(userId, accountId); | ||
| } | ||
|
|
||
| @Patch(':id') | ||
| @UseGuards(RoleGuard) | ||
| @Roles('OWNER', 'ADMIN') | ||
| update( | ||
| @Param('id') id: string, | ||
| @Body() updateAdminProfileDto: UpdateAdminProfileDto, | ||
| ) { | ||
| return this.adminProfileService.update(id, updateAdminProfileDto); | ||
| } | ||
|
|
||
| @Get(':accountId') | ||
| @UseGuards(RoleGuard) | ||
| @Roles('OWNER') | ||
| findAll(@Param('accountId') accountId) { | ||
| return this.adminProfileService.findAll(accountId); | ||
| } | ||
|
|
||
| @Patch(':userId/activate/:accountId') | ||
| @UseGuards(RoleGuard) | ||
| @Roles('OWNER') | ||
| activate( | ||
| @Param('accountId') accountId: string, | ||
| @Param('userId') userId: string, | ||
| ) { | ||
| return this.adminProfileService.toggleActiveStatus(accountId, userId); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { AdminProfileService } from './admin-profile.service'; | ||
| import { AdminProfileController } from './admin-profile.controller'; | ||
| import { PrismaService } from '../../prisma/prisma.service'; | ||
| import { JwtService } from '@nestjs/jwt'; | ||
|
|
||
| @Module({ | ||
| controllers: [AdminProfileController], | ||
| providers: [AdminProfileService, PrismaService, JwtService], | ||
| }) | ||
| export class AdminProfileModule {} | ||
18 changes: 18 additions & 0 deletions
18
src/modules/admin/adminProfile/admin-profile.service.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { Test, TestingModule } from '@nestjs/testing'; | ||
| import { AdminProfileService } from './admin-profile.service'; | ||
|
|
||
| describe('AdminProfileService', () => { | ||
| let service: AdminProfileService; | ||
|
|
||
| beforeEach(async () => { | ||
| const module: TestingModule = await Test.createTestingModule({ | ||
| providers: [AdminProfileService], | ||
| }).compile(); | ||
|
|
||
| service = module.get<AdminProfileService>(AdminProfileService); | ||
| }); | ||
|
|
||
| it('should be defined', () => { | ||
| expect(service).toBeDefined(); | ||
| }); | ||
| }); |
167 changes: 167 additions & 0 deletions
167
src/modules/admin/adminProfile/admin-profile.service.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| import { Injectable, NotFoundException } from '@nestjs/common'; | ||
| import { PrismaService } from 'src/modules/prisma/prisma.service'; | ||
| import { AdminDto } from './dto/admin.dto'; | ||
| import { UpdateAdminProfileDto } from './dto/update-admin-profile.dto'; | ||
|
|
||
| @Injectable() | ||
| export class AdminProfileService { | ||
| constructor(private prisma: PrismaService) {} | ||
|
|
||
| async findOne(userId: string, accountId: string): Promise<AdminDto> { | ||
| const admin = await this.prisma.user.findFirst({ | ||
| where: { | ||
| id: userId, | ||
| deletedAt: null, | ||
| }, | ||
| include: { | ||
| AccountUser: { | ||
| where: { | ||
| accountId: accountId, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| if (!admin) { | ||
|
||
| throw new NotFoundException('Admin not found'); | ||
| } | ||
|
|
||
| const accountUser = admin.AccountUser.find( | ||
| (au) => au.accountId === accountId, | ||
| ); | ||
|
|
||
| return new AdminDto({ | ||
| id: admin.id, | ||
| name: admin.name, | ||
| email: admin.email, | ||
| isActive: accountUser?.isActive, | ||
| }); | ||
| } | ||
|
|
||
| async update( | ||
| id: string, | ||
| updateAdminProfileDto: UpdateAdminProfileDto, | ||
| ): Promise<AdminDto> { | ||
| console.log('Updating admin with ID:', id); | ||
|
|
||
| const existingAdmin = await this.prisma.user.findFirst({ | ||
| where: { | ||
| id: id, | ||
| deletedAt: null, | ||
| }, | ||
| }); | ||
|
|
||
| if (!existingAdmin) { | ||
| throw new NotFoundException('Admin not found'); | ||
| } | ||
|
|
||
| const updatedAdmin = await this.prisma.user.update({ | ||
| where: { id: id }, | ||
| data: { | ||
| ...updateAdminProfileDto, | ||
| }, | ||
| }); | ||
|
|
||
| console.log('Updated admin:', updatedAdmin); | ||
|
|
||
| return new AdminDto({ ...updatedAdmin }); | ||
| } | ||
|
|
||
| async findAll(accountId: string): Promise<AdminDto[]> { | ||
| const admins = await this.prisma.user.findMany({ | ||
| where: { | ||
| deletedAt: null, | ||
| OR: [ | ||
| { | ||
| AccountUser: { | ||
| some: { | ||
| accountId: accountId, | ||
| Role: { | ||
| name: 'Admin', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| AccountUser: { | ||
| some: { | ||
| accountId: accountId, | ||
| Role: { | ||
| name: 'Owner', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| include: { | ||
| AccountUser: { | ||
| include: { | ||
| Role: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| console.log('Filtered Admin Data:', JSON.stringify(admins, null, 2)); | ||
|
|
||
| return admins.map((admin) => { | ||
| const accountUser = admin.AccountUser.find( | ||
| (au) => au.accountId === accountId, | ||
| ); | ||
|
|
||
| return new AdminDto({ | ||
| id: admin.id, | ||
| name: admin.name, | ||
| email: admin.email, | ||
| isActive: accountUser?.isActive, | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| async toggleActiveStatus( | ||
| accountId: string, | ||
| userId: string, | ||
| ): Promise<AdminDto> { | ||
| const accountUser = await this.prisma.accountUser.findFirst({ | ||
| where: { | ||
| accountId: accountId, | ||
| userId: userId, | ||
| }, | ||
| }); | ||
|
|
||
| const updatedAccountUser = await this.prisma.accountUser.update({ | ||
| where: { | ||
| id: accountUser.id, | ||
| }, | ||
| data: { | ||
| isActive: !accountUser.isActive, | ||
| }, | ||
| }); | ||
|
|
||
| const updatedUser = await this.prisma.user.findFirst({ | ||
| where: { | ||
| id: userId, | ||
| deletedAt: null, | ||
| }, | ||
| include: { | ||
| AccountUser: { | ||
| where: { | ||
| accountId: accountId, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| if (!updatedUser) { | ||
| throw new NotFoundException('User not found'); | ||
| } | ||
|
|
||
| return new AdminDto({ | ||
| id: updatedUser.id, | ||
| name: updatedUser.name, | ||
| email: updatedUser.email, | ||
| isActive: updatedAccountUser.isActive, | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // src/posts/dto/post.dto.ts | ||
|
|
||
| import { Expose } from 'class-transformer'; | ||
|
|
||
| export class AdminDto { | ||
| @Expose() | ||
| id: string; | ||
|
|
||
| @Expose() | ||
| name: string; | ||
|
|
||
| @Expose() | ||
| email: string; | ||
|
|
||
| @Expose() | ||
| isActive: boolean; | ||
|
|
||
| @Expose() | ||
| AccountUser: any; | ||
|
|
||
| @Expose() | ||
| roleName: string; // Add the roleName field | ||
|
|
||
| constructor(partial: Partial<AdminDto>) { | ||
| // console.log('partial', partial); | ||
| Object.assign(this, { | ||
| id: partial.id, | ||
| name: partial.name, | ||
| email: partial.email, | ||
| isActive: partial.isActive, | ||
| AccountUser: partial.AccountUser, | ||
| }); | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/modules/admin/adminProfile/dto/update-admin-profile.dto.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { IsOptional, IsString } from 'class-validator'; | ||
|
|
||
| export class UpdateAdminProfileDto { | ||
| @IsOptional() | ||
| @IsString() | ||
| name?: string; | ||
|
|
||
| @IsOptional() | ||
| @IsString() | ||
| email?: string; | ||
|
|
||
| @IsOptional() | ||
| @IsString() | ||
| password?: string; | ||
| } |
1 change: 1 addition & 0 deletions
1
src/modules/admin/adminProfile/entities/admin-profile.entity.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export class AdminProfile {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export class CreateRoleDto {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { PartialType } from '@nestjs/mapped-types'; | ||
| import { CreateRoleDto } from './create-role.dto'; | ||
|
|
||
| export class UpdateRoleDto extends PartialType(CreateRoleDto) {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this module is under Admin module you dont need to name is as "AdminProfileModule"
rename this module as "ProfileModule"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have removed all the module and moved the logic to the user