Skip to content

Renting

Raghad.Matar edited this page Oct 31, 2024 · 7 revisions

Renting Process Documentation

Overview

The renting process managing the renting process between the owner and the renter. This includes the ability to send a renting request from the renter to the user who notified about this, then the owner accept or deny requests from renters and notify them of the decision.

Function: createRental

Purpose

This function facilitates the creation of a new rental request by extracting necessary details from the request body, validating the inputs, calculating the rental price, and inserting the rental record into the database.

Parameters

  • Request (req): Contains the authorization headers and the body with rental details.
  • Response (res): Used to send back HTTP responses to the client.

Workflow

  1. Authorization Check:

    • Extracts the token from the Authorization header.
    • Verifies the token and retrieves the renter's user ID. Responds with a 401 status if the token is missing or invalid.
  2. Input Validation:

    • Checks for the presence of item_id,code, start_date, and end_date in the request body.
    • Responds with a 400 status if any of these fields are missing.
  3. Date Validation:

    • Validates that the start_date is before the end_date. Responds with a 400 status if the dates are invalid.
  4. Item Availability Check:

    • Queries the database to check if the item is available for the specified rental period.
    • Responds with a 404 status if the item is not found or not available.
  5. Coupon Availability Check:

    • Retrieves the coupon discount percent, and its valid time.
    • Check if the coupon is valid at current time by check if current date in in validation date range.
  6. Calculate Rental Price:

    • Retrieves the daily rental price of the item from the database.
    • Calculates the total price based on the duration of the rental (difference between start_date and end_date) taking the coupon discount in calculations and initializes the late fee to zero.
  7. Insert Rental Record:

    • Inserts the new rental request into the rentals table with a status of pending, along with the calculated total price, renter ID, and owner ID (extracted from the item details).
  8. Response:

    • Sends a 201 status response with a success message, rental ID, and total price.
  9. Notify The Owner:

    • Sends a notification to the owner of the item, that he has a new renting request to check.

Example Request

POST /api/rentals/Rentals
Authorization: Bearer <token>
Content-Type: application/json

{
  "item_id": 3,
  "start_date": "2024-11-01",
  "end_date": "2024-11-07",
  "code":"SAVE30"
}

Function: acceptOrDenyRental

Purpose

This function is responsible for handling rental requests by verifying user authorization, checking the validity of the rental request, updating the rental status, and sending notifications to the renters.

Parameters

  • Request (req): Contains the authorization headers and the body with rental details.
  • Response (res): Used to send back HTTP responses to the client.

Workflow

  1. Authorization Check:

    • Extracts the token from the Authorization header.
    • Verifies the token and retrieves the user ID. Responds with a 401 status if the token is missing or invalid.
  2. Input Validation:

    • Checks for the presence of rental_id and action (expected values: accept or deny).
    • Responds with a 400 status if either is missing.
    • Validates the action to ensure it is either accept or deny.
  3. Rental Existence and Ownership Check:

    • Queries the database to find the rental record based on rental_id and the owner's user ID.
    • Responds with a 404 status if the rental does not exist or if the user is not the owner.
  4. Update Rental Status:

    • Depending on the action:
      • If accept, updates the rental status to ongoing and sets the item's availability to unavailable.
      • If deny, updates the rental status to cancelled.
  5. Send Notification:

    • Inserts a notification into the notifications table to tell the renter about the decision (either accepted or denied).
  6. Response:

    • Sends a 200 status response with a success message and the new rental status.

Example Request

POST /api/rentals/decision
Authorization: Bearer <token>
Content-Type: application/json

{
  "rental_id": 2,
  "action": "accept" 
}

Clone this wiki locally