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

Finished behandling: All saksbehandlere can edit documents, unfinishe… #914

Merged
merged 3 commits into from
Jan 14, 2025
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
4 changes: 2 additions & 2 deletions frontend/src/components/smart-editor/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +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, creator.employee.navIdent);
const canEdit = useCanEditDocument(templateId, creator.employee.navIdent);
const canManage = useCanManageDocument(templateId);
const canEdit = useCanEditDocument(templateId);

return (
<SmartEditorContext.Provider
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, smartDocument.creator.employee.navIdent);
const canManage = useCanManageDocument(templateId);

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

Expand Down
439 changes: 208 additions & 231 deletions frontend/src/components/smart-editor/hooks/use-can-edit-document.test.ts

Large diffs are not rendered by default.

134 changes: 59 additions & 75 deletions frontend/src/components/smart-editor/hooks/use-can-edit-document.ts
Original file line number Diff line number Diff line change
@@ -1,106 +1,90 @@
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 type {
IAnkebehandling,
IKlagebehandling,
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, creator: string): boolean => {
const supportsRol = (oppgave: IOppgavebehandling): oppgave is IKlagebehandling | IAnkebehandling =>
oppgave.typeId === SaksTypeEnum.KLAGE || oppgave.typeId === SaksTypeEnum.ANKE;

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

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

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 &&
templateId === TemplateIdEnum.ROL_QUESTIONS
) {
// No one can edit ROL questions after they have been sent.
return false;
}

if (isRol(oppgave, user)) {
return rolCanEdit(templateId, oppgave);
}

if (isMu(oppgave, user)) {
return oppgave.avsluttetAvSaksbehandlerDate !== null && saksbehandlerCanEdit(templateId, oppgave, user, creator);
}
export const useCanEditDocument = (templateId: TemplateIdEnum): boolean => {
const { data: oppgave, isSuccess } = useOppgave();
const { user } = useContext(StaticDataContext);

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

const saksbehandlerCanEdit = (
templateId: TemplateIdEnum,
oppgave: IOppgavebehandling,
user: IUserData,
creator: string,
): boolean => {
const isCreator = creator === user.navIdent;
const isFinished = oppgave.avsluttetAvSaksbehandlerDate !== null;
const isAssigned = oppgave.saksbehandler?.navIdent === user.navIdent;
const isRol = (oppgave: IOppgavebehandling, user: IUserData): boolean =>
supportsRol(oppgave) && oppgave.rol.employee?.navIdent === user.navIdent;

const canWrite = (!isFinished && isAssigned) || (isFinished && isCreator);
const rolCanEdit = (oppgave: IOppgavebehandling, user: IUserData): boolean =>
supportsRol(oppgave) &&
isRol(oppgave, user) &&
oppgave.avsluttetAvSaksbehandlerDate === null &&
oppgave.rol?.flowState === FlowState.SENT;

export const canEditDocument = (templateId: TemplateIdEnum, oppgave: IOppgavebehandling, user: IUserData): boolean => {
if (templateId === TemplateIdEnum.ROL_ANSWERS) {
return false;
return rolCanEdit(oppgave, user);
}

// 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
);
const hasSaksbehandlerRole = user.roller.includes(Role.KABAL_SAKSBEHANDLING);

if (oppgave.avsluttetAvSaksbehandlerDate !== null) {
return hasSaksbehandlerRole;
}

return canWrite && oppgave.medunderskriver?.flowState !== FlowState.SENT;
};
const isAssignedSaksbehandler = oppgave.saksbehandler?.navIdent === user.navIdent;
const isMu = oppgave.medunderskriver?.employee?.navIdent === user.navIdent;
const sentToMu = oppgave.medunderskriver.flowState === FlowState.SENT;

const isMu = (oppgave: IOppgavebehandling, user: IUserData): boolean =>
oppgave.medunderskriver?.employee?.navIdent === user.navIdent;
if (templateId === TemplateIdEnum.ROL_QUESTIONS && supportsRol(oppgave)) {
if (isRol(oppgave, user)) {
return false;
}

const isRol = (oppgave: IOppgavebehandling, user: IUserData): boolean =>
(oppgave.typeId === SaksTypeEnum.KLAGE || oppgave.typeId === SaksTypeEnum.ANKE) &&
oppgave.rol.employee?.navIdent === user.navIdent;
if (oppgave.rol?.flowState === FlowState.SENT) {
return false;
}

// Only ROL can edit answers after they have been sent.
const rolCanEdit = (templateId: TemplateIdEnum, oppgave: IOppgavebehandling): boolean =>
(oppgave.typeId === SaksTypeEnum.KLAGE || oppgave.typeId === SaksTypeEnum.ANKE) &&
oppgave.rol.flowState === FlowState.SENT &&
templateId === TemplateIdEnum.ROL_ANSWERS;
if (isAssignedSaksbehandler) {
return !sentToMu;
}

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

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

const muCanEdit = (templateId: TemplateIdEnum, oppgave: IOppgavebehandling): boolean =>
oppgave.medunderskriver.flowState === FlowState.SENT && templateId !== TemplateIdEnum.ROL_ANSWERS;
if (sentToMu) {
return false;
}

export const canEditDocument = (
templateId: TemplateIdEnum,
oppgave: IOppgavebehandling,
user: IUserData,
creator: string,
): boolean =>
canManageDocument(templateId, oppgave, user, creator) || (isMu(oppgave, user) && muCanEdit(templateId, oppgave));
const isFinished = oppgave.avsluttetAvSaksbehandlerDate !== null;

return (!isFinished && isAssignedSaksbehandler) || (isFinished && hasSaksbehandlerRole);
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useHasDocumentsAccess } from '@app/hooks/use-has-documents-access';
import { useIsFeilregistrert } from '@app/hooks/use-is-feilregistrert';
import { useIsRol } from '@app/hooks/use-is-rol';
import { useIsSaksbehandler } from '@app/hooks/use-is-saksbehandler';
import { GENERELT_BREV_WITHOUT_MU_TEMPLATE, NOTAT_TEMPLATE } from '@app/plate/templates/simple-templates';
import { NOTAT_TEMPLATE, getGenereltBrevTemplate } from '@app/plate/templates/simple-templates';
import {
ANKE_I_TRYGDERETTEN_TEMPLATES,
ANKE_TEMPLATES,
Expand Down Expand Up @@ -104,6 +104,7 @@ export const NewDocument = ({ onCreate }: Props) => {
const useTemplates = (oppgave: IOppgavebehandling | undefined) => {
const isSaksbehandler = useIsSaksbehandler();
const isRol = useIsRol();
const { user } = useContext(StaticDataContext);

if (oppgave === undefined) {
return [];
Expand All @@ -112,7 +113,7 @@ const useTemplates = (oppgave: IOppgavebehandling | undefined) => {
const { isAvsluttetAvSaksbehandler, typeId } = oppgave;

if (isAvsluttetAvSaksbehandler) {
return [GENERELT_BREV_WITHOUT_MU_TEMPLATE, NOTAT_TEMPLATE];
return [getGenereltBrevTemplate(false, user.navIdent), NOTAT_TEMPLATE];
}

if (isSaksbehandler || isRol) {
Expand Down
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, creator } = smartDocument;
const { id, templateId } = smartDocument;
const { newCommentSelection } = useContext(SmartEditorContext);
const { user } = useContext(StaticDataContext);
const canEdit = useCanEditDocument(templateId, creator.employee.navIdent);
const canEdit = useCanEditDocument(templateId);
const plugins = collaborationSaksbehandlerPlugins(oppgave.id, id, smartDocument, user);

const editor = usePlateEditor<KabalValue, (typeof plugins)[0]>({
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, smartDocument.creator.employee.navIdent);
const canEditDocument = useCanEditDocument(smartDocument.templateId);
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
8 changes: 4 additions & 4 deletions frontend/src/plate/components/signature/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ export const useMainSignature = (element: SignatureElement): ISignature | undefi
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) {
Expand All @@ -64,6 +60,10 @@ const toSignature = (
return undefined;
}

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

return {
name: getName(signature, useShortName),
title: getTitle(signature.customJobTitle, suffix) ?? MISSING_TITLE,
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/plate/components/signature/signature.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ export const Signature = (props: PlateElementProps<SignatureElement>) => {

const isRolAnswers = templateId === TemplateIdEnum.ROL_ANSWERS;

const { children, element, editor } = props;
const hasMedunderskriver = oppgave.medunderskriver.employee !== null;
const showMedunderskriverCheckbox = hasMedunderskriver && !isRolAnswers;
const showForkortedeNavnCheckbox = hasMedunderskriver || !signature.anonymous;
const showForkortedeNavnCheckbox = element.includeMedunderskriver || !signature.anonymous;
const showSuffixCheckbox = !(signature.anonymous || isRolAnswers);
const showUseMyNameCheckbox = oppgave.avsluttetAvSaksbehandlerDate === null;
const showUseMyNameCheckbox = oppgave.avsluttetAvSaksbehandlerDate !== null;

const hideAll = !(showForkortedeNavnCheckbox || showSuffixCheckbox || hasMedunderskriver);

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

const options: SetNodesOptions = { at: [], voids: true, mode: 'lowest', match: (n) => n === element };
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/plate/templates/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ export const createSimpleBulletList = (...textItems: string[]): BulletListElemen
children: textItems.map(createSimpleListItem),
});

export const createSignature = (includeMedunderskriver = true): SignatureElement => ({
export const createSignature = (includeMedunderskriver = true, overriddenSaksbehandler?: string): SignatureElement => ({
type: ELEMENT_SIGNATURE,
useShortName: false,
includeMedunderskriver,
useSuffix: true,
overriddenSaksbehandler: undefined,
overriddenSaksbehandler,
children: [{ text: '' }],
threadIds: [],
});
Expand Down
8 changes: 5 additions & 3 deletions frontend/src/plate/templates/simple-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,24 @@ import {
createSimpleParagraph,
} from './helpers';

const getGenereltBrevTemplate = (includeMedunderskriver: boolean): Immutable<IMutableSmartEditorTemplate> =>
export const getGenereltBrevTemplate = (
includeMedunderskriver: boolean,
overriddenSaksbehandler?: string,
): Immutable<IMutableSmartEditorTemplate> =>
deepFreeze({
templateId: TemplateIdEnum.GENERELT_BREV,
tittel: 'Generelt brev',
richText: [
createCurrentDate(),
createHeader(),
createSimpleParagraph(),
createSignature(includeMedunderskriver),
createSignature(includeMedunderskriver, overriddenSaksbehandler),
createFooter(),
],
dokumentTypeId: DistribusjonsType.BREV,
});

export const GENERELT_BREV_TEMPLATE = getGenereltBrevTemplate(true);
export const GENERELT_BREV_WITHOUT_MU_TEMPLATE = getGenereltBrevTemplate(false);

export const NOTAT_TEMPLATE = deepFreeze<IMutableSmartEditorTemplate>({
templateId: TemplateIdEnum.NOTAT,
Expand Down
Loading