Skip to content

Commit

Permalink
Add returned_date field in loan module (#352)
Browse files Browse the repository at this point in the history
This change is required for the PR
aeecleclair/Titan#310.

It should be merged after #360
  • Loading branch information
julien4215 authored Mar 16, 2024
1 parent 0a3d4a1 commit 78196c8
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
11 changes: 9 additions & 2 deletions app/modules/loan/cruds_loan.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections.abc import Sequence
from datetime import datetime

from sqlalchemy import delete, func, select, update
from sqlalchemy.exc import IntegrityError
Expand Down Expand Up @@ -190,13 +191,19 @@ async def update_loan(

async def update_loan_returned_status(
loan_id: str,
returned: bool,
db: AsyncSession,
returned: bool,
returned_date: datetime,
):
await db.execute(
update(models_loan.Loan)
.where(models_loan.Loan.id == loan_id)
.values({"returned": returned}),
.values(
{
"returned": returned,
"returned_date": returned_date,
},
),
)
await db.commit()

Expand Down
6 changes: 3 additions & 3 deletions app/modules/loan/endpoints_loan.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import uuid
from datetime import timedelta
from datetime import UTC, datetime, timedelta
from typing import TYPE_CHECKING

from fastapi import Depends, HTTPException
Expand Down Expand Up @@ -809,7 +809,6 @@ async def return_loan(
detail=f"Unauthorized to manage {loan.loaner_id} loaner",
)

# We need to update the item loaned quantity thanks to the quantity in the loan content
# We need to update the item loaned quantity thanks to the quantity in the loan content
for item in loan.items:
loan_content = await cruds_loan.get_loan_content_by_loan_id_item_id(
Expand All @@ -825,8 +824,9 @@ async def return_loan(

await cruds_loan.update_loan_returned_status(
loan_id=loan_id,
returned=True,
db=db,
returned=True,
returned_date=datetime.now(UTC),
)


Expand Down
1 change: 1 addition & 0 deletions app/modules/loan/models_loan.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class Loan(Base):
notes: Mapped[str | None] = mapped_column(TEXT)
caution: Mapped[str | None] = mapped_column(String)
returned: Mapped[bool] = mapped_column(Boolean, nullable=False)
returned_date: Mapped[date | None] = mapped_column(Date, nullable=True)

items: Mapped[list["Item"]] = relationship(
"Item",
Expand Down
1 change: 1 addition & 0 deletions app/modules/loan/schemas_loan.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class Loan(LoanBase):

id: str
returned: bool
returned_date: date | None
items_qty: list[ItemQuantity]
borrower: schemas_core.CoreUserSimple
loaner: Loaner
Expand Down
47 changes: 47 additions & 0 deletions migrations/versions/5-loan_return_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Add a returned date to the loan table
Revision ID: 2fcadbe2f0ad
Revises: f17e6182b0a9
Create Date: 2024-03-01 23:33:20.431056
"""

from collections.abc import Sequence
from datetime import UTC, datetime

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "2fcadbe2f0ad"
down_revision: str | None = "3f9843f165e9"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
# Schema migration
op.add_column("loan", sa.Column("returned_date", sa.Date(), nullable=True))

# Data migration
t_loan = sa.Table(
"loan",
sa.MetaData(),
sa.Column("id", sa.String()),
sa.Column("returned", sa.Boolean()),
sa.Column("returned_date", sa.DateTime(timezone=True)),
)

conn = op.get_bind()
res = conn.execute(sa.select(t_loan.c.id).where(t_loan.c.returned)).fetchall()
for id_ in res:
conn.execute(
t_loan.update()
.where(t_loan.c.id == id_)
.values(returned_date=datetime.now(UTC)),
)


def downgrade() -> None:
# Schema migration
op.drop_column("loan", "returned_date")

0 comments on commit 78196c8

Please sign in to comment.