To install the project, you need to have Node.js, npm, and a postgres database set up on your machine.
npm installIn 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=ourApiKeyForDescriptionCreate the schema on your database:
npx drizzle-kit pushAfter that, you can run the project:
npm run startConfigure the project for your usage.
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
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
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.
To run the project on the server:
npm run startTo run the project in the development environment:
npm run devTo test the project:
npm run testThe API operates using ExpressTS.
Below is a list of available endpoints:
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
401status code is returned.
{
"mail": "string",
"password": "string" (hashed password)
}401Server error during the request.401Incorrect email or password.200Request successful.
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.
{
"email": "string",
"password": "string" (hashed password),
"accept_rgpd": true
}400If any parameter is missing or invalid.400rgpd is not accepted400bad email format400account already exist500If there is an error inserting the user into the database.200If the user is successfully created.
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.
- This is a GET request and does not require a request body.
500If there is an internal server error or an error in the select query.404If the user is not found.200If the user information is successfully retrieved.
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.
- This is a GET request and does not require a request body.
- ̀
400If the user is not found. - ̀
500If there is an error during the database queries. - ̀
200If the points logs are successfully retrieved.
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.
{
"newEmail": "string"
}400If the new email format is invalid or the email is already in use.500If there is an error during the database operations.200If the email is successfully updated.
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.
{
"newPassword": "string" (hashed password)
}400If the new password format is invalid.500If there is an error updating the password in the database.200If the password is successfully updated.
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.
- This is a GET request and does not require a request body.
500If there is an error during the deletion process.200If the user is successfully deleted.
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.
{
"name": "string"
}400If the bridge name is missing or empty.404If photos for the specified bridge are not found.500If the bridge is not found or an internal server error occurs.
Retrieves all bridge coordinates from the database and sends them in the response. The function authenticates the user.
- This is a GET request and does not require a request body.
500If there is an error in the database query.404If no bridge found200If the coordinates are successfully retrieved.
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.
- This is a GET request and does not require a request body.
500If there is an error during the database query.401If the user is not authenticated.
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.
{
"id_reward": "number",
"user_id": "number"
}500error in select400not enough points500error to purchase200reward purchased with discount code
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.
- This is a GET request and does not require a request body.
404If the user is not found in story mode.404If no bridge is found in random mode.404If 4 photos are not found for the selected bridge.200If the game session is successfully started, with the selected bridge photos.
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.
{
"lat": "number",
"lng": "number"
}400If the user has no active game session.500If the bridge data is not found or the coordinate format is invalid.