mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-24 06:37:55 +00:00
Added all community endpoints
This commit is contained in:
@@ -1 +1,2 @@
|
||||
export * from './user.controller';
|
||||
export * from './user-communities.controller';
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
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<BaseResponseDto> {
|
||||
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<BaseResponseDto> {
|
||||
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<BaseResponseDto> {
|
||||
return await this.userCommunityService.disassociateUserFromCommunity(
|
||||
params.userUuid,
|
||||
params.communityUuid,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1 +1,3 @@
|
||||
export * from './update.user.dto';
|
||||
export * from './user-community-param.dto';
|
||||
export * from './user-param.dto';
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsUUID } from 'class-validator';
|
||||
import { UserParamDto } from './user-param.dto';
|
||||
|
||||
export class UserCommunityParamDto extends UserParamDto {
|
||||
@ApiProperty({
|
||||
description: 'UUID of the community',
|
||||
example: 'a9b2c7f5-4d6e-423d-a24d-6f9c758b1b92',
|
||||
})
|
||||
@IsUUID()
|
||||
communityUuid: string;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsUUID } from 'class-validator';
|
||||
|
||||
export class UserParamDto {
|
||||
@ApiProperty({
|
||||
description: 'UUID of the user',
|
||||
example: 'e7e8ddf5-3f7d-4e3d-bf3e-8c745b9b7d2c',
|
||||
})
|
||||
@IsUUID()
|
||||
userUuid: string;
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
export * from './user.service';
|
||||
export * from './user-community.service';
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
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<BaseResponseDto> {
|
||||
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<BaseResponseDto> {
|
||||
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<BaseResponseDto> {
|
||||
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 };
|
||||
}
|
||||
}
|
||||
@@ -2,18 +2,28 @@ import { Module } from '@nestjs/common';
|
||||
import { UserService } from './services/user.service';
|
||||
import { UserController } from './controllers/user.controller';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||
import {
|
||||
UserRepository,
|
||||
UserSpaceRepository,
|
||||
} from '@app/common/modules/user/repositories';
|
||||
import { RegionRepository } from '@app/common/modules/region/repositories';
|
||||
import { TimeZoneRepository } from '@app/common/modules/timezone/repositories';
|
||||
import { UserCommunityController } from './controllers';
|
||||
import { CommunityModule } from 'src/community/community.module';
|
||||
import { UserCommunityService } from './services';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule],
|
||||
controllers: [UserController],
|
||||
imports: [ConfigModule, CommunityModule],
|
||||
controllers: [UserController, UserCommunityController],
|
||||
providers: [
|
||||
UserService,
|
||||
UserRepository,
|
||||
RegionRepository,
|
||||
TimeZoneRepository,
|
||||
UserSpaceRepository,
|
||||
CommunityRepository,
|
||||
UserCommunityService,
|
||||
],
|
||||
exports: [UserService],
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user