Skip to content

Commit

Permalink
Merge pull request #328 from GSA/feature/fix_solicitation_updating
Browse files Browse the repository at this point in the history
Ticket 157: Fixing the Update Solicitation in History
  • Loading branch information
BuckinghamAJ authored Jan 8, 2025
2 parents d80b00f + 063e213 commit fc463f4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 29 deletions.
55 changes: 32 additions & 23 deletions src/fbo_scraper/db/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@ def posted_date_to_datetime(posted_date_string):
logger.error("Unable to parse posted date. Returning current utc datetime.")
return datetime.now(timezone.utc)

def is_opp_update(existing_date, posted_date, sol_existed_in_db):
def is_opp_update(previous_posting: datetime, current_posting: datetime, sol_existed_in_db: bool):
if (
sol_existed_in_db
and existing_date
and posted_date
and existing_date < posted_date_to_datetime(posted_date)
and previous_posting
and current_posting
and previous_posting < current_posting
):
return True
return False
Expand Down Expand Up @@ -277,28 +277,35 @@ def update_solicitation_history(solicitation,
now: datetime,
in_database: bool = False,
posted_at: datetime = None,
):

if isinstance(posted_at, str):
posted_at = posted_date_to_datetime(posted_at)

original_sol_date = posted_at or now # need this later to see if this is an update or not
previous_posting: datetime = None):
posted_at = convert_to_datetime(posted_at)
previous_posting = convert_to_datetime(previous_posting)
now_datetime_string = now.strftime("%Y-%m-%dT%H:%M:%SZ")


if in_database:
if not solicitation.history:
solicitation.history = []
if is_opp_update(existing_date=original_sol_date, posted_date=solicitation.date, sol_existed_in_db=in_database):
solicitation.history.append({ "date": now_datetime_string, "user": "", "action": "Solicitation Updated on SAM", "status": "" })
solicitation.updatedAt = now_datetime_string
update_history_in_database(solicitation, now_datetime_string, previous_posting, posted_at)
else:
if not solicitation.action:
solicitation.action = []
solicitation.action.append({"date": now_datetime_string, "user": "", "action": "Solicitation Posted", "status": "complete"})
solicitation.actionDate = now
solicitation.actionStatus = "Solicitation Posted"
solicitation.predictions = { "value": "red", "508": "red", "estar": "red", "history" : [] }
update_history_not_in_database(solicitation, now, now_datetime_string)

def convert_to_datetime(date):
if isinstance(date, str):
return posted_date_to_datetime(date)
return date

def update_history_in_database(solicitation, now_datetime_string, previous_posting, posted_at):
if not solicitation.history:
solicitation.history = []
if is_opp_update(previous_posting, posted_at, True):
solicitation.history.append({"date": now_datetime_string, "user": "", "action": "Solicitation Updated on SAM", "status": ""})
solicitation.updatedAt = now_datetime_string

def update_history_not_in_database(solicitation, now, now_datetime_string):
if not solicitation.action:
solicitation.action = []
solicitation.action.append({"date": now_datetime_string, "user": "", "action": "Solicitation Posted", "status": "complete"})
solicitation.actionDate = now
solicitation.actionStatus = "Solicitation Posted"
solicitation.predictions = {"value": "red", "508": "red", "estar": "red", "history": []}

def handle_attachments(opportunity: dict, solicitation: Solicitation, session=None, now: datetime = datetime.now(timezone.utc)) -> int:
"""
Expand Down Expand Up @@ -462,6 +469,7 @@ def insert_data_into_solicitations_table(session, data) -> list[Solicitation]:

sol = create_new_or_exisiting_sol(opp['solnbr'], session)
sol_existed_in_db = True if sol.solNum else False
last_posted_date = sol.date if sol.date else None # grab initial posting date to check if it was truly updated.
sol.notice_type_id = notice_type_id


Expand All @@ -471,7 +479,8 @@ def insert_data_into_solicitations_table(session, data) -> list[Solicitation]:
update_solicitation_history(sol,
now_datetime,
in_database=sol_existed_in_db,
posted_at=opp.get('postedDate', None))
posted_at=opp.get('postedDate', None),
previous_posting=last_posted_date)


sol_prediction = handle_attachments(opp, sol, session=session, now=now_datetime)
Expand Down
19 changes: 13 additions & 6 deletions tests/test_db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from tests.mock_opps import mock_schematized_opp_two
from fbo_scraper.db.db import Notice, NoticeType, Solicitation, Attachment, Model, now_minus_two
from fbo_scraper.db.db_utils import insert_data_into_solicitations_table, \
from fbo_scraper.db.db_utils import convert_to_datetime, insert_data_into_solicitations_table, \
DataAccessLayer, insert_notice_types, update_solicitation_history, search_for_agency, handle_attachments, apply_predictions_to,create_new_or_exisiting_sol, insert_data_into

from fbo_scraper.db.connection import get_db_url


from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from addict import Addict

from unittest import mock
Expand Down Expand Up @@ -60,11 +60,12 @@ def __init__(self):
self.actionStatus = None
self.predictions = None

now = datetime.now()
now = datetime.now(timezone.utc)
previous_posting = "2023-05-17T10:02:11Z"

# Test case 1: new solicitation
sol1 = MockSolicitation()
update_solicitation_history(sol1, now, in_database=False)
update_solicitation_history(sol1, now, in_database=False, previous_posting=None)
assert sol1.action[0]["action"] == "Solicitation Posted"
assert sol1.actionDate == now
assert sol1.actionStatus == "Solicitation Posted"
Expand All @@ -73,15 +74,15 @@ def __init__(self):
# Test case 2: existing solicitation with no history
sol2 = MockSolicitation()
sol2.date = now
update_solicitation_history(sol2, now, in_database=True, posted_at=now - timedelta(days=7))
update_solicitation_history(sol2, now, in_database=True, posted_at=now - timedelta(days=7), previous_posting=previous_posting)
assert sol2.history[0]["action"] == "Solicitation Updated on SAM"
assert sol2.updatedAt == now.strftime("%Y-%m-%dT%H:%M:%SZ")

# Test case 3: existing solicitation with history
sol3 = MockSolicitation()
sol3.date = now
sol3.history = [{ "date": "2022-01-01T00:00:00Z", "user": "admin", "action": "Solicitation Created", "status": "complete" }]
update_solicitation_history(sol3, now, in_database=True, posted_at=now - timedelta(days=7))
update_solicitation_history(sol3, now, in_database=True, posted_at=now - timedelta(days=7), previous_posting="2022-01-01T00:00:00Z" )
assert sol3.history[1]["action"] == "Solicitation Updated on SAM"
assert sol3.updatedAt == now.strftime("%Y-%m-%dT%H:%M:%SZ")

Expand Down Expand Up @@ -129,6 +130,12 @@ def __init__(self, solNum):
assert sol.agency == None


def test_convert_to_datetime():
date_str = "2023-05-19T10:02:11Z"
result = convert_to_datetime(date_str)
assert isinstance(result, datetime)


def test_handle_attachments():
# Create a mock Solicitation object
class MockSolicitation:
Expand Down

0 comments on commit fc463f4

Please sign in to comment.