merge with master but css broken
This commit is contained in:
1406
srcs/requirements/nestjs/api_back/package-lock.json
generated
1406
srcs/requirements/nestjs/api_back/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -24,10 +24,12 @@ export class AuthenticationService {
|
||||
if (!check_name)
|
||||
return await this.userService.create(createUsersDto);
|
||||
let createUsersDtoWithUsername : CreateUsersDto = createUsersDto;
|
||||
let i = 0;
|
||||
while (check_name === true)
|
||||
{
|
||||
createUsersDtoWithUsername = { ...createUsersDto, username: createUsersDto.username + randomUUID() };
|
||||
createUsersDtoWithUsername = { ...createUsersDto, username: createUsersDto.username + '_' + i};
|
||||
check_name = await this.userService.isUsernameExists(createUsersDtoWithUsername.username);
|
||||
i++;
|
||||
}
|
||||
return this.userService.create(createUsersDtoWithUsername);
|
||||
}
|
||||
|
||||
@@ -28,12 +28,8 @@ export class ChatService {
|
||||
private readonly chatroomRepository: Repository<Chatroom>,
|
||||
@InjectRepository(Friendship)
|
||||
private readonly friendshipRepository: Repository<Friendship>,
|
||||
)
|
||||
{
|
||||
// this.sockets = new Map<string, socketDto>();
|
||||
}
|
||||
) {}
|
||||
|
||||
// sockets = new mapSocket();
|
||||
|
||||
addSocket(key: string, socket: socketDto)
|
||||
{
|
||||
@@ -54,9 +50,9 @@ export class ChatService {
|
||||
}
|
||||
|
||||
// temp for test
|
||||
sleep(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
// sleep(ms) {
|
||||
// return new Promise(resolve => setTimeout(resolve, ms));
|
||||
// }
|
||||
|
||||
|
||||
/* GETTERS ************************************************
|
||||
@@ -66,9 +62,6 @@ export class ChatService {
|
||||
{
|
||||
printCaller("-- in ");
|
||||
|
||||
console.log("---- username:", username)
|
||||
console.log("---- all rooms:", await this.getAllRooms());
|
||||
|
||||
//await this.sleep(1000);
|
||||
let rooms: Chatroom[];
|
||||
|
||||
@@ -237,7 +230,8 @@ export class ChatService {
|
||||
}
|
||||
|
||||
printCaller("-- out ");
|
||||
return user_db.currentRoom;
|
||||
if (user_db)
|
||||
return user_db.currentRoom;
|
||||
}
|
||||
|
||||
async getRoomByName(room_name: string, fieldsToReturn: string[] = null): Promise<Chatroom>
|
||||
@@ -351,7 +345,6 @@ export class ChatService {
|
||||
printCallerError(`ERROR in chat: room not found for ${username}`);
|
||||
return;
|
||||
}
|
||||
console.log("---- room_db:", room_db);
|
||||
|
||||
if (!room_db.admins.includes(username))
|
||||
{
|
||||
@@ -408,7 +401,6 @@ export class ChatService {
|
||||
console.log("throw error: error: true, code: 'NO_PASSWORD', message: 'this room has no password protection'");
|
||||
throw new HttpException({ error: true, code: 'NO_PASSWORD', message: `this room has no password protection` }, HttpStatus.FORBIDDEN);
|
||||
}
|
||||
console.log("---- room_db:", room_db);
|
||||
if (room_db.protection)
|
||||
{
|
||||
if (room.protection && !old_password)
|
||||
@@ -574,6 +566,7 @@ export class ChatService {
|
||||
let users: string[] = [];
|
||||
let friends_users: SendableFriendship[];
|
||||
let user = await this.getUserByName(username);
|
||||
|
||||
if (user)
|
||||
friends_users = await this.friendshipService.findAllBlockedFriends(user.id);
|
||||
if (friends_users)
|
||||
@@ -713,8 +706,6 @@ export class ChatService {
|
||||
async removeUserFromRoom(username: string, room_name: string): Promise<string[]>
|
||||
{
|
||||
printCaller("-- in ");
|
||||
console.log("---- username:", username);
|
||||
console.log("---- room_name:", room_name);
|
||||
|
||||
let messages = [`${username} left the room`];
|
||||
|
||||
@@ -755,11 +746,8 @@ export class ChatService {
|
||||
else
|
||||
room.owner = "";
|
||||
}
|
||||
console.log("---- room:", room);
|
||||
await this.chatroomRepository.save(room);
|
||||
|
||||
console.log("---- all rooms:", await this.getAllRooms());
|
||||
|
||||
printCaller("-- out ");
|
||||
return messages;
|
||||
}
|
||||
@@ -911,11 +899,6 @@ export class ChatService {
|
||||
}
|
||||
}
|
||||
|
||||
console.log("---- socket.username", socket.username);
|
||||
console.log("---- room_name", room_name);
|
||||
console.log("---- socket.room", socket.room);
|
||||
console.log("---- socket.username", socket.username);
|
||||
|
||||
let socket_name = `${socket.username}_not_emit`;
|
||||
await this.addMessageToRoom(room_name, socket.username, message);
|
||||
await socket.to(room_name).except(socket_name).emit('message', socket.username, message);
|
||||
@@ -965,10 +948,6 @@ export class ChatService {
|
||||
{
|
||||
printCaller("-- in ");
|
||||
|
||||
console.log("-- old_name:", old_name);
|
||||
console.log("-- new_name:", new_name);
|
||||
|
||||
|
||||
let rooms: Chatroom[] = await this.getAllRooms();
|
||||
if(!rooms)
|
||||
{
|
||||
|
||||
@@ -17,10 +17,10 @@ export class Friendship {
|
||||
@CreateDateColumn()
|
||||
date : Date;
|
||||
|
||||
@ManyToOne(type => User, user => user.username)
|
||||
@ManyToOne(type => User, user => user.username, {onDelete: 'CASCADE'})
|
||||
sender: User;
|
||||
|
||||
@ManyToOne(type => User, user => user.username)
|
||||
@ManyToOne(type => User, user => user.username, {onDelete: 'CASCADE'})
|
||||
receiver: User;
|
||||
|
||||
@Column({ type: 'enum', enum: FriendshipStatus, default: FriendshipStatus.REQUESTED})
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { User } from 'src/users/entities/user.entity';
|
||||
|
||||
import { SendableUser } from 'src/users/sendableUsers';
|
||||
|
||||
import { Repository, Brackets } from 'typeorm';
|
||||
import { CreateFriendshipDto } from './dto/create-friendship.dto';
|
||||
import { Friendship, FriendshipStatus } from './entities/friendship.entity';
|
||||
@@ -19,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')
|
||||
@@ -42,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);
|
||||
}
|
||||
@@ -55,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')
|
||||
@@ -77,12 +68,8 @@ export class FriendshipService {
|
||||
)
|
||||
}),
|
||||
)
|
||||
// .andWhere('friendship.status != :status', {status : FriendshipStatus.BLOCKED})
|
||||
.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);
|
||||
}
|
||||
@@ -104,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;
|
||||
}
|
||||
|
||||
@@ -146,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));
|
||||
@@ -171,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));
|
||||
@@ -190,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));
|
||||
@@ -202,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')
|
||||
@@ -222,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);
|
||||
@@ -246,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;
|
||||
@@ -263,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) {
|
||||
@@ -308,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);
|
||||
@@ -332,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')
|
||||
@@ -366,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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Body, Controller, Get, HttpException, HttpStatus, Post, Req, Res, UseGuards } from '@nestjs/common';
|
||||
import { Body, Controller, Get, HttpException, HttpStatus, Param, Post, Query, Req, Res, UseGuards } from '@nestjs/common';
|
||||
import { AuthenticateGuard, TwoFactorGuard } from 'src/auth/42/guards/42guards';
|
||||
import { User } from 'src/users/entities/user.entity';
|
||||
import { Response } from 'express';
|
||||
@@ -21,6 +21,17 @@ export class GameController {
|
||||
return this.gameService.getMatchesForSpectator();
|
||||
}
|
||||
|
||||
@Get('match/history')
|
||||
@UseGuards(AuthenticateGuard)
|
||||
@UseGuards(TwoFactorGuard)
|
||||
async getMatchHistory(@Req() req, @Query('username') username: string, @Res() res)
|
||||
{
|
||||
if (!username) {
|
||||
username = req.user.username;
|
||||
}
|
||||
return this.gameService.getMatchHistory(username, res);
|
||||
}
|
||||
|
||||
@Get('ranking')
|
||||
@UseGuards(AuthenticateGuard)
|
||||
@UseGuards(TwoFactorGuard)
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Module, forwardRef } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Friendship } from 'src/friendship/entities/friendship.entity';
|
||||
import { FriendshipService } from 'src/friendship/friendship.service';
|
||||
import { MatchHistory } from 'src/users/entities/matchHistory.entity';
|
||||
import { User } from 'src/users/entities/user.entity';
|
||||
import { UsersService } from 'src/users/users.service';
|
||||
import { Game } from './entity/game.entity';
|
||||
@@ -14,7 +15,7 @@ import { Chatroom } from 'src/chat/entities/chatroom.entity';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([TokenGame, User, Game, Friendship, Chatroom]),
|
||||
TypeOrmModule.forFeature([TokenGame, User, Game, Friendship, Chatroom, MatchHistory]),
|
||||
ChatModule,
|
||||
],
|
||||
controllers: [GameController],
|
||||
|
||||
@@ -14,6 +14,8 @@ import { CreateGameDto } from './dto/createGame.dto';
|
||||
import { UpdateGameDto } from './dto/updateGame.dto';
|
||||
import { FriendshipService } from 'src/friendship/friendship.service';
|
||||
import { STATUS } from 'src/common/constants/constants';
|
||||
import { MatchHistory } from 'src/users/entities/matchHistory.entity';
|
||||
import { SendableMatchHistory } from 'src/users/class/matchHistory.class';
|
||||
|
||||
@Injectable()
|
||||
export class GameService {
|
||||
@@ -22,6 +24,8 @@ export class GameService {
|
||||
private readonly gameRepository : Repository<Game>,
|
||||
@InjectRepository(User)
|
||||
private readonly userRepository : Repository<User>,
|
||||
@InjectRepository(MatchHistory)
|
||||
private readonly matchHistory : Repository<MatchHistory>,
|
||||
@InjectRepository(TokenGame)
|
||||
private readonly tokenGameRepository : Repository<TokenGame>,
|
||||
private readonly userService : UsersService,
|
||||
@@ -43,6 +47,31 @@ export class GameService {
|
||||
return gamesToReturn;
|
||||
}
|
||||
|
||||
async getMatchHistory(username : string, @Res() res : Response)
|
||||
{
|
||||
const user = await this.userRepository.createQueryBuilder("user")
|
||||
.where('user.username = :username', {username: username})
|
||||
.getOne();
|
||||
if (!user)
|
||||
return res.status(HttpStatus.NOT_FOUND).json({message : "History for " + username + " not found"});
|
||||
console.log(user.username + 'OK USERNAME')
|
||||
const gameHistory = await this.matchHistory.
|
||||
createQueryBuilder('history')
|
||||
.leftJoinAndSelect('history.playerOne', 'playerOne')
|
||||
.leftJoinAndSelect('history.playerTwo', 'playerTwo')
|
||||
.where('history.playerOne.id = :userOne', {userOne : user.id})
|
||||
.orWhere('history.playerTwo.id = :userTwo', {userTwo: user.id})
|
||||
.getMany()
|
||||
let sendableHistory : SendableMatchHistory[] = []
|
||||
for (const history of gameHistory)
|
||||
{
|
||||
sendableHistory.push(new SendableMatchHistory(history))
|
||||
}
|
||||
console.log("sendable history")
|
||||
console.log(sendableHistory);
|
||||
return res.status(HttpStatus.OK).json(sendableHistory);
|
||||
}
|
||||
|
||||
async getRankingForAllUsers(currentUser : User) {
|
||||
const users = await this.userRepository.createQueryBuilder("user")
|
||||
.leftJoinAndSelect("user.stats", "stats")
|
||||
@@ -293,6 +322,14 @@ export class GameService {
|
||||
this.userService.incrementVictories(playerOne.id)
|
||||
this.userService.incrementDefeats(playerTwo.id)
|
||||
}
|
||||
const matchHistory = new MatchHistory();
|
||||
matchHistory.playerOne = playerOne;
|
||||
matchHistory.playerTwo = playerTwo;
|
||||
matchHistory.playerOneResult = game.playerOneUsernameResult;
|
||||
matchHistory.playerTwoResult = game.playerTwoUsernameResult;
|
||||
const savedHistory = await this.matchHistory.save(matchHistory);
|
||||
console.log("MATCH HISTORYsssssssssss")
|
||||
console.log(savedHistory);
|
||||
return HttpStatus.OK
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
|
||||
import { match } from "assert";
|
||||
import { MatchHistory } from "../entities/matchHistory.entity";
|
||||
|
||||
export class SendableMatchHistory {
|
||||
id: number;
|
||||
date: Date;
|
||||
playerOneUsername: string;
|
||||
playerTwoUsername: string;
|
||||
playerTwoResult : number;
|
||||
playerOneResult : number;
|
||||
|
||||
constructor(matchHistory: MatchHistory) {
|
||||
this.id = matchHistory.id;
|
||||
this.date = matchHistory.date
|
||||
this.playerOneUsername = matchHistory.playerOne.username;
|
||||
this.playerTwoUsername = matchHistory.playerTwo.username;
|
||||
this.playerOneResult = matchHistory.playerOneResult;
|
||||
this.playerTwoResult = matchHistory.playerTwoResult;
|
||||
};
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
import { IsBoolean, Matches, IsEmail, IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
||||
import { IsBoolean, Matches, MaxLength, IsEmail, IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
||||
import { isSet } from 'util/types';
|
||||
|
||||
export class CreateUsersDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@Matches(/^[a-zA-Z0-9'-_]+$/)
|
||||
@MaxLength(50)
|
||||
readonly username: string;
|
||||
readonly fortyTwoId: string;
|
||||
@IsEmail()
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Column, CreateDateColumn, Entity, ManyToOne, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm";
|
||||
import { User } from "./user.entity";
|
||||
|
||||
@Entity('matchHistory')
|
||||
export class MatchHistory {
|
||||
@PrimaryGeneratedColumn()
|
||||
id : number;
|
||||
@ManyToOne(type => User, user => user.username, {onDelete: 'CASCADE'})
|
||||
playerOne: User;
|
||||
@ManyToOne(type => User, user => user.username, {onDelete: 'CASCADE'})
|
||||
playerTwo: User;
|
||||
@Column()
|
||||
playerOneResult : number;
|
||||
@Column()
|
||||
playerTwoResult : number;
|
||||
@CreateDateColumn()
|
||||
date : Date;
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { Exclude } from "class-transformer";
|
||||
import { IsEmail, Length } from "class-validator";
|
||||
import { Column, Entity, JoinColumn, JoinTable, ManyToMany, OneToMany, OneToOne, PrimaryGeneratedColumn, Unique } from "typeorm";
|
||||
import { Friendship } from "../../friendship/entities/friendship.entity";
|
||||
import { MatchHistory } from "./matchHistory.entity";
|
||||
import { UserStats } from "./userStat.entities";
|
||||
|
||||
|
||||
@@ -43,18 +44,26 @@ export class User {
|
||||
@Column({ nullable: true })
|
||||
secretTwoFactorAuth: string;
|
||||
|
||||
@OneToMany(type => Friendship , (friendship) => friendship.sender)
|
||||
@OneToMany(type => Friendship , (friendship) => friendship.sender, {onDelete: 'CASCADE'})
|
||||
sentFriendRequest: Friendship[];
|
||||
|
||||
@OneToMany(type => Friendship , (friendship) => friendship.receiver)
|
||||
@OneToMany(type => Friendship , (friendship) => friendship.receiver, {onDelete: 'CASCADE'})
|
||||
receivedFriendRequest: Friendship[];
|
||||
|
||||
@OneToMany(type => MatchHistory , (matchHistory) => matchHistory.playerOne, {onDelete: 'CASCADE'})
|
||||
playerOneMatch: MatchHistory[];
|
||||
|
||||
@OneToMany(type => MatchHistory , (matchHistory) => matchHistory.playerTwo, {onDelete: 'CASCADE'})
|
||||
playerTwoMatch: MatchHistory[];
|
||||
|
||||
@JoinColumn()
|
||||
@OneToOne(() => UserStats, { cascade: true })
|
||||
@OneToOne(() => UserStats, { cascade: true, onDelete: 'CASCADE' })
|
||||
stats: UserStats;
|
||||
|
||||
|
||||
// ROOMS :
|
||||
|
||||
@Column({ nullable: true })
|
||||
currentRoom: string; // chatroom name
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import {
|
||||
Body, Controller, Delete, Get, NotFoundException,HttpStatus, Param, Patch, Post, Query, Redirect, Req, Res, UploadedFile, UseGuards, UseInterceptors
|
||||
Body, Controller, Delete, Get, NotFoundException,HttpStatus, Next, Patch, Post, Query, Redirect, Req, Res, UploadedFile, UseGuards, UseInterceptors
|
||||
} from '@nestjs/common';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import { Response } from 'express';
|
||||
@@ -73,8 +73,14 @@ export class UsersController {
|
||||
@UseGuards(AuthenticateGuard)
|
||||
@UseGuards(TwoFactorGuard)
|
||||
@Delete()
|
||||
remove(@Req() req) {
|
||||
return this.usersService.remove(req.user.id);
|
||||
remove(@Req() req, @Res() response, @Next() next) {
|
||||
this.usersService.remove(req.user.id);
|
||||
req.logout(function(err) {
|
||||
if (err) { return next(err); }
|
||||
response.redirect('/');
|
||||
});
|
||||
req.session.cookie.maxAge = 0;
|
||||
return {msg : 'Your account has been deleted'};
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -4,12 +4,11 @@ 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';
|
||||
import { ChatService } from 'src/chat/chat.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.
|
||||
import { MatchHistory } from './entities/matchHistory.entity';
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
|
||||
@@ -25,16 +24,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 })
|
||||
@@ -42,8 +37,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,
|
||||
@@ -51,9 +44,6 @@ export class UsersService {
|
||||
status: user.status,
|
||||
stats: user.stats,
|
||||
};
|
||||
|
||||
// console.log(`Returned Partial User.` + partialUser.username + user.username);
|
||||
|
||||
return partialUser;
|
||||
}
|
||||
|
||||
@@ -72,26 +62,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);
|
||||
@@ -103,8 +81,7 @@ 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')
|
||||
if (await this.isUsernameExists(updateUserDto.username) === true && updateUserDto.username.toLowerCase() !== username.toLowerCase()) {
|
||||
throw new HttpException(`The username is already in use.`,HttpStatus.CONFLICT);
|
||||
}
|
||||
const user = await this.userRepository.preload(
|
||||
@@ -149,15 +126,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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user