Skip to content

Commit

Permalink
employee_record: Use the transition name over update_as_*
Browse files Browse the repository at this point in the history
  • Loading branch information
rsebille committed Dec 30, 2024
1 parent 639ab70 commit dd1c52d
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def handle(self, *, wet_run, **options):
self.stdout.write(f"Archiving {employee_record.pk=}")
if wet_run:
try:
employee_record.update_as_archived()
employee_record.archive()
except Exception as ex:
self.stdout.write(f"Can't archive {employee_record.pk=} {ex=}")
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ def _upload_batch_file(self, sftp: paramiko.SFTPClient, employee_records: list[E
# and store in which file they have been sent
renderer = JSONRenderer()
for idx, employee_record in enumerate(employee_records, 1):
employee_record.update_as_sent(
remote_path, idx, renderer.render(batch_data["lignesTelechargement"][idx - 1])
)
employee_record.sent(remote_path, idx, renderer.render(batch_data["lignesTelechargement"][idx - 1]))

def _parse_feedback_file(self, feedback_file: str, batch: dict, dry_run: bool) -> None:
"""
Expand Down Expand Up @@ -97,17 +95,15 @@ def _parse_feedback_file(self, feedback_file: str, batch: dict, dry_run: bool) -
archived_json = JSONRenderer().render(raw_employee_record)
if processing_code == EmployeeRecord.ASP_PROCESSING_SUCCESS_CODE: # Processed by ASP
if not dry_run:
employee_record.update_as_processed(processing_code, processing_label, archived_json)
employee_record.process(processing_code, processing_label, archived_json)
else:
self.stdout.write(f"DRY-RUN: Accepted {employee_record=}, {processing_code=}, {processing_label=}")
else: # Rejected by ASP
if not dry_run:
# One special case added for support concerns:
# 3436 processing code are automatically converted as PROCESSED
if processing_code == EmployeeRecord.ASP_DUPLICATE_ERROR_CODE:
employee_record.update_as_processed(
processing_code, processing_label, archived_json, as_duplicate=True
)
employee_record.process(processing_code, processing_label, archived_json, as_duplicate=True)

# If the ASP mark the employee record as duplicate,
# and there is a suspension or a prolongation for the associated approval,
Expand All @@ -127,7 +123,7 @@ def _parse_feedback_file(self, feedback_file: str, batch: dict, dry_run: bool) -

continue

employee_record.update_as_rejected(processing_code, processing_label, archived_json)
employee_record.reject(processing_code, processing_label, archived_json)
else:
self.stdout.write(f"DRY-RUN: Rejected {employee_record=}, {processing_code=}, {processing_label=}")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ def _upload_batch_file(

renderer = JSONRenderer()
for idx, notification in enumerate(notifications, 1):
notification.update_as_sent(
remote_path, idx, renderer.render(batch_data["lignesTelechargement"][idx - 1])
)
notification.sent(remote_path, idx, renderer.render(batch_data["lignesTelechargement"][idx - 1]))

def _parse_feedback_file(self, feedback_file: str, batch: dict, dry_run: bool) -> None:
"""
Expand Down Expand Up @@ -102,12 +100,12 @@ def _parse_feedback_file(self, feedback_file: str, batch: dict, dry_run: bool) -
archived_json = JSONRenderer().render(employee_record)
if processing_code == EmployeeRecordUpdateNotification.ASP_PROCESSING_SUCCESS_CODE: # Processed by ASP
if not dry_run:
notification.update_as_processed(processing_code, processing_label, archived_json)
notification.process(processing_code, processing_label, archived_json)
else:
self.stdout.write(f"DRY-RUN: Processed {notification}, {processing_code=}, {processing_label=}")
else: # Rejected by ASP
if not dry_run:
notification.update_as_rejected(processing_code, processing_label, archived_json)
notification.reject(processing_code, processing_label, archived_json)
else:
self.stdout.write(f"DRY-RUN: Rejected {notification}: {processing_code=}, {processing_label=}")

Expand Down
40 changes: 18 additions & 22 deletions itou/employee_record/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ def _fill_denormalized_fields(self):

# Business methods

@xwf_models.transition(EmployeeRecordTransition.READY)
def update_as_ready(self):
@xwf_models.transition()
def ready(self):
"""
Prepare the employee record for transmission
"""
Expand All @@ -310,25 +310,25 @@ def update_as_ready(self):
# To prevent some ASP processing errors, we do a refresh on some mutable fields.
self._fill_denormalized_fields()

@xwf_models.transition(EmployeeRecordTransition.SENT)
def update_as_sent(self, asp_filename, line_number, archive):
@xwf_models.transition()
def sent(self, asp_filename, line_number, archive):
"""
An employee record is sent to ASP via a JSON file,
The file name is stored for further feedback processing (also done via a file)
"""
self.clean()
self.set_asp_batch_information(asp_filename, line_number, archive)

@xwf_models.transition(EmployeeRecordTransition.REJECT)
def update_as_rejected(self, code, label, archive):
@xwf_models.transition()
def reject(self, code, label, archive):
"""
Update status after an ASP rejection of the employee record
"""
self.clean()
self.set_asp_processing_information(code, label, archive)

@xwf_models.transition(EmployeeRecordTransition.PROCESS)
def update_as_processed(self, code, label, archive, *, as_duplicate=False):
@xwf_models.transition()
def process(self, code, label, archive, *, as_duplicate=False):
if as_duplicate and code != self.ASP_DUPLICATE_ERROR_CODE:
raise ValueError(f"Code needs to be {self.ASP_DUPLICATE_ERROR_CODE} and not {code} when {as_duplicate=}")

Expand All @@ -339,16 +339,12 @@ def update_as_processed(self, code, label, archive, *, as_duplicate=False):
code, label if not as_duplicate else "Statut forcé suite à doublon ASP", archive
)

@xwf_models.transition(EmployeeRecordTransition.DISABLE)
def update_as_disabled(self):
pass

@xwf_models.transition(EmployeeRecordTransition.ENABLE)
def update_as_new(self):
@xwf_models.transition()
def enable(self):
self._fill_denormalized_fields()

@xwf_models.transition(EmployeeRecordTransition.ARCHIVE)
def update_as_archived(self):
@xwf_models.transition()
def archive(self):
# We only archive an employee record when the job seeker's approval is expired and can no longer be prolonged
if self.job_application.approval.is_valid() or self.job_application.approval.can_be_prolonged:
raise xwf_models.InvalidTransitionError()
Expand Down Expand Up @@ -588,14 +584,14 @@ class Meta(ASPExchangeInformation.Meta):
def __repr__(self):
return f"<{type(self).__name__} pk={self.pk}>"

@xwf_models.transition(EmployeeRecordTransition.SENT)
def update_as_sent(self, filename, line_number, archive):
@xwf_models.transition()
def sent(self, filename, line_number, archive):
self.set_asp_batch_information(filename, line_number, archive)

@xwf_models.transition(EmployeeRecordTransition.REJECT)
def update_as_rejected(self, code, label, archive):
@xwf_models.transition()
def reject(self, code, label, archive):
self.set_asp_processing_information(code, label, archive)

@xwf_models.transition(EmployeeRecordTransition.PROCESS)
def update_as_processed(self, code, label, archive):
@xwf_models.transition()
def process(self, code, label, archive):
self.set_asp_processing_information(code, label, archive)
2 changes: 1 addition & 1 deletion itou/templates/employee_record/includes/list_item.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ <h3>{{ employee_record.job_application.job_seeker.get_full_name }}</h3>
</div>
{% else %}
<div class="d-flex flex-column flex-md-row justify-content-md-end gap-3">
{% if employee_record.update_as_disabled.is_available %}
{% if employee_record.disable.is_available %}
<a href="{% url "employee_record_views:disable" employee_record.id %}"
class="btn btn-outline-primary btn-block w-100 w-md-auto"
aria-label="Désactiver la fiche salarié de {{ employee_record.job_application.job_seeker.get_full_name }}">Désactiver</a>
Expand Down
8 changes: 4 additions & 4 deletions itou/www/employee_record_views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ def create_step_5(request, job_application_id, template_name="employee_record/cr

if request.method == "POST":
back_url = f'{reverse("employee_record_views:list")}?status={employee_record.status}'
employee_record.update_as_ready()
employee_record.ready()
toast_title, toast_message = (
"La création de cette fiche salarié est terminée",
"Vous pouvez suivre l'avancement de son traitement par l'ASP en sélectionnant les différents statuts.",
Expand Down Expand Up @@ -482,12 +482,12 @@ def disable(request, employee_record_id, template_name="employee_record/disable.

back_url = f'{reverse("employee_record_views:list")}?status={employee_record.status}'

if not employee_record.update_as_disabled.is_available():
if not employee_record.disable.is_available():
messages.error(request, EmployeeRecord.ERROR_EMPLOYEE_RECORD_INVALID_STATE)
return HttpResponseRedirect(back_url)

if request.method == "POST" and request.POST.get("confirm") == "true":
employee_record.update_as_disabled()
employee_record.disable()
messages.success(request, "La fiche salarié a bien été désactivée.", extra_tags="toast")
return HttpResponseRedirect(back_url)

Expand Down Expand Up @@ -520,7 +520,7 @@ def reactivate(request, employee_record_id, template_name="employee_record/react

if request.method == "POST" and request.POST.get("confirm") == "true":
try:
employee_record.update_as_new()
employee_record.enable()
messages.success(request, "La fiche salarié a bien été réactivée.")
return HttpResponseRedirect(back_url)
except ValidationError as ex:
Expand Down
26 changes: 13 additions & 13 deletions tests/api/employee_record_api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def setup_method(self):
# We only care about status filtering: no coherence check on ASP return values
job_application = JobApplicationWithCompleteJobSeekerProfileFactory()
self.employee_record = EmployeeRecord.from_job_application(job_application)
self.employee_record.update_as_ready()
self.employee_record.ready()

self.siae = job_application.to_company
self.employer = self.siae.members.first()
Expand All @@ -101,7 +101,7 @@ def test_fetch_employee_record_list(self, api_client, mocker, faker):
# Get list without filtering by status (PROCESSED)
# note: there is no way to create a processed employee record
# (and this is perfectly normal)
self.employee_record.update_as_sent(faker.asp_batch_filename(), 1, None)
self.employee_record.sent(faker.asp_batch_filename(), 1, None)
process_code, process_message = "0000", "La ligne de la fiche salarié a été enregistrée avec succès."

# There should be no result at this point
Expand All @@ -111,7 +111,7 @@ def test_fetch_employee_record_list(self, api_client, mocker, faker):
result = response.json()
assert len(result.get("results")) == 0

self.employee_record.update_as_processed(process_code, process_message, "{}")
self.employee_record.process(process_code, process_message, "{}")
response = api_client.get(ENDPOINT_URL, format="json")
assert response.status_code == 200

Expand All @@ -122,7 +122,7 @@ def test_fetch_employee_record_list(self, api_client, mocker, faker):
# status = SENT
job_application = JobApplicationWithCompleteJobSeekerProfileFactory(to_company=self.siae)
employee_record_sent = EmployeeRecord.from_job_application(job_application=job_application)
employee_record_sent.update_as_ready()
employee_record_sent.ready()

# There should be no result at this point
response = api_client.get(ENDPOINT_URL + "?status=SENT", format="json")
Expand All @@ -131,7 +131,7 @@ def test_fetch_employee_record_list(self, api_client, mocker, faker):
result = response.json()
assert len(result.get("results")) == 0

employee_record_sent.update_as_sent(faker.asp_batch_filename(), 1, None)
employee_record_sent.sent(faker.asp_batch_filename(), 1, None)
response = api_client.get(ENDPOINT_URL + "?status=SENT", format="json")
assert response.status_code == 200

Expand All @@ -142,8 +142,8 @@ def test_fetch_employee_record_list(self, api_client, mocker, faker):
# status = REJECTED
job_application = JobApplicationWithCompleteJobSeekerProfileFactory(to_company=self.siae)
employee_record_rejected = EmployeeRecord.from_job_application(job_application=job_application)
employee_record_rejected.update_as_ready()
employee_record_rejected.update_as_sent(faker.asp_batch_filename(), 1, None)
employee_record_rejected.ready()
employee_record_rejected.sent(faker.asp_batch_filename(), 1, None)

# There should be no result at this point
response = api_client.get(ENDPOINT_URL + "?status=REJECTED", format="json")
Expand All @@ -153,7 +153,7 @@ def test_fetch_employee_record_list(self, api_client, mocker, faker):
assert len(result.get("results")) == 0

err_code, err_message = "12", "JSON Invalide"
employee_record_rejected.update_as_rejected(err_code, err_message, None)
employee_record_rejected.reject(err_code, err_message, None)

# Status case is not important
response = api_client.get(ENDPOINT_URL + "?status=rEjEcTeD", format="json")
Expand Down Expand Up @@ -198,7 +198,7 @@ def test_status_parameter(self, api_client, mocker):
)
job_application = JobApplicationWithCompleteJobSeekerProfileFactory()
employee_record = EmployeeRecord.from_job_application(job_application)
employee_record.update_as_ready()
employee_record.ready()

member = employee_record.job_application.to_company.members.first()
api_client.force_login(member)
Expand All @@ -223,14 +223,14 @@ def test_status_array_parameter(self, api_client, mocker, faker):
)
job_application_1 = JobApplicationWithCompleteJobSeekerProfileFactory()
employee_record = EmployeeRecord.from_job_application(job_application_1)
employee_record.update_as_ready()
employee_record.ready()

job_application_2 = JobApplicationWithCompleteJobSeekerProfileFactory(
to_company=employee_record.job_application.to_company
)
employee_record = EmployeeRecord.from_job_application(job_application_2)
employee_record.update_as_ready()
employee_record.update_as_sent(faker.asp_batch_filename(), 1, None)
employee_record.ready()
employee_record.sent(faker.asp_batch_filename(), 1, None)

member = employee_record.job_application.to_company.members.first()
api_client.force_login(member)
Expand Down Expand Up @@ -345,7 +345,7 @@ def test_chain_parameters(self, api_client, mocker):
job_application_2 = JobApplicationWithCompleteJobSeekerProfileFactory(to_company=job_application_1.to_company)
employee_record_2 = EmployeeRecord.from_job_application(job_application_2)
employee_record_2.created_at = ancient_ts
employee_record_2.update_as_ready()
employee_record_2.ready()

member = employee_record_1.job_application.to_company.members.first()

Expand Down
Loading

0 comments on commit dd1c52d

Please sign in to comment.