diff --git a/src/users/controllers/index.ts b/src/users/controllers/index.ts index 291e2fc..f79349e 100644 --- a/src/users/controllers/index.ts +++ b/src/users/controllers/index.ts @@ -1,3 +1,2 @@ export * from './user.controller'; -export * from './user-communities.controller'; -export * from './user-space.controller'; \ No newline at end of file +export * from './user-space.controller'; diff --git a/src/users/controllers/user-communities.controller.ts b/src/users/controllers/user-communities.controller.ts deleted file mode 100644 index 1ebb6cf..0000000 --- a/src/users/controllers/user-communities.controller.ts +++ /dev/null @@ -1,80 +0,0 @@ -import { - Controller, - Delete, - Get, - Param, - Post, - UseGuards, -} from '@nestjs/common'; -import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger'; -import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard'; -import { EnableDisableStatusEnum } from '@app/common/constants/days.enum'; -import { ControllerRoute } from '@app/common/constants/controller-route'; -import { BaseResponseDto } from '@app/common/dto/base.response.dto'; -import { UserCommunityService } from '../services'; -import { UserCommunityParamDto, UserParamDto } from '../dtos'; - -@ApiTags('User Module') -@Controller({ - version: EnableDisableStatusEnum.ENABLED, - path: ControllerRoute.USER_COMMUNITY.ROUTE, -}) -export class UserCommunityController { - constructor(private readonly userCommunityService: UserCommunityService) {} - - @ApiBearerAuth() - @UseGuards(JwtAuthGuard) - @Get() - @ApiOperation({ - summary: - ControllerRoute.USER_COMMUNITY.ACTIONS.GET_USER_COMMUNITIES_SUMMARY, - description: - ControllerRoute.USER_COMMUNITY.ACTIONS.GET_USER_COMMUNITIES_DESCRIPTION, - }) - async getCommunitiesByUserId( - @Param() params: UserParamDto, - ): Promise { - return await this.userCommunityService.getCommunitiesByUserId( - params.userUuid, - ); - } - - @ApiBearerAuth() - @UseGuards(JwtAuthGuard) - @Post(':communityUuid') - @ApiOperation({ - summary: - ControllerRoute.USER_COMMUNITY.ACTIONS.ASSOCIATE_USER_COMMUNITY_SUMMARY, - description: - ControllerRoute.USER_COMMUNITY.ACTIONS - .ASSOCIATE_USER_COMMUNITY_DESCRIPTION, - }) - async associateUserWithCommunity( - @Param() params: UserCommunityParamDto, - ): Promise { - return await this.userCommunityService.associateUserWithCommunity( - params.userUuid, - params.communityUuid, - ); - } - - @ApiBearerAuth() - @UseGuards(JwtAuthGuard) - @ApiOperation({ - summary: - ControllerRoute.USER_COMMUNITY.ACTIONS - .DISASSOCIATE_USER_COMMUNITY_SUMMARY, - description: - ControllerRoute.USER_COMMUNITY.ACTIONS - .DISASSOCIATE_USER_COMMUNITY_DESCRIPTION, - }) - @Delete(':communityUuid') - async disassociateUserFromCommunity( - @Param() params: UserCommunityParamDto, - ): Promise { - return await this.userCommunityService.disassociateUserFromCommunity( - params.userUuid, - params.communityUuid, - ); - } -} diff --git a/src/users/services/index.ts b/src/users/services/index.ts index 755afb6..c22eedd 100644 --- a/src/users/services/index.ts +++ b/src/users/services/index.ts @@ -1,3 +1,2 @@ export * from './user.service'; -export * from './user-community.service'; export * from './user-space.service'; diff --git a/src/users/services/user-community.service.ts b/src/users/services/user-community.service.ts deleted file mode 100644 index 935c675..0000000 --- a/src/users/services/user-community.service.ts +++ /dev/null @@ -1,114 +0,0 @@ -import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; -import { UserRepository } from '@app/common/modules/user/repositories'; -import { BaseResponseDto } from '@app/common/dto/base.response.dto'; -import { SuccessResponseDto } from '@app/common/dto/success.response.dto'; -import { CommunityRepository } from '@app/common/modules/community/repositories'; - -@Injectable() -export class UserCommunityService { - constructor( - private readonly userRepository: UserRepository, - private readonly communityRepository: CommunityRepository, - ) {} - async associateUserWithCommunity( - userUuid: string, - communityUuid: string, - ): Promise { - const { user, community } = await this.getUserAndCommunity( - userUuid, - communityUuid, - ); - // Check if the user is already associated - if (user.communities.some((c) => c.uuid === communityUuid)) { - throw new HttpException( - 'User is already associated with the community', - HttpStatus.BAD_REQUEST, - ); - } - - user.communities.push(community); - await this.userRepository.save(user); - - return new SuccessResponseDto({ - message: `User ${userUuid} successfully associated with community ${communityUuid}`, - }); - } - - async disassociateUserFromCommunity( - userUuid: string, - communityUuid: string, - ): Promise { - const { user } = await this.getUserAndCommunity(userUuid, communityUuid); - - if (!user.communities) { - throw new HttpException( - `User ${userUuid} is not associated with any community`, - HttpStatus.BAD_REQUEST, - ); - } - - const isAssociated = user.communities.some((c) => c.uuid === communityUuid); - - // Check if there is no association - if (!isAssociated) { - throw new HttpException( - `User ${userUuid} is not associated with community ${communityUuid}`, - HttpStatus.BAD_REQUEST, - ); - } - - // Remove the community association - user.communities = user.communities.filter((c) => c.uuid !== communityUuid); - await this.userRepository.save(user); - - return new SuccessResponseDto({ - message: `User ${userUuid} successfully disassociated from community ${communityUuid}`, - }); - } - - async getCommunitiesByUserId(userUuid: string): Promise { - const user = await this.userRepository.findOne({ - where: { uuid: userUuid }, - relations: ['communities'], - }); - - if (!user) { - throw new HttpException( - `User with ID ${userUuid} not found`, - HttpStatus.NOT_FOUND, - ); - } - - if (!user.communities || user.communities.length === 0) { - throw new HttpException( - `User ${userUuid} is not associated with any communities`, - HttpStatus.BAD_REQUEST, - ); - } - - return new SuccessResponseDto({ - data: user.communities, - message: `Communities for user ${userUuid} retrieved successfully`, - }); - } - - private async getUserAndCommunity(userUuid: string, communityUuid: string) { - const user = await this.userRepository.findOne({ - where: { uuid: userUuid }, - relations: ['communities'], - }); - - const community = await this.communityRepository.findOne({ - where: { uuid: communityUuid }, - }); - - if (!user || !community) { - throw new HttpException( - 'User or Community not found', - HttpStatus.NOT_FOUND, - ); - } - - return { user, community }; - } -} diff --git a/src/users/user.module.ts b/src/users/user.module.ts index 61fc323..d65ee68 100644 --- a/src/users/user.module.ts +++ b/src/users/user.module.ts @@ -8,14 +8,14 @@ import { } from '@app/common/modules/user/repositories'; import { RegionRepository } from '@app/common/modules/region/repositories'; import { TimeZoneRepository } from '@app/common/modules/timezone/repositories'; -import { UserCommunityController, UserSpaceController } from './controllers'; +import { UserSpaceController } from './controllers'; import { CommunityModule } from 'src/community/community.module'; -import { UserCommunityService, UserSpaceService } from './services'; +import { UserSpaceService } from './services'; import { CommunityRepository } from '@app/common/modules/community/repositories'; @Module({ imports: [ConfigModule, CommunityModule], - controllers: [UserController, UserSpaceController, UserCommunityController], + controllers: [UserController, UserSpaceController], providers: [ UserService, UserRepository, @@ -23,7 +23,6 @@ import { CommunityRepository } from '@app/common/modules/community/repositories' TimeZoneRepository, UserSpaceRepository, CommunityRepository, - UserCommunityService, UserSpaceService, ], exports: [UserService],