Skip to content

Commit

Permalink
[#192] Add updated_by column n implement updated_by in update case
Browse files Browse the repository at this point in the history
endpoint
  • Loading branch information
wayangalihpratama committed Jan 5, 2024
1 parent 073ff85 commit 652e071
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""add updated_by column to case table
Revision ID: 9bafa33c5c7e
Revises: c50aa947afb3
Create Date: 2024-01-05 04:04:12.817493
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "9bafa33c5c7e"
down_revision: Union[str, None] = "c50aa947afb3"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
op.add_column(
"case",
sa.Column(
"updated_by", sa.Integer(), sa.ForeignKey("user.id"), nullable=True
),
)
op.create_foreign_key(
"case_updated_by_user_constraint",
"case",
"user",
["updated_by"],
["id"],
ondelete="CASCADE",
)


def downgrade() -> None:
op.drop_constraint(
"case_updated_by_user_constraint", "case", type_="foreignkey"
)
op.drop_column("case", "updated_by")
12 changes: 12 additions & 0 deletions backend/db/crud_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ def get_case_by_id(session: Session, id: int) -> CaseDict:
return case


def case_updated_by(session: Session, case_id: int, user_id: int) -> CaseDict:
case = get_case_by_id(session=session, id=case_id)
case.updated_by = user_id
case.updated_at = datetime.now()
session.commit()
session.flush()
session.refresh(case)
return case


def update_case(session: Session, id: int, payload: CaseBase) -> CaseDict:
case = get_case_by_id(session=session, id=id)
case.name = payload.name
Expand All @@ -147,6 +157,8 @@ def update_case(session: Session, id: int, payload: CaseBase) -> CaseDict:
case.multiple_commodities = 1 if payload.multiple_commodities else 0
case.logo = payload.logo
case.private = 1 if payload.private else 0
# don't update updated_at value
case.updated_at = case.updated_at
# reporting period
if payload.reporting_period:
case.reporting_period = payload.reporting_period
Expand Down
11 changes: 10 additions & 1 deletion backend/models/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class Case(Base):
server_default=func.now(),
onupdate=func.now(),
)
updated_by = Column(Integer, ForeignKey("user.id"), nullable=True)

case_commodities = relationship(
CaseCommodity,
Expand Down Expand Up @@ -156,7 +157,15 @@ class Case(Base):
# backref='cases'
# )
created_by_user = relationship(
"User", cascade="all, delete", passive_deletes=True, backref="cases"
"User",
foreign_keys=[created_by],
cascade="all, delete",
passive_deletes=True,
backref="cases",
)
updated_by_user = relationship(
"User",
foreign_keys=[updated_by],
)

def __init__(
Expand Down
19 changes: 16 additions & 3 deletions backend/models/user.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import enum
import json
from db.connection import Base
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, SmallInteger, Enum
from sqlalchemy import (
Column,
Integer,
String,
DateTime,
ForeignKey,
SmallInteger,
Enum,
)
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from typing import Optional, List
Expand Down Expand Up @@ -128,7 +136,10 @@ class User(Base):
invitation_id = Column(String, nullable=True)
created_at = Column(DateTime, nullable=False, server_default=func.now())
updated_at = Column(
DateTime, nullable=False, server_default=func.now(), onupdate=func.now()
DateTime,
nullable=False,
server_default=func.now(),
onupdate=func.now(),
)

user_organisation = relationship(
Expand Down Expand Up @@ -197,7 +208,9 @@ def to_user_info(self) -> UserInfo:
business_unit_detail = [
bu.to_business_unit_detail for bu in self.user_business_units
]
business_unit_detail = business_unit_detail if business_unit_detail else None
business_unit_detail = (
business_unit_detail if business_unit_detail else None
)

case_access = []
if self.user_case_access:
Expand Down
7 changes: 6 additions & 1 deletion backend/routes/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,18 @@ def update_Case(
req: Request,
case_id: int,
payload: CaseBase,
updated: Optional[bool] = Query(None),
session: Session = Depends(get_session),
credentials: credentials = Depends(security),
):
verify_case_editor(
user = verify_case_editor(
session=session, authenticated=req.state.authenticated, case_id=case_id
)
case = crud_case.update_case(session=session, id=case_id, payload=payload)
if updated:
case = crud_case.case_updated_by(
session=session, case_id=case_id, user_id=user.id
)
return case.serialize


Expand Down

0 comments on commit 652e071

Please # to comment.