mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-24 18:47:55 +00:00
removed user community
This commit is contained in:
@@ -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 };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user