Skip to content

Commit

Permalink
Let all saksbehandlere edit documents in finished behandling, add che…
Browse files Browse the repository at this point in the history
…ckbox for forcing own signature instead of assigned saksbehandler
  • Loading branch information
eriksson-daniel committed Jan 13, 2025
1 parent 070db97 commit b6e081a
Show file tree
Hide file tree
Showing 11 changed files with 479 additions and 144 deletions.
13 changes: 10 additions & 3 deletions frontend/src/components/smart-editor/context.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCanManageDocument } from '@app/components/smart-editor/hooks/use-can-edit-document';
import { useCanEditDocument, useCanManageDocument } from '@app/components/smart-editor/hooks/use-can-edit-document';
import {
useSmartEditorAnnotationsAtOrigin,
useSmartEditorGodeFormuleringerOpen,
Expand All @@ -25,6 +25,8 @@ interface ISmartEditorContext extends Pick<ISmartDocument, 'templateId' | 'dokum
setShowAnnotationsAtOrigin: (show: boolean) => void;
sheetRef: MutableRefObject<HTMLDivElement | null>;
canManage: boolean;
canEdit: boolean;
creator: string;
}

export const SmartEditorContext = createContext<ISmartEditorContext>({
Expand All @@ -43,6 +45,8 @@ export const SmartEditorContext = createContext<ISmartEditorContext>({
setShowAnnotationsAtOrigin: noop,
sheetRef: { current: null },
canManage: false,
canEdit: false,
creator: '',
});

interface Props {
Expand All @@ -51,7 +55,7 @@ interface Props {
}

export const SmartEditorContextComponent = ({ children, smartDocument }: Props) => {
const { dokumentTypeId, templateId, id } = smartDocument;
const { dokumentTypeId, templateId, id, creator } = smartDocument;
const { value: showGodeFormuleringer = false, setValue: setShowGodeFormuleringer } =
useSmartEditorGodeFormuleringerOpen();
const { value: showHistory = false, setValue: setShowHistory } = useSmartEditorHistoryOpen();
Expand All @@ -61,7 +65,8 @@ export const SmartEditorContextComponent = ({ children, smartDocument }: Props)
useSmartEditorAnnotationsAtOrigin();
// const [sheetRef, setSheetRef] = useState<HTMLDivElement | null>(null);
const sheetRef = useRef<HTMLDivElement | null>(null);
const canManage = useCanManageDocument(templateId);
const canManage = useCanManageDocument(templateId, creator.employee.navIdent);
const canEdit = useCanEditDocument(templateId, creator.employee.navIdent);

return (
<SmartEditorContext.Provider
Expand All @@ -81,6 +86,8 @@ export const SmartEditorContextComponent = ({ children, smartDocument }: Props)
setShowAnnotationsAtOrigin,
sheetRef,
canManage,
canEdit,
creator: creator.employee.navIdent,
}}
>
{children}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const HistoryEditor = memo(
({ smartDocument, version, versionId }: Props) => {
const mainEditor = useMyPlateEditorRef(smartDocument.id);
const { templateId } = useContext(SmartEditorContext);
const canManage = useCanManageDocument(templateId);
const canManage = useCanManageDocument(templateId, smartDocument.creator.employee.navIdent);

const id = `${smartDocument.id}-${versionId}`;

Expand Down
430 changes: 359 additions & 71 deletions frontend/src/components/smart-editor/hooks/use-can-edit-document.test.ts

Large diffs are not rendered by default.

52 changes: 35 additions & 17 deletions frontend/src/components/smart-editor/hooks/use-can-edit-document.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import { StaticDataContext } from '@app/components/app/static-data-context';
import { useOppgave } from '@app/hooks/oppgavebehandling/use-oppgave';
import type { IUserData } from '@app/types/bruker';
import { type IUserData, Role } from '@app/types/bruker';
import { SaksTypeEnum } from '@app/types/kodeverk';
import { FlowState } from '@app/types/oppgave-common';
import type { IOppgavebehandling } from '@app/types/oppgavebehandling/oppgavebehandling';
import { TemplateIdEnum } from '@app/types/smart-editor/template-enums';
import { useContext, useMemo } from 'react';

export const useCanManageDocument = (templateId: TemplateIdEnum): boolean => {
export const useCanManageDocument = (templateId: TemplateIdEnum, creator: string): boolean => {
const { data: oppgave, isSuccess } = useOppgave();
const { user } = useContext(StaticDataContext);

return useMemo<boolean>(
() => isSuccess && canManageDocument(templateId, oppgave, user),
[oppgave, isSuccess, templateId, user],
() => isSuccess && canManageDocument(templateId, oppgave, user, creator),
[oppgave, isSuccess, templateId, user, creator],
);
};

const canManageDocument = (templateId: TemplateIdEnum, oppgave: IOppgavebehandling, user: IUserData): boolean => {
const canManageDocument = (
templateId: TemplateIdEnum,
oppgave: IOppgavebehandling,
user: IUserData,
creator: string,
): boolean => {
if (
(oppgave.typeId === SaksTypeEnum.KLAGE || oppgave.typeId === SaksTypeEnum.ANKE) &&
oppgave.rol?.flowState === FlowState.SENT &&
Expand All @@ -32,16 +37,23 @@ const canManageDocument = (templateId: TemplateIdEnum, oppgave: IOppgavebehandli
}

if (isMu(oppgave, user)) {
return false;
return oppgave.avsluttetAvSaksbehandlerDate !== null && saksbehandlerCanEdit(templateId, oppgave, user, creator);
}

return saksbehandlerCanEdit(templateId, oppgave, user);
return saksbehandlerCanEdit(templateId, oppgave, user, creator);
};

const saksbehandlerCanEdit = (templateId: TemplateIdEnum, oppgave: IOppgavebehandling, user: IUserData): boolean => {
if (oppgave.saksbehandler?.navIdent !== user.navIdent) {
return false;
}
const saksbehandlerCanEdit = (
templateId: TemplateIdEnum,
oppgave: IOppgavebehandling,
user: IUserData,
creator: string,
): boolean => {
const isCreator = creator === user.navIdent;
const isFinished = oppgave.avsluttetAvSaksbehandlerDate !== null;
const hasSaksbehandlerRole = user.roller.includes(Role.KABAL_SAKSBEHANDLING);

const canWrite = (!isFinished && isCreator) || (isFinished && hasSaksbehandlerRole);

if (templateId === TemplateIdEnum.ROL_ANSWERS) {
return false;
Expand All @@ -50,12 +62,13 @@ const saksbehandlerCanEdit = (templateId: TemplateIdEnum, oppgave: IOppgavebehan
// When behandling is sent to ROL, saksbehandler can edit everything except questions.
if (templateId === TemplateIdEnum.ROL_QUESTIONS) {
return (
canWrite &&
(oppgave.typeId === SaksTypeEnum.KLAGE || oppgave.typeId === SaksTypeEnum.ANKE) &&
oppgave.rol?.flowState !== FlowState.SENT
);
}

return oppgave.medunderskriver?.flowState !== FlowState.SENT;
return canWrite && oppgave.medunderskriver?.flowState !== FlowState.SENT;
};

const isMu = (oppgave: IOppgavebehandling, user: IUserData): boolean =>
Expand All @@ -71,18 +84,23 @@ const rolCanEdit = (templateId: TemplateIdEnum, oppgave: IOppgavebehandling): bo
oppgave.rol.flowState === FlowState.SENT &&
templateId === TemplateIdEnum.ROL_ANSWERS;

export const useCanEditDocument = (templateId: TemplateIdEnum): boolean => {
export const useCanEditDocument = (templateId: TemplateIdEnum, creator: string): boolean => {
const { data: oppgave, isSuccess } = useOppgave();
const { user } = useContext(StaticDataContext);

return useMemo<boolean>(
() => isSuccess && canEditDocument(templateId, oppgave, user),
[oppgave, isSuccess, templateId, user],
() => isSuccess && canEditDocument(templateId, oppgave, user, creator),
[oppgave, isSuccess, templateId, user, creator],
);
};

const muCanEdit = (templateId: TemplateIdEnum, oppgave: IOppgavebehandling): boolean =>
oppgave.medunderskriver.flowState === FlowState.SENT && templateId !== TemplateIdEnum.ROL_ANSWERS;

export const canEditDocument = (templateId: TemplateIdEnum, oppgave: IOppgavebehandling, user: IUserData): boolean =>
canManageDocument(templateId, oppgave, user) || (isMu(oppgave, user) && muCanEdit(templateId, oppgave));
export const canEditDocument = (
templateId: TemplateIdEnum,
oppgave: IOppgavebehandling,
user: IUserData,
creator: string,
): boolean =>
canManageDocument(templateId, oppgave, user, creator) || (isMu(oppgave, user) && muCanEdit(templateId, oppgave));
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ interface LoadedEditorProps extends EditorProps {
}

const LoadedEditor = ({ oppgave, smartDocument, scalingGroup }: LoadedEditorProps) => {
const { id, templateId } = smartDocument;
const { id, templateId, creator } = smartDocument;
const { newCommentSelection } = useContext(SmartEditorContext);
const { user } = useContext(StaticDataContext);
const canEdit = useCanEditDocument(templateId);
const canEdit = useCanEditDocument(templateId, creator.employee.navIdent);
const plugins = collaborationSaksbehandlerPlugins(oppgave.id, id, smartDocument, user);

const editor = usePlateEditor<KabalValue, (typeof plugins)[0]>({
Expand Down Expand Up @@ -295,8 +295,7 @@ interface EditorWithNewCommentAndFloatingToolbarProps {
}

const EditorWithNewCommentAndFloatingToolbar = ({ id, isConnected }: EditorWithNewCommentAndFloatingToolbarProps) => {
const { templateId, sheetRef } = useContext(SmartEditorContext);
const canEdit = useCanEditDocument(templateId);
const { sheetRef, canEdit } = useContext(SmartEditorContext);
const [containerElement, setContainerElement] = useState<HTMLDivElement | null>(null);
const lang = useSmartEditorSpellCheckLanguage();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const TabPanel = ({ smartDocument }: TabPanelProps) => {
const { id } = smartDocument;
const smartDocumentRef = useRef<ISmartDocument>(smartDocument);

const canEditDocument = useCanEditDocument(smartDocument.templateId);
const canEditDocument = useCanEditDocument(smartDocument.templateId, smartDocument.creator.employee.navIdent);
const canEditDocumentRef = useRef(canEditDocument);

// Ensure that smartDocumentRef and canEditDocumentRef are always up to date in order to avoid the unmount debounce triggering on archive/delete/fradeling
Expand Down
48 changes: 35 additions & 13 deletions frontend/src/plate/components/signature/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { SmartEditorContext } from '@app/components/smart-editor/context';
import { useOppgave } from '@app/hooks/oppgavebehandling/use-oppgave';
import { getName, getTitle } from '@app/plate/components/signature/functions';
import { MISSING_TITLE } from '@app/plate/components/signature/title';
import type { ISignature, SignatureElement } from '@app/plate/types';
import { useGetSignatureQuery } from '@app/redux-api/bruker';
import type { ISignatureResponse } from '@app/types/bruker';
import { SaksTypeEnum } from '@app/types/kodeverk';
import { TemplateIdEnum } from '@app/types/smart-editor/template-enums';
import { skipToken } from '@reduxjs/toolkit/query';
import { useContext } from 'react';

export const useMedunderskriverSignature = () => {
const { data: oppgave } = useOppgave();
Expand All @@ -19,31 +25,47 @@ export const useMedunderskriverSignature = () => {
return medunderskriverSignature;
};

export const useMainSignature = (template: TemplateIdEnum) => {
export const useMainSignature = (element: SignatureElement): ISignature | undefined => {
const { data: oppgave } = useOppgave();
const { templateId, creator } = useContext(SmartEditorContext);

const isRolAnswers = template === TemplateIdEnum.ROL_ANSWERS;
const isRolAnswers = templateId === TemplateIdEnum.ROL_ANSWERS;
const isRolSakstype = oppgave?.typeId === SaksTypeEnum.KLAGE || oppgave?.typeId === SaksTypeEnum.ANKE;

const { data: saksbehandlerSignature } = useGetSignatureQuery(
!isRolAnswers && typeof oppgave?.saksbehandler?.navIdent === 'string' ? oppgave.saksbehandler.navIdent : skipToken,
);

const { data: creatorSignature } = useGetSignatureQuery(isRolAnswers ? skipToken : creator);
const { data: overrideSignature } = useGetSignatureQuery(element.overriddenSaksbehandler ?? skipToken);
const { data: rolSignature } = useGetSignatureQuery(
isRolAnswers && isRolSakstype ? (oppgave.rol.employee?.navIdent ?? skipToken) : skipToken,
);

if (element.anonymous) {
return { name: 'Nav klageinstans' };
}

const suffix = templateId !== TemplateIdEnum.ROL_ANSWERS && element.useSuffix ? 'saksbehandler' : undefined;

if (isRolAnswers) {
if (oppgave === undefined || !isRolSakstype || oppgave.rol.employee === null || rolSignature === undefined) {
return null;
}
return toSignature(rolSignature, element.useShortName, suffix);
}

return rolSignature;
if (element.overriddenSaksbehandler !== undefined) {
return toSignature(overrideSignature, element.useShortName, suffix);
}

if (oppgave === undefined || oppgave.saksbehandler === null || saksbehandlerSignature === undefined) {
return null;
return toSignature(creatorSignature, element.useShortName, suffix);
};

const toSignature = (
signature: ISignatureResponse | undefined,
useShortName: boolean,
suffix: string | undefined,
): ISignature | undefined => {
if (signature === undefined) {
return undefined;
}

return saksbehandlerSignature;
return {
name: getName(signature, useShortName),
title: getTitle(signature.customJobTitle, suffix) ?? MISSING_TITLE,
};
};
40 changes: 8 additions & 32 deletions frontend/src/plate/components/signature/individual-signature.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SmartEditorContext } from '@app/components/smart-editor/context';
import { getName, getTitle } from '@app/plate/components/signature/functions';
import { getName } from '@app/plate/components/signature/functions';
import { useMainSignature, useMedunderskriverSignature } from '@app/plate/components/signature/hooks';
import { type ISignature, type SignatureElement, useMyPlateEditorRef } from '@app/plate/types';
import { type SignatureElement, useMyPlateEditorRef } from '@app/plate/types';
import { TemplateIdEnum } from '@app/types/smart-editor/template-enums';
import { setNodes } from '@udecode/plate-common';
import { useContext, useEffect, useMemo } from 'react';
Expand All @@ -14,41 +14,17 @@ interface Props {

export const SaksbehandlerSignature = ({ element }: Props) => {
const editor = useMyPlateEditorRef();
const { templateId } = useContext(SmartEditorContext);
const saksbehandlerSignature = useMainSignature(templateId);

const signature: ISignature | undefined = useMemo(() => {
if (saksbehandlerSignature === null) {
return undefined;
}

const suffix = templateId !== TemplateIdEnum.ROL_ANSWERS && element.useSuffix ? 'saksbehandler' : undefined;

if (saksbehandlerSignature.anonymous) {
return { name: 'Nav klageinstans' };
}

return {
name: getName(saksbehandlerSignature, element.useShortName),
title: getTitle(saksbehandlerSignature.customJobTitle, suffix) ?? MISSING_TITLE,
};
}, [saksbehandlerSignature, templateId, element.useSuffix, element.useShortName]);
const signature = useMainSignature(element);

useEffect(() => {
if (element.saksbehandler?.name === signature?.name && element.saksbehandler?.title === signature?.title) {
if (
signature === element.saksbehandler ||
(signature?.name === element.saksbehandler?.name && signature?.title === element.saksbehandler?.title)
) {
return;
}

const data: Partial<SignatureElement> = {
useShortName: element.useShortName,
medunderskriver: element.medunderskriver,
saksbehandler: signature,
};

setNodes(editor, data, {
at: [],
match: (n) => n === element,
});
setNodes(editor, { saksbehandler: signature }, { at: [], match: (n) => n === element });
}, [editor, element, signature]);

if (signature === undefined) {
Expand Down
27 changes: 25 additions & 2 deletions frontend/src/plate/components/signature/signature.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { StaticDataContext } from '@app/components/app/static-data-context';
import { SmartEditorContext } from '@app/components/smart-editor/context';
import { useOppgave } from '@app/hooks/oppgavebehandling/use-oppgave';
import { AddNewParagraphs } from '@app/plate/components/common/add-new-paragraph-buttons';
Expand All @@ -17,7 +18,8 @@ export const Signature = (props: PlateElementProps<SignatureElement>) => {
const isReadOnly = useEditorReadOnly();
const { data: signature } = useGetMySignatureQuery();
const { data: oppgave } = useOppgave();
const { canManage, templateId } = useContext(SmartEditorContext);
const { canManage, templateId, creator } = useContext(SmartEditorContext);
const { user } = useContext(StaticDataContext);

if (oppgave === undefined || signature === undefined) {
return null;
Expand All @@ -33,9 +35,17 @@ export const Signature = (props: PlateElementProps<SignatureElement>) => {
const hideAll = !(showForkortedeNavnCheckbox || showSuffixCheckbox || hasMedunderskriver);

const { children, element, editor } = props;
const overriddenWithSelf = element.overriddenSaksbehandler === user.navIdent;

const setSignatureProp = (prop: Partial<SignatureElement>) =>
setNodes(editor, { ...element, ...prop }, { at: [], voids: true, mode: 'lowest', match: (n) => n === element });
setNodes(
editor,
{ ...prop, overriddenSaksbehandler: overriddenWithSelf ? element.overriddenSaksbehandler : undefined },
{ at: [], voids: true, mode: 'lowest', match: (n) => n === element },
);

const setOverriddenSaksbehandler = (overriddenSaksbehandler: string | undefined) =>
setNodes(editor, { overriddenSaksbehandler }, { at: [], voids: true, mode: 'lowest', match: (n) => n === element });

return (
<PlateElement<SignatureElement> {...props} asChild contentEditable={false}>
Expand Down Expand Up @@ -79,6 +89,19 @@ export const Signature = (props: PlateElementProps<SignatureElement>) => {
Bruk «/saksbehandler»-tittel
</Checkbox>
) : null}

<Checkbox
disabled={
isReadOnly ||
(user.navIdent === creator && (overriddenWithSelf || element.overriddenSaksbehandler === undefined))
}
checked={
overriddenWithSelf || (user.navIdent === creator && element.overriddenSaksbehandler === undefined)
}
onChange={({ target }) => setOverriddenSaksbehandler(target.checked ? user.navIdent : undefined)}
>
Signer med mitt navn
</Checkbox>
</Checkboxes>
)}

Expand Down
1 change: 1 addition & 0 deletions frontend/src/plate/templates/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export const createSignature = (): SignatureElement => ({
useShortName: false,
includeMedunderskriver: true,
useSuffix: true,
overriddenSaksbehandler: undefined,
children: [{ text: '' }],
threadIds: [],
});
Expand Down
Loading

0 comments on commit b6e081a

Please sign in to comment.