From d02057dc997d86f60239eadc25fc61592b3fd815 Mon Sep 17 00:00:00 2001 From: VineetBala-AOT <90332175+VineetBala-AOT@users.noreply.github.com> Date: Tue, 10 Sep 2024 16:01:13 -0700 Subject: [PATCH] Bug fix for submission after engagement is closed (#84) * bug fix for submission after engagement is closed * fixing linting * fxing linting on survey service * linting --- analytics-api/setup.cfg | 2 +- met-api/setup.cfg | 2 +- met-api/src/met_api/models/survey.py | 13 +++++++++---- met-api/src/met_api/services/submission_service.py | 10 ++++++++-- met-api/src/met_api/services/survey_service.py | 9 ++++++++- met-api/tests/unit/models/test_survey.py | 2 +- met-cron/requirements/repo-libraries.txt | 2 +- met-cron/setup.cfg | 2 +- notify-api/setup.cfg | 2 +- 9 files changed, 31 insertions(+), 13 deletions(-) diff --git a/analytics-api/setup.cfg b/analytics-api/setup.cfg index 95d34db33..502c0e0cb 100644 --- a/analytics-api/setup.cfg +++ b/analytics-api/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = met-analytics -url = https://github.com/bcgov/met-public +url = https://github.com/bcgov/epic-engage author = MET Team author_email = classifiers = diff --git a/met-api/setup.cfg b/met-api/setup.cfg index 581e98e82..589413e8a 100644 --- a/met-api/setup.cfg +++ b/met-api/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = app -url = https://github.com/bcgov/met-public +url = https://github.com/bcgov/epic-engage author = MET Team author_email = classifiers = diff --git a/met-api/src/met_api/models/survey.py b/met-api/src/met_api/models/survey.py index 378c52fc9..be4e7826e 100644 --- a/met-api/src/met_api/models/survey.py +++ b/met-api/src/met_api/models/survey.py @@ -5,7 +5,7 @@ from __future__ import annotations -from datetime import datetime +from datetime import datetime, timedelta from typing import Optional from sqlalchemy import ForeignKey, and_, asc, desc, func, or_ @@ -18,6 +18,7 @@ from met_api.models.pagination_options import PaginationOptions from met_api.models.survey_search_options import SurveySearchOptions from met_api.schemas.survey import SurveySchema +from met_api.utils.datetime import local_datetime from .base_model import BaseModel from .db import db @@ -43,11 +44,15 @@ class Survey(BaseModel): # pylint: disable=too-few-public-methods @classmethod def get_open(cls, survey_id) -> Survey: """Get an open survey.""" - now = datetime.now().date() # Get the current date without the timestamp + now = local_datetime().date() # Get the current PST date without the timestamp + + # Calculate the threshold time (8 hours after the end_date) + extended_end_date = Engagement.end_date + timedelta(days=1, hours=8) + survey: Survey = db.session.query(Survey).filter_by(id=survey_id) \ .join(Engagement) \ - .filter_by(status_id=Status.Published.value) \ - .filter(and_(func.date(Engagement.start_date) <= now, func.date(Engagement.end_date) >= now)) \ + .filter(or_(Engagement.status_id == Status.Published.value, Engagement.status_id == Status.Closed.value)) \ + .filter(and_(func.date(Engagement.start_date) <= now, local_datetime() <= extended_end_date)) \ .join(EngagementStatus) \ .first() return survey diff --git a/met-api/src/met_api/services/submission_service.py b/met-api/src/met_api/services/submission_service.py index 4c80d5bb3..356509ee5 100644 --- a/met-api/src/met_api/services/submission_service.py +++ b/met-api/src/met_api/services/submission_service.py @@ -1,5 +1,5 @@ """Service for submission management.""" -from datetime import datetime +from datetime import datetime, timedelta from http import HTTPStatus from flask import current_app @@ -30,6 +30,7 @@ from met_api.services.staff_user_service import StaffUserService from met_api.services.survey_service import SurveyService from met_api.utils import notification +from met_api.utils.datetime import local_datetime from met_api.utils.roles import Role from met_api.utils.template import Template from met_api.config import get_gc_notify_config @@ -144,7 +145,12 @@ def _validate_fields(submission): if not engagement: raise ValueError('Survey not linked to an Engagement') - if engagement.status_id != SubmissionStatus.Open.value: + # Calculate the threshold time (8 hours after the end_date) + extended_end_date = engagement.end_date + timedelta(days=1, hours=8) + # Get the current local datetime + current_datetime = local_datetime().replace(tzinfo=None) + if engagement.status_id != SubmissionStatus.Open.value and ( + engagement.status_id == SubmissionStatus.Closed.value and extended_end_date < current_datetime): raise ValueError('Engagement not open to submissions') @classmethod diff --git a/met-api/src/met_api/services/survey_service.py b/met-api/src/met_api/services/survey_service.py index 71282cc8e..4ab20be3f 100644 --- a/met-api/src/met_api/services/survey_service.py +++ b/met-api/src/met_api/services/survey_service.py @@ -1,4 +1,5 @@ """Service for survey management.""" +from datetime import timedelta from http import HTTPStatus from met_api.constants.engagement_status import Status @@ -15,6 +16,7 @@ from met_api.services.membership_service import MembershipService from met_api.services.object_storage_service import ObjectStorageService from met_api.services.report_setting_service import ReportSettingService +from met_api.utils.datetime import local_datetime from met_api.utils.roles import Role from met_api.utils.token_info import TokenInfo from ..exceptions.business_exception import BusinessException @@ -41,7 +43,12 @@ def get(cls, survey_id): engagement_model = EngagementModel.find_by_id(survey_model.engagement_id) if engagement_model: eng_id = engagement_model.id - if engagement_model.status_id == Status.Published.value: + # Calculate the threshold time (8 hours after the end_date) + extended_end_date = engagement_model.end_date + timedelta(days=1, hours=8) + # Get the current local datetime + current_datetime = local_datetime().replace(tzinfo=None) + if ((engagement_model.status_id == Status.Published.value) or + (engagement_model.status_id == Status.Closed.value and current_datetime <= extended_end_date)): # Published Engagement anyone can access. skip_auth = True else: diff --git a/met-api/tests/unit/models/test_survey.py b/met-api/tests/unit/models/test_survey.py index 8a6af02db..9ad966fc7 100644 --- a/met-api/tests/unit/models/test_survey.py +++ b/met-api/tests/unit/models/test_survey.py @@ -73,7 +73,7 @@ def test_get_open_survey_time_based(session): assert survey_new is not None, 'survey fetchable on the day of closure' # Move time forward by 1 day - day_after_time_delay = now + timedelta(days=1) + day_after_time_delay = now + timedelta(days=1, hours=8, minutes=1) with freeze_time(day_after_time_delay): survey_new = SurveyModel.get_open(survey.id) assert survey_new is None, 'survey is not fetchable after one day of closure.' diff --git a/met-cron/requirements/repo-libraries.txt b/met-cron/requirements/repo-libraries.txt index 21bda45a6..4ca7e9351 100644 --- a/met-cron/requirements/repo-libraries.txt +++ b/met-cron/requirements/repo-libraries.txt @@ -1 +1 @@ --e git+https://github.com/bcgov/met-public.git#egg=met-api&subdirectory=met-api \ No newline at end of file +-e git+https://github.com/bcgov/epic-engage.git#egg=met-api&subdirectory=met-api \ No newline at end of file diff --git a/met-cron/setup.cfg b/met-cron/setup.cfg index 2f55bc0ff..7947cf3b7 100644 --- a/met-cron/setup.cfg +++ b/met-cron/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = met-cron -url = https://github.com/bcgov/met-public/ +url = https://github.com/bcgov/epic-engage/ author = MET Team author_email = classifiers = diff --git a/notify-api/setup.cfg b/notify-api/setup.cfg index ed64e975c..6f8bbc14a 100644 --- a/notify-api/setup.cfg +++ b/notify-api/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = app -url = https://github.com/bcgov/met-public +url = https://github.com/bcgov/epic-engage author = MET Team author_email = classifiers =