|
1 | | -import logging |
2 | | -from datetime import date |
3 | | -from typing import Optional |
4 | | -from classes.subject import Subject |
5 | | -from classes.repositories.subject_repository import SubjectRepository |
6 | | -from classes.pi_subject import PISubject |
7 | | -from utils.date_time_utils import DateTimeUtils |
8 | | - |
9 | | - |
10 | | -class SSPIChangeSteps: |
11 | | - def sspi_update_to_change_dob_received( |
12 | | - self, nhs_no: str, age_to_change_to: int |
13 | | - ) -> None: |
14 | | - """ |
15 | | - Receives an SSPI update to change the subject's date of birth to the specified age. |
16 | | - Args: |
17 | | - nhs_no (str): The NHS number of the subject to update. |
18 | | - age_to_change_to (int): The age to change the subject's date of birth to. |
19 | | - """ |
20 | | - logging.debug( |
21 | | - f"start: sspi_update_to_change_dob_received(age_to_change_to={age_to_change_to})" |
22 | | - ) |
23 | | - |
24 | | - subject = Subject().populate_subject_object_from_nhs_no(nhs_no) |
25 | | - # Calculate the new birth date |
26 | | - birth_date = DateTimeUtils.calculate_birth_date_for_age(age_to_change_to) |
27 | | - logging.debug(f"change date of birth to: {birth_date}") |
28 | | - |
29 | | - # Pass control to handle_update to make the DB changes |
30 | | - self.handle_update(subject, birth_date) |
31 | | - |
32 | | - logging.debug("exit: sspi_update_to_change_dob_received()") |
33 | | - |
34 | | - def handle_update(self, subject, birth_date: Optional[date]) -> None: |
35 | | - """ |
36 | | - Performs the SSPI update to make the changes in the database. |
37 | | - Args: |
38 | | - subject (Subject): The subject object to update. |
39 | | - birth_date (Optional[date]): The new birth date to set. |
40 | | - """ |
41 | | - logging.debug("start: handle_update(Subject, date)") |
42 | | - |
43 | | - subject_repo = SubjectRepository() |
44 | | - |
45 | | - pi_subject = PISubject().from_subject(subject) |
46 | | - pi_subject.pi_reference = "AUTOMATED TEST" |
47 | | - # Check if a date of birth change needs to be made first |
48 | | - if birth_date is not None: |
49 | | - pi_subject.birth_date = birth_date |
50 | | - |
51 | | - # Run the update into the DB (SSPI updates are always run as automated process user 2) |
52 | | - subject_repo.update_pi_subject(2, pi_subject) |
53 | | - |
54 | | - logging.debug("exit: handle_update()") |
| 1 | +import logging |
| 2 | +from datetime import date |
| 3 | +from typing import Optional |
| 4 | +from classes.subject import Subject |
| 5 | +from classes.repositories.subject_repository import SubjectRepository |
| 6 | +from classes.pi_subject import PISubject |
| 7 | +from utils.date_time_utils import DateTimeUtils |
| 8 | + |
| 9 | + |
| 10 | +class SSPIChangeSteps: |
| 11 | + def sspi_update_to_change_dob_received( |
| 12 | + self, nhs_no: str, age_to_change_to: int |
| 13 | + ) -> None: |
| 14 | + """ |
| 15 | + Receives an SSPI update to change the subject's date of birth to the specified age. |
| 16 | + Args: |
| 17 | + nhs_no (str): The NHS number of the subject to update. |
| 18 | + age_to_change_to (int): The age to change the subject's date of birth to. |
| 19 | + """ |
| 20 | + logging.debug( |
| 21 | + f"start: sspi_update_to_change_dob_received(age_to_change_to={age_to_change_to})" |
| 22 | + ) |
| 23 | + |
| 24 | + subject = Subject().populate_subject_object_from_nhs_no(nhs_no) |
| 25 | + # Calculate the new birth date |
| 26 | + birth_date = DateTimeUtils.calculate_birth_date_for_age(age_to_change_to) |
| 27 | + logging.debug(f"change date of birth to: {birth_date}") |
| 28 | + |
| 29 | + # Pass control to handle_update to make the DB changes |
| 30 | + self.handle_update(subject, birth_date) |
| 31 | + |
| 32 | + logging.debug("exit: sspi_update_to_change_dob_received()") |
| 33 | + |
| 34 | + def handle_update(self, subject, birth_date: Optional[date]) -> None: |
| 35 | + """ |
| 36 | + Performs the SSPI update to make the changes in the database. |
| 37 | + Args: |
| 38 | + subject (Subject): The subject object to update. |
| 39 | + birth_date (Optional[date]): The new birth date to set. |
| 40 | + """ |
| 41 | + logging.debug("start: handle_update(Subject, date)") |
| 42 | + |
| 43 | + subject_repo = SubjectRepository() |
| 44 | + |
| 45 | + pi_subject = PISubject().from_subject(subject) |
| 46 | + pi_subject.pi_reference = "AUTOMATED TEST" |
| 47 | + # Check if a date of birth change needs to be made first |
| 48 | + if birth_date is not None: |
| 49 | + pi_subject.birth_date = birth_date |
| 50 | + |
| 51 | + # Run the update into the DB (SSPI updates are always run as automated process user 2) |
| 52 | + subject_repo.update_pi_subject(2, pi_subject) |
| 53 | + |
| 54 | + logging.debug("exit: handle_update()") |
0 commit comments