|
| 1 | +import { FormsApiFormTranslation } from '@navikt/skjemadigitalisering-shared-domain'; |
| 2 | +import { RequestHandler } from 'express'; |
| 3 | +import { HttpError as OldHttpError } from '../../../fetchUtils'; |
| 4 | +import { formTranslationsService } from '../../../services'; |
| 5 | +import { HttpError } from '../helpers/errors'; |
| 6 | + |
| 7 | +const get: RequestHandler = async (req, res, next) => { |
| 8 | + const { formPath } = req.params; |
| 9 | + try { |
| 10 | + const translations = await formTranslationsService.get(formPath); |
| 11 | + res.json(translations); |
| 12 | + } catch (error) { |
| 13 | + next(error); |
| 14 | + } |
| 15 | +}; |
| 16 | + |
| 17 | +const post: RequestHandler = async (req, res, next) => { |
| 18 | + const { formPath } = req.params; |
| 19 | + const accessToken = req.headers.AzureAccessToken as string; |
| 20 | + const { key, nb, nn, en, globalTranslationId } = req.body as FormsApiFormTranslation; |
| 21 | + const body = globalTranslationId ? { key, globalTranslationId } : { key, nb, nn, en }; |
| 22 | + try { |
| 23 | + const translation = await formTranslationsService.post(formPath, body, accessToken); |
| 24 | + res.status(201).json(translation); |
| 25 | + } catch (error) { |
| 26 | + if (error instanceof OldHttpError) { |
| 27 | + next(new HttpError(error.message, error.response.status)); |
| 28 | + } else { |
| 29 | + next(error); |
| 30 | + } |
| 31 | + } |
| 32 | +}; |
| 33 | + |
| 34 | +const put: RequestHandler = async (req, res, next) => { |
| 35 | + const { formPath, id } = req.params; |
| 36 | + const { revision, nb, nn, en, globalTranslationId } = req.body as FormsApiFormTranslation; |
| 37 | + const accessToken = req.headers.AzureAccessToken as string; |
| 38 | + const body = globalTranslationId ? { globalTranslationId } : { nb, nn, en }; |
| 39 | + try { |
| 40 | + const translation = await formTranslationsService.put(formPath, id, body, revision!, accessToken); |
| 41 | + res.json(translation); |
| 42 | + } catch (error) { |
| 43 | + if (error instanceof OldHttpError) { |
| 44 | + next(new HttpError(error.message, error.response.status)); |
| 45 | + } else { |
| 46 | + next(error); |
| 47 | + } |
| 48 | + } |
| 49 | +}; |
| 50 | + |
| 51 | +const deleteTranslation: RequestHandler = async (req, res, next) => { |
| 52 | + const { formPath, id } = req.params; |
| 53 | + const accessToken = req.headers.AzureAccessToken as string; |
| 54 | + try { |
| 55 | + await formTranslationsService.delete(formPath, parseInt(id), accessToken); |
| 56 | + res.sendStatus(204); |
| 57 | + } catch (error) { |
| 58 | + if (error instanceof OldHttpError) { |
| 59 | + next(new HttpError(error.message, error.response.status)); |
| 60 | + } else { |
| 61 | + next(error); |
| 62 | + } |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +const formTranslations = { |
| 67 | + get, |
| 68 | + post, |
| 69 | + put, |
| 70 | + delete: deleteTranslation, |
| 71 | +}; |
| 72 | +export default formTranslations; |
0 commit comments