Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IS-1766: Change calculation of varighet #372

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Comment on lines +12 to 13
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Når vi bruker dayjs til å regne ut dette, så runder man ned antall uker, så må være mer enn 14 dager for å være 2 uker. Vi diskuterte dette da vi gjorde samme endring i syfomodiaperson, og landet på at det blir mest riktig å gjøre det på denne måten: Hvis man er sykmeldt i 1,6 uker, så er ikke det 2 uker enda.

});
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