-
Notifications
You must be signed in to change notification settings - Fork 1
Renting
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.
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.
- Request (req): Contains the authorization headers and the body with rental details.
- Response (res): Used to send back HTTP responses to the client.
-
Authorization Check:
- Extracts the token from the
Authorizationheader. - Verifies the token and retrieves the renter's user ID. Responds with a
401status if the token is missing or invalid.
- Extracts the token from the
-
Input Validation:
- Checks for the presence of
item_id,code,start_date, andend_datein the request body. - Responds with a
400status if any of these fields are missing.
- Checks for the presence of
-
Date Validation:
- Validates that the
start_dateis before theend_date. Responds with a400status if the dates are invalid.
- Validates that the
-
Item Availability Check:
- Queries the database to check if the item is available for the specified rental period.
- Responds with a
404status if the item is not found or not available.
-
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.
-
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_dateandend_date) taking the coupon discount in calculations and initializes the late fee to zero.
-
Insert Rental Record:
- Inserts the new rental request into the
rentalstable with a status ofpending, along with the calculated total price, renter ID, and owner ID (extracted from the item details).
- Inserts the new rental request into the
-
Response:
- Sends a
201status response with a success message, rental ID, and total price.
- Sends a
-
Notify The Owner:
- Sends a notification to the owner of the item, that he has a new renting request to check.
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"
}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.
- Request (req): Contains the authorization headers and the body with rental details.
- Response (res): Used to send back HTTP responses to the client.
-
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.
-
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.
-
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.
-
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.
- Depending on the action:
-
Send Notification:
- Inserts a notification into the notifications table to tell the renter about the decision (either accepted or denied).
-
Response:
- Sends a 200 status response with a success message and the new rental status.
POST /api/rentals/decision
Authorization: Bearer <token>
Content-Type: application/json
{
"rental_id": 2,
"action": "accept"
}