Added all community endpoints

This commit is contained in:
hannathkadher
2024-10-17 10:55:26 +04:00
parent b8d4a080ef
commit d6777c8ec1
17 changed files with 333 additions and 126 deletions
+1
View File
@@ -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 };
}
}