From 78196c83d06ddee0eb9e69d72ffe9e7a0d51aa74 Mon Sep 17 00:00:00 2001 From: julien4215 <120588494+julien4215@users.noreply.github.com> Date: Sat, 16 Mar 2024 12:56:06 +0100 Subject: [PATCH] Add returned_date field in loan module (#352) This change is required for the PR https://github.com/aeecleclair/Titan/pull/310. It should be merged after #360 --- app/modules/loan/cruds_loan.py | 11 +++++- app/modules/loan/endpoints_loan.py | 6 +-- app/modules/loan/models_loan.py | 1 + app/modules/loan/schemas_loan.py | 1 + migrations/versions/5-loan_return_date.py | 47 +++++++++++++++++++++++ 5 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 migrations/versions/5-loan_return_date.py diff --git a/app/modules/loan/cruds_loan.py b/app/modules/loan/cruds_loan.py index 232e797c3..308294c78 100644 --- a/app/modules/loan/cruds_loan.py +++ b/app/modules/loan/cruds_loan.py @@ -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 @@ -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() diff --git a/app/modules/loan/endpoints_loan.py b/app/modules/loan/endpoints_loan.py index c63673ea5..9b64f1f1d 100644 --- a/app/modules/loan/endpoints_loan.py +++ b/app/modules/loan/endpoints_loan.py @@ -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 @@ -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( @@ -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), ) diff --git a/app/modules/loan/models_loan.py b/app/modules/loan/models_loan.py index 1bffd173a..4f4130814 100644 --- a/app/modules/loan/models_loan.py +++ b/app/modules/loan/models_loan.py @@ -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", diff --git a/app/modules/loan/schemas_loan.py b/app/modules/loan/schemas_loan.py index 04d049010..8aaaf3971 100644 --- a/app/modules/loan/schemas_loan.py +++ b/app/modules/loan/schemas_loan.py @@ -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 diff --git a/migrations/versions/5-loan_return_date.py b/migrations/versions/5-loan_return_date.py new file mode 100644 index 000000000..a77fe9e10 --- /dev/null +++ b/migrations/versions/5-loan_return_date.py @@ -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")