mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-23 10:37:56 +00:00
removed user community
This commit is contained in:
@@ -1,3 +1,2 @@
|
||||
export * from './user.controller';
|
||||
export * from './user-communities.controller';
|
||||
export * from './user-space.controller';
|
||||
export * from './user-space.controller';
|
||||
|
||||
@@ -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<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,3 +1,2 @@
|
||||
export * from './user.service';
|
||||
export * from './user-community.service';
|
||||
export * from './user-space.service';
|
||||
|
||||
@@ -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<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 };
|
||||
}
|
||||
}
|
||||
@@ -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],
|
||||
|
||||
Reference in New Issue
Block a user