API-18: Friendship data endpoint (#8)
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				Jenkins Production Deployment
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	Jenkins Production Deployment
				
			Co-authored-by: Patrick Müller <patrick@mueller-patrick.tech> Reviewed-on: #8 Co-authored-by: Patrick Müller <patrick@plutodev.de> Co-committed-by: Patrick Müller <patrick@plutodev.de>
This commit is contained in:
		
							parent
							
								
									bd82fac051
								
							
						
					
					
						commit
						1d46904a52
					
				| 
						 | 
					@ -74,3 +74,35 @@ dataRouter.get('/session/:isDevCall', async (req: Request, res: Response) => {
 | 
				
			||||||
        res.status(500).send({'message': 'Internal Server Error. Try again later.'});
 | 
					        res.status(500).send({'message': 'Internal Server Error. Try again later.'});
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					dataRouter.get('/friendship/:isDevCall', async (req: Request, res: Response) => {
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					        let userId = (req.query.userId ?? '').toString();
 | 
				
			||||||
 | 
					        let sessionId = (req.query.sessionId ?? '').toString();
 | 
				
			||||||
 | 
					        let sessionKey = (req.query.sessionKey ?? '').toString();
 | 
				
			||||||
 | 
					        let useDev: boolean = (req.params.isDevCall ?? '') === 'dev'; // TBD
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (userId === '' || sessionId === '' || sessionKey === '') {
 | 
				
			||||||
 | 
					            res.status(400).send({
 | 
				
			||||||
 | 
					                'status': 'WRONG_PARAMS',
 | 
				
			||||||
 | 
					                'message': 'Missing or wrong parameters'
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (!await UserService.checkSession(useDev, userId, sessionId, sessionKey)) {
 | 
				
			||||||
 | 
					            res.status(403).send({
 | 
				
			||||||
 | 
					                'status': 'INVALID_SESSION',
 | 
				
			||||||
 | 
					                'message': 'The user or session could not be found or the session is invalid'
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        let data = await DataService.getFriendshipData(useDev, userId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        res.status(200).send(data);
 | 
				
			||||||
 | 
					    } catch (e) {
 | 
				
			||||||
 | 
					        logger.error('Error handling a request: ' + e.message);
 | 
				
			||||||
 | 
					        res.status(500).send({'message': 'Internal Server Error. Try again later.'});
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -41,6 +41,17 @@ export interface SessionData {
 | 
				
			||||||
    lastIp: string;
 | 
					    lastIp: string;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Used in the getFriendshipData method as a return value
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					export interface Friendship {
 | 
				
			||||||
 | 
					    friendshipId: string;
 | 
				
			||||||
 | 
					    friendId: string;
 | 
				
			||||||
 | 
					    friendFirstName: string;
 | 
				
			||||||
 | 
					    friendLastName: string;
 | 
				
			||||||
 | 
					    friendUsername: string;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Returns all data about the given user
 | 
					 * Returns all data about the given user
 | 
				
			||||||
 * @param useDev If the dev or prod database should be used
 | 
					 * @param useDev If the dev or prod database should be used
 | 
				
			||||||
| 
						 | 
					@ -88,7 +99,7 @@ export const getUserData = async (useDev: boolean, userId: string): Promise<User
 | 
				
			||||||
 * Returns all active sessions of the given user
 | 
					 * Returns all active sessions of the given user
 | 
				
			||||||
 * @param useDev If the dev or prod database should be used
 | 
					 * @param useDev If the dev or prod database should be used
 | 
				
			||||||
 * @param userId The userId of the user to return the sessions for
 | 
					 * @param userId The userId of the user to return the sessions for
 | 
				
			||||||
 * @return SessionData An object containing the session data
 | 
					 * @return SessionData[] A list containing objects with the session data
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
export const getSessionData = async (useDev: boolean, userId: string): Promise<SessionData[]> => {
 | 
					export const getSessionData = async (useDev: boolean, userId: string): Promise<SessionData[]> => {
 | 
				
			||||||
    let conn;
 | 
					    let conn;
 | 
				
			||||||
| 
						 | 
					@ -123,3 +134,44 @@ export const getSessionData = async (useDev: boolean, userId: string): Promise<S
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Returns all friends of the given user
 | 
				
			||||||
 | 
					 * @param useDev If the dev or prod database should be used
 | 
				
			||||||
 | 
					 * @param userId The userId of the user to fetch the friends for
 | 
				
			||||||
 | 
					 * @return Friendship A list of friends
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					export const getFriendshipData = async (useDev: boolean, userId: string): Promise<Friendship[]> => {
 | 
				
			||||||
 | 
					    let conn;
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					        if (useDev) {
 | 
				
			||||||
 | 
					            conn = await dev_pool.getConnection();
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            conn = await prod_pool.getConnection();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        let rows = await conn.query('SELECT f.friendship_id, f.friend_id, u.first_name as friend_first_name, u.last_name as friend_last_name, u.username as friend_username FROM friendships f LEFT OUTER JOIN users u ON f.friend_id = u.user_id WHERE f.user_id = ?', userId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        let friends: Friendship[] = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (let row in rows) {
 | 
				
			||||||
 | 
					            if (row !== 'meta') {
 | 
				
			||||||
 | 
					                friends.push({
 | 
				
			||||||
 | 
					                    friendshipId: rows[row].friendship_id,
 | 
				
			||||||
 | 
					                    friendId: rows[row].friend_id,
 | 
				
			||||||
 | 
					                    friendFirstName: rows[row].friend_first_name,
 | 
				
			||||||
 | 
					                    friendLastName: rows[row].friend_last_name,
 | 
				
			||||||
 | 
					                    friendUsername: rows[row].friend_username
 | 
				
			||||||
 | 
					                });
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return friends;
 | 
				
			||||||
 | 
					    } catch (err) {
 | 
				
			||||||
 | 
					        throw err;
 | 
				
			||||||
 | 
					    } finally {
 | 
				
			||||||
 | 
					        if (conn) {
 | 
				
			||||||
 | 
					            conn.end();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,6 +20,24 @@ const dev_pool = mariadb.createPool({
 | 
				
			||||||
    connectionLimit: 5
 | 
					    connectionLimit: 5
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Used in the registerUser and loginUser methods as return value
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					export interface Session {
 | 
				
			||||||
 | 
					    userId: string;
 | 
				
			||||||
 | 
					    sessionId: string;
 | 
				
			||||||
 | 
					    sessionKey: string;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Used in the checkUsernameAndEmail method as return value
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					export interface Status {
 | 
				
			||||||
 | 
					    hasProblems: boolean;
 | 
				
			||||||
 | 
					    messages: string[];
 | 
				
			||||||
 | 
					    status: string[];
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Fetches all usernames and emails from the database and returns them in an object
 | 
					 * Fetches all usernames and emails from the database and returns them in an object
 | 
				
			||||||
 * @param useDev If the dev or prod database should be used
 | 
					 * @param useDev If the dev or prod database should be used
 | 
				
			||||||
| 
						 | 
					@ -59,15 +77,6 @@ export const getExistingUsernamesAndEmails = async (useDev: boolean): Promise<an
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					 | 
				
			||||||
 * Used in the registerUser and loginUser methods as return value
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
export interface Session {
 | 
					 | 
				
			||||||
    userId: string;
 | 
					 | 
				
			||||||
    sessionId: string;
 | 
					 | 
				
			||||||
    sessionKey: string;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Creates a new user and a new session
 | 
					 * Creates a new user and a new session
 | 
				
			||||||
 * @param useDev If the dev or prod database should be used
 | 
					 * @param useDev If the dev or prod database should be used
 | 
				
			||||||
| 
						 | 
					@ -224,15 +233,6 @@ export const loginUser = async (useDev: boolean, username: string, email: string
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					 | 
				
			||||||
 * Used in the checkUsernameAndEmail method as return value
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
export interface Status {
 | 
					 | 
				
			||||||
    hasProblems: boolean;
 | 
					 | 
				
			||||||
    messages: string[];
 | 
					 | 
				
			||||||
    status: string[];
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Checks if the given username and email are valid and not yet used by another user
 | 
					 * Checks if the given username and email are valid and not yet used by another user
 | 
				
			||||||
 * @param useDev If the dev database has to be used
 | 
					 * @param useDev If the dev database has to be used
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user