Skip to content

Commit

Permalink
FWF-3766 and FWF-3768 added published on process History and fixed pr…
Browse files Browse the repository at this point in the history
…ocessListing (#2281)

* FWF-3766 and FWF-3768 added published on processHistory and fixed processListing

* changed the function to get latest row and fixed publish api bug

* removed print
  • Loading branch information
shuhaib-aot authored Oct 20, 2024
1 parent bffda9b commit f0ecfd0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
41 changes: 39 additions & 2 deletions forms-flow-api/src/formsflow_api/models/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from flask_sqlalchemy.query import Query
from formsflow_api_utils.utils import FILTER_MAPS, add_sort_filter
from formsflow_api_utils.utils.user_context import UserContext, user_context
from sqlalchemy import LargeBinary, and_, desc, or_
from sqlalchemy import LargeBinary, and_, desc, func, or_
from sqlalchemy.dialects.postgresql import ENUM

from .audit_mixin import AuditDateTimeMixin, AuditUserMixin
Expand Down Expand Up @@ -132,6 +132,20 @@ def filter_conditions(cls, **filters):
query = cls.query.filter(*filter_conditions) if filter_conditions else cls.query
return query

@classmethod
def subquery_for_getting_latest_process(cls):
"""Subquery to get the latest process by parent_process_key."""
subquery = (
db.session.query(
cls.parent_process_key,
func.max(cls.major_version).label("latest_major_version"),
func.max(cls.minor_version).label("latest_minor_version"),
)
.group_by(cls.parent_process_key)
.subquery()
)
return subquery

@classmethod
def find_all_process( # pylint: disable=too-many-arguments, too-many-positional-arguments
cls,
Expand All @@ -144,7 +158,15 @@ def find_all_process( # pylint: disable=too-many-arguments, too-many-positional
):
"""Find all processes."""
query = cls.filter_conditions(**filters)
query = query.filter(cls.status_changed.is_(False))
# take the latest row by grouping parent_process_key
subquery = cls.subquery_for_getting_latest_process()
query = query.join(
subquery,
(cls.parent_process_key == subquery.c.parent_process_key)
& (cls.major_version == subquery.c.latest_major_version)
& (cls.minor_version == subquery.c.latest_minor_version),
)

if is_subflow:
query = query.filter(cls.is_subflow.is_(True))
query = cls.auth_query(query=query)
Expand All @@ -167,6 +189,21 @@ def get_latest_version_by_key(cls, process_key):

return query

@classmethod
def fetch_published_history_by_parent_process_key(
cls, parent_process_key: str
) -> Process:
"""Fetch published version of a process by parent_process_key."""
query = cls.auth_query(
cls.query.filter(
and_(
cls.parent_process_key == parent_process_key,
cls.status == ProcessStatus.PUBLISHED,
)
)
)
return query.all()

@classmethod
def get_latest_version_by_parent_key(cls, parent_process_key):
"""Get latest version of process."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Meta: # pylint: disable=too-few-public-methods
minor_version = fields.Int(data_key="minorVersion")
process_type = fields.Method("get_process_type", data_key="processType")
isMajor = fields.Method("get_is_major", dump_only=True)
published_on = fields.Str(data_key="publishedOn", dump_only=True)
published_by = fields.Str(data_key="publishedBy", dump_only=True)

def get_is_major(self, obj):
"""Determine if the version is major."""
Expand Down
22 changes: 21 additions & 1 deletion forms-flow-api/src/formsflow_api/services/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import json


from flask import current_app
from formsflow_api_utils.exceptions import BusinessException
from formsflow_api_utils.utils.user_context import UserContext, user_context
Expand Down Expand Up @@ -319,7 +318,25 @@ def get_all_history(parent_process_key: str, request_args):
process_histories, count = Process.fetch_histories_by_parent_process_key(
parent_process_key, page_no, limit
)

published_histories = Process.fetch_published_history_by_parent_process_key(
parent_process_key
)

if process_histories:
# populating published on and publised by to the history
published_history_dict = {
f"{history.major_version}.{history.minor_version}": history
for history in published_histories
}
for history in process_histories:
published_history = published_history_dict.get(
f"{history.major_version}.{history.minor_version}"
)
if published_history:
history.published_on = published_history.created
history.published_by = published_history.created_by

process_history_schema = ProcessHistorySchema(many=True)
return process_history_schema.dump(process_histories), count
raise BusinessException(BusinessErrorCode.PROCESS_ID_NOT_FOUND)
Expand Down Expand Up @@ -349,6 +366,9 @@ def publish(cls, process_id, **kwargs):
"""Publish by process_id."""
user: UserContext = kwargs["user"]
process = cls.validate_process_by_id(process_id)
latest = Process.get_latest_version_by_parent_key(process.parent_process_key)
if process.id != latest.id:
raise BusinessException(BusinessErrorCode.PROCESS_NOT_LATEST_VERSION)
FormProcessMapperService.update_process_status(process, "PUBLISHED", user)
FormProcessMapperService().deploy_process(
process.name, process.process_data, user.tenant_key, user.bearer_token
Expand Down

0 comments on commit f0ecfd0

Please sign in to comment.