Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions rental_backend/routes/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from rental_backend import settings
from rental_backend.exceptions import ObjectNotFound
from rental_backend.models.db import Item, ItemType
from rental_backend.models.db import Event, Item, ItemType, RentalSession, RentStatus, Strike
from rental_backend.schemas.base import StatusResponseModel
from rental_backend.schemas.models import ItemGet, ItemPost
from rental_backend.settings import Settings, get_settings
Expand Down Expand Up @@ -105,10 +105,21 @@ async def delete_item(

Raises **ObjectNotFound** if the item with the specified ID is not found.
"""
item = Item.get(id, session=db.session)
if item is None:
rental_sessions = db.session.query(RentalSession).filter(RentalSession.item_id == id)
session = rental_sessions.filter(
RentalSession.status.in_([RentStatus.ACTIVE, RentStatus.OVERDUE, RentStatus.RESERVED])
).one_or_none()
if session is not None:
raise ObjectNotFound(Item, id)
Item.delete(id, session=db.session)
for rental_session in rental_sessions:
RentalSession.delete(rental_session.id, session=db.session)
strikes = db.session.query(Strike).filter(Strike.session_id == rental_session.id)
for strike in strikes:
Strike.delete(strike.id, session=db.session)
events = db.session.query(Event).filter(Event.session_id == rental_session.id)
for event in events:
Event.delete(event.id, session=db.session)
ActionLogger.log_event(
user_id=None,
admin_id=user.get('id'),
Expand Down
30 changes: 30 additions & 0 deletions rental_backend/routes/rental_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
SessionExists,
)
from rental_backend.models.db import Item, ItemType, RentalSession, Strike
from rental_backend.schemas.base import StatusResponseModel
from rental_backend.schemas.models import (
RentalSessionGet,
RentalSessionPatch,
Expand Down Expand Up @@ -470,6 +471,35 @@ async def get_my_sessions(
)


@rental_session.delete("/{session_id}", response_model=StatusResponseModel)
async def delete_rental_session(session_id: int, user=Depends(UnionAuth(scopes=["rental.session.admin"]))):
"""
Deletes a session.

Scopes: `["rental.session.admin"]`

- **session_id**: The ID of the rental session to delete.

Returns the deleted rental session.

Raises **ForbiddenAction** if the session is in RESERVED or ACTIVE status.
Raises **ObjectNotFound** if the session does not exist.
"""
session = RentalSession.get(id=session_id, session=db.session)
if (
session.status == RentStatus.ACTIVE
or session.status == RentStatus.RESERVED
or session.status == RentStatus.OVERDUE
):
raise ForbiddenAction(RentalSession)
RentalSession.delete(id=session_id, session=db.session)
return StatusResponseModel(
status="Success",
message=f"Rental session with id = {session_id} has been deleted",
ru="Сессия удалена из RentalAPI",
)


@rental_session.delete(
"/{session_id}/cancel", response_model=RentalSessionGet, dependencies=[Depends(check_sessions_expiration)]
)
Expand Down
Loading