diff --git a/src/components/Personrad.tsx b/src/components/Personrad.tsx index f40aab22..88594a5d 100644 --- a/src/components/Personrad.tsx +++ b/src/components/Personrad.tsx @@ -10,9 +10,10 @@ import { } from '@/utils/lenkeUtil'; import { PersonData } from '@/api/types/personregisterTypes'; import { useAktivBruker } from '@/data/modiacontext/useAktivBruker'; -import { getWeeksSinceDate } from '@/utils/dateUtils'; +import { getEarliestDate, getWeeksBetween } from '@/utils/dateUtils'; import { PersonRadVirksomhetColumn } from '@/components/PersonRadVirksomhetColumn'; import { Labels } from '@/components/Labels'; +import { OppfolgingstilfelleDTO } from '@/api/types/personoversiktTypes'; interface PersonradProps { fnr: string; @@ -43,6 +44,22 @@ const VelgBoks = styled(Checkbox)` padding-bottom: 2em; `; +const getVarighetOppfolgingstilfelle = ( + oppfolgingstilfelle: OppfolgingstilfelleDTO | undefined +): string => { + if (oppfolgingstilfelle) { + const start = oppfolgingstilfelle.oppfolgingstilfelleStart; + const now = new Date(); + const end = getEarliestDate( + oppfolgingstilfelle.oppfolgingstilfelleEnd, + now + ); + return `${getWeeksBetween(start, end)} uker`; + } else { + return 'Ukjent'; + } +}; + export const Personrad = (props: PersonradProps): ReactElement => { const { fnr, @@ -63,11 +80,9 @@ export const Personrad = (props: PersonradProps): ReactElement => { }); }; - const startDatoOppfolgingstilfelle = - personData.latestOppfolgingstilfelle?.oppfolgingstilfelleStart; - const oppfolgingstilfelleLengthInWeeks = !!startDatoOppfolgingstilfelle - ? `${getWeeksSinceDate(startDatoOppfolgingstilfelle)} uker` - : 'Ukjent'; + const oppfolgingstilfelleLengthInWeeks = getVarighetOppfolgingstilfelle( + personData.latestOppfolgingstilfelle + ); return ( diff --git a/src/utils/dateUtils.ts b/src/utils/dateUtils.ts index 81029b34..99341d4e 100644 --- a/src/utils/dateUtils.ts +++ b/src/utils/dateUtils.ts @@ -1,16 +1,11 @@ import dayjs from 'dayjs'; -const ONE_WEEK_MILLIS = 7 * 24 * 60 * 60 * 1000; - -export const getWeeksSinceDate = (date: Date): number => { - const now = new Date(); - return getWeeksBetween(new Date(date), now); +export const getEarliestDate = (date1: Date, date2: Date): Date => { + return date1 < date2 ? date1 : date2; }; export const getWeeksBetween = (date1: Date, date2: Date): number => { - return Math.round( - Math.abs(date1.getTime() - date2.getTime()) / ONE_WEEK_MILLIS - ); + return Math.abs(dayjs(date1).diff(date2, 'week')); }; export const toReadableDate = (dateArg: Date | null): string => { diff --git a/test/components/Personrad.test.tsx b/test/components/Personrad.test.tsx index 18237cec..cb8c6084 100644 --- a/test/components/Personrad.test.tsx +++ b/test/components/Personrad.test.tsx @@ -70,6 +70,14 @@ const personDataAktivitetskravAvventMedFrist: PersonData = { ...personDataAktivitetskravAvventUtenFrist, aktivitetskravVurderingFrist: new Date('2023-04-01'), }; +const personWithOppfolgingstilfelle: PersonData = { + ...defaultPersonData, + latestOppfolgingstilfelle: { + oppfolgingstilfelleStart: new Date('2023-01-01'), + oppfolgingstilfelleEnd: new Date('2023-01-15'), + virksomhetList: [], + }, +}; describe('Personrad', () => { beforeEach(() => { @@ -95,14 +103,22 @@ describe('Personrad', () => { expect(screen.getByText(testdata.fnr1)).to.exist; expect(screen.getByText('diskresjonsmerket')).to.exist; }); + it('Skal rendre label med frist-dato for aktivitetskrav AVVENT', () => { renderPersonrad(personDataAktivitetskravAvventMedFrist); expect(screen.getByText('Avventer (01.04.2023)')).to.exist; }); + it('Skal rendre label uten frist-dato for aktivitetskrav AVVENT når frist mangler', () => { renderPersonrad(personDataAktivitetskravAvventUtenFrist); expect(screen.getByText('Avventer')).to.exist; }); + + it('Viser riktig utregning av varighet på sykefraværet', () => { + renderPersonrad(personWithOppfolgingstilfelle); + + expect(screen.getByText('2 uker')).to.exist; + }); }); diff --git a/test/utils/dateUtils.test.ts b/test/utils/dateUtils.test.ts index 6d8e5ae4..e31c88c3 100644 --- a/test/utils/dateUtils.test.ts +++ b/test/utils/dateUtils.test.ts @@ -1,13 +1,22 @@ import { expect } from 'chai'; -import { getWeeksBetween, toReadableDate } from '@/utils/dateUtils'; +import { + getEarliestDate, + getWeeksBetween, + toReadableDate, +} from '@/utils/dateUtils'; describe('dateUtils', () => { - describe('week calculations', () => { + describe('Calculations', () => { it('Will calculate number of weeks between two dates', () => { const date1 = new Date('2022-10-01'); - const date2 = new Date('2022-10-14'); + const date2 = new Date('2022-10-15'); expect(getWeeksBetween(date1, date2)).to.equal(2); }); + it('Will calculate earliest date', () => { + const earliesDate = new Date('2023-01-01'); + const latestDate = new Date('2023-02-02'); + expect(getEarliestDate(earliesDate, latestDate)).to.equal(earliesDate); + }); }); describe('readable date', () => { it('returns empty string if date is null', () => {