Query Builder, more specifically an orWhere, is being difficult... hard to test the friendships flow in the frontend without it, still have a good bit of work as a result

This commit is contained in:
Me
2022-12-15 05:42:09 +01:00
parent 6eaf28d4d8
commit db02946268
3 changed files with 153 additions and 14 deletions

View File

@@ -82,6 +82,19 @@ export class FriendshipController {
return this.friendshipService.acceptFriendship(relationshipId, user);
}
// TEST !!!!!!!!!!
@Get('myfriends/:relationshipId/test')
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
test(@Param('relationshipId') relationshipId: string, @Req() req)
{
const user : User = req.user;
console.log('testing a friendship MMMMMMMMMMMMMMMMM')
return this.friendshipService.testFriendship(relationshipId, user);
}
@Patch('myfriends/:relationshipId/decline')
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)

View File

@@ -29,26 +29,30 @@ export class FriendshipService {
.where(
new Brackets((qb) => {
qb.where(
new Brackets((subQb) => {
subQb.where('friendship.senderUsername = :username', {username : username})
new Brackets((subAQb) => {
subAQb.where('friendship.senderUsername = :username', {username : username})
.andWhere('friendship.receiverUsername = :friendUsername', {friendUsername : friendUsername})
})
)
.orWhere(
new Brackets((subQb) => {
subQb.where('friendship.senderUsername = :friendUsername', {friendUsername : friendUsername})
new Brackets((subBQb) => {
subBQb.where('friendship.senderUsername = :friendUsername', {friendUsername : friendUsername})
.andWhere('friendship.receiverUsername = :username', {username : username})
})
)
}),
)
.andWhere(
new Brackets((qb) => {
qb.where('friendship.status = :status', {status : FriendshipStatus.ACCEPTED})
.orWhere('friendship.status = :status', {status : FriendshipStatus.REQUESTED})
new Brackets((qb2) => {
qb2.where('friendship.status = :status', {status : FriendshipStatus.ACCEPTED})
// .orWhere('friendship.status = :status', {status : FriendshipStatus.REQUESTED})
}),
)
.getOne()
console.log('Find one friend by username: ')
console.log({...friendship})
if (!friendship) {
throw new HttpException(`There is no such friendship`, HttpStatus.NOT_FOUND);
}
@@ -166,13 +170,101 @@ export class FriendshipService {
return partialFriendship;
}
/*
{
'0': Friendship {
id: 1,
date: 2022-12-15T03:00:05.134Z,
senderUsername: 'chbadad',
receiverUsername: 'erlazo',
status: 'R',
sender: User {
id: 2,
fortyTwoId: '84193',
username: 'chbadad',
email: 'chbadad@student.42.fr',
image_url: 'default.png',
phone: null,
status: 'connected',
isEnabledTwoFactorAuth: false,
isTwoFactorAuthenticated: false,
secretTwoFactorAuth: null
},
receiver: User {
id: 1,
fortyTwoId: '40588',
username: 'erlazo',
email: 'erlazo@student.42.fr',
image_url: 'default.png',
phone: null,
status: 'connected',
isEnabledTwoFactorAuth: false,
isTwoFactorAuthenticated: false,
secretTwoFactorAuth: null
}
}
}
VS ------------------------------------------------
id: 1,
date: 2022-12-15T03:00:05.134Z,
senderUsername: 'chbadad',
receiverUsername: 'erlazo',
status: 'R',
sender: User {
id: 2,
fortyTwoId: '84193',
username: 'chbadad',
email: 'chbadad@student.42.fr',
image_url: 'default.png',
phone: null,
status: 'connected',
isEnabledTwoFactorAuth: false,
isTwoFactorAuthenticated: false,
secretTwoFactorAuth: null
},
receiver: User {
id: 1,
fortyTwoId: '40588',
username: 'erlazo',
email: 'erlazo@student.42.fr',
image_url: 'default.png',
phone: null,
status: 'connected',
isEnabledTwoFactorAuth: false,
isTwoFactorAuthenticated: false,
secretTwoFactorAuth: null
}
}
*/
// TEST !!!!!!!!!
async testFriendship(relationshipId: string, user: User) {
const relation = await this.friendshipRepository.find({where: {id: +relationshipId }, relations: ['sender', 'receiver']} );
// const relation = await this.friendshipRepository.find({take: 1, where: {id: +relationshipId }, relations: ['sender', 'receiver']} );
// const relation = await this.friendshipRepository.find({select: {0: Friendship}, where: {id: +relationshipId }, relations: ['sender', 'receiver']} );
// const relation = await this.friendshipRepository.findOne({where: {id: +relationshipId }, relations: ['sender', 'receiver']});
console.log('.service test friendship')
console.log({...relation})
if (relation[0].sender.id) {
console.log('it worked!')
}
if (!relation) {
throw new HttpException(`Found NOTHING.`, HttpStatus.NOT_FOUND);
}
return relation;
}
async acceptFriendship(relationshipId: string, user: User) {
// const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId });
// ok gotta swap out hte findOneBy for a find, recall what you did in the Nest.js tutorial
// const relation = await this.friendshipRepository.find({where: {id: +relationshipId }, relations: ['sender', 'receiver']} );
// const relation = await this.friendshipRepository.find({where: {id: +relationshipId } });
// this is what i want ideally...
const relation = await this.friendshipRepository.find({where: {id: +relationshipId }, relations: ['sender', 'receiver']} );
// const relation = await this.friendshipRepository.findOneBy({where: {id: +relationshipId }, relations: ['sender', 'receiver']});
const relation = await this.friendshipRepository.findOne({where: {id: +relationshipId }, relations: ['sender', 'receiver']});
// const relation = await this.friendshipRepository.findOne({where: {id: +relationshipId }, relations: ['sender', 'receiver']});
console.log('.service accept friendship')
console.log({...relation})
@@ -181,11 +273,13 @@ export class FriendshipService {
// console.log(relation.sender)
// ok so you can't query sender cuz it's not a column in the entity, not sure how you access it then...
// if (relation.sender.id === user.id) {
if (relation.sender.id === user.id) {
if (relation[0].sender.id === user.id) {
throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND);
}
relation.status = FriendshipStatus.ACCEPTED;
const savedFriendship = this.friendshipRepository.save(relation);
relation[0].status = FriendshipStatus.ACCEPTED;
// are we sure saving relation[0] won't fuck things up? cuz aren't there 2 User in there?
// should i partial friendship intermediary first?
const savedFriendship = this.friendshipRepository.save(relation[0]);
const partialFriendship : Partial<Friendship> = {
id : (await savedFriendship).id,
date : (await savedFriendship).date,