Repository: akvo/akvo-rsr

Issue:
Results filter inconsistency, Old vs. New results tab



Discussion:

Resolving patch:
diff --git a/akvo/rest/views/project.py b/akvo/rest/views/project.py
--- a/akvo/rest/views/project.py
+++ b/akvo/rest/views/project.py
@@ -29,7 +29,7 @@
 from akvo.rest.models import TastyTokenAuthentication
 from akvo.rsr.models import Project, OrganisationCustomField, IndicatorPeriodData
 from akvo.rsr.views.my_rsr import user_viewable_projects
-from akvo.utils import codelist_choices
+from akvo.utils import codelist_choices, single_period_dates
 from ..viewsets import PublicProjectViewSet, ReadOnlyPublicProjectViewSet
 
 
@@ -357,6 +357,9 @@ def add_project_to_program(request, program_pk):
 @api_view(['GET'])
 def project_title(request, project_pk):
     project = get_object_or_404(Project, pk=project_pk)
+    hierarchy_name = project.uses_single_indicator_period()
+    needs_reporting_timeout_days, _, _ = single_period_dates(hierarchy_name) if hierarchy_name else (None, None, None)
+
     data = {
         'title': project.title,
         'publishing_status': project.publishingstatus.status,
@@ -364,6 +367,7 @@ def project_title(request, project_pk):
         'pending_update_count': IndicatorPeriodData.objects.filter(
             period__indicator__result__project=project,
             status=IndicatorPeriodData.STATUS_PENDING_CODE
-        ).count()
+        ).count(),
+        'needs_reporting_timeout_days': needs_reporting_timeout_days,
     }
     return Response(data)


Test patch:
diff --git a/akvo/rsr/spa/app/modules/results/filters.test.js b/akvo/rsr/spa/app/modules/results/filters.test.js
new file mode 100644
--- /dev/null
+++ b/akvo/rsr/spa/app/modules/results/filters.test.js
@@ -0,0 +1,103 @@
+/* global jest, describe, it */
+import expect from 'expect'
+import { isPeriodNeedsReporting, isPeriodApproved } from './filters'
+
+jest.mock('moment', () => jest.fn((...args) => {
+  args = args.length > 0 ? args : ['2021-01-01T00:00:00.000']
+  return jest.requireActual('moment')(...args)
+}))
+
+describe('isPeriodNeedsReporting', () => {
+  it('should false for locked periods', () => {
+    const period = { locked: true, updates: [] }
+    expect(isPeriodNeedsReporting(period)).toBe(false)
+  })
+  it('should true for unlocked periods without updates', () => {
+    const period = { locked: false, updates: [] }
+    expect(isPeriodNeedsReporting(period)).toBe(true)
+  })
+  it('should false for unlocked period with all approved updates', () => {
+    const period = {
+      locked: false,
+      updates: [
+        { status: 'A' },
+        { status: 'A' },
+      ]
+    }
+    expect(isPeriodNeedsReporting(period)).toBe(false)
+  })
+  it('should true if there is a draft update', () => {
+    const period = {
+      locked: false,
+      updates: [
+        { status: 'A' },
+        { status: 'D' },
+      ]
+    }
+    expect(isPeriodNeedsReporting(period)).toBe(true)
+  })
+  it('should true if there is a revision update', () => {
+    const period = {
+      locked: false,
+      updates: [
+        { status: 'A' },
+        { status: 'R' },
+      ]
+    }
+    expect(isPeriodNeedsReporting(period)).toBe(true)
+  })
+  it('should true if all updates createdAt passed timeout days', () => {
+    const period = {
+      locked: false,
+      updates: [
+        { status: 'A', createdAt: '2020-11-01T00:00:00.000' },
+        { status: 'A', createdAt: '2020-12-01T00:00:00.000' },
+      ]
+    }
+    expect(isPeriodNeedsReporting(period, 30)).toBe(true)
+  })
+  it('should false if has update with cratedAt not passed timout days', () => {
+    const period = {
+      locked: false,
+      updates: [
+        { status: 'A', createdAt: '2020-11-01T00:00:00.000' },
+        { status: 'A', createdAt: '2020-12-01T00:00:00.000' },
+      ]
+    }
+    expect(isPeriodNeedsReporting(period, 90)).toBe(false)
+  })
+})
+
+describe('isPeriodApproved', () => {
+  it('should false for locked period', () => {
+    const period = {
+      locked: true,
+      updates: [
+        { status: 'A' }
+      ]
+    }
+    expect(isPeriodApproved(period)).toBe(false)
+  })
+  it('should false for unlocked period without updates', () => {
+    const period = { locked: false, updates: [] }
+    expect(isPeriodApproved(period)).toBe(false)
+  })
+  it('shold true if there is an approved update', () => {
+    const period = {
+      locked: false,
+      updates: [
+        { status: 'A' }
+      ]
+    }
+    expect(isPeriodApproved(period)).toBe(true)
+  })
+  it('should false if there is no approved update', () => {
+    const period = {
+      locked: false,
+      updates: [
+        { status: 'R' }
+      ]
+    }
+    expect(isPeriodApproved(period)).toBe(false)
+  })
+})