Skip to content
This repository was archived by the owner on Oct 29, 2025. It is now read-only.

IUT-INFO-UGA/Bridgeguesser-Backend

Repository files navigation

BridgeGuesser Backend


Installation

To install the project, you need to have Node.js, npm, and a postgres database set up on your machine.

npm install

In the root directory of the project, create a .env file:

DATABASE_URL=postgresql://USER:USER@localhost:5432/DB_NAME
PROD_DATABASE_URL=postgresql://USER:USER@HOST:5432/DB_NAME
ISDEV=TRUE
ISPROD=FALSE
privateJWTkey=anSaltForJWT
OPENAI_API_KEY=ourApiKeyForDescription

Create the schema on your database:

npx drizzle-kit push

After that, you can run the project:

npm run start

Configuration

Configure the project for your usage.

Server Configuration

On our server, do not use the .env file but rely on default values. All default values are pre-defined in the code. On the server, the .env file should only define PROD_DATABASE_URL and ISDEV=FALSE. If you do not add PROD_DATABASE_URL, the server will use the default value for the database connection:

postgresql://postgres:superpass@localhost:5432/postgres

Development Configuration

In the development environment, use the .env file. In this file, you can set DATABASE_URL and ISDEV=TRUE. If you do not add DATABASE_URL, the server will use the default value for the database connection:

postgresql://postgres:superpass@localhost:5432/postgres

Test Configuration

All tests are run on the main database. Do not use the test command on the production database. When running tests, the server does not start; only the tests are executed. This allows the server to be started and tested without port conflicts.

Usage

Start

To run the project on the server:

npm run start

Dev

To run the project in the development environment:

npm run dev

Test

To test the project:

npm run test

REST API

The API operates using ExpressTS.

Below is a list of available endpoints:

/user/login

Handles user login by verifying the provided email and password.

  • If the credentials are valid, a JWT token is generated and returned.
  • If the credentials are invalid, a 401 status code is returned.

Request Body:

{
  "mail": "string",
  "password": "string" (hashed password)
}

Responses:

  • 401 Server error during the request.
  • 401 Incorrect email or password.
  • 200 Request successful.

/user/createAccount

Creates a new user account by validating the provided email, password, and GDPR (RGPD) acceptance.

  • If the parameters are valid, the user is added to the database.
  • If any parameter is invalid, an appropriate error response is returned.

Request Body:

{
  "email": "string",
  "password": "string" (hashed password),
  "accept_rgpd": true
}

Responses:

  • 400 If any parameter is missing or invalid.
  • 400 rgpd is not accepted
  • 400 bad email format
  • 400 account already exist
  • 500 If there is an error inserting the user into the database.
  • 200 If the user is successfully created.

/user/getInfos

Retrieves user information based on the authenticated user's ID. The function first authenticates the user, then queries the database to fetch details such as user_id, email, points, last_connection, and accept_rgpd.

  • If the user is not found or an error occurs, an appropriate error response is returned.

Request Details:

  • This is a GET request and does not require a request body.

Responses:

  • 500 If there is an internal server error or an error in the select query.
  • 404 If the user is not found.
  • 200 If the user information is successfully retrieved.

/user/getPointsLogs

Retrieves the points logs for the authenticated user.

  • If no logs are found for the user, an empty array is returned.
  • If there is an error during the database queries, an appropriate error response is returned.

Request Details:

  • This is a GET request and does not require a request body.

Responses:

  • ̀400 If the user is not found.
  • ̀500 If there is an error during the database queries.
  • ̀200 If the points logs are successfully retrieved.

/user/emailModif

Modifies the email address of an authenticated user. Validates the new email format and checks if the email is already in use.

  • If the new email is valid and not in use, updates the user's email in the database.

Request Body:

{
	"newEmail": "string"
}

Responses:

  • 400 If the new email format is invalid or the email is already in use.
  • 500 If there is an error during the database operations.
  • 200 If the email is successfully updated.

/user/passwordModif

Modifies the user's password by validating the provided new password.

  • If the parameters are valid and the password format is correct, the password is updated in the database.
  • If any parameter is invalid or the password format is incorrect, an appropriate error response is returned.

Request Body:

{
	"newPassword": "string" (hashed password)
}

Responses:

  • 400 If the new password format is invalid.
  • 500 If there is an error updating the password in the database.
  • 200 If the password is successfully updated.

/user/deleteAccount

Deletes a user account and its associated logs from the database. The user is authenticated before the deletion process begins. Logs the start and end of the deletion process, as well as any errors encountered.

Request Details:

  • This is a GET request and does not require a request body.

Responses:

  • 500 If there is an error during the deletion process.
  • 200 If the user is successfully deleted.

/bridge/getBridgeInfos

Retrieves bridge information and associated photos based on the provided bridge name. The function first authenticates the user and checks the request parameters.

  • If the bridge name is valid, it fetches the bridge details and photos from the database.

Request Body:

{
	"name": "string"
}

Responses:

  • 400 If the bridge name is missing or empty.
  • 404 If photos for the specified bridge are not found.
  • 500 If the bridge is not found or an internal server error occurs.

/bridge/getAllCoordinate

Retrieves all bridge coordinates from the database and sends them in the response. The function authenticates the user.

Request Details:

  • This is a GET request and does not require a request body.

Responses:

  • 500 If there is an error in the database query.
  • 404 If no bridge found
  • 200 If the coordinates are successfully retrieved.

/reward/getAllReward

Retrieves all rewards from the database and sends them in the response.

  • If the user is not authenticated, the request is terminated.
  • If there is an error during the database query, a 500 status code is returned.
  • If rewards are found, they are returned in the response with a success message.

Request Details:

  • This is a GET request and does not require a request body.

Responses:

  • 500 If there is an error during the database query.
  • 401 If the user is not authenticated.

/reward/purchase

Handles the purchase of a reward by a user. This function authenticates the user, checks the request parameters, verifies the user's points, and processes the purchase transaction.

  • If the purchase is successful, a discount code is generated and returned.

Request Body:

{
	"id_reward": "number",
	"user_id": "number"
}

Responses:

  • 500 error in select
  • 400 not enough points
  • 500 error to purchase
  • 200 reward purchased with discount code

/game/start

Starts a new game session for the authenticated user. This function determines whether the game is in story mode or random mode, selects an appropriate bridge, fetches 4 random photos for the chosen bridge, and registers a new game session for the user.

Request Details:

  • This is a GET request and does not require a request body.

Responses:

  • 404 If the user is not found in story mode.
  • 404 If no bridge is found in random mode.
  • 404 If 4 photos are not found for the selected bridge.
  • 200 If the game session is successfully started, with the selected bridge photos.

/game/end

Ends the current game session for the authenticated user by comparing the provided coordinates with the actual bridge coordinates. If the coordinates are correct, the user is awarded points based on the time taken to guess. The function also updates the user's points in the database and logs the points gained for the current day. Finally, it deletes the game session and returns the result along with the bridge information.

Request Body:

{
	"lat": "number",
	"lng": "number"
}

Responses:

  • 400 If the user has no active game session.
  • 500 If the bridge data is not found or the coordinate format is invalid.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •