From 230ed4eac1f69118811abb2202c00ff5eae7f825 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Tue, 16 Apr 2024 15:33:37 +0300 Subject: [PATCH] Add UserSpaceRepositoryModule and UserSpaceRepository to CommunityModule --- src/community/community.module.ts | 16 +++++++- .../controllers/community.controller.ts | 22 ++++++++++- src/community/dtos/get.community.dto.ts | 10 +++++ .../interface/community.interface.ts | 6 +++ src/community/services/community.service.ts | 37 +++++++++++++++++++ 5 files changed, 88 insertions(+), 3 deletions(-) diff --git a/src/community/community.module.ts b/src/community/community.module.ts index 36b9da2..de5a7c0 100644 --- a/src/community/community.module.ts +++ b/src/community/community.module.ts @@ -6,11 +6,23 @@ import { SpaceRepositoryModule } from '@app/common/modules/space/space.repositor import { SpaceRepository } from '@app/common/modules/space/repositories'; import { SpaceTypeRepositoryModule } from '@app/common/modules/space-type/space.type.repository.module'; import { SpaceTypeRepository } from '@app/common/modules/space-type/repositories'; +import { UserSpaceRepositoryModule } from '@app/common/modules/user-space/user.space.repository.module'; +import { UserSpaceRepository } from '@app/common/modules/user-space/repositories'; @Module({ - imports: [ConfigModule, SpaceRepositoryModule, SpaceTypeRepositoryModule], + imports: [ + ConfigModule, + SpaceRepositoryModule, + SpaceTypeRepositoryModule, + UserSpaceRepositoryModule, + ], controllers: [CommunityController], - providers: [CommunityService, SpaceRepository, SpaceTypeRepository], + providers: [ + CommunityService, + SpaceRepository, + SpaceTypeRepository, + UserSpaceRepository, + ], exports: [CommunityService], }) export class CommunityModule {} diff --git a/src/community/controllers/community.controller.ts b/src/community/controllers/community.controller.ts index f889e13..ea9805c 100644 --- a/src/community/controllers/community.controller.ts +++ b/src/community/controllers/community.controller.ts @@ -10,11 +10,15 @@ import { Put, Query, UseGuards, + ValidationPipe, } from '@nestjs/common'; import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard'; import { AddCommunityDto } from '../dtos/add.community.dto'; -import { GetCommunityChildDto } from '../dtos/get.community.dto'; +import { + GetCommunitiesByUserIdDto, + GetCommunityChildDto, +} from '../dtos/get.community.dto'; import { UpdateCommunityNameDto } from '../dtos/update.community.dto'; @ApiTags('Community Module') @@ -77,6 +81,22 @@ export class CommunityController { } } + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Get() + async getCommunitiesByUserId( + @Query(ValidationPipe) dto: GetCommunitiesByUserIdDto, + ) { + try { + return await this.communityService.getCommunitiesByUserId(dto.userUuid); + } catch (error) { + throw new HttpException( + error.message || 'Internal server error', + error.status || HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Put('rename/:communityUuid') diff --git a/src/community/dtos/get.community.dto.ts b/src/community/dtos/get.community.dto.ts index be614e5..5d0e08c 100644 --- a/src/community/dtos/get.community.dto.ts +++ b/src/community/dtos/get.community.dto.ts @@ -49,3 +49,13 @@ export class GetCommunityChildDto { }) public includeSubSpaces: boolean = false; } + +export class GetCommunitiesByUserIdDto { + @ApiProperty({ + description: 'userUuid', + required: true, + }) + @IsString() + @IsNotEmpty() + public userUuid: string; +} diff --git a/src/community/interface/community.interface.ts b/src/community/interface/community.interface.ts index ca18aca..31c0579 100644 --- a/src/community/interface/community.interface.ts +++ b/src/community/interface/community.interface.ts @@ -18,3 +18,9 @@ export interface RenameCommunityByUuidInterface { name: string; type: string; } + +export interface GetCommunityByUserUuidInterface { + uuid: string; + name: string; + type: string; +} diff --git a/src/community/services/community.service.ts b/src/community/services/community.service.ts index 5e9b7ea..1c3109a 100644 --- a/src/community/services/community.service.ts +++ b/src/community/services/community.service.ts @@ -10,17 +10,20 @@ import { SpaceRepository } from '@app/common/modules/space/repositories'; import { AddCommunityDto } from '../dtos'; import { CommunityChildInterface, + GetCommunityByUserUuidInterface, GetCommunityByUuidInterface, RenameCommunityByUuidInterface, } from '../interface/community.interface'; import { SpaceEntity } from '@app/common/modules/space/entities'; import { UpdateCommunityNameDto } from '../dtos/update.community.dto'; +import { UserSpaceRepository } from '@app/common/modules/user-space/repositories'; @Injectable() export class CommunityService { constructor( private readonly spaceRepository: SpaceRepository, private readonly spaceTypeRepository: SpaceTypeRepository, + private readonly userSpaceRepository: UserSpaceRepository, ) {} async addCommunity(addCommunityDto: AddCommunityDto) { @@ -151,6 +154,40 @@ export class CommunityService { return childHierarchies; } + + async getCommunitiesByUserId( + userUuid: string, + ): Promise { + try { + const communities = await this.userSpaceRepository.find({ + relations: ['space', 'space.spaceType'], + where: { + user: { uuid: userUuid }, + space: { spaceType: { type: 'community' } }, + }, + }); + + if (communities.length === 0) { + throw new HttpException( + 'this user has no communities', + HttpStatus.NOT_FOUND, + ); + } + const spaces = communities.map((community) => ({ + uuid: community.space.uuid, + name: community.space.spaceName, + type: community.space.spaceType.type, + })); + + return spaces; + } catch (err) { + if (err instanceof HttpException) { + throw err; + } else { + throw new HttpException('user not found', HttpStatus.NOT_FOUND); + } + } + } async renameCommunityByUuid( communityUuid: string, updateCommunityDto: UpdateCommunityNameDto,