Skip to content

Commit

Permalink
Redaktør: Automatically query for global ytelse when querying for spe…
Browse files Browse the repository at this point in the history
…cific hjemmel
  • Loading branch information
eriksson-daniel committed Dec 4, 2024
1 parent cc30342 commit 2c3d122
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 39 deletions.
4 changes: 2 additions & 2 deletions frontend/src/redux-api/maltekstseksjoner/queries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MaltekstseksjonTagTypes, maltekstseksjonerApi } from '@app/redux-api/maltekstseksjoner/maltekstseksjoner';
import { paramsWithGlobalSections } from '@app/redux-api/redaktør-helpers';
import { paramsWithGlobalQueries } from '@app/redux-api/redaktør-helpers';
import type { IGetMaltekstseksjonParams } from '@app/types/common-text-types';
import type { IMaltekstseksjon } from '@app/types/maltekstseksjoner/responses';
import { IS_LOCALHOST } from '../common';
Expand All @@ -8,7 +8,7 @@ export const maltekstseksjonerQuerySlice = maltekstseksjonerApi.injectEndpoints(
overrideExisting: IS_LOCALHOST,
endpoints: (builder) => ({
getMaltekstseksjoner: builder.query<IMaltekstseksjon[], IGetMaltekstseksjonParams>({
query: (params) => ({ url: '/maltekstseksjoner', params: paramsWithGlobalSections(params) }),
query: (params) => ({ url: '/maltekstseksjoner', params: paramsWithGlobalQueries(params) }),
providesTags: (maltekstseksjoner) =>
maltekstseksjoner
?.map(({ id }) => ({ type: MaltekstseksjonTagTypes.MALTEKSTSEKSJON, id }))
Expand Down
76 changes: 53 additions & 23 deletions frontend/src/redux-api/redaktør-helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,71 @@
import { describe, expect, it } from 'bun:test';
import { GLOBAL, LIST_DELIMITER } from '@app/components/smart-editor-texts/types';
import { paramsWithGlobalSections } from '@app/redux-api/redaktør-helpers';
import { GLOBAL, LIST_DELIMITER, WILDCARD } from '@app/components/smart-editor-texts/types';
import { getListWithGlobal, paramsWithGlobalQueries } from '@app/redux-api/redaktør-helpers';
import type { IGetMaltekstseksjonParams } from '@app/types/common-text-types';

describe('paramsWithGlobalSections', () => {
it('should add global sections to templateSectionIdList', () => {
const untouched = {
enhetIdList: ['enhet'],
describe('getListWithGlobal', () => {
it('should add global items to list', () => {
const list = [`some-template${LIST_DELIMITER}some-section`];

const result = getListWithGlobal(list);
const expected = [`${GLOBAL}${LIST_DELIMITER}some-section`, `some-template${LIST_DELIMITER}some-section`];

expect(result).toEqual(expected);
});

it('should not do anything if list already contains all global items', () => {
const list = [
`${GLOBAL}${LIST_DELIMITER}some-other-section`,
`${GLOBAL}${LIST_DELIMITER}some-section`,
`some-other-template${LIST_DELIMITER}some-other-section`,
`some-template${LIST_DELIMITER}some-section`,
];

expect(getListWithGlobal(list)).toEqual(list);
});

it('should add GLOBAL to list if last part if wildcard', () => {
const list = [`some-template${LIST_DELIMITER}${WILDCARD}`];

const result = getListWithGlobal(list);

const expected = [GLOBAL, `some-template${LIST_DELIMITER}${WILDCARD}`];

expect(result).toEqual(expected);
});

it('should not add GLOBAL if GLOBAL is already in list', () => {
const list = [`some-template${LIST_DELIMITER}${WILDCARD}`, GLOBAL];

const result = getListWithGlobal(list);

const expected = [GLOBAL, `some-template${LIST_DELIMITER}${WILDCARD}`];

expect(result).toEqual(expected);
});
});

describe('paramsWithGlobalQueries', () => {
it('should add global sections and hjemler', () => {
const untouched: Partial<IGetMaltekstseksjonParams> = {
enhetIdList: ['some-enhet'],
utfallIdList: 'utfall',
};

const params: IGetMaltekstseksjonParams = {
const params = {
...untouched,
templateSectionIdList: [`some-template${LIST_DELIMITER}some-section`],
ytelseHjemmelIdList: [`some-ytelse${LIST_DELIMITER}some-hjemmel`],
};

const result = paramsWithGlobalSections(params);
const result = paramsWithGlobalQueries(params);

const expected = {
...untouched,
templateSectionIdList: [`${GLOBAL}${LIST_DELIMITER}some-section`, `some-template${LIST_DELIMITER}some-section`],
ytelseHjemmelIdList: [`${GLOBAL}${LIST_DELIMITER}some-hjemmel`, `some-ytelse${LIST_DELIMITER}some-hjemmel`],
};

expect(result).toEqual(expected);
});

it('should not do anything if templateSectionIdList already contains all global templates', () => {
const params: IGetMaltekstseksjonParams = {
enhetIdList: ['enhet'],
utfallIdList: 'utfall',
templateSectionIdList: [
`${GLOBAL}${LIST_DELIMITER}some-other-section`,
`${GLOBAL}${LIST_DELIMITER}some-section`,
`some-other-template${LIST_DELIMITER}some-other-section`,
`some-template${LIST_DELIMITER}some-section`,
],
};

expect(params).toEqual(paramsWithGlobalSections(params));
});
});
39 changes: 27 additions & 12 deletions frontend/src/redux-api/redaktør-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
import { GLOBAL, LIST_DELIMITER } from '@app/components/smart-editor-texts/types';
import { GLOBAL, LIST_DELIMITER, WILDCARD } from '@app/components/smart-editor-texts/types';
import type { IGetMaltekstseksjonParams, IGetTextsParams } from '@app/types/common-text-types';

type Params = IGetMaltekstseksjonParams | IGetTextsParams;

export const paramsWithGlobalSections = <T extends Params>(params: T): T => {
if (params.templateSectionIdList === undefined) {
return params;
}
export const getListWithGlobal = (list: string[]) => {
const newList = [...list];

for (const item of list) {
const [, lastPart] = item.split(LIST_DELIMITER);

const list = [...params.templateSectionIdList];
if (lastPart === WILDCARD || lastPart === undefined) {
if (!newList.includes(GLOBAL)) {
newList.push(GLOBAL);
}

continue;
}

for (const templateSectionId of list) {
const [, sectionId] = templateSectionId.split(LIST_DELIMITER);
const globalSection = `${GLOBAL}${LIST_DELIMITER}${sectionId}`;
const withGlobal = `${GLOBAL}${LIST_DELIMITER}${lastPart}`;

if (!list.includes(globalSection)) {
list.push(globalSection);
if (!newList.includes(withGlobal)) {
newList.push(withGlobal);
}
}

return { ...params, templateSectionIdList: list.toSorted((a, b) => a.localeCompare(b)) };
return newList.toSorted((a, b) => a.localeCompare(b));
};

export const paramsWithGlobalQueries = <T extends Params>(params: T): T => {
const templateSectionIdList =
params.templateSectionIdList === undefined ? undefined : getListWithGlobal(params.templateSectionIdList);

const ytelseHjemmelIdList =
params.ytelseHjemmelIdList === undefined ? undefined : getListWithGlobal(params.ytelseHjemmelIdList);

return { ...params, templateSectionIdList, ytelseHjemmelIdList };
};
4 changes: 2 additions & 2 deletions frontend/src/redux-api/texts/queries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IS_LOCALHOST } from '@app/redux-api/common';
import { paramsWithGlobalSections } from '@app/redux-api/redaktør-helpers';
import { paramsWithGlobalQueries } from '@app/redux-api/redaktør-helpers';
import { TextsTagTypes, textsApi } from '@app/redux-api/texts/texts';
import type { IGetTextsParams } from '@app/types/texts/params';
import type { IText } from '@app/types/texts/responses';
Expand All @@ -16,7 +16,7 @@ export const textsQuerySlice = textsApi.injectEndpoints({
overrideExisting: IS_LOCALHOST,
endpoints: (builder) => ({
getTexts: builder.query<IText[], IGetTextsParams>({
query: (params) => ({ url: '/texts', params: paramsWithGlobalSections(params) }),
query: (params) => ({ url: '/texts', params: paramsWithGlobalQueries(params) }),
providesTags: textsListTags,
}),
getTextById: builder.query<IText, string>({
Expand Down

0 comments on commit 2c3d122

Please sign in to comment.