@@ -396,6 +396,14 @@ def _parse_optional_date(value: str | None) -> date | None:
396396 except ValueError :
397397 return None
398398
399+ @staticmethod
400+ def _sanitize_date_range (
401+ date_start : date | None , date_end : date | None
402+ ) -> tuple [date | None , date | None ]:
403+ if date_end is not None and (date_start is None or date_end < date_start ):
404+ return None , None
405+ return date_start , date_end
406+
399407 @classmethod
400408 def group_parse_rows (
401409 cls , rows : list [AffiliationParseRow ]
@@ -425,13 +433,17 @@ def group_parse_rows(
425433 organization = row .organization
426434 is_unaffiliated = organization .is_unaffiliated
427435 domain = cls ._strip (organization .domain )
436+ date_start , date_end = cls ._sanitize_date_range (
437+ cls ._parse_optional_date (organization .date_start ),
438+ cls ._parse_optional_date (organization .date_end ),
439+ )
428440
429441 if is_unaffiliated :
430442 stint = AffiliationOrganizationStint (
431443 name = "Individual" ,
432444 domain = "nonameaccount.com" ,
433- date_start = cls . _parse_optional_date ( organization . date_start ) ,
434- date_end = cls . _parse_optional_date ( organization . date_end ) ,
445+ date_start = date_start ,
446+ date_end = date_end ,
435447 is_unaffiliated = True ,
436448 )
437449 elif not domain :
@@ -440,8 +452,8 @@ def group_parse_rows(
440452 stint = AffiliationOrganizationStint (
441453 name = cls ._strip (organization .name ),
442454 domain = domain .lower (),
443- date_start = cls . _parse_optional_date ( organization . date_start ) ,
444- date_end = cls . _parse_optional_date ( organization . date_end ) ,
455+ date_start = date_start ,
456+ date_end = date_end ,
445457 is_unaffiliated = False ,
446458 )
447459
@@ -890,8 +902,9 @@ async def apply_affiliations(
890902 existing_msas = segment_affiliations_by_member .get (member_id , [])
891903 deleted_mos = deleted_member_organizations_by_member .get (member_id , [])
892904 deleted_msas = deleted_segment_affiliations_by_member .get (member_id , [])
893- date_start = organization .date_start
894- date_end = organization .date_end
905+ date_start , date_end = self ._sanitize_date_range (
906+ organization .date_start , organization .date_end
907+ )
895908
896909 if not self .has_existing_stint (
897910 existing_mos , organization_id , date_start , date_end
0 commit comments