Skip to content

Commit

Permalink
OM-315: added possibility to registry worker via upload - skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
sniedzielski committed Oct 11, 2024
1 parent 5c2daf9 commit 24768a6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
2 changes: 1 addition & 1 deletion worker_voucher/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# voucher_expiry_type = "fixed_period" or "end_of_year"
"voucher_expiry_type": "end_of_year",
"yearly_worker_voucher_limit": 120,
"validate_created_worker_online": False,
"validate_created_worker_online": True,
"csv_worker_upload_errors_column": "errors",
"worker_upload_chf_id_type": "national_id"
}
Expand Down
49 changes: 34 additions & 15 deletions worker_voucher/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from core.services import BaseService
from core.signals import register_service_signal
from insuree.models import Insuree
from insuree.gql_mutations import update_or_create_insuree
from invoice.models import Bill
from invoice.services import BillService
from policyholder.models import PolicyHolder, PolicyHolderInsuree
Expand Down Expand Up @@ -441,15 +442,15 @@ def upload_worker(self, economic_unit_code, file, upload):

summary = {
'affected_rows': affected_rows,
'total_number_of_benefits_in_file': total_number_of_records_in_file,
'total_number_of_records_in_file': total_number_of_records_in_file,
'skipped_items': skipped_items
}

error_df = df[df[WorkerVoucherConfig.csv_worker_upload_errors_column].apply(lambda x: bool(x))]
if not error_df.empty:
in_memory_file = BytesIO()
df.to_csv(in_memory_file, index=False)
return in_memory_file, error_df.set_index(WorkerVoucherConfig.csv_reconciliation_code_column)\
return in_memory_file, error_df.set_index(WorkerVoucherConfig.worker_upload_chf_id_type)\
[WorkerVoucherConfig.csv_worker_upload_errors_column].to_dict(), summary
return file, None, summary

Expand Down Expand Up @@ -487,31 +488,51 @@ def _upload_record_with_worker(self, economic_unit, row):
errors.append({
"message": _("worker_upload.validation.no_authority_to_use_selected_economic_unit")
})
"""

data_from_mconnect = {}
if WorkerVoucherConfig.validate_created_worker_online:
online_result = MConnectWorkerService().fetch_worker_data(chf_id, self.user, ph)
#TODO add here connection with real service, at this stage data is hardcoded for local development
# online_result = MConnectWorkerService().fetch_worker_data(chf_id, self.user, ph)
online_result = {
"success": True,
"data": {
"GivenName": "Test",
"FamilyName": "Test",
"Sex": "M",
"DateOfBirth": "1999-04-04"
}
}
print(online_result)
if not online_result.get("success", False):
return online_result
else:
data_from_mconnect['chf_id'] = chf_id
data_from_mconnect['other_names'] = online_result["data"]["GivenName"]
data_from_mconnect['last_name'] = online_result["data"]["FamilyName"]
data_from_mconnect['gender'] = online_result["data"]["Sex"]
# data_from_mconnect['gender'] = online_result["data"]["Sex"]
data_from_mconnect['dob'] = online_result["data"]["DateOfBirth"]
data_from_mconnect['photo'] = {"photo": online_result["data"]["Photo"]}
# TODO uncomment photo when integration is turn on
#data_from_mconnect['photo'] = {"photo": online_result["data"]["Photo"]}
print(data_from_mconnect)
if economic_unit:
phi = PolicyHolderInsuree.objects.filter(
insuree__chf_id=chf_id,
policy_holder__code=economic_unit.code,
is_deleted=False,
).first()
if not phi:
result = None
worker = Insuree.objects.filter(chf_id=chf_id).first()
if not worker:
result = super().async_mutate(self.user, **data_from_mconnect)
if not result:
data_from_mconnect['audit_user_id'] = self.user.id_for_audit
from core.utils import TimeUtils
data_from_mconnect['validity_from'] = TimeUtils.now()
try:
worker = update_or_create_insuree(data_from_mconnect, self.user)
print(worker)
except Exception as e:
errors.append({"success": False, "error": str(e)})
if worker:
print('worker add relations with company')
worker = Insuree.objects.filter(chf_id=chf_id).first()
policy_holder_insuree_service = PolicyHolderInsureeService(self.user)
policy_holder = PolicyHolder.objects.get(code=economic_unit.code, is_deleted=False)
Expand All @@ -520,12 +541,10 @@ def _upload_record_with_worker(self, economic_unit, row):
'insuree_id': worker.id,
'contribution_plan_bundle_id': None,
}
policy_holder_insuree_service.create(policy_holder_insuree)
else:
return result
result = policy_holder_insuree_service.create(policy_holder_insuree)
print(result)
else:
return errors.append({"message": _("workers.validation.worker_already_assigned_to_unit")})
"""
errors.append({"message": _("workers.validation.worker_already_assigned_to_unit")})
return errors if errors else None

def worker_voucher_bill_user_filter(qs: QuerySet, user: User) -> QuerySet:
Expand Down
7 changes: 4 additions & 3 deletions worker_voucher/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@ def post(self, request):
file_handler.check_file_path()
service = WorkerUploadService(request.user)
file_to_upload, errors, summary = service.upload_worker(economic_unit_code, file, upload)
print(errors)
if errors:
upload.status = WorkerUpload.Status.PARTIAL_SUCCESS
upload.status = WorkerUpload.Status.FAIL
upload.error = errors
upload.json_ext = {'extra_info': summary}
else:
upload.status = WorkerUpload.Status.SUCCESS
upload.json_ext = {'extra_info': summary}
upload.save(username=request.user.login_name)
file_handler.save_file(file_to_upload)
return Response({'success': True, 'error': None}, status=201)
return Response({'success': True, 'error': errors, 'summary': summary}, status=201)
except Exception as exc:
logger.error("Error while uploading CSV reconciliation", exc_info=exc)
if upload:
Expand All @@ -51,4 +52,4 @@ def post(self, request):
}
upload.json_ext = {'extra_info': summary}
upload.save(username=request.user.login_name)
return Response({'success': False, 'error': str(exc)}, status=500)
return Response({'success': False, 'error': str(exc), 'summary': summary}, status=500)

0 comments on commit 24768a6

Please sign in to comment.