Skip to content

Commit

Permalink
Dev backend (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoubessabiry committed Oct 26, 2023
2 parents dc07a01 + c02ee00 commit 7d3ae3e
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 36 deletions.
6 changes: 5 additions & 1 deletion backend/code/prisma/dbml/schema.dbml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Table users {
tfaStatus Boolean [not null, default: false]
left_friends friends [not null]
right_friends friends [not null]
matches matches [not null]
participant1 matches [not null]
participant2 matches [not null]
owned_rooms rooms [not null]
roomMember room_members [not null]
blocked_by blocked_friends [not null]
Expand Down Expand Up @@ -65,6 +66,7 @@ Table matches {
score2 Int
gametype String
participant1 users [not null]
participant2 users [not null]
}

Table messages {
Expand Down Expand Up @@ -139,6 +141,8 @@ Ref: blocked_friends.blocked_id > users.userId

Ref: matches.participant1Id > users.userId

Ref: matches.participant2Id > users.userId

Ref: messages.roomId > rooms.id [delete: Cascade]

Ref: rooms.ownerId > users.userId
Expand Down
4 changes: 3 additions & 1 deletion backend/code/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ model User {
tfaStatus Boolean @default(false)
left_friends Friend[] @relation("from")
right_friends Friend[] @relation("to")
matches Match[] @relation("participant1")
participant1 Match[] @relation("participant1")
participant2 Match[] @relation("participant2")
owned_rooms Room[] @relation("owner")
roomMember RoomMember[]
blocked_by BlockedUsers[] @relation("blocked_by")
Expand Down Expand Up @@ -85,6 +86,7 @@ model Match {
score2 Int?
gametype String?
participant1 User @relation("participant1", fields: [participant1Id], references: [userId])
participant2 User @relation("participant2", fields: [participant2Id], references: [userId])
@@map("matches")
}
Expand Down
13 changes: 8 additions & 5 deletions backend/code/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ import { APP_GUARD } from '@nestjs/core';
ThrottlerModule.forRoot([
{
ttl: 6000,
limit: 1000000,
limit: 1000000,
},
]),
],
controllers: [AppController],
providers: [Gateways, {
provide: APP_GUARD,
useClass: ThrottlerGuard,
}],
providers: [
Gateways,
{
provide: APP_GUARD,
useClass: ThrottlerGuard,
},
],
})
export class AppModule {}
14 changes: 13 additions & 1 deletion backend/code/src/game/game.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Controller, Post } from '@nestjs/common';
import { Controller, Get, Post, Query, UseGuards } from '@nestjs/common';
import { GameService } from './game.service';
import { AtGuard } from 'src/auth/guards/at.guard';
import { GetCurrentUser } from 'src/auth/decorator/get_current_user.decorator';
import { QueryOffsetDto } from 'src/friends/dto/query-ofsset-dto';

@Controller('game')
export class GameController {
Expand All @@ -9,4 +12,13 @@ export class GameController {
startGame() {
// return this.gameService.startGame();
}

@Get('history')
@UseGuards(AtGuard)
getHistory(
@GetCurrentUser('userId') userId: string,
@Query() { offset, limit }: QueryOffsetDto,
) {
return this.gameService.getHistory(userId, offset, limit);
}
}
68 changes: 67 additions & 1 deletion backend/code/src/game/game.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Injectable } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
import { PrismaService } from 'src/prisma/prisma.service';
import { PICTURE } from 'src/profile/dto/profile.dto';

@Injectable()
export class GameService {
constructor() {
constructor(private readonly prisma: PrismaService) {
// this.launchGame();
}

Expand All @@ -25,4 +27,68 @@ export class GameService {
}
}, 1000);
}

async getHistory(userId: string, offset: number, limit: number) {
const matches = await this.prisma.match.findMany({
skip: offset,
take: limit,
where: {
OR: [
{
participant1Id: userId,
},
{
participant2Id: userId,
},
],
},
orderBy: {
createdAt: 'desc',
},
select: {
createdAt: true,
score1: true,
score2: true,
participant1: {
select: {
Username: true,
avatar: true,
},
},
participant2: {
select: {
Username: true,
avatar: true,
},
},
},
});
return matches.map((match) => {
const avatar1: PICTURE = {
thumbnail: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_48,w_48/${match.participant1.avatar}`,
medium: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_72,w_72/${match.participant1.avatar}`,
large: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_128,w_128/${match.participant1.avatar}`,
};
const avatar2: PICTURE = {
thumbnail: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_48,w_48/${match.participant2.avatar}`,
medium: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_72,w_72/${match.participant2.avatar}`,
large: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_128,w_128/${match.participant2.avatar}`,
};
return {
match: {
createdAt: match.createdAt,
Player1: {
username: match.participant1.Username,
score: match.score1,
avatar: avatar1,
},
Player2: {
username: match.participant2.Username,
score: match.score2,
avatar: avatar2,
},
},
};
});
}
}
10 changes: 10 additions & 0 deletions backend/code/src/rooms/rooms.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ export class RoomsController {
return await this.roomsService.banMember(memberData, userId);
}

@Post('unban')
@HttpCode(HttpStatus.OK)
@UseGuards(AtGuard)
async unbanMember(
@Body() memberData: ChangeOwnerDto,
@GetCurrentUser('userId') userId: string,
) {
return await this.roomsService.unbanMember(memberData, userId);
}

@Post('add')
@HttpCode(HttpStatus.OK)
@UseGuards(AtGuard)
Expand Down
95 changes: 68 additions & 27 deletions backend/code/src/rooms/rooms.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export class RoomsService {
if (room.ownerId !== userId)
throw new UnauthorizedException('You are not the owner of this room');
if (!member)
throw new UnauthorizedException('new owner is not a member of this room');
throw new UnauthorizedException('user is not a member of this room');
await this.prisma.room.update({
where: { id: roomData.roomId },
data: { owner: { connect: { userId: roomData.memberId } } },
Expand Down Expand Up @@ -251,7 +251,7 @@ export class RoomsService {
if (room.ownerId !== userId)
throw new UnauthorizedException('You are not the owner of this room');
if (!user)
throw new UnauthorizedException('new admin is not a member of this room');
throw new UnauthorizedException('user is not a member of this room');
if (user.is_admin || room.ownerId === roomData.memberId)
throw new UnauthorizedException(
'new admin is already an admin of this room',
Expand Down Expand Up @@ -334,6 +334,11 @@ export class RoomsService {
roomId: roomData.roomId,
},
},
select: {
room: true,
is_mueted: true,
userId: true,
},
});
if (!room) throw new HttpException('room not found', HttpStatus.NOT_FOUND);
if (!member)
Expand All @@ -342,6 +347,8 @@ export class RoomsService {
throw new UnauthorizedException('You are not admin of this room');
if (member.is_mueted)
throw new UnauthorizedException('member is already muted');
if (member.room.ownerId === roomData.memberId)
throw new UnauthorizedException('You cannot mute the owner');
if (member.userId === userId)
throw new UnauthorizedException('You can not mute yourself');
const afterFiveMin = new Date(Date.now() + 5 * 60 * 1000);
Expand Down Expand Up @@ -382,42 +389,40 @@ export class RoomsService {
const user = await this.prisma.roomMember.findUnique({
where: { unique_user_room: { userId: userId, roomId: roomId } },
});

if (!user)
throw new UnauthorizedException('You are not a member of this room');

const members = await this.prisma.roomMember.findMany({
skip: offset,
take: limit,
where: {
roomId: roomId,
is_banned: false,
},
select: {
user: {
select: {
userId: true,
firstName: true,
lastName: true,
avatar: true,
},
},
is_banned: true,
},
});
return members.map((member) => {
const avatar: PICTURE = {
thumbnail: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_48,w_48/${member.user.avatar}`,
medium: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_72,w_72/${member.user.avatar}`,
large: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_128,w_128/${member.user.avatar}`,
};
return {
id: member.user.userId,
name: { first: member.user.firstName, last: member.user.lastName },
avatar,
if (!member.is_banned || user.is_admin) {
const avatar: PICTURE = {
thumbnail: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_48,w_48/${member.user.avatar}`,
medium: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_72,w_72/${member.user.avatar}`,
large: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_128,w_128/${member.user.avatar}`,
};
return {
firstname: member.user.firstName,
lastname: member.user.lastName,
avatar: avatar,
};
}
});
}

async banMember(memberData: ChangeOwnerDto, userId: string) {
const user = await this.prisma.roomMember.findUnique({
where: {
Expand All @@ -433,8 +438,8 @@ export class RoomsService {
if (userId == memberData.memberId)
throw new UnauthorizedException('You cannot ban yourself');
if (ownerId == memberData.memberId)
throw new UnauthorizedException('You cannot ban the Owner of thi Room');
return await this.prisma.roomMember.update({
throw new UnauthorizedException('You cannot ban the Owner of this Room');
await this.prisma.roomMember.update({
where: {
unique_user_room: {
userId: memberData.memberId,
Expand All @@ -446,6 +451,41 @@ export class RoomsService {
bannedAt: new Date(Date.now()),
},
});
return { message: 'member banned successfully' };
}

async unbanMember(memberData: ChangeOwnerDto, userId: string) {
const user = await this.prisma.roomMember.findUnique({
where: {
unique_user_room: { userId: userId, roomId: memberData.roomId },
},
});
const member = await this.prisma.roomMember.findUnique({
where: {
unique_user_room: {
userId: memberData.memberId,
roomId: memberData.roomId,
},
},
});
if (!member)
throw new HttpException('user not found', HttpStatus.NOT_FOUND);
if (!member.is_banned)
throw new HttpException('member is not banned', HttpStatus.BAD_REQUEST);
if (!user.is_admin)
throw new UnauthorizedException('You are not admin of this room');
await this.prisma.roomMember.update({
where: {
unique_user_room: {
userId: memberData.memberId,
roomId: memberData.roomId,
},
},
data: {
is_banned: false,
},
});
return { message: 'member unbanned successfully' };
}

async addMember(memberData: ChangeOwnerDto, userId: string) {
Expand Down Expand Up @@ -498,18 +538,19 @@ export class RoomsService {
name: true,
type: true,
ownerId: true,
...(joined && {members: {
where: {
userId: userId,
},
select: {
is_admin: true,
...(joined && {
members: {
where: {
userId: userId,
},
select: {
is_admin: true,
},
},
}})
}),
},
});
if (!joined)
return rooms;
if (!joined) return rooms;
return rooms.map((room) => {
const is_owner = room.ownerId === userId;
return {
Expand Down

0 comments on commit 7d3ae3e

Please sign in to comment.