@@ -349,31 +349,33 @@ def test_create_immunization(self):
349349 self .mock_redis .hget .return_value = "COVID"
350350 self .mock_redis_getter .return_value = self .mock_redis
351351 self .authoriser .authorise .return_value = True
352- self .imms_repo .check_immunization_identifier_exists .return_value = False
352+ self .imms_repo .get_immunization_by_identifier .return_value = ( None , None )
353353 self .imms_repo .create_immunization .return_value = self ._MOCK_NEW_UUID
354354
355355 nhs_number = VALID_NHS_NUMBER
356356 req_imms = create_covid_immunization_dict_no_id (nhs_number )
357357
358358 # When
359- created_id = self .fhir_service .create_immunization (req_imms , "Test" )
359+ created_id , created_version = self .fhir_service .create_immunization (req_imms , "Test" )
360360
361361 # Then
362362 self .authoriser .authorise .assert_called_once_with ("Test" , ApiOperationCode .CREATE , {"COVID" })
363- self .imms_repo .check_immunization_identifier_exists .assert_called_once_with (
364- "https://supplierABC/identifiers/vacc" , "ACME-vacc123456"
365- )
363+ self .imms_repo .get_immunization_by_identifier .assert_called_once ()
364+ create_identifier = self .imms_repo .get_immunization_by_identifier .call_args .args [0 ]
365+ self .assertEqual (create_identifier .system , "https://supplierABC/identifiers/vacc" )
366+ self .assertEqual (create_identifier .value , "ACME-vacc123456" )
366367 self .imms_repo .create_immunization .assert_called_once_with (Immunization .parse_obj (req_imms ), "Test" )
367368
368369 self .validator .validate .assert_called_once_with (req_imms )
369370 self .assertEqual (self ._MOCK_NEW_UUID , created_id )
371+ self .assertEqual (1 , created_version )
370372
371373 def test_create_immunization_keeps_first_site_and_route_snomed_coding (self ):
372374 """it should keep the first SNOMED coding for site and route during API create"""
373375 self .mock_redis .hget .return_value = "COVID"
374376 self .mock_redis_getter .return_value = self .mock_redis
375377 self .authoriser .authorise .return_value = True
376- self .imms_repo .check_immunization_identifier_exists .return_value = False
378+ self .imms_repo .get_immunization_by_identifier .return_value = ( None , None )
377379 self .imms_repo .create_immunization .return_value = self ._MOCK_NEW_UUID
378380
379381 req_imms = create_covid_immunization_dict_no_id (VALID_NHS_NUMBER )
@@ -382,9 +384,10 @@ def test_create_immunization_keeps_first_site_and_route_snomed_coding(self):
382384 req_imms , "route" , "888888888" , "Replacement route that should be ignored"
383385 )
384386
385- created_id = self .pre_validate_fhir_service .create_immunization (req_imms , "Test" )
387+ created_id , created_version = self .pre_validate_fhir_service .create_immunization (req_imms , "Test" )
386388
387389 self .assertEqual (self ._MOCK_NEW_UUID , created_id )
390+ self .assertEqual (1 , created_version )
388391 self .assertEqual (req_imms ["site" ]["coding" ], [first_site_coding ])
389392 self .assertEqual (req_imms ["route" ]["coding" ], [first_route_coding ])
390393 self .imms_repo .create_immunization .assert_called_once_with (Immunization .parse_obj (req_imms ), "Test" )
@@ -496,7 +499,15 @@ def test_raises_duplicate_error_if_identifier_already_exits(self):
496499 self .mock_redis .hget .return_value = "COVID"
497500 self .mock_redis_getter .return_value = self .mock_redis
498501 self .authoriser .authorise .return_value = True
499- self .imms_repo .check_immunization_identifier_exists .return_value = True
502+ existing_immunisation = create_covid_immunization_dict ("existing-id" , VALID_NHS_NUMBER )
503+ identifier = Identifier (
504+ system = existing_immunisation ["identifier" ][0 ]["system" ],
505+ value = existing_immunisation ["identifier" ][0 ]["value" ],
506+ )
507+ self .imms_repo .get_immunization_by_identifier .return_value = (
508+ existing_immunisation ,
509+ ImmunizationRecordMetadata (identifier = identifier , resource_version = 1 , is_deleted = False , is_reinstated = False ),
510+ )
500511
501512 nhs_number = VALID_NHS_NUMBER
502513 req_imms = create_covid_immunization_dict_no_id (nhs_number )
@@ -507,16 +518,53 @@ def test_raises_duplicate_error_if_identifier_already_exits(self):
507518
508519 # Then
509520 self .authoriser .authorise .assert_called_once_with ("Test" , ApiOperationCode .CREATE , {"COVID" })
510- self .imms_repo .check_immunization_identifier_exists .assert_called_once_with (
511- "https://supplierABC/identifiers/vacc" , "ACME-vacc123456"
512- )
521+ self .imms_repo .get_immunization_by_identifier .assert_called_once ()
522+ duplicate_identifier = self .imms_repo .get_immunization_by_identifier .call_args .args [0 ]
523+ self .assertEqual (duplicate_identifier .system , "https://supplierABC/identifiers/vacc" )
524+ self .assertEqual (duplicate_identifier .value , "ACME-vacc123456" )
513525 self .imms_repo .create_immunization .assert_not_called ()
526+ self .imms_repo .update_immunization .assert_not_called ()
514527 self .validator .validate .assert_called_once_with (req_imms )
515528 self .assertEqual (
516529 "The provided identifier: https://supplierABC/identifiers/vacc#ACME-vacc123456 is duplicated" ,
517530 str (error .exception ),
518531 )
519532
533+ def test_reinstates_deleted_duplicate_identifier_on_create (self ):
534+ """it should reinstate a deleted record when a create request matches a deleted identifier"""
535+ self .mock_redis .hget .return_value = "COVID"
536+ self .mock_redis_getter .return_value = self .mock_redis
537+ self .authoriser .authorise .return_value = True
538+
539+ existing_immunisation = create_covid_immunization_dict ("existing-id" , VALID_NHS_NUMBER )
540+ identifier = Identifier (
541+ system = existing_immunisation ["identifier" ][0 ]["system" ],
542+ value = existing_immunisation ["identifier" ][0 ]["value" ],
543+ )
544+ existing_metadata = ImmunizationRecordMetadata (
545+ identifier = identifier ,
546+ resource_version = 2 ,
547+ is_deleted = True ,
548+ is_reinstated = False ,
549+ )
550+ self .imms_repo .get_immunization_by_identifier .return_value = (existing_immunisation , existing_metadata )
551+ self .imms_repo .update_immunization .return_value = 3
552+
553+ req_imms = create_covid_immunization_dict_no_id (VALID_NHS_NUMBER )
554+
555+ reinstated_id , reinstated_version = self .fhir_service .create_immunization (req_imms , "Test" )
556+
557+ self .authoriser .authorise .assert_called_once_with ("Test" , ApiOperationCode .CREATE , {"COVID" })
558+ self .imms_repo .create_immunization .assert_not_called ()
559+ self .imms_repo .update_immunization .assert_called_once_with (
560+ "existing-id" ,
561+ Immunization .parse_obj (req_imms ),
562+ existing_metadata ,
563+ "Test" ,
564+ )
565+ self .assertEqual ("existing-id" , reinstated_id )
566+ self .assertEqual (3 , reinstated_version )
567+
520568
521569class TestUpdateImmunization (TestFhirServiceBase ):
522570 """Tests for FhirService.update_immunization"""
0 commit comments