diff --git a/pages/[step].tsx b/pages/[step].tsx index 63f94b00..1ec2c9a0 100644 --- a/pages/[step].tsx +++ b/pages/[step].tsx @@ -28,7 +28,6 @@ import { beskyttetSide } from 'auth/beskyttetSide'; import { GetServerSidePropsResult, NextPageContext } from 'next'; import { getAccessToken } from 'auth/accessToken'; import { getSøker } from './api/oppslag/soeker'; -import { lesBucket } from './api/buckets/les'; import { logSkjemaFullførtEvent, logVeiledningVistEvent } from 'utils/amplitude'; import metrics from 'utils/metrics'; import { scrollRefIntoView } from 'utils/dom'; @@ -146,10 +145,6 @@ const Steps = ({ søker, mellomlagretSøknad, kontaktinformasjon, barn }: PagePr } return false; }; - const postSøknadMedSoknadApi = async (data?: any) => - fetchPOST('/aap/soknad/api/innsending/soknadapi', { - ...data, - }); const postSøknadMedAAPInnsending = async (søknad?: Soknad, requiredVedlegg?: RequiredVedlegg[]) => fetchPOST('/aap/soknad/api/innsending/soknadinnsending/', { diff --git a/pages/api/buckets/lagre.ts b/pages/api/buckets/lagre.ts deleted file mode 100644 index 77368abf..00000000 --- a/pages/api/buckets/lagre.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { NextApiRequest, NextApiResponse } from 'next'; -import { getAccessTokenFromRequest } from 'auth/accessToken'; -import { beskyttetApi } from 'auth/beskyttetApi'; -import { logError, tokenXApiProxy } from '@navikt/aap-felles-utils'; -import { lagreCache } from 'mock/mellomlagringsCache'; -import { erGyldigSøknadsType, GYLDIGE_SØKNADS_TYPER, SøknadsType } from 'utils/api'; -import { isFunctionalTest, isMock } from 'utils/environments'; -import { getStringFromPossiblyArrayQuery } from 'utils/string'; -import metrics from 'utils/metrics'; -import { lesBucket } from './les'; -import { StepType } from 'components/StepWizard/Step'; - -const handler = beskyttetApi(async (req: NextApiRequest, res: NextApiResponse) => { - const type = getStringFromPossiblyArrayQuery(req.query.type); - if (erGyldigSøknadsType(type)) { - res.status(400).json({ error: 'type må være en av ' + GYLDIGE_SØKNADS_TYPER.join(', ') }); - } - const accessToken = getAccessTokenFromRequest(req); - - const eksisterendeSøknad = await lesBucket(type as SøknadsType, accessToken); - if ( - eksisterendeSøknad && - eksisterendeSøknad.søknad && - Object.keys(eksisterendeSøknad.søknad).length > 0 && - Object.keys(req.body.søknad).length === 0 - ) { - const activeStepIndex = eksisterendeSøknad?.lagretStepList?.find( - (e: StepType) => e.active, - )?.stepIndex; - - logError( - `Overskriver eksisterende søknad med en tom søknad på side ${activeStepIndex ?? 'ukjent'}`, - ); - } - await lagreBucket(type as SøknadsType, req.body, accessToken); - res.status(201).json({}); -}); - -export const lagreBucket = async (type: SøknadsType, data: string, accessToken?: string) => { - if (isFunctionalTest()) return; - if (isMock()) return await lagreCache(JSON.stringify(data)); - await tokenXApiProxy({ - url: `${process.env.SOKNAD_API_URL}/buckets/lagre/${type}`, - prometheusPath: `buckets/lagre/${type}`, - method: 'POST', - data: JSON.stringify(data), - audience: process.env.SOKNAD_API_AUDIENCE!, - noResponse: true, - bearerToken: accessToken, - metricsStatusCodeCounter: metrics.backendApiStatusCodeCounter, - metricsTimer: metrics.backendApiDurationHistogram, - }); - return; -}; - -export default handler; diff --git a/pages/api/buckets/les.ts b/pages/api/buckets/les.ts deleted file mode 100644 index be81855d..00000000 --- a/pages/api/buckets/les.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { NextApiRequest, NextApiResponse } from 'next'; -import { getAccessTokenFromRequest } from 'auth/accessToken'; -import { beskyttetApi } from 'auth/beskyttetApi'; -import { logInfo, tokenXApiProxy } from '@navikt/aap-felles-utils'; -import metrics from 'utils/metrics'; -import { erGyldigSøknadsType, GYLDIGE_SØKNADS_TYPER, SøknadsType } from 'utils/api'; -import { isMock } from 'utils/environments'; -import { getStringFromPossiblyArrayQuery } from 'utils/string'; -import { SoknadContextState } from 'context/soknadcontext/soknadContext'; - -const handler = beskyttetApi(async (req: NextApiRequest, res: NextApiResponse) => { - const type = getStringFromPossiblyArrayQuery(req.query.type); - if (erGyldigSøknadsType(type)) { - res.status(400).json({ error: 'type må være en av ' + GYLDIGE_SØKNADS_TYPER.join(', ') }); - } - const accessToken = getAccessTokenFromRequest(req); - const result = await lesBucket(type as SøknadsType, accessToken); - res.status(200).json(result); -}); - -export const lesBucket = async ( - type: SøknadsType, - accessToken?: string, - retryCount = 3, -): Promise => { - if (retryCount === 0) { - return undefined; - } - - // Returnerer undefined ettersom vi heller bruker mellomlagring fra innsending lokalt - if (isMock()) { - return; - } - try { - const mellomlagretSøknad = await tokenXApiProxy({ - url: `${process.env.SOKNAD_API_URL}/buckets/les/${type}`, - prometheusPath: `buckets/les/${type}`, - method: 'GET', - audience: process.env.SOKNAD_API_AUDIENCE!, - bearerToken: accessToken, - metricsStatusCodeCounter: metrics.backendApiStatusCodeCounter, - metricsTimer: metrics.backendApiDurationHistogram, - }); - - if (!mellomlagretSøknad) { - await new Promise((resolve) => setTimeout(resolve, 300)); - return await lesBucket(type, accessToken, retryCount - 1); - } - - return mellomlagretSøknad; - } catch (error: any) { - if (error?.status === 503) { - await new Promise((resolve) => setTimeout(resolve, 300)); - return await lesBucket(type, accessToken, retryCount - 1); - } - logInfo('Fant ingen mellomlagret søknad hos soknad-api'); - return undefined; - } -}; - -export default handler; diff --git a/pages/api/buckets/slett.ts b/pages/api/buckets/slett.ts deleted file mode 100644 index 6a77cb1c..00000000 --- a/pages/api/buckets/slett.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { NextApiRequest, NextApiResponse } from 'next'; -import { getAccessTokenFromRequest } from 'auth/accessToken'; -import { beskyttetApi } from 'auth/beskyttetApi'; -import { tokenXApiProxy } from '@navikt/aap-felles-utils'; -import metrics from 'utils/metrics'; -import { deleteCache } from 'mock/mellomlagringsCache'; -import { erGyldigSøknadsType, GYLDIGE_SØKNADS_TYPER, SøknadsType } from 'utils/api'; -import { isMock } from 'utils/environments'; -import { getStringFromPossiblyArrayQuery } from 'utils/string'; - -const handler = beskyttetApi(async (req: NextApiRequest, res: NextApiResponse) => { - const type = getStringFromPossiblyArrayQuery(req.query.type); - if (erGyldigSøknadsType(type)) { - res.status(400).json({ error: 'type må være en av ' + GYLDIGE_SØKNADS_TYPER.join(', ') }); - return; - } - const accessToken = getAccessTokenFromRequest(req); - await slettBucket(type as SøknadsType, accessToken); - res.status(204).json({}); -}); - -export const slettBucket = async (type: SøknadsType, accessToken?: string) => { - if (isMock()) { - await deleteCache(); - return; - } - - await tokenXApiProxy({ - url: `${process.env.SOKNAD_API_URL}/buckets/slett/${type}`, - prometheusPath: `buckets/slett/${type}`, - method: 'DELETE', - noResponse: true, - audience: process.env.SOKNAD_API_AUDIENCE!, - bearerToken: accessToken, - metricsStatusCodeCounter: metrics.backendApiStatusCodeCounter, - metricsTimer: metrics.backendApiDurationHistogram, - }); -}; - -export default handler; diff --git a/pages/api/innsending/soknadapi.ts b/pages/api/innsending/soknadapi.ts deleted file mode 100644 index f469a411..00000000 --- a/pages/api/innsending/soknadapi.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { randomUUID } from 'crypto'; -import { NextApiRequest, NextApiResponse } from 'next'; -import { getAccessTokenFromRequest } from 'auth/accessToken'; -import { beskyttetApi } from 'auth/beskyttetApi'; -import { tokenXApiProxy } from '@navikt/aap-felles-utils'; -import { isFunctionalTest, isMock } from 'utils/environments'; -import { slettBucket } from '../buckets/slett'; -import { ErrorMedStatus } from 'auth/ErrorMedStatus'; -import metrics from 'utils/metrics'; - -const handler = beskyttetApi(async (req: NextApiRequest, res: NextApiResponse) => { - const accessToken = getAccessTokenFromRequest(req); - try { - const soknadRes = await sendSoknad(req.body, accessToken); - metrics.sendSoknadCounter.inc({ type: 'STANDARD' }); - res.status(201).json(soknadRes); - } catch (err) { - if (err instanceof ErrorMedStatus) { - res.status(err.status).json({ navCallId: err.navCallId }); - } else { - throw err; - } - } -}); - -export const sendSoknad = async (data: string, accessToken?: string) => { - if (isFunctionalTest()) { - return { uri: `https://localhost:3000/aap/soknad/api/vedlegg/les?uuid=${randomUUID()}` }; - } - if (isMock()) { - await slettBucket('STANDARD', accessToken); - return { uri: `https://localhost:3000/aap/soknad/api/vedlegg/les?uuid=${randomUUID()}` }; - } - const søknad = await tokenXApiProxy({ - url: `${process.env.SOKNAD_API_URL}/innsending/soknad`, - prometheusPath: 'innsending/soknad', - method: 'POST', - data: JSON.stringify(data), - audience: process.env.SOKNAD_API_AUDIENCE!, - bearerToken: accessToken, - metricsStatusCodeCounter: metrics.backendApiStatusCodeCounter, - metricsTimer: metrics.backendApiDurationHistogram, - }); - return søknad; -}; - -export default handler; diff --git a/pages/api/innsending/soknadinnsending.ts b/pages/api/innsending/soknadinnsending.ts index 6636321e..56d29642 100644 --- a/pages/api/innsending/soknadinnsending.ts +++ b/pages/api/innsending/soknadinnsending.ts @@ -4,7 +4,6 @@ import { NextApiRequest, NextApiResponse } from 'next'; import metrics from 'utils/metrics'; import { ErrorMedStatus } from 'auth/ErrorMedStatus'; import { isFunctionalTest, isMock } from 'utils/environments'; -import { slettBucket } from 'pages/api/buckets/slett'; import { createIntl } from 'react-intl'; import { flattenMessages, messages } from 'utils/message'; import links from 'translations/links.json'; @@ -152,7 +151,6 @@ export const sendSoknadViaAapInnsending = async ( return 'Vi har mottat søknaden din.'; } if (isMock()) { - await slettBucket('STANDARD', accessToken); return 'Vi har mottat søknaden din.'; } const søknad = await tokenXApiProxy({ diff --git a/pages/api/vedlegg/lagre.ts b/pages/api/vedlegg/lagre.ts deleted file mode 100644 index 608f9df8..00000000 --- a/pages/api/vedlegg/lagre.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { randomUUID } from 'crypto'; -import { getAccessTokenFromRequest } from 'auth/accessToken'; -import { beskyttetApi } from 'auth/beskyttetApi'; -import { tokenXApiStreamProxy, logInfo } from '@navikt/aap-felles-utils'; -import metrics from 'utils/metrics'; -import { isMock } from 'utils/environments'; - -const handler = beskyttetApi(async (req, res) => { - logInfo('Har mottatt request om filopplasting'); - const accessToken = getAccessTokenFromRequest(req); - if (isMock()) { - res.status(201).json(randomUUID()); - } else { - await tokenXApiStreamProxy({ - url: `${process.env.SOKNAD_API_URL}/vedlegg/lagre`, - prometheusPath: '/vedlegg/lagre', - req, - res, - audience: process.env.SOKNAD_API_AUDIENCE!, - bearerToken: accessToken, - metricsStatusCodeCounter: metrics.backendApiStatusCodeCounter, - metricsTimer: metrics.backendApiDurationHistogram, - }); - } -}); - -export const config = { - api: { - bodyParser: false, - }, -}; - -export default handler; diff --git a/pages/api/vedlegg/les.ts b/pages/api/vedlegg/les.ts deleted file mode 100644 index 9fec2a64..00000000 --- a/pages/api/vedlegg/les.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { beskyttetApi } from 'auth/beskyttetApi'; -import { getStringFromPossiblyArrayQuery } from 'utils/string'; -import { tokenXProxy } from 'lib/utils/TokenxProxy'; - -const handler = beskyttetApi(async (req, res) => { - const uuid = getStringFromPossiblyArrayQuery(req.query.uuid); - if (!uuid) { - res.status(400).json({ error: 'uuid må være en string' }); - } - return await tokenXProxy(req, res, `/vedlegg/les/${uuid}`, '/vedlegg/les'); -}); - -export const config = { - api: { - responseLimit: '50mb', - bodyParser: false, - externalResolver: true, - }, -}; - -export default handler; diff --git a/pages/api/vedlegg/slett.ts b/pages/api/vedlegg/slett.ts deleted file mode 100644 index 1d1f6a45..00000000 --- a/pages/api/vedlegg/slett.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { getAccessTokenFromRequest } from 'auth/accessToken'; -import { beskyttetApi } from 'auth/beskyttetApi'; -import { tokenXApiProxy } from '@navikt/aap-felles-utils'; -import metrics from 'utils/metrics'; -import { isMock } from 'utils/environments'; -import { getCommaSeparatedStringFromStringOrArray } from 'utils/string'; - -const handler = beskyttetApi(async (req, res) => { - const uuid = req.query.uuid ?? []; - if (!uuid) { - res.status(400).json({ error: 'uuid må være en string' }); - } - const accessToken = getAccessTokenFromRequest(req); - await slettVedlegg(uuid, accessToken); - res.status(204).end(); -}); - -export const slettVedlegg = async (uuids: string | string[], accessToken?: string) => { - if (isMock()) return; - const commaSeparatedUuids = getCommaSeparatedStringFromStringOrArray(uuids); - await tokenXApiProxy({ - url: `${process.env.SOKNAD_API_URL}/vedlegg/slett?uuids=${commaSeparatedUuids}`, - prometheusPath: '/vedlegg/slett', - method: 'DELETE', - noResponse: true, - audience: process.env.SOKNAD_API_AUDIENCE!, - bearerToken: accessToken, - metricsStatusCodeCounter: metrics.backendApiStatusCodeCounter, - metricsTimer: metrics.backendApiDurationHistogram, - }); -}; - -export const config = { - api: { - bodyParser: false, - externalResolver: true, - }, -}; - -export default handler; diff --git a/pages/index.tsx b/pages/index.tsx index 614d2beb..2997c4ad 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -6,7 +6,6 @@ import { GetServerSidePropsResult, NextPageContext } from 'next/types'; import { beskyttetSide } from 'auth/beskyttetSide'; import { getAccessToken } from 'auth/accessToken'; import { fetchPOST } from 'api/fetch'; -import { lesBucket } from 'pages/api/buckets/les'; import { StepType } from 'components/StepWizard/Step'; import { logSkjemaStartetEvent } from 'utils/amplitude'; import metrics from 'utils/metrics'; diff --git a/pages/vedlegg/[uuid].tsx b/pages/vedlegg/[uuid].tsx index c03421ca..0e274de6 100644 --- a/pages/vedlegg/[uuid].tsx +++ b/pages/vedlegg/[uuid].tsx @@ -14,30 +14,15 @@ const Vedlegg = ({}: PageProps) => { useEffect(() => { const getFile = async () => { - const [fileFromSoknadApi, fileFromInnsending] = await Promise.all([ - fetch(`/aap/soknad/api/vedlegg/les/?uuid=${uuid}`) - .then((res) => { - if (res.ok) { - return res.blob(); - } - return undefined; - }) - .catch(() => undefined), - fetch(`/aap/soknad/api/vedlegginnsending/les/?uuid=${uuid}`) - .then((res) => { - if (res.ok) { - return res.blob(); - } - return undefined; - }) - .catch(() => undefined), - ]); - - if (fileFromInnsending) { - setFile(fileFromInnsending); - } else if (fileFromSoknadApi) { - setFile(fileFromSoknadApi); - } + const fileFromInnsending = await fetch(`/aap/soknad/api/vedlegginnsending/les/?uuid=${uuid}`) + .then((res) => { + if (res.ok) { + return res.blob(); + } + return undefined; + }) + .catch(() => undefined); + setFile(fileFromInnsending); }; getFile();