Skip to content

Commit

Permalink
IS-1766: Change calculation of varighet (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
eirikdahlen authored Nov 7, 2023
1 parent 1661f4c commit 453b43d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 17 deletions.
27 changes: 21 additions & 6 deletions src/components/Personrad.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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 (
<StyledPersonRad index={index} selected={kryssAv}>
Expand Down
11 changes: 3 additions & 8 deletions src/utils/dateUtils.ts
Original file line number Diff line number Diff line change
@@ -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 => {
Expand Down
16 changes: 16 additions & 0 deletions test/components/Personrad.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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;
});
});
15 changes: 12 additions & 3 deletions test/utils/dateUtils.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down

0 comments on commit 453b43d

Please sign in to comment.