Skip to content

Commit

Permalink
fix: changes for data entry app [LIBS-684]
Browse files Browse the repository at this point in the history
  • Loading branch information
tomzemp authored Sep 26, 2024
2 parents bbe6f5e + 4bfaa7c commit ea7e8af
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dhis2-verify-lib.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ jobs:
with:
path: '**/node_modules'
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}

- name: Build
run: yarn build

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import i18n from '@dhis2/d2-i18n'
import { dhis2CalendarsMap } from '../../constants/dhis2CalendarsMap'
import { SupportedCalendar } from '../../types'
import getValidLocale from '../../utils/getValidLocale'
import { fromAnyDate, getCustomCalendarIfExists } from '../../utils/index'
import { buildDailyFixedPeriod } from '../daily-periods/index'
import { generateFixedPeriods } from '../generate-fixed-periods/index'
Expand Down Expand Up @@ -31,6 +32,8 @@ const createFixedPeriodFromPeriodId: ParseFixedPeriodId = ({
dhis2CalendarsMap[requestedCalendar] ?? requestedCalendar
) as SupportedCalendar

const validLocale = getValidLocale(locale) ?? 'en'

if (isAnyYearlyPeriodId(periodId)) {
const year = parseInt(periodId.substring(0, 4), 10)
const periodType = getYearlyFixedPeriodTypeForPeriodId(periodId)
Expand All @@ -45,7 +48,7 @@ const createFixedPeriodFromPeriodId: ParseFixedPeriodId = ({
periodType,
year,
calendar,
locale,
locale: validLocale,
})

const foundThisYear = monthlyPeriodsForYear.find(
Expand All @@ -62,7 +65,7 @@ const createFixedPeriodFromPeriodId: ParseFixedPeriodId = ({
year: year + 1,
periodType,
calendar,
locale,
locale: validLocale,
}).slice(-1)

const foundNextYear = monthlyPeriodsForNextYear.find(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { Temporal } from '@js-temporal/polyfill'
import { SupportedCalendar } from '../../types'
import { fromAnyDate } from '../../utils'

type DoesPeriodEndBefore = (args: {
period: { startDate: string; endDate: string }
date: Temporal.PlainDate
calendar: SupportedCalendar
}) => boolean

const doesPeriodEndBefore: DoesPeriodEndBefore = ({ period, date }) => {
const periodStartDay = Temporal.PlainDate.from(period.startDate)
const periodEndDay = Temporal.PlainDate.from(period.endDate)
const doesPeriodEndBefore: DoesPeriodEndBefore = ({
period,
date,
calendar,
}) => {
const periodStartDay = fromAnyDate({ calendar, date: period.startDate })
const periodEndDay = fromAnyDate({ calendar, date: period.endDate })

const periodStartsOnOrAfterDate =
Temporal.PlainDate.compare(date, periodStartDay) < 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const generateFixedPeriodsMonthly: GenerateFixedPeriodsMonthly = ({

if (
endsBefore &&
doesPeriodEndBefore({ period, date: endsBefore })
doesPeriodEndBefore({ period, date: endsBefore, calendar })
) {
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ const generateFixedPeriodsWeekly: GenerateFixedPeriodsWeekly = ({
endsBefore &&
doesPeriodEndBefore({
period: {
startDate: date.toString(),
endDate: endofWeek.toString(),
startDate: formatYyyyMmDD(date),

Check failure on line 55 in src/period-calculation/generate-fixed-periods/generate-fixed-periods-weekly.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find name 'formatYyyyMmDD'.
endDate: formatYyyyMmDD(endofWeek),

Check failure on line 56 in src/period-calculation/generate-fixed-periods/generate-fixed-periods-weekly.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find name 'formatYyyyMmDD'.
},
date: endsBefore,
calendar,
})
) {
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ const generateFixedPeriodsYearly: GenerateFixedPeriodsYearly = ({
calendar,
})

if (endsBefore && doesPeriodEndBefore({ period, date: endsBefore })) {
if (
endsBefore &&
doesPeriodEndBefore({ period, date: endsBefore, calendar })
) {
continue
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,101 @@ describe('Ethiopic Calendar fixed period calculation', () => {
// expect(periods[periods.length - 1]).toEqual("2015-11-01/2015-13-06");
})
})

describe('periods calculated with endsBefore', () => {
it('should omit every period on/after the exclude date (daily)', () => {
const results = generateFixedPeriods({
periodType: 'DAILY',
year: 2016,
calendar: 'ethiopic',
locale: 'en',
endsBefore: '2016-03-03',
})

expect(results.length).toBe(62)
expect(results[0]).toMatchObject({
displayName: 'Meskerem 1, 2016 ERA0',
endDate: '2016-01-01',
id: '20160101',
iso: '20160101',
name: '2016-01-01',
periodType: 'DAILY',
startDate: '2016-01-01',
})
expect(results[results.length - 1]).toMatchObject({
displayName: 'Hedar 2, 2016 ERA0',
endDate: '2016-03-02',
id: '20160302',
iso: '20160302',
name: '2016-03-02',
periodType: 'DAILY',
startDate: '2016-03-02',
})
})

it('should omit every period on/after the exclude date (monthly)', () => {
const results = generateFixedPeriods({
periodType: 'MONTHLY',
year: 2014,
calendar: 'ethiopic',
locale: 'en',
endsBefore: '2014-07-12',
})

expect(results.length).toBe(6)
expect(results[0]).toMatchObject({
periodType: 'MONTHLY',
id: '201401',
iso: '201401',
name: 'Meskerem 2014',
displayName: 'Meskerem 2014',
startDate: '2014-01-01',
endDate: '2014-01-30',
})
expect(results[results.length - 1]).toMatchObject({
periodType: 'MONTHLY',
id: '201406',
iso: '201406',
name: 'Yekatit 2014',
displayName: 'Yekatit 2014',
startDate: '2014-06-01',
endDate: '2014-06-30',
})
})

it('should omit every period on/after the exclude date (weekly)', () => {
const results = generateFixedPeriods({
periodType: 'WEEKLY',
year: 2014,
calendar: 'ethiopic',
locale: 'en',
endsBefore: '2014-07-12',
startingDay: 1,
})

expect(results[results.length - 1]).toMatchObject({
id: '2014W26',
iso: '2014W26',
name: 'Week 26 - 2014-06-29 - 2014-07-05',
displayName: 'Week 26 - 2014-06-29 - 2014-07-05',
})
})

it('should omit every period on/after the exclude date (yearly)', () => {
const result = generateFixedPeriods({
periodType: 'YEARLY',
year: 2015,
endsBefore: '2015-03-01',
calendar: 'ethiopic',
locale: 'en',
yearsCount: null,
})
expect(result[0]).toMatchObject({
id: '2014',
iso: '2014',
name: '2014',
displayName: '2014',
})
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,101 @@ describe('Nepali Calendar fixed period calculation', () => {
expect(periods[periods.length - 1]).toEqual('2079-11-01/2079-12-30')
})
})

describe('periods calculated with endsBefore', () => {
it('should omit every period on/after the exclude date (daily)', () => {
const results = generateFixedPeriods({
periodType: 'DAILY',
year: 2076,
calendar: 'nepali',
locale: 'en',
endsBefore: '2076-03-03',
})

expect(results.length).toBe(65)
expect(results[0]).toMatchObject({
displayName: '2076-01-01',
endDate: '2076-01-01',
id: '20760101',
iso: '20760101',
name: '2076-01-01',
periodType: 'DAILY',
startDate: '2076-01-01',
})
expect(results[results.length - 1]).toMatchObject({
displayName: '2076-03-02',
endDate: '2076-03-02',
id: '20760302',
iso: '20760302',
name: '2076-03-02',
periodType: 'DAILY',
startDate: '2076-03-02',
})
})

it('should omit every period on/after the exclude date (monthly)', () => {
const results = generateFixedPeriods({
periodType: 'MONTHLY',
year: 2074,
calendar: 'nepali',
locale: 'en',
endsBefore: '2074-07-12',
})

expect(results.length).toBe(6)
expect(results[0]).toMatchObject({
periodType: 'MONTHLY',
id: '207401',
iso: '207401',
name: 'Baisakh 2074',
displayName: 'Baisakh 2074',
startDate: '2074-01-01',
endDate: '2074-01-31',
})
expect(results[results.length - 1]).toMatchObject({
periodType: 'MONTHLY',
id: '207406',
iso: '207406',
name: 'Ashwin 2074',
displayName: 'Ashwin 2074',
startDate: '2074-06-01',
endDate: '2074-06-31',
})
})

it('should omit every period on/after the exclude date (weekly)', () => {
const results = generateFixedPeriods({
periodType: 'WEEKLY',
year: 2014,
calendar: 'nepali',
locale: 'en',
endsBefore: '2014-07-12',
startingDay: 1,
})

expect(results[results.length - 1]).toMatchObject({
id: '2014W28',
iso: '2014W28',
name: 'Week 28 - 2014-07-05 - 2014-07-11',
displayName: 'Week 28 - 2014-07-05 - 2014-07-11',
})
})

it('should omit every period on/after the exclude date (yearly)', () => {
const result = generateFixedPeriods({
periodType: 'YEARLY',
year: 2075,
endsBefore: '2075-03-01',
calendar: 'nepali',
locale: 'en',
yearsCount: null,
})
expect(result[0]).toMatchObject({
id: '2074',
iso: '2074',
name: '2074',
displayName: '2074',
})
})
})
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { dhis2CalendarsMap } from '../../constants/dhis2CalendarsMap'
import { SupportedCalendar } from '../../types'
import getValidLocale from '../../utils/getValidLocale'
import { fromAnyDate, getCustomCalendarIfExists } from '../../utils/index'
import {
monthlyFixedPeriodTypes,
Expand Down Expand Up @@ -53,6 +54,8 @@ const generateFixedPeriods: GenerateFixedPeriods = ({
dhis2CalendarsMap[requestedCalendar] ?? requestedCalendar
) as SupportedCalendar

const validLocale = getValidLocale(locale) ?? 'en'

const endsBefore = _endsBefore
? fromAnyDate({ calendar, date: _endsBefore })
: undefined
Expand All @@ -71,7 +74,7 @@ const generateFixedPeriods: GenerateFixedPeriods = ({
return generateFixedPeriodsYearly({
year,
periodType,
locale,
locale: validLocale,
calendar,
endsBefore,
yearsCount,
Expand All @@ -82,7 +85,7 @@ const generateFixedPeriods: GenerateFixedPeriods = ({
return generateFixedPeriodsMonthly({
year,
periodType,
locale,
locale: validLocale,
calendar,
endsBefore,
})
Expand All @@ -91,7 +94,7 @@ const generateFixedPeriods: GenerateFixedPeriods = ({
if (periodType === 'DAILY') {
return generateFixedPeriodsDaily({
year,
locale,
locale: validLocale,
calendar,
endsBefore,
})
Expand Down
33 changes: 33 additions & 0 deletions src/utils/getValidLocale.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import getValidLocale from './getValidLocale'

describe('getValidLocale', () => {
it('returns language only for a valid language', () => {
const requestedLocale = 'pt'
const outputLocale = 'pt'
expect(getValidLocale(requestedLocale)).toEqual(outputLocale)
})

it('returns language-region from java format', () => {
const requestedLocale = 'pt_BR'
const outputLocale = 'pt-BR'
expect(getValidLocale(requestedLocale)).toEqual(outputLocale)
})

it('returns language-region from valid ISO format', () => {
const requestedLocale = 'pt-BR'
const outputLocale = 'pt-BR'
expect(getValidLocale(requestedLocale)).toEqual(outputLocale)
})

it('returns language-script-region from DHIS2 format', () => {
const requestedLocale = 'uz_UZ_Cyrl'
const outputLocale = 'uz-Cyrl-UZ'
expect(getValidLocale(requestedLocale)).toEqual(outputLocale)
})

it('returns undefined for an invalid locale', () => {
const requestedLocale = 'pb'
const outputLocale = undefined
expect(getValidLocale(requestedLocale)).toEqual(outputLocale)
})
})
Loading

0 comments on commit ea7e8af

Please sign in to comment.