Skip to content

Commit

Permalink
Merge pull request #1751 from bcgov/feat/prashanth-history-save-name-…
Browse files Browse the repository at this point in the history
…1693

Feat: LCFS - Update the history record to save name #1693
  • Loading branch information
prv-proton authored Jan 21, 2025
2 parents 2639a06 + 766292a commit 2475b91
Show file tree
Hide file tree
Showing 25 changed files with 255 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""Add display names to history tables.
Revision ID: c306137b57ab
Revises: 98d79870df6b
Create Date: 2025-01-20 16:23:50.597769
"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "c306137b57ab"
down_revision = "98d79870df6b"
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"admin_adjustment_history",
sa.Column(
"display_name",
sa.String(length=255),
nullable=True,
comment="Display name for the admin_adjustment history record",
),
)
op.add_column(
"compliance_report_history",
sa.Column(
"display_name",
sa.String(length=255),
nullable=True,
comment="Display name for the compliance report history",
),
)
op.add_column(
"initiative_agreement_history",
sa.Column(
"display_name",
sa.String(length=255),
nullable=True,
comment="Display name for the initiative agreement history record",
),
)
op.add_column(
"transfer_history",
sa.Column(
"display_name",
sa.String(length=255),
nullable=True,
comment="Display name for the transfer history record",
),
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("transfer_history", "display_name")
op.drop_column("initiative_agreement_history", "display_name")
op.drop_column("compliance_report_history", "display_name")
op.drop_column("admin_adjustment_history", "display_name")
# ### end Alembic commands ###
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

# revision identifiers, used by Alembic.
revision = "9785f4cfa370"
down_revision = "98d79870df6b"
down_revision = "c306137b57ab"
branch_labels = None
depends_on = None

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy import Column, Integer, ForeignKey, String
from sqlalchemy.orm import relationship

from lcfs.db.base import BaseModel, Auditable, EffectiveDates
Expand Down Expand Up @@ -27,6 +27,11 @@ class AdminAdjustmentHistory(BaseModel, Auditable, EffectiveDates):
ForeignKey("user_profile.user_profile_id"),
comment="Foreign key to user_profile",
)
display_name = Column(
String(255),
comment="Display name for the admin_adjustment history record",
nullable=True
)

admin_adjustment = relationship("AdminAdjustment", back_populates="history")
admin_adjustment_status = relationship("AdminAdjustmentStatus")
Expand Down
7 changes: 6 additions & 1 deletion backend/lcfs/db/models/compliance/ComplianceReportHistory.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy import Column, Integer, ForeignKey, String
from sqlalchemy.orm import relationship
from lcfs.db.base import BaseModel, Auditable

Expand Down Expand Up @@ -30,6 +30,11 @@ class ComplianceReportHistory(BaseModel, Auditable):
ForeignKey("user_profile.user_profile_id"),
comment="Identifier for the user associated with the status change",
)
display_name = Column(
String(255),
comment="Display name for the compliance report history",
nullable=True
)

compliance_report = relationship("ComplianceReport", back_populates="history")
status = relationship("ComplianceReportStatus")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy import Column, Integer, ForeignKey, String
from sqlalchemy.orm import relationship

from lcfs.db.base import BaseModel, Auditable, EffectiveDates
Expand Down Expand Up @@ -28,7 +28,11 @@ class InitiativeAgreementHistory(BaseModel, Auditable, EffectiveDates):
ForeignKey("user_profile.user_profile_id"),
comment="Foreign key to user_profile",
)

display_name = Column(
String(255),
comment="Display name for the initiative agreement history record",
nullable=True
)
initiative_agreement = relationship("InitiativeAgreement", back_populates="history")
initiative_agreement_status = relationship("InitiativeAgreementStatus")
user_profile = relationship("UserProfile")
7 changes: 6 additions & 1 deletion backend/lcfs/db/models/transfer/TransferHistory.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy import Column, Integer, ForeignKey, String
from sqlalchemy.orm import relationship

from lcfs.db.base import BaseModel, Auditable, EffectiveDates
Expand All @@ -23,6 +23,11 @@ class TransferHistory(BaseModel, Auditable, EffectiveDates):
ForeignKey("user_profile.user_profile_id"),
comment="Foreign key to user_profile",
)
display_name = Column(
String(255),
comment="Display name for the transfer history record",
nullable=True
)

transfer = relationship("Transfer", back_populates="transfer_history")
transfer_status = relationship("TransferStatus")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from unittest.mock import MagicMock, AsyncMock, Mock

from lcfs.web.api.initiative_agreement.schema import (
CreateInitiativeAgreementHistorySchema,
)
import pytest
from sqlalchemy.ext.asyncio import AsyncSession

Expand Down Expand Up @@ -105,7 +108,14 @@ async def test_get_initiative_agreement_status_by_name_not_found(

@pytest.mark.anyio
async def test_add_initiative_agreement_history(repository, mock_db_session):
result = await repository.add_initiative_agreement_history(1, 1, 1)
result = await repository.add_initiative_agreement_history(
CreateInitiativeAgreementHistorySchema(
initiative_agreement_id=1,
initiative_agreement_status_id=1,
user_profile_id=1,
display_name="History User",
)
)

assert isinstance(result, InitiativeAgreementHistory)
mock_db_session.add.assert_called_once()
Expand All @@ -119,7 +129,14 @@ async def test_update_initiative_agreement_history(repository, mock_db_session):
)
mock_db_session.scalar.return_value = mock_history

result = await repository.update_initiative_agreement_history(1, 1, 2)
result = await repository.update_initiative_agreement_history(
CreateInitiativeAgreementHistorySchema(
initiative_agreement_id=1,
initiative_agreement_status_id=1,
user_profile_id=2,
display_name="History User",
)
)

assert result.user_profile_id == 2
mock_db_session.add.assert_called_once_with(mock_history)
Expand Down
18 changes: 10 additions & 8 deletions backend/lcfs/tests/transfer/test_transfer_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from lcfs.db.models.transfer.TransferHistory import TransferHistory

from unittest.mock import MagicMock, AsyncMock
from lcfs.web.api.transfer.schema import TransferSchema
from lcfs.web.api.transfer.schema import CreateTransferHistorySchema, TransferSchema
from datetime import date
from lcfs.db.models.organization import Organization
from lcfs.db.models.transfer import TransferStatus, TransferCategory
Expand Down Expand Up @@ -117,18 +117,20 @@ async def test_update_transfer_success(transfer_repo):

@pytest.mark.anyio
async def test_add_transfer_history_success(transfer_repo):
transfer_id = 1
transfer_status_id = 1
user_profile_id = 1

result = await transfer_repo.add_transfer_history(
transfer_id, transfer_status_id, user_profile_id
CreateTransferHistorySchema(
transfer_id=1,
transfer_status_id=1,
user_profile_id=1,
display_name="History User",
)
)

assert isinstance(result, TransferHistory)
assert result.transfer_id == transfer_id
assert result.transfer_status_id == transfer_status_id
assert result.user_profile_id == user_profile_id
assert result.transfer_id == 1
assert result.transfer_status_id == 1
assert result.user_profile_id == 1


@pytest.mark.anyio
Expand Down
27 changes: 12 additions & 15 deletions backend/lcfs/web/api/admin_adjustment/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from lcfs.db.models.admin_adjustment.AdminAdjustmentHistory import (
AdminAdjustmentHistory,
)
from lcfs.web.api.admin_adjustment.schema import AdminAdjustmentSchema
from lcfs.web.api.admin_adjustment.schema import CreateAdminAdjustmentHistorySchema

from lcfs.db.dependencies import get_async_db_session
from lcfs.web.core.decorators import repo_handler
Expand Down Expand Up @@ -88,10 +88,7 @@ async def get_admin_adjustment_status_by_name(

@repo_handler
async def add_admin_adjustment_history(
self,
admin_adjustment_id: int,
admin_adjustment_status_id: int,
user_profile_id: int,
self, history: CreateAdminAdjustmentHistorySchema
) -> AdminAdjustmentHistory:
"""
Adds a new record to the admin adjustment history in the database.
Expand All @@ -105,20 +102,18 @@ async def add_admin_adjustment_history(
AdminAdjustmentHistory: The newly created admin adjustment history record.
"""
new_history_record = AdminAdjustmentHistory(
admin_adjustment_id=admin_adjustment_id,
admin_adjustment_status_id=admin_adjustment_status_id,
user_profile_id=user_profile_id,
admin_adjustment_id=history.admin_adjustment_id,
admin_adjustment_status_id=history.admin_adjustment_status_id,
user_profile_id=history.user_profile_id,
display_name=history.display_name,
)
self.db.add(new_history_record)
await self.db.flush()
return new_history_record

@repo_handler
async def update_admin_adjustment_history(
self,
admin_adjustment_id: int,
admin_adjustment_status_id: int,
user_profile_id: int,
self, history: CreateAdminAdjustmentHistorySchema
) -> AdminAdjustmentHistory:
"""
Updates an admin adjustment history record in the database.
Expand All @@ -134,15 +129,17 @@ async def update_admin_adjustment_history(
existing_history = await self.db.scalar(
select(AdminAdjustmentHistory).where(
and_(
AdminAdjustmentHistory.admin_adjustment_id == admin_adjustment_id,
AdminAdjustmentHistory.admin_adjustment_id
== history.admin_adjustment_id,
AdminAdjustmentHistory.admin_adjustment_status_id
== admin_adjustment_status_id,
== history.admin_adjustment_status_id,
)
)
)
existing_history.create_date = datetime.now()
existing_history.update_date = datetime.now()
existing_history.user_profile_id = user_profile_id
existing_history.user_profile_id = history.user_profile_id
existing_history.display_name = history.display_name
self.db.add(existing_history)
await self.db.flush()
return existing_history
Expand Down
7 changes: 7 additions & 0 deletions backend/lcfs/web/api/admin_adjustment/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class AdminAdjustmentHistorySchema(BaseSchema):
create_date: datetime
admin_adjustment_status: AdminAdjustmentStatusSchema
user_profile: HistoryUserSchema
display_name: Optional[str] = None

class Config:
from_attributes = True
Expand Down Expand Up @@ -63,3 +64,9 @@ class AdminAdjustmentCreateSchema(AdminAdjustmentBaseSchema):
class AdminAdjustmentUpdateSchema(AdminAdjustmentBaseSchema):
admin_adjustment_id: int
current_status: str

class CreateAdminAdjustmentHistorySchema(BaseSchema):
admin_adjustment_id: int
admin_adjustment_status_id: int
user_profile_id: int
display_name: str
23 changes: 13 additions & 10 deletions backend/lcfs/web/api/admin_adjustment/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
AdminAdjustmentCreateSchema,
AdminAdjustmentSchema,
AdminAdjustmentUpdateSchema,
CreateAdminAdjustmentHistorySchema,
)
from lcfs.web.api.admin_adjustment.repo import AdminAdjustmentRepository
from lcfs.web.exception.exceptions import DataNotFoundException
Expand Down Expand Up @@ -106,11 +107,12 @@ async def update_admin_adjustment(
)
# We only track history changes on Recommended and Approved, not Draft
if new_status.status != AdminAdjustmentStatusEnum.Draft:
await history_method(
admin_adjustment.admin_adjustment_id,
new_status.admin_adjustment_status_id,
self.request.user.user_profile_id,
)
await history_method(CreateAdminAdjustmentHistorySchema(
admin_adjustment_id=admin_adjustment.admin_adjustment_id,
admin_adjustment_status_id=new_status.admin_adjustment_status_id,
user_profile_id=self.request.user.user_profile_id,
display_name=(f"{self.request.user.first_name} {self.request.user.last_name}")
))

# Save the updated admin adjustment
updated_admin_adjustment = await self.repo.update_admin_adjustment(
Expand Down Expand Up @@ -148,11 +150,12 @@ async def create_admin_adjustment(
admin_adjustment = await self.repo.create_admin_adjustment(admin_adjustment)

if current_status.status == AdminAdjustmentStatusEnum.Recommended:
await self.repo.add_admin_adjustment_history(
admin_adjustment.admin_adjustment_id,
current_status.admin_adjustment_status_id,
self.request.user.user_profile_id,
)
await self.repo.add_admin_adjustment_history(CreateAdminAdjustmentHistorySchema(
admin_adjustment_id=admin_adjustment.admin_adjustment_id,
admin_adjustment_status_id=current_status.admin_adjustment_status_id,
user_profile_id=self.request.user.user_profile_id,
display_name=(f"{self.request.user.first_name} {self.request.user.last_name}")
))

# Create internal comment if provided
if admin_adjustment_data.internal_comment:
Expand Down
2 changes: 2 additions & 0 deletions backend/lcfs/web/api/compliance_report/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,13 @@ async def add_compliance_report_history(
history.create_date = datetime.now()
history.status_id = report.current_status_id
history.user_profile_id = user.user_profile_id
history.display_name = (f"{user.first_name} {user.last_name}")
else:
history = ComplianceReportHistory(
compliance_report_id=report.compliance_report_id,
status_id=report.current_status_id,
user_profile_id=user.user_profile_id,
display_name=(f"{user.first_name} {user.last_name}")
)
self.db.add(history)
await self.db.flush()
Expand Down
1 change: 1 addition & 0 deletions backend/lcfs/web/api/compliance_report/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class ComplianceReportHistorySchema(BaseSchema):
compliance_report_id: int
status: ComplianceReportStatusSchema
user_profile: ComplianceReportUserSchema
display_name: Optional[str] = None
create_date: datetime


Expand Down
Loading

0 comments on commit 2475b91

Please sign in to comment.