cleaned up everything, basically all comments and console.logs are gone

This commit is contained in:
Me
2023-01-17 21:29:41 +01:00
parent 11f54bde83
commit 3b72079663
16 changed files with 10 additions and 194 deletions

View File

@@ -8,17 +8,13 @@ import { FriendshipService } from './friendship.service';
export class FriendshipController {
constructor(private readonly friendshipService: FriendshipService) { }
// new and improved finder of people
// GET http://transcendance:8080/api/v2/network/myfriends
@Get('myfriends')
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
findOne(@Query('username') otherUsername: string, @Req() req) {
// console.log('GET myfriends')
const user = req.user;
if (otherUsername !== undefined) {
// console.log('my friend: ' + otherUsername)
return this.friendshipService.findOneRelationshipByUsername(otherUsername, user.username);
}
return this.friendshipService.findAllFriendships(user.id);
@@ -30,10 +26,8 @@ export class FriendshipController {
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
create(@Body() createFriendshipDto: CreateFriendshipDto, @Req() req) {
// console.log('friendship.service create')
const user = req.user;
if (user.username !== createFriendshipDto.receiverUsername) {
// console.log('friendship.service create have a receiver name')
return this.friendshipService.create(createFriendshipDto, user);
}
return new HttpException('You can\'t request a frienship to yourself', HttpStatus.BAD_REQUEST);
@@ -72,7 +66,6 @@ export class FriendshipController {
@UseGuards(TwoFactorGuard)
remove(@Param('relationshipId') relationshipId: number, @Req() req) {
const user : User = req.user;
// console.log('deleting a friendship')
return this.friendshipService.removeFriendship(relationshipId, user);
}
@@ -99,8 +92,6 @@ export class FriendshipController {
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
findBlocked(@Query('relationshipId') relationshipId: number, @Req() req) {
// console.log('friendship.controller fetching blocked users')
// console.log(relationshipId)
const user = req.user;
if (Number.isNaN(relationshipId))
return this.friendshipService.findAllBlockedFriends(user.id);

View File

@@ -16,7 +16,6 @@ export class FriendshipService {
private readonly userRepository: Repository<User>,
) { }
//kinda useless but someone else might use it
async findOneRelationshipById(friendId : number, userId : number) {
const friendship = await this.friendshipRepository
.createQueryBuilder('friendship')
@@ -39,12 +38,8 @@ export class FriendshipService {
)
}),
)
// .andWhere('friendship.status != :status', {status : FriendshipStatus.BLOCKED})
.getOne()
// console.log('END Find one friend by ID: ')
// console.log({...friendship})
if (!friendship) {
throw new HttpException(`There is no such friendship`, HttpStatus.NOT_FOUND);
}
@@ -52,7 +47,6 @@ export class FriendshipService {
}
async findOneRelationshipByUsername(friendUsername : string, username : string) {
// This seems to work, finding friendships by username but checking the actual users not the friendship
const friendship = await this.friendshipRepository
.createQueryBuilder('friendship')
.leftJoinAndSelect('friendship.sender', 'sender')
@@ -76,9 +70,6 @@ export class FriendshipService {
)
.getOne()
// console.log('END Find one friend by username: ')
// console.log({...friendship})
if (!friendship) {
throw new HttpException(`There is no such friendship`, HttpStatus.NOT_FOUND);
}
@@ -100,8 +91,6 @@ export class FriendshipService {
for (const friendship of friendships) {
sendFrienships.push(new SendableFriendship(friendship));
}
// console.log('friendship.service my friends:')
// console.log({...sendFrienships})
return sendFrienships;
}
@@ -142,16 +131,6 @@ export class FriendshipService {
.where('sender.id = :requestee', { requestee: userId })
.andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED })
.getMany();
// let partialFriendship : Partial<Friendship>[] = [];
// for (const friendship of friendships) {
// partialFriendship.push({id: friendship.id, date: friendship.date, senderUsername: friendship.senderUsername, receiverUsername: friendship.receiverUsername, status: friendship.status});
// }
// console.log('friendship.service findAllBlockedFriends, friendships:')
// console.log({...friendships})
// console.log('friendship.service findAllBlockedFriends, partial friendship:')
// return partialFriendship;
let sendFrienships: SendableFriendship[] = []
for (const friendship of friendships) {
sendFrienships.push(new SendableFriendship(friendship));
@@ -167,10 +146,6 @@ export class FriendshipService {
.where('sender.id = :requestee', { requestee: userId })
.andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED })
.getMany();
// console.log('friendships services pendant friendships:')
// console.log({...friendships})
let sendFrienships: SendableFriendship[] = []
for (const friendship of friendships) {
sendFrienships.push(new SendableFriendship(friendship));
@@ -186,10 +161,6 @@ export class FriendshipService {
.where('receiver.id = :addressee', { addressee: userId })
.andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED })
.getMany();
// console.log('friendship service received requests')
// console.log({...friendships})
let sendFrienships: SendableFriendship[] = []
for (const friendship of friendships) {
sendFrienships.push(new SendableFriendship(friendship));
@@ -198,15 +169,11 @@ export class FriendshipService {
}
async create(createFriendshipDto: CreateFriendshipDto, creator : User) {
// console.log("DTO : \n")
// console.log({...createFriendshipDto})
const receiver = await this.userRepository.findOneBy({username: createFriendshipDto.receiverUsername});
if (!receiver)
throw new HttpException(`The addressee does not exist.`, HttpStatus.NOT_FOUND);
if (createFriendshipDto.status !== FriendshipStatus.REQUESTED && createFriendshipDto.status !== FriendshipStatus.BLOCKED)
throw new HttpException(`The status is not valid.`, HttpStatus.NOT_FOUND);
const friendship = await this.friendshipRepository
.createQueryBuilder('friendship')
@@ -218,21 +185,13 @@ export class FriendshipService {
.andWhere('receiver.id = :receiverId2', {receiverId2: creator.id})
.getOne();
// console.log('friendship.service create, friendship: \n\n\n')
// console.log({...friendship})
if (friendship) {
// console.log('there is already a friend request')
if (friendship.status && friendship.status === FriendshipStatus.ACCEPTED)
throw new HttpException(`The friendship request has already been accepted.`, HttpStatus.OK);
else if (friendship.status && friendship.status === FriendshipStatus.REQUESTED) {
if (friendship && friendship.sender && friendship.sender.id === creator.id) {
throw new HttpException(`The friendship request has already been sent the ${friendship.date}.`, HttpStatus.OK);
} else {
// console.log('the friend request is being updated to Accepted')
friendship.status = FriendshipStatus.ACCEPTED;
const saveAFriendship = await this.friendshipRepository.save(friendship);
return new SendableFriendship(saveAFriendship);
@@ -242,7 +201,6 @@ export class FriendshipService {
else if (friendship.status && friendship.status === FriendshipStatus.DECLINED)
throw new HttpException(`The request has been declined.`, HttpStatus.OK);
}
// console.log('friendship.service create, we are still saving a new friendship')
const newFriendship = new Friendship();
newFriendship.sender = creator;
@@ -259,10 +217,6 @@ export class FriendshipService {
.leftJoinAndSelect('friendship.receiver', 'receiver')
.where('friendship.id = :friendshipId', { friendshipId: relationshipId })
.getOne();
// console.log('.service accept friendship')
// console.log({...relation})
if (!relation)
throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
if (relation.sender.id === user.id) {
@@ -304,8 +258,6 @@ export class FriendshipService {
// in the case where you RECEIVED the friendship but now want to block that person
if (relation.receiver && relation.receiver.id === user.id) {
// console.log('friendship.service blockFriendship trying to delete and recreate a friendship with block')
// console.log({...relation})
const newFriendshipDto = {"receiverUsername": relation.sender.username, "status": FriendshipStatus.BLOCKED};
await this.removeFriendship(relationshipId, user);
return await this.create(newFriendshipDto, user);
@@ -328,17 +280,10 @@ export class FriendshipService {
if (friendship.sender.id !== user.id && friendship.receiver.id !== user.id) {
throw new HttpException(`You can't do that.`, HttpStatus.FORBIDDEN);
}
// console.log('.service deleted a friendship')
return this.friendshipRepository.remove(friendship);
}
async findIfUserIsBlockedOrHasBlocked(userConnectedId: number, userToFindId: number) {
// console.log("finding if user is blocked")
// console.log('user connected: ' + userConnectedId)
// console.log('user to find: ' + userToFindId)
const friendship = await this.friendshipRepository
.createQueryBuilder('friendship')
.leftJoinAndSelect('friendship.sender', 'sender')
@@ -362,17 +307,11 @@ export class FriendshipService {
.andWhere('friendship.status = :status', {status : FriendshipStatus.BLOCKED})
.getOne()
// console.log('printing friendship queried')
// console.log({...friendship})
if (friendship) {
console.log('we are blocked in friendship.service')
return true;
}
// console.log('we are not blocked in friendship service')
return false;
}
}

View File

@@ -1,11 +1,6 @@
import { Friendship, FriendshipStatus } from "./entities/friendship.entity";
// Ok i don't really know what i'm doing but i want a thing that is typeset that i can use to send info back to the Front when they create a friendship or ask for a friendship
// Ok it doesn't seem like i really need an interface, that just enfoces the types
// export interface SendableFriendship {
export class SendableFriendship {
id: number;
date: Date;
@@ -13,7 +8,6 @@ export class SendableFriendship {
receiverUsername: string;
status: FriendshipStatus;
// if my constructor is here won't it get sent all over the place too?
constructor(friendship: Friendship) {
this.id = friendship.id;
this.date = friendship.date

View File

@@ -8,7 +8,6 @@ export class SendableUser {
status: string;
stats: UserStats;
// if my constructor is here won't it get sent all over the place too?
constructor(user: User) {
this.username = user.username;
this.image_url = user.image_url;

View File

@@ -4,11 +4,9 @@ import { User } from './entities/user.entity';
import { Repository, Not } from 'typeorm';
import { CreateUsersDto } from './dto/create-users.dto';
import { UpdateUsersDto } from './dto/update-users.dto';
import { PaginationQueryDto } from 'src/common/dto/pagination-query.dto';
import { UserStats } from './entities/userStat.entities';
import { FriendshipService } from 'src/friendship/friendship.service';
// On va devoir sûrement trouver un moyen plus simple pour passer l'id, sûrement via des pipes
// ou des interceptors, mais pour l'instant on va faire comme ça.
@Injectable()
export class UsersService {
@@ -22,16 +20,12 @@ export class UsersService {
const user = await this.userRepository.findOneBy({fortyTwoId: fortytwo_id});
if (!user)
{
// console.log(`The requested user not found.`);
return null;
}
// console.log(`The requested user found.`);
return user;
}
async findOne(username: string) {
// console.log(`FIND ONE USER SERVICE Find user ${username}`);
const user = await this.userRepository.createQueryBuilder('user')
.leftJoinAndSelect('user.stats', 'stats')
.where('user.username = :username', { username: username })
@@ -39,8 +33,6 @@ export class UsersService {
if (!user)
throw new NotFoundException(`The requested user not found.`);
// console.log(`FIND ONE USER SERVICE The requested user found. ` + user.username + " " + user.stats.id + user.stats.winGame + user.stats.loseGame + user.stats.drawGame + user.stats.totalGame);
const partialUser : Partial<User> = {
username: user.username,
image_url: user.image_url,
@@ -48,9 +40,6 @@ export class UsersService {
status: user.status,
stats: user.stats,
};
// console.log(`Returned Partial User.` + partialUser.username + user.username);
return partialUser;
}
@@ -69,26 +58,14 @@ export class UsersService {
let partialUsers : Partial<User>[] = [];
for (const otherUser of otherUsers) {
// console.log('other user: ')
// console.log({...otherUser})
// let tmp = await this.friendshipService.findIfUserIsBlockedOrHasBlocked(currentUser.id, otherUser.id);
// console.log('user.services findIF Blocked... : ')
// console.log(tmp)
// if (tmp === false) {
if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(currentUser.id, otherUser.id) === false) {
partialUsers.push({username: otherUser.username, image_url: otherUser.image_url, status: otherUser.status, stats: otherUser.stats});
}
}
// console.log('user.services findAll, partialUsers:')
// console.log({...partialUsers})
return partialUsers;
}
async create(createUserDto: CreateUsersDto) {
// console.log('\nuser.services create a new user, createUserDto: ')
// console.log({...createUserDto})
if (await this.userRepository.findOneBy({fortyTwoId: createUserDto.fortyTwoId}))
throw new HttpException(`The user already exists.`,HttpStatus.CONFLICT);
const user = this.userRepository.create(createUserDto);
@@ -101,7 +78,6 @@ export class UsersService {
async update(id: number, updateUserDto: UpdateUsersDto, username : string) {
console.log("Maj user username : " + username + " updateuser dto " + updateUserDto.username )
if (await this.isUsernameExists(updateUserDto.username) === true && updateUserDto.username !== username) {
// console.log('updating username ' + updateUserDto.username + ' but it already is in use')
throw new HttpException(`The username is already in use.`,HttpStatus.CONFLICT);
}
const user = await this.userRepository.preload(
@@ -143,15 +119,12 @@ export class UsersService {
return this.userRepository.update(id, {image_url: avatar});
}
// doing search with username not id because userService.findOne doesn't return an Id anymore, just username... fuck this architecture is big trash...
async getAvatarUrl(username: string) {
const user = await this.userRepository.findOneBy({username: username});
if (!user)
throw new HttpException(`The user could not be found.`,HttpStatus.NOT_FOUND);
// console.log('user.service getAvatarUrl of ' + user.username)
if (!user.image_url)
throw new HttpException(`The user has no avatar.`,HttpStatus.NOT_FOUND);
// console.log('User.service Avatar URL ' + user.image_url);
return user.image_url;
}