diff --git a/api/construct/views.py b/api/construct/views.py index 3327947d96..23cce5ae0d 100644 --- a/api/construct/views.py +++ b/api/construct/views.py @@ -65,13 +65,14 @@ def get_lab_research(request): def update_lab_research(request): request_data = json.loads(request.body) result = Researches.update_lab_research_and_fractions(request_data["research"], True) - Log.log( - result["old_data"]["pk"], - 220002, - request.user.doctorprofile, - {"old_data": result["old_data"], "new_data": result["new_data"]}, - ) - return JsonResponse(result) + if result["ok"]: + Log.log( + result["old_data"]["pk"], + 220002, + request.user.doctorprofile, + {"old_data": result["old_data"], "new_data": result["new_data"]}, + ) + return JsonResponse({"ok": result["ok"], "message": result["message"]}) @login_required @@ -79,13 +80,14 @@ def update_lab_research(request): def create_lab_research(request): request_data = json.loads(request.body) result = Researches.create_lab_research_and_fractions(request_data["research"], True) - Log.log( - result["log_data"]["pk"], - 220003, - request.user.doctorprofile, - result["log_data"], - ) - return JsonResponse(result) + if result["ok"]: + Log.log( + result["log_data"]["pk"], + 220003, + request.user.doctorprofile, + result["log_data"], + ) + return JsonResponse({"ok": result["ok"], "pk": result["pk"], "message": result["message"]}) @login_required diff --git a/directory/models.py b/directory/models.py index 413653ec2d..9547056179 100644 --- a/directory/models.py +++ b/directory/models.py @@ -616,6 +616,11 @@ def get_tube_data(research_pk: int, need_fractions: bool = False) -> dict: research_tubes[fraction.relation_id]["fractions"] = [fraction_data] return research_tubes + @staticmethod + def check_duplicated_internal_code(internal_code): + result = Researches.objects.filter(internal_code=internal_code) + return result.exists() + @staticmethod def get_laboratory_researches(podrazdelenie_id: int): if podrazdelenie_id == -1: @@ -715,6 +720,10 @@ def update_lab_research_and_fractions(research_data, need_log_data: bool = False old_log_data = {} new_log_data = {} service_data = Researches.normalize_research_data(research_data) + internal_code_is_duplicated = Researches.check_duplicated_internal_code(service_data["internal_code"]) + if internal_code_is_duplicated: + return {"ok": False, "message": "Такой внутренний код уже есть"} + service = Researches.objects.filter(pk=service_data["pk"]).first() if need_log_data: old_log_data = service.as_json_lab_full() @@ -745,8 +754,8 @@ def update_lab_research_and_fractions(research_data, need_log_data: bool = False if need_log_data: new_log_data["fractions"][new_fraction.pk] = Fractions.as_json(new_fraction) if need_log_data: - return {"ok": True, "old_data": old_log_data, "new_data": new_log_data} - return {"ok": True} + return {"ok": True, "old_data": old_log_data, "new_data": new_log_data, "message": ""} + return {"ok": True, "message": ""} @staticmethod def create_lab_service(service_data): @@ -770,6 +779,9 @@ def create_lab_service(service_data): def create_lab_research_and_fractions(research_data, need_log_data: bool = False): log_data = {} service_data = Researches.normalize_research_data(research_data) + internal_code_is_duplicated = Researches.check_duplicated_internal_code(service_data["internal_code"]) + if internal_code_is_duplicated: + return {"ok": False, "pk": None, "message": "Такой внутренний код уже есть"} new_service = Researches.create_lab_service(service_data) if need_log_data: log_data = new_service.as_json_lab_full() diff --git a/l2-frontend/src/construct/ResearchDetail.vue b/l2-frontend/src/construct/ResearchDetail.vue index a2080a9446..3b93dda291 100644 --- a/l2-frontend/src/construct/ResearchDetail.vue +++ b/l2-frontend/src/construct/ResearchDetail.vue @@ -568,14 +568,14 @@ const updateResearch = async () => { const researchValidate = validateResearch(); if (researchValidate.ok) { await store.dispatch(actions.INC_LOADING); - const { ok } = await api('construct/laboratory/update-research', { research: research.value }); + const { ok, message } = await api('construct/laboratory/update-research', { research: research.value }); await store.dispatch(actions.DEC_LOADING); if (ok) { root.$emit('msg', 'ok', 'Обновлено'); await getResearch(); emit('updateResearch'); } else { - root.$emit('msg', 'error', 'Ошибка'); + root.$emit('msg', 'error', message); } } else { root.$emit('msg', 'error', researchValidate.message); @@ -586,7 +586,7 @@ const createResearch = async () => { const researchValidate = validateResearch(); if (researchValidate.ok) { await store.dispatch(actions.INC_LOADING); - const { ok, pk } = await api('construct/laboratory/create-research', { research: research.value }); + const { ok, pk, message } = await api('construct/laboratory/create-research', { research: research.value }); await store.dispatch(actions.DEC_LOADING); if (ok) { research.value.pk = pk; @@ -594,7 +594,7 @@ const createResearch = async () => { await getResearch(); emit('updateResearch'); } else { - root.$emit('msg', 'error', 'Ошибка'); + root.$emit('msg', 'error', message); } } else { root.$emit('msg', 'error', researchValidate.message);