Skip to content

Commit

Permalink
Bug fix for submission after engagement is closed (#84)
Browse files Browse the repository at this point in the history
* bug fix for submission after engagement is closed

* fixing linting

* fxing linting on survey service

* linting
  • Loading branch information
VineetBala-AOT authored Sep 10, 2024
1 parent 83279f2 commit d02057d
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion analytics-api/setup.cfg
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion met-api/setup.cfg
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down
13 changes: 9 additions & 4 deletions met-api/src/met_api/models/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand All @@ -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
Expand All @@ -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
Expand Down
10 changes: 8 additions & 2 deletions met-api/src/met_api/services/submission_service.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion met-api/src/met_api/services/survey_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Service for survey management."""
from datetime import timedelta
from http import HTTPStatus

from met_api.constants.engagement_status import Status
Expand All @@ -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
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion met-api/tests/unit/models/test_survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand Down
2 changes: 1 addition & 1 deletion met-cron/requirements/repo-libraries.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
-e git+https://github.com/bcgov/met-public.git#egg=met-api&subdirectory=met-api
-e git+https://github.com/bcgov/epic-engage.git#egg=met-api&subdirectory=met-api
2 changes: 1 addition & 1 deletion met-cron/setup.cfg
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion notify-api/setup.cfg
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down

0 comments on commit d02057d

Please sign in to comment.