diff --git a/backend/api/v1/v1_data/serializers.py b/backend/api/v1/v1_data/serializers.py index 1b1917569..74416877e 100644 --- a/backend/api/v1/v1_data/serializers.py +++ b/backend/api/v1/v1_data/serializers.py @@ -442,6 +442,7 @@ def __init__(self, **kwargs): class ListPendingDataAnswerSerializer(serializers.ModelSerializer): history = serializers.SerializerMethodField() value = serializers.SerializerMethodField() + last_value = serializers.SerializerMethodField() @extend_schema_field(AnswerHistorySerializer(many=True)) def get_history(self, instance): @@ -457,9 +458,19 @@ def get_history(self, instance): def get_value(self, instance: Answers): return get_answer_value(instance) + @extend_schema_field(OpenApiTypes.ANY) + def get_last_value(self, instance: Answers): + if self.context['last_data']: + answer = self.context['last_data'].data_answer.filter( + question=instance.question + ).first() + if answer: + return get_answer_value(answer=answer) + return None + class Meta: model = PendingAnswers - fields = ["history", "question", "value"] + fields = ["history", "question", "value", "last_value"] class PendingBatchDataFilterSerializer(serializers.Serializer): diff --git a/backend/api/v1/v1_data/tests/test_update_data.py b/backend/api/v1/v1_data/tests/test_update_data.py index c8cafa4fd..5c76cd49a 100644 --- a/backend/api/v1/v1_data/tests/test_update_data.py +++ b/backend/api/v1/v1_data/tests/test_update_data.py @@ -325,11 +325,13 @@ def test_update_datapoint_by_data_entry_role(self): results = [{ 'history': None, 'question': 101, - 'value': 'User Wayan' + 'value': 'User Wayan', + 'last_value': None, }, { 'history': None, 'question': 102, - 'value': ['Male'] + 'value': ['Male'], + 'last_value': None, }] self.assertEqual(data, results) # test get form data with pending data object inside @@ -517,11 +519,13 @@ def test_update_datapoint_by_county_admin_with_national_form(self): results = [{ 'history': None, 'question': 201, - 'value': 'Made County Admin' + 'value': 'Made County Admin', + 'last_value': None, }, { 'history': None, 'question': 202, - 'value': ['Male'] + 'value': ['Male'], + 'last_value': None, }] self.assertEqual(data, results) # test get form data with pending data object inside diff --git a/backend/api/v1/v1_data/tests/tests_pending_data.py b/backend/api/v1/v1_data/tests/tests_pending_data.py index e885063af..457bb1e10 100644 --- a/backend/api/v1/v1_data/tests/tests_pending_data.py +++ b/backend/api/v1/v1_data/tests/tests_pending_data.py @@ -6,7 +6,7 @@ from api.v1.v1_data.constants import DataApprovalStatus from api.v1.v1_data.models import PendingFormData, PendingDataApproval, \ - PendingDataBatch + PendingDataBatch, FormData from api.v1.v1_forms.constants import FormTypes, QuestionTypes from api.v1.v1_forms.models import Forms, Questions from api.v1.v1_profile.constants import UserRoleTypes @@ -14,6 +14,7 @@ MAX_LEVEL_IN_SOURCE_FILE) from api.v1.v1_profile.models import Administration from api.v1.v1_users.models import SystemUser +from utils.functions import get_answer_value @override_settings(USE_TZ=False) @@ -56,8 +57,10 @@ def tests_pending_data(self): content_type='application/json', **header) self.assertEqual(200, response.status_code) - self.assertEqual(['history', 'question', 'value'], - list(response.json()[0])) + self.assertEqual( + ['history', 'question', 'value', 'last_value'], + list(response.json()[0]) + ) response = self.client.get( '/api/v1/form-pending-data-batch/{}'.format(data[0]['id']), content_type='application/json', @@ -114,6 +117,26 @@ def tests_pending_data(self): 'created', 'updated', 'status', 'approvers' ]) + def tests_pending_data_with_last_reponse(self): + call_command("fake_data_seeder", "-r", 1, '-t', True) + call_command("fake_data_monitoring_seeder", "-r", 1, '-t', True) + pending_data = PendingFormData.objects.first() + t = RefreshToken.for_user(pending_data.created_by) + header = {'HTTP_AUTHORIZATION': f'Bearer {t.access_token}'} + response = self.client.get( + '/api/v1/pending-data/{0}'.format(pending_data.id), + content_type='application/json', + **header + ) + data = FormData.objects.filter( + uuid=pending_data.uuid + ).order_by('-created').first() + first_res = response.json()[0] + question_id = first_res['question'] + answer = data.data_answer.filter(question_id=question_id).first() + self.assertEqual(response.status_code, 200) + self.assertEqual(first_res['last_value'], get_answer_value(answer)) + def test_pending_batch_list(self): call_command('fake_pending_data_seeder', '-r', 5, '-t', True, '-b', 5) diff --git a/backend/api/v1/v1_data/tests/tests_update_pending_data.py b/backend/api/v1/v1_data/tests/tests_update_pending_data.py index 8d8fd5bd9..be3543aaf 100644 --- a/backend/api/v1/v1_data/tests/tests_update_pending_data.py +++ b/backend/api/v1/v1_data/tests/tests_update_pending_data.py @@ -98,16 +98,20 @@ def test_update_pending_data(self): # update pending data payload = [{ "question": 101, - "value": "Jane Doe" + "value": "Jane Doe", + 'last_value': None, }, { "question": 102, - "value": ["Female"] + "value": ["Female"], + 'last_value': None, }, { "question": 104, - "value": 4 + "value": 4, + 'last_value': None, }, { "question": 109, - "value": 5.5 + "value": 5.5, + 'last_value': None, }] data = self.client.put( '/api/v1/form-pending-data/{0}?pending_data_id={1}' diff --git a/backend/api/v1/v1_data/views.py b/backend/api/v1/v1_data/views.py index c8d01f6cb..6ffb57acf 100644 --- a/backend/api/v1/v1_data/views.py +++ b/backend/api/v1/v1_data/views.py @@ -852,11 +852,16 @@ class PendingDataDetailDeleteView(APIView): summary='To get list of answers for pending data') def get(self, request, pending_data_id, version): data = get_object_or_404(PendingFormData, pk=pending_data_id) + last_data = FormData.objects.filter(uuid=data.uuid)\ + .order_by('-created').first() return Response( ListPendingDataAnswerSerializer( + context={'last_data': last_data}, instance=data.pending_data_answer.all(), - many=True).data, - status=status.HTTP_200_OK) + many=True + ).data, + status=status.HTTP_200_OK + ) @extend_schema( responses={