Skip to content

Commit

Permalink
Changes for sending project updates when dates changes (#2291)
Browse files Browse the repository at this point in the history
  • Loading branch information
saravanpa-aot authored Sep 29, 2023
1 parent 1a0059c commit 1620dba
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
3 changes: 3 additions & 0 deletions met-api/src/met_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ class _Config(): # pylint: disable=too-few-public-methods
EPIC_MILESTONE = os.getenv('EPIC_MILESTONE')
EPIC_KC_CLIENT_ID = os.getenv('EPIC_KC_CLIENT_ID')

# Timezone in BC
LEGISLATIVE_TIMEZONE = os.getenv('LEGISLATIVE_TIMEZONE', 'America/Vancouver')


class DevConfig(_Config): # pylint: disable=too-few-public-methods
"""Dev Config."""
Expand Down
4 changes: 2 additions & 2 deletions met-api/src/met_api/services/engagement_metadata_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def create_metadata(request_json: dict):
metadata_model.commit()
updated_metadata: EngagementMetadataModel = metadata_model.find_by_id(metadata_model.engagement_id)
# publish changes to EPIC
ProjectService.update_project_info(updated_metadata.project_id, updated_metadata.engagement_id)
ProjectService.update_project_info(updated_metadata.engagement_id)
return updated_metadata

@staticmethod
Expand Down Expand Up @@ -79,6 +79,6 @@ def update_metadata(data: dict):
updated_metadata = EngagementMetadataService._create_metadata_model(data)

# publish changes to EPIC
ProjectService.update_project_info(updated_metadata.project_id, updated_metadata.engagement_id)
ProjectService.update_project_info(updated_metadata.engagement_id)

return updated_metadata
7 changes: 7 additions & 0 deletions met-api/src/met_api/services/engagement_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from met_api.services.engagement_settings_service import EngagementSettingsService
from met_api.services.engagement_slug_service import EngagementSlugService
from met_api.services.object_storage_service import ObjectStorageService

from met_api.services.project_service import ProjectService
from met_api.utils import email_util, notification
from met_api.utils.enums import SourceAction, SourceType
from met_api.utils.roles import Role
Expand Down Expand Up @@ -241,6 +243,11 @@ def edit_engagement(data: dict):
updated_engagement = EngagementModel.edit_engagement(data)
if not updated_engagement:
raise ValueError('Engagement to update was not found')

has_epic_fields_getting_updated = 'end_date' in data or 'start_date' in data
if has_epic_fields_getting_updated:
ProjectService.update_project_info(updated_engagement.id)

if survey_block:
EngagementService._save_or_update_eng_block(engagement_id, survey_block)
return EngagementModel.find_by_id(engagement_id)
Expand Down
14 changes: 11 additions & 3 deletions met-api/src/met_api/services/project_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
from met_api.services.email_verification_service import EmailVerificationService
from met_api.services.rest_service import RestService
from met_api.utils import notification
from met_api.utils.datetime import convert_and_format_to_utc_str


class ProjectService:
"""Project management service."""

@staticmethod
def update_project_info(project_id: str, eng_id: str) -> EngagementModel:
def update_project_info(eng_id: str) -> EngagementModel:
"""Publish new comment period to EPIC/EAO system."""
logger = logging.getLogger(__name__)

Expand All @@ -24,8 +25,13 @@ def update_project_info(project_id: str, eng_id: str) -> EngagementModel:
if not is_eao_environment:
return

engagement_metadata: EngagementMetadataModel
engagement, engagement_metadata = ProjectService._get_engagement_and_metadata(eng_id)

if not (project_id := engagement_metadata.project_id):
# EPIC is not interested in the data without project Id.So Skip the process.
return

epic_comment_period_payload = ProjectService._construct_epic_payload(engagement, project_id)

eao_service_account_token = ProjectService._get_eao_service_account_token()
Expand Down Expand Up @@ -60,8 +66,10 @@ def _get_engagement_and_metadata(eng_id: str):
@staticmethod
def _construct_epic_payload(engagement, project_id):
site_url = notification.get_tenant_site_url(engagement.tenant_id)
start_date_utc = engagement.start_date.isoformat()
end_date_utc = engagement.end_date.isoformat()
# the dates have to be converted to UTC since EPIC accepts UTC date and converts to PST
start_date_utc = convert_and_format_to_utc_str(engagement.start_date)
end_date_utc = convert_and_format_to_utc_str(engagement.end_date)

epic_comment_period_payload = {
'isMet': 'true',
# metURL is the public url using slug
Expand Down
22 changes: 20 additions & 2 deletions met-api/src/met_api/utils/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Datetime object helper."""
from datetime import datetime

import pytz
from flask import current_app

from datetime import datetime


def local_datetime():
Expand All @@ -29,3 +30,20 @@ def utc_datetime():
utcmoment = datetime.utcnow().replace(tzinfo=pytz.utc)
now = utcmoment.astimezone(pytz.timezone('UTC'))
return now


def convert_and_format_to_utc_str(date_val: datetime, dt_format='%Y-%m-%d %H:%M:%S', timezone_override=None):
"""Convert a datetime object to UTC and format it as a string."""
tz_name = timezone_override or current_app.config['LEGISLATIVE_TIMEZONE']
tz_local = pytz.timezone(tz_name)

# Assume the input datetime is in the local time zone
date_val = tz_local.localize(date_val)

# Convert to UTC
date_val_utc = date_val.astimezone(pytz.UTC)

# Format as a string
utc_datetime_str = date_val_utc.strftime(dt_format)

return utc_datetime_str

0 comments on commit 1620dba

Please sign in to comment.