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

Feature/handle 404 for mellomlagring #1011

Merged
merged 6 commits into from
Feb 6, 2024
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
31 changes: 23 additions & 8 deletions packages/fyllut-backend/src/routers/api/helpers/sendInn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface HovedDokument {
document: string | null;
}

export interface Attachment {
interface Attachment {
vedleggsnr: string;
tittel: string;
label: string;
Expand All @@ -28,7 +28,7 @@ export interface Attachment {
vedleggskjema?: string;
}

export interface SendInnSoknadBody {
interface SendInnSoknadBody {
brukerId: string;
skjemanr: string;
skjemapath: string;
Expand All @@ -49,13 +49,13 @@ const isValidUuid = (innsendingsId: string): boolean => {
};

const DEFAULT_LANGUAGE = 'nb-NO';
export const objectToByteArray = (obj: object) => Array.from(new TextEncoder().encode(JSON.stringify(obj)));
const objectToByteArray = (obj: object) => Array.from(new TextEncoder().encode(JSON.stringify(obj)));

export const byteArrayToObject = (byteArray?: Buffer) => JSON.parse(new TextDecoder().decode(byteArray));
const byteArrayToObject = (byteArray?: Buffer) => JSON.parse(new TextDecoder().decode(byteArray));

export const sanitizeInnsendingsId = (innsendingsId: string) => innsendingsId.replace(/[./]/g, '');
const sanitizeInnsendingsId = (innsendingsId: string) => innsendingsId.replace(/[./]/g, '');

export const validateInnsendingsId = (innsendingsId: string | undefined, supplementaryMessage?: string) => {
const validateInnsendingsId = (innsendingsId: string | undefined, supplementaryMessage?: string) => {
let errorMessage;
if (!innsendingsId) {
errorMessage = 'InnsendingsId mangler.';
Expand All @@ -71,7 +71,7 @@ export const validateInnsendingsId = (innsendingsId: string | undefined, supplem
return errorMessage;
};

export const isMellomLagringEnabled = (featureToggles: FeatureTogglesMap) => {
const isMellomLagringEnabled = (featureToggles: FeatureTogglesMap) => {
if (!featureToggles?.enableMellomlagring) {
logger.debug('Mellomlagring not enabled, returning data in body');
return false;
Expand All @@ -84,7 +84,7 @@ export const isMellomLagringEnabled = (featureToggles: FeatureTogglesMap) => {
return true;
};

export const assembleSendInnSoknadBody = (
const assembleSendInnSoknadBody = (
requestBody: {
form: NavFormType;
submission: Submission;
Expand Down Expand Up @@ -159,3 +159,18 @@ export const assembleSendInnSoknadBody = (

return body;
};

const isNotFound = (response: { status: number }, responseError: Error | undefined) =>
response.status === 404 ||
responseError?.['http_response_body']?.errorCode === 'illegalAction.applicationSentInOrDeleted';

export {
assembleSendInnSoknadBody,
byteArrayToObject,
isMellomLagringEnabled,
isNotFound,
objectToByteArray,
sanitizeInnsendingsId,
validateInnsendingsId,
};
export type { Attachment, SendInnSoknadBody };
33 changes: 27 additions & 6 deletions packages/fyllut-backend/src/routers/api/send-inn-soknad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
assembleSendInnSoknadBody,
byteArrayToObject,
isMellomLagringEnabled,
isNotFound,
sanitizeInnsendingsId,
validateInnsendingsId,
} from './helpers/sendInn';
Expand Down Expand Up @@ -57,12 +58,11 @@ const sendInnSoknad = {
`Feil ved kall til SendInn. ${getErrorMessage}`,
true,
);
if (
sendInnResponse.status === 404 ||
responseError?.['http_response_body']?.errorCode === 'illegalAction.applicationSentInOrDeleted'
) {
if (isNotFound(sendInnResponse, responseError)) {
logger.info(`${sanitizedInnsendingsId}: Not found. Failed to get`, responseError);
return res.sendStatus(404);
}

logger.debug('Failed to fetch data from SendInn');
return next(responseError);
}
Expand Down Expand Up @@ -115,6 +115,7 @@ const sendInnSoknad = {
next(err);
}
},

put: async (req: Request, res: Response, next: NextFunction) => {
try {
const idportenPid = getIdportenPid(req);
Expand Down Expand Up @@ -152,8 +153,18 @@ const sendInnSoknad = {
logger.debug('Successfylly updated data in SendInn');
res.json(await sendInnResponse.json());
} else {
const responseError = await responseToError(
sendInnResponse,
`Feil ved kall til SendInn. ${putErrorMessage}`,
true,
);
if (isNotFound(sendInnResponse, responseError)) {
logger.info(`${sanitizedInnsendingsId}: Not found. Failed to update`, responseError);
return res.sendStatus(404);
Copy link
Member

Choose a reason for hiding this comment

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

Burde vi logge noe her?

}

logger.debug('Failed to update data in SendInn');
next(await responseToError(sendInnResponse, `Feil ved kall til SendInn. ${putErrorMessage}`, true));
next(responseError);
}
} catch (err) {
next(err);
Expand Down Expand Up @@ -192,8 +203,18 @@ const sendInnSoknad = {
const json = await sendInnResponse.json();
res.json(json);
} else {
const responseError = await responseToError(
sendInnResponse,
`Feil ved kall til SendInn. ${deleteErrorMessage}`,
true,
);
if (isNotFound(sendInnResponse, responseError)) {
logger.info(`${sanitizedInnsendingsId}: Not found. Failed to delete`, responseError);
return res.sendStatus(404);
}

logger.debug(`Failed to delete soknad with innsendingsId ${sanitizedInnsendingsId}`);
next(await responseToError(sendInnResponse, `Feil ved kall til SendInn. ${deleteErrorMessage}`, true));
next(responseError);
}
} catch (err) {
next(err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createPdfAsByteArray } from './helpers/pdfService';
import {
assembleSendInnSoknadBody,
isMellomLagringEnabled,
isNotFound,
sanitizeInnsendingsId,
validateInnsendingsId,
} from './helpers/sendInn';
Expand Down Expand Up @@ -74,8 +75,14 @@ const sendInnUtfyltSoknad = {
});
res.sendStatus(201);
} else {
const responseError = await responseToError(sendInnResponse, 'Feil ved kall til SendInn', true);
if (isNotFound(sendInnResponse, responseError)) {
logger.info(`${sanitizedInnsendingsId}: Not found. Failed to submit`, responseError);
return res.sendStatus(404);
}

logger.debug('Failed to post data to SendInn');
next(await responseToError(sendInnResponse, 'Feil ved kall til SendInn', true));
next(responseError);
}
} catch (err) {
next(err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ import { useSendInn } from '../../../../context/sendInn/sendInnContext';
export interface Props {
submission?: Submission;
isValid?: (e: React.MouseEvent<HTMLElement>) => boolean;
onError: Function;
onError: (err: Error) => void;
onDone?: () => void;
children: string;
withIcon?: boolean;
}

const DigitalSubmissionButton = ({ submission, isValid, onError, children, withIcon = false }: Props) => {
const DigitalSubmissionButton = ({
submission,
isValid,
onError,
onDone = () => {},
children,
withIcon = false,
}: Props) => {
const { loggNavigering } = useAmplitude();
const { app } = useAppConfig();
const { translate } = useLanguages();
Expand Down Expand Up @@ -44,6 +52,7 @@ const DigitalSubmissionButton = ({ submission, isValid, onError, children, withI
onError(err);
} finally {
setLoading(false);
onDone();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const DigitalSubmissionWithPrompt = ({ submission, isValid, onError }: Props) =>
onError(err);
setIsOpen(false);
}}
onDone={() => setIsOpen(false)}
>
{translate(TEXTS.grensesnitt.submitToNavPrompt.confirm)}
</DigitalSubmissionButton>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ const SummaryPageNavigation = ({ form, submission, formUrl, panelValidationList,
<Heading size="small" level="4">
{translate(mellomlagringError.title)}
</Heading>
{mellomlagringError.linkText && (
<p>
{translate(mellomlagringError.messageStart)}
<a href={translate(mellomlagringError.url)}>{translate(mellomlagringError.linkText)}</a>
{translate(mellomlagringError.messageEnd)}
</p>
)}
{translate(mellomlagringError.message, mellomlagringError?.messageParams)}
</Alert>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,65 @@ export const mellomlagringReducer = (

const getError = (type: ErrorType, savedDate?: string): MellomlagringError => {
switch (type) {
case 'GET FAILED':
case 'GET_FAILED':
return {
type,
title: TEXTS.statiske.mellomlagringError.get.title,
message: TEXTS.statiske.mellomlagringError.get.message,
};
case 'CREATE FAILED':
case 'CREATE_FAILED':
return {
type,
title: TEXTS.statiske.mellomlagringError.create.title,
message: TEXTS.statiske.mellomlagringError.create.message,
};
case 'UPDATE FAILED':
case 'UPDATE_FAILED':
return {
type,
title: TEXTS.statiske.mellomlagringError.update.title,
message: TEXTS.statiske.mellomlagringError.update.message,
};
case 'DELETE FAILED':
case 'UPDATE_FAILED_NOT_FOUND':
return {
type,
title: TEXTS.statiske.mellomlagringError.updateNotFound.title,
messageStart: TEXTS.statiske.mellomlagringError.updateNotFound.messageStart,
messageEnd: TEXTS.statiske.mellomlagringError.updateNotFound.messageEnd,
linkText: TEXTS.statiske.external.minSide.linkText,
url: TEXTS.statiske.external.minSide.url,
};
case 'DELETE_FAILED':
return {
type,
title: TEXTS.statiske.mellomlagringError.delete.title,
message: TEXTS.statiske.mellomlagringError.delete.message,
};
case 'SUBMIT FAILED':
case 'DELETE_FAILED_NOT_FOUND':
return {
type,
title: TEXTS.statiske.mellomlagringError.deleteNotFound.title,
messageStart: TEXTS.statiske.mellomlagringError.deleteNotFound.messageStart,
messageEnd: TEXTS.statiske.mellomlagringError.deleteNotFound.messageEnd,
linkText: TEXTS.statiske.external.minSide.linkText,
url: TEXTS.statiske.external.minSide.url,
};
case 'SUBMIT_FAILED_NOT_FOUND':
return {
type,
title: TEXTS.statiske.mellomlagringError.submitNotFound.title,
messageStart: TEXTS.statiske.mellomlagringError.submitNotFound.messageStart,
messageEnd: TEXTS.statiske.mellomlagringError.submitNotFound.messageEnd,
linkText: TEXTS.statiske.external.minSide.linkText,
url: TEXTS.statiske.external.minSide.url,
};
case 'SUBMIT_FAILED':
return {
type,
title: TEXTS.statiske.mellomlagringError.submit.title,
message: TEXTS.statiske.mellomlagringError.submit.draftSaved,
messageParams: { date: savedDate },
};
case 'SUBMIT AND UPDATE FAILED':
case 'SUBMIT_AND_UPDATE_FAILED':
const draftSavedMessage = savedDate
? {
message: TEXTS.statiske.mellomlagringError.submit.draftSaved,
Expand Down
53 changes: 35 additions & 18 deletions packages/shared-components/src/context/sendInn/sendInnContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const SendInnProvider = ({
return;
}
logger?.error(`${innsendingsIdFromParams}: Failed to retrieve mellomlagring`, error as Error);
dispatchFyllutMellomlagring({ type: 'error', error: 'GET FAILED' });
dispatchFyllutMellomlagring({ type: 'error', error: 'GET_FAILED' });
setInitStatus('error');
}
};
Expand Down Expand Up @@ -199,7 +199,7 @@ const SendInnProvider = ({
}
return response;
} catch (error: any) {
dispatchFyllutMellomlagring({ type: 'error', error: 'CREATE FAILED' });
dispatchFyllutMellomlagring({ type: 'error', error: 'CREATE_FAILED' });
logger?.error('Failed to create mellomlagring', error);
}
};
Expand All @@ -223,10 +223,15 @@ const SendInnProvider = ({
logger?.info(`${innsendingsId}: Mellomlagring was updated`);
dispatchFyllutMellomlagring({ type: 'update', response });
return response;
} catch (error) {
dispatchFyllutMellomlagring({ type: 'error', error: 'UPDATE FAILED' });
logger?.error(`${innsendingsId}: Failed to update mellomlagring`, error as Error);
throw error;
} catch (error: any) {
if (error.status === 404) {
dispatchFyllutMellomlagring({ type: 'error', error: 'UPDATE_FAILED_NOT_FOUND' });
throw error;
} else {
dispatchFyllutMellomlagring({ type: 'error', error: 'UPDATE_FAILED' });
logger?.error(`${innsendingsId}: Failed to update mellomlagring`, error as Error);
throw error;
}
}
};

Expand All @@ -239,10 +244,15 @@ const SendInnProvider = ({
const response = await deleteSoknad(appConfig, innsendingsId);
logger?.info(`${innsendingsId}: Mellomlagring was deleted`);
return response;
} catch (error) {
dispatchFyllutMellomlagring({ type: 'error', error: 'DELETE FAILED' });
logger?.error(`${innsendingsId}: Failed to delete mellomlagring`, error as Error);
throw error;
} catch (error: any) {
if (error.status === 404) {
dispatchFyllutMellomlagring({ type: 'error', error: 'DELETE_FAILED_NOT_FOUND' });
throw error;
} else {
dispatchFyllutMellomlagring({ type: 'error', error: 'DELETE_FAILED' });
logger?.error(`${innsendingsId}: Failed to delete mellomlagring`, error as Error);
throw error;
}
}
};

Expand Down Expand Up @@ -273,14 +283,21 @@ const SendInnProvider = ({
window.location.href = redirectLocation;
}
return response;
} catch (submitError) {
logger?.error(`${innsendingsId}: Failed to submit, will try to store changes`, submitError as Error);
try {
await updateSoknad(appConfig, form, submission, currentLanguage, translation, innsendingsId);
dispatchFyllutMellomlagring({ type: 'error', error: 'SUBMIT FAILED' });
} catch (updateError) {
logger?.error(`${innsendingsId}: Failed to update mellomlagring after a failed submit`, updateError as Error);
dispatchFyllutMellomlagring({ type: 'error', error: 'SUBMIT AND UPDATE FAILED' });
} catch (submitError: any) {
if (submitError.status === 404) {
dispatchFyllutMellomlagring({ type: 'error', error: 'SUBMIT_FAILED_NOT_FOUND' });
} else {
logger?.error(`${innsendingsId}: Failed to submit, will try to store changes`, submitError as Error);
try {
await updateSoknad(appConfig, form, submission, currentLanguage, translation, innsendingsId);
dispatchFyllutMellomlagring({ type: 'error', error: 'SUBMIT_FAILED' });
} catch (updateError) {
logger?.error(
`${innsendingsId}: Failed to update mellomlagring after a failed submit`,
updateError as Error,
);
dispatchFyllutMellomlagring({ type: 'error', error: 'SUBMIT_AND_UPDATE_FAILED' });
}
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
<h4 class="navds-heading navds-heading--small">{{ctx.t(ctx.submission?.fyllutState?.mellomlagring?.error?.title)}}</h4>
{% } %}
<div class="navds-alert__wrapper">
{{ctx.t(ctx.submission?.fyllutState?.mellomlagring?.error?.message)}}
{% if (ctx.submission?.fyllutState?.mellomlagring?.error?.linkText) { %}
{{ctx.t(ctx.submission?.fyllutState?.mellomlagring?.error?.messageStart)}}
<a href={{ctx.t(ctx.submission?.fyllutState?.mellomlagring?.error?.url)}}>{{ctx.t(ctx.submission?.fyllutState?.mellomlagring?.error?.linkText)}}</a>
{{ctx.t(ctx.submission?.fyllutState?.mellomlagring?.error?.messageEnd)}}
{% } else { %}
{{ctx.t(ctx.submission?.fyllutState?.mellomlagring?.error?.message)}}
{% } %}
</div>
</div>
</div>
Expand Down
Loading