From cb6cf5373ded23cdca01c12a7dcb3ea84979ee9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20R=C3=B8ed=20Hestvik?= Date: Thu, 9 Jan 2025 13:24:01 +0100 Subject: [PATCH] Publish to github with dummy-tag --- .../global-translations.ts | 10 ++--- .../utils/mapToFormioFormat.ts | 43 ++++++------------- .../translation/GlobalTranslationsService.ts | 13 +++++- .../src/services/translation/types.ts | 7 ++- packages/shared-domain/src/index.ts | 2 + .../shared-domain/src/languages/types.d.ts | 2 +- .../src/translations/FormsApiTranslation.ts | 13 +++++- 7 files changed, 49 insertions(+), 41 deletions(-) diff --git a/packages/bygger-backend/src/routers/api/forms-api-global-translations/global-translations.ts b/packages/bygger-backend/src/routers/api/forms-api-global-translations/global-translations.ts index a2d4b0e40..8752f1db3 100644 --- a/packages/bygger-backend/src/routers/api/forms-api-global-translations/global-translations.ts +++ b/packages/bygger-backend/src/routers/api/forms-api-global-translations/global-translations.ts @@ -55,13 +55,11 @@ const publish: RequestHandler = async (req, res, next) => { const accessToken = req.headers.AzureAccessToken as string; await globalTranslationsService.publish(accessToken); - const translations = await globalTranslationsService.get(); - const { en, 'nn-NO': nn }: GlobalTranslationsResourceContent = mapGlobalToFormioFormat(translations); - const resultEnglish = await backendInstance.publishResource('global-translations-en', { en }); - const resultNynorsk = await backendInstance.publishResource('global-translations-nn-NO', { 'nn-NO': nn }); + const publishedTranslations = await globalTranslationsService.getPublished(['nn', 'en'], accessToken); + const { en, 'nn-NO': nn }: GlobalTranslationsResourceContent = mapGlobalToFormioFormat(publishedTranslations); - console.log(resultEnglish); - console.log(resultNynorsk); + await backendInstance.publishResource('global-translations-en', { en }); + await backendInstance.publishResource('global-translations-nn-NO', { 'nn-NO': nn }); res.sendStatus(201); } catch (error) { diff --git a/packages/bygger-backend/src/routers/api/forms-api-global-translations/utils/mapToFormioFormat.ts b/packages/bygger-backend/src/routers/api/forms-api-global-translations/utils/mapToFormioFormat.ts index 072d94b6a..568d87688 100644 --- a/packages/bygger-backend/src/routers/api/forms-api-global-translations/utils/mapToFormioFormat.ts +++ b/packages/bygger-backend/src/routers/api/forms-api-global-translations/utils/mapToFormioFormat.ts @@ -1,43 +1,26 @@ import { - FormsApiGlobalTranslation, GlobalTranslationsResourceContent, + PublishedTranslations, TranslationResource, } from '@navikt/skjemadigitalisering-shared-domain'; -const mapGlobalToFormioFormat = (translations: FormsApiGlobalTranslation[]): GlobalTranslationsResourceContent => { - const initTags = { skjematekster: {}, grensesnitt: {}, validering: {}, 'statiske-tekster': {} }; - const accumulated = translations.reduce( - (acc, { key, en, nn, tag }) => { - const resultEn = { value: en, scope: 'global' }; - const resultNn = { value: nn, scope: 'global' }; - return { - ...acc, - en: { ...acc.en, [tag]: { ...acc.en[tag], [key]: resultEn } }, - nn: { ...acc.nn, [tag]: { ...acc.nn[tag], [key]: resultNn } }, - }; - }, - { en: { ...initTags }, nn: { ...initTags } }, - ); - const resourceDefaults: Pick = { +const mapGlobalToFormioFormat = ({ translations }: PublishedTranslations): GlobalTranslationsResourceContent => { + const resourceDefaults: Pick = { id: '', name: 'global', scope: 'global', + tag: 'dummy-tag', }; - + const populateWithScope = ( + translationRecord: Record, + ): Record => + Object.entries(translationRecord)?.reduce( + (acc, [key, value]) => ({ ...acc, [key]: { value, scope: 'global' } }), + {}, + ); return { - en: [ - { ...resourceDefaults, tag: 'skjematekster', translations: accumulated.en.skjematekster }, - { ...resourceDefaults, tag: 'grensesnitt', translations: accumulated.en.grensesnitt }, - { ...resourceDefaults, tag: 'validering', translations: accumulated.en.validering }, - { ...resourceDefaults, tag: 'statiske-tekster', translations: accumulated.en['statiske-tekster'] }, - ], - 'nn-NO': [ - { ...resourceDefaults, tag: 'skjematekster', translations: accumulated.nn.skjematekster }, - { ...resourceDefaults, tag: 'grensesnitt', translations: accumulated.nn.grensesnitt }, - { ...resourceDefaults, tag: 'validering', translations: accumulated.nn.validering }, - { ...resourceDefaults, tag: 'statiske-tekster', translations: accumulated.nn['statiske-tekster'] }, - ], - 'nb-NO': [], + en: [{ ...resourceDefaults, translations: populateWithScope(translations.en ?? {}) }], + 'nn-NO': [{ ...resourceDefaults, translations: populateWithScope(translations.nn ?? {}) }], }; }; diff --git a/packages/bygger-backend/src/services/translation/GlobalTranslationsService.ts b/packages/bygger-backend/src/services/translation/GlobalTranslationsService.ts index bda6e6396..a4dfbb009 100644 --- a/packages/bygger-backend/src/services/translation/GlobalTranslationsService.ts +++ b/packages/bygger-backend/src/services/translation/GlobalTranslationsService.ts @@ -1,10 +1,11 @@ -import { FormsApiGlobalTranslation } from '@navikt/skjemadigitalisering-shared-domain'; +import { FormsApiGlobalTranslation, PublishedTranslations } from '@navikt/skjemadigitalisering-shared-domain'; import { fetchWithErrorHandling } from '../../fetchUtils'; import { GlobalTranslationPostBody, GlobalTranslationPutBody, GlobalTranslationService } from './types'; import { createHeaders } from './utils'; const createGlobalTranslationService = (formsApiUrl: string): GlobalTranslationService => { - const globalTranslationsPath: string = '/v1/global-translations'; + const globalTranslationsPath = '/v1/global-translations'; + const publishedGlobalTranslationsPath = '/v1/published-global-translations'; return { get: async (): Promise => { @@ -40,6 +41,14 @@ const createGlobalTranslationService = (formsApiUrl: string): GlobalTranslationS headers: createHeaders(accessToken), }); }, + getPublished: async (languageCodes: Array<'nn' | 'en'>, accessToken: string): Promise => { + const searchParams = new URLSearchParams({ languageCodes: languageCodes.toString() }); + const response = await fetchWithErrorHandling( + `${formsApiUrl}${publishedGlobalTranslationsPath}?${searchParams}`, + { headers: createHeaders(accessToken) }, + ); + return response.data as PublishedTranslations; + }, }; }; diff --git a/packages/bygger-backend/src/services/translation/types.ts b/packages/bygger-backend/src/services/translation/types.ts index 0efb279f5..f5aacbda4 100644 --- a/packages/bygger-backend/src/services/translation/types.ts +++ b/packages/bygger-backend/src/services/translation/types.ts @@ -1,4 +1,8 @@ -import { FormsApiFormTranslation, FormsApiGlobalTranslation } from '@navikt/skjemadigitalisering-shared-domain'; +import { + FormsApiFormTranslation, + FormsApiGlobalTranslation, + PublishedTranslations, +} from '@navikt/skjemadigitalisering-shared-domain'; type FormTranslationPostBody = Pick; type FormTranslationPutBody = Pick; @@ -27,6 +31,7 @@ type GlobalTranslationService = { accessToken: string, ) => Promise; publish: (accessToken: string) => Promise; + getPublished: (languageCodes: Array<'nn' | 'en'>, accessCode: string) => Promise; }; export type { diff --git a/packages/shared-domain/src/index.ts b/packages/shared-domain/src/index.ts index a233a1f98..e33ce1014 100644 --- a/packages/shared-domain/src/index.ts +++ b/packages/shared-domain/src/index.ts @@ -116,6 +116,7 @@ import { FormsApiGlobalTranslation, FormsApiTranslation, formsApiTranslations, + PublishedTranslations, TranslationLang, } from './translations/FormsApiTranslation'; import currencyUtils from './utils/currencyUtils'; @@ -231,6 +232,7 @@ export type { PrefillAddress, PrefillData, PrefillKey, + PublishedTranslations, Recipient, ReportDefinition, ResourceAccess, diff --git a/packages/shared-domain/src/languages/types.d.ts b/packages/shared-domain/src/languages/types.d.ts index 991599192..114e511b6 100644 --- a/packages/shared-domain/src/languages/types.d.ts +++ b/packages/shared-domain/src/languages/types.d.ts @@ -2,7 +2,7 @@ import { FormioResource } from '../resource'; export type Language = 'nb-NO' | 'nn-NO' | 'en'; export type TranslationScope = 'global' | 'local' | 'component-countryName'; -export type TranslationTag = 'skjematekster' | 'grensesnitt' | 'statiske-tekster' | 'validering'; +export type TranslationTag = 'skjematekster' | 'grensesnitt' | 'statiske-tekster' | 'validering' | 'dummy-tag'; export type I18nTranslationMap = Record; export type I18nTranslationReplacements = Record; export type I18nTranslations = Record; diff --git a/packages/shared-domain/src/translations/FormsApiTranslation.ts b/packages/shared-domain/src/translations/FormsApiTranslation.ts index eda8c3404..bcf6a39db 100644 --- a/packages/shared-domain/src/translations/FormsApiTranslation.ts +++ b/packages/shared-domain/src/translations/FormsApiTranslation.ts @@ -14,6 +14,11 @@ type FormsApiTranslationCore = { type FormsApiGlobalTranslation = FormsApiTranslationCore & { tag: TranslationTag }; type FormsApiFormTranslation = FormsApiTranslationCore & { globalTranslationId?: number }; type FormsApiTranslation = FormsApiGlobalTranslation | FormsApiFormTranslation; +type PublishedTranslations = { + publishedAt: string; + publishedBy: string; + translations: { nn?: Record; en?: Record }; +}; const isGlobalTranslation = (translation: FormsApiTranslation): translation is FormsApiGlobalTranslation => !!(translation as FormsApiGlobalTranslation).tag; @@ -23,4 +28,10 @@ const isFormTranslation = (translation: FormsApiTranslation): translation is For const formsApiTranslations = { isFormTranslation, isGlobalTranslation }; export { formsApiTranslations }; -export type { FormsApiFormTranslation, FormsApiGlobalTranslation, FormsApiTranslation, TranslationLang }; +export type { + FormsApiFormTranslation, + FormsApiGlobalTranslation, + FormsApiTranslation, + PublishedTranslations, + TranslationLang, +};