ok after talking with cherif we decided not to send things like the user.id so i need to recreate more or less what i had before, but i'm gonna keep using the user.id everywhere in the back and keep my better simpler friendship.entity, working on all that now, got a thing that compiles in the front and back, a few more things to fix and then test everything

This commit is contained in:
Me
2023-01-03 19:42:14 +01:00
parent b7b9b3d645
commit 000987fbde
7 changed files with 75 additions and 66 deletions

View File

@@ -2,12 +2,12 @@ import { IsEnum, IsNotEmpty, IsString, IsPositive } from 'class-validator';
import { FriendshipStatus } from '../entities/friendship.entity';
export class CreateFriendshipDto {
@IsPositive()
// @IsPositive()
// @Max(1000) ?
readonly receiverId: number;
// @IsNotEmpty()
// @IsString()
// readonly receiverUsername: string;
// readonly receiverId: number;
@IsNotEmpty()
@IsString()
readonly receiverUsername: string;
@IsEnum(FriendshipStatus)
readonly status: FriendshipStatus;
}

View File

@@ -25,12 +25,10 @@ export class FriendshipController {
@Get('myfriends')
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
findOne(@Query('username') otherUsername: string, @Query('id') id: number, @Req() req) {
findOne(@Query('username') otherUsername: string, @Req() req) {
console.log('GET myfriend')
const user = req.user;
if (id !== undefined) {
return this.friendshipService.findOneRelationshipById(id, user.id)
} else if (otherUsername !== undefined) {
if (otherUsername !== undefined) {
return this.friendshipService.findOneRelationshipByUsername(otherUsername, user.username);
}
// might change this
@@ -59,7 +57,7 @@ export class FriendshipController {
@UseGuards(TwoFactorGuard)
create(@Body() createFriendshipDto: CreateFriendshipDto, @Req() req) {
const user = req.user;
if (user.id !== createFriendshipDto.receiverId)
if (user.username !== createFriendshipDto.receiverUsername)
return this.friendshipService.create(createFriendshipDto, user);
return new HttpException('You can\'t request a frienship to yourself', HttpStatus.BAD_REQUEST);
}
@@ -124,8 +122,11 @@ 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 (relationshipId === undefined)
// if (relationshipId === undefined)
if (Number.isNaN(relationshipId))
return this.friendshipService.findAllBlockedFriends(user.id);
else
return this.friendshipService.findOneBlocked(relationshipId, user.id);

View File

@@ -103,7 +103,7 @@ export class FriendshipService {
// lets see what happens here, doing directly receiver.id not LeftJoinAndSelect ...
async findAllFriends(userId: number) {
const friendship = await this.friendshipRepository
const friendships = await this.friendshipRepository
.createQueryBuilder('friendship')
.leftJoinAndSelect('friendship.sender', 'sender')
.leftJoinAndSelect('friendship.receiver', 'receiver')
@@ -115,7 +115,13 @@ export class FriendshipService {
// for (const friend of friendship)
// console.log("FRIENDSHIP : " + friend.status);
// return friendship;
return new SendableFriendship(friendship);
// return new SendableFriendship(friendship);
let sendFrienships: SendableFriendship[] = []
for (const friendship of friendships) {
sendFrienships.push(new SendableFriendship(friendship));
}
// return new SendableFriendship(friendship);
return sendFrienships;
}
async findOneBlocked(friendshipId: number, userId: number) {
@@ -225,7 +231,7 @@ export class FriendshipService {
async create(createFriendshipDto: CreateFriendshipDto, creator : User) {
console.log("DTO : \n")
console.log({...createFriendshipDto})
const receiver = await this.userRepository.findOneBy({id: createFriendshipDto.receiverId});
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)
@@ -278,7 +284,7 @@ export class FriendshipService {
// console.log({...relation})
if (!relation)
throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
if (relation.senderId === user.id) {
if (relation.sender.id === user.id) {
throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND);
}
relation.status = FriendshipStatus.ACCEPTED;
@@ -301,7 +307,7 @@ export class FriendshipService {
const relation = await this.friendshipRepository.findOneBy({id: relationshipId });
if (!relation)
throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
if (relation.senderId === user.id) {
if (relation.sender.id === user.id) {
throw new HttpException(`You can't decline your own request.`, HttpStatus.NOT_FOUND);
}
relation.status = FriendshipStatus.DECLINED;
@@ -325,14 +331,14 @@ export class FriendshipService {
// do i need to check if they've already been blocked?
if (relation.receiverId === user.id) {
if (relation.receiver.id === user.id) {
// throw new HttpException(`You can't block yourself.`, HttpStatus.NOT_FOUND);
console.log('friendship.service blockFriendship trying to delete and recreate a friendship with block')
console.log({...relation})
// const newFriendshipDto = new CreateFriendshipDto(relation[0].receiver, FriendshipStatus.BLOCKED)
// const newFriendshipDto = new CreateFriendshipDto({"receiverUsername": relation[0].receiver, "status": "R"})
// we create a new one where you are the sender
const newFriendshipDto = {"receiverUsername": relation.senderUsername, "receiverId": relation.senderId, "status": FriendshipStatus.BLOCKED};
const newFriendshipDto = {"receiverUsername": relation.sender.username, "receiverId": relation.sender.id, "status": FriendshipStatus.BLOCKED};
// can't do it this way cuz READONLY
// const newFriendshipDto = new CreateFriendshipDto();
// newFriendshipDto.receiverUsername = relation[0].sender.username;
@@ -366,7 +372,7 @@ export class FriendshipService {
console.log({...user})
if (!friendship)
throw new HttpException(`Your friend could not be deleted.`, HttpStatus.NOT_FOUND);
if (friendship.senderId !== user.id && friendship.receiverId !== user.id) {
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')

View File

@@ -62,15 +62,15 @@ export class UsersController {
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Get()
findOne(@Query('id') toFindId: number, @Req() req) {
console.log('users service findOne toFindId:')
console.log(toFindId)
console.log('users service findOne my Id:')
console.log(req.user.id)
if (toFindId === undefined)
return this.usersService.findOne(req.user.id);
findOne(@Query('username') usernameToFind: string, @Req() req) {
console.log('users service findOne usernameToFind:')
console.log(usernameToFind)
console.log('users service findOne my Username:')
console.log(req.user.username)
if (usernameToFind === undefined)
return this.usersService.findOne(req.user.username);
else
return this.usersService.findOne(toFindId);
return this.usersService.findOne(usernameToFind);
// i would rather just use numbers but i'm guessing Cherif uses this all over
}

View File

@@ -32,11 +32,11 @@ export class UsersService {
return user;
}
async findOne(id: number) {
console.log(`FIND ONE USER SERVICE Find user ${id}`);
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.id = :id', { id: id })
.where('user.username = :username', { username: username })
.getOne();
if (!user)
throw new NotFoundException(`The requested user not found.`);
@@ -53,6 +53,9 @@ export class UsersService {
return partialUser;
}
/***** THIS IS THE THING I REALLY NEED TO FIX!!!!!!! *****/
// Ok this gets called in the Authenitcation Service, but like i was still able to make a username === someone else's
async isUsernameExists(usernameToSearch: string): Promise<boolean> {
const user = await this.userRepository.findOneBy({username : usernameToSearch});
@@ -123,28 +126,28 @@ export class UsersService {
return this.userRepository.save(user);
}
async update(id: string, updateUserDto: UpdateUsersDto) {
console.log(`Update user ${id} with ${updateUserDto.isEnabledTwoFactorAuth}`);
async update(id: number, updateUserDto: UpdateUsersDto) {
// console.log(`Update user ${id} with ${updateUserDto.isEnabledTwoFactorAuth}`);
const user = await this.userRepository.preload(
{id: +id,
{id: id,
...updateUserDto});
if (!user)
throw new HttpException(`The user could not be updated.`,HttpStatus.NOT_FOUND);
return this.userRepository.save(user);
}
async remove(id: string) {
const user = await this.userRepository.findOneBy({id: +id});
async remove(id: number) {
const user = await this.userRepository.findOneBy({id: id});
if (!user)
throw new HttpException(`The user could not be deleted.`,HttpStatus.NOT_FOUND);
return this.userRepository.remove(user);
}
async enableTwoFactorAuth(id: string) {
async enableTwoFactorAuth(id: number) {
return this.userRepository.update(id, {isEnabledTwoFactorAuth: true});
}
async authenticateUserWith2FA(id: string) {
async authenticateUserWith2FA(id: number) {
return this.userRepository.update(id, { isTwoFactorAuthenticated: true})
}