Skip to content

Commit

Permalink
Publish to github with dummy-tag
Browse files Browse the repository at this point in the history
  • Loading branch information
magnurh-cx committed Jan 9, 2025
1 parent 6473df3 commit cb6cf53
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<TranslationResource, 'id' | 'name' | 'scope'> = {
const mapGlobalToFormioFormat = ({ translations }: PublishedTranslations): GlobalTranslationsResourceContent => {
const resourceDefaults: Pick<TranslationResource, 'id' | 'name' | 'scope' | 'tag'> = {
id: '',
name: 'global',
scope: 'global',
tag: 'dummy-tag',
};

const populateWithScope = (
translationRecord: Record<string, string>,
): Record<string, { value: string; scope: 'global' }> =>
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 ?? {}) }],
};
};

Expand Down
Original file line number Diff line number Diff line change
@@ -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<FormsApiGlobalTranslation[]> => {
Expand Down Expand Up @@ -40,6 +41,14 @@ const createGlobalTranslationService = (formsApiUrl: string): GlobalTranslationS
headers: createHeaders(accessToken),
});
},
getPublished: async (languageCodes: Array<'nn' | 'en'>, accessToken: string): Promise<PublishedTranslations> => {
const searchParams = new URLSearchParams({ languageCodes: languageCodes.toString() });
const response = await fetchWithErrorHandling(
`${formsApiUrl}${publishedGlobalTranslationsPath}?${searchParams}`,
{ headers: createHeaders(accessToken) },
);
return response.data as PublishedTranslations;
},
};
};

Expand Down
7 changes: 6 additions & 1 deletion packages/bygger-backend/src/services/translation/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { FormsApiFormTranslation, FormsApiGlobalTranslation } from '@navikt/skjemadigitalisering-shared-domain';
import {
FormsApiFormTranslation,
FormsApiGlobalTranslation,
PublishedTranslations,
} from '@navikt/skjemadigitalisering-shared-domain';

type FormTranslationPostBody = Pick<FormsApiFormTranslation, 'key' | 'nb' | 'nn' | 'en' | 'globalTranslationId'>;
type FormTranslationPutBody = Pick<FormsApiFormTranslation, 'nb' | 'nn' | 'en' | 'globalTranslationId'>;
Expand Down Expand Up @@ -27,6 +31,7 @@ type GlobalTranslationService = {
accessToken: string,
) => Promise<FormsApiGlobalTranslation>;
publish: (accessToken: string) => Promise<void>;
getPublished: (languageCodes: Array<'nn' | 'en'>, accessCode: string) => Promise<PublishedTranslations>;
};

export type {
Expand Down
2 changes: 2 additions & 0 deletions packages/shared-domain/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ import {
FormsApiGlobalTranslation,
FormsApiTranslation,
formsApiTranslations,
PublishedTranslations,
TranslationLang,
} from './translations/FormsApiTranslation';
import currencyUtils from './utils/currencyUtils';
Expand Down Expand Up @@ -231,6 +232,7 @@ export type {
PrefillAddress,
PrefillData,
PrefillKey,
PublishedTranslations,
Recipient,
ReportDefinition,
ResourceAccess,
Expand Down
2 changes: 1 addition & 1 deletion packages/shared-domain/src/languages/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>;
export type I18nTranslationReplacements = Record<string | number, any>;
export type I18nTranslations = Record<string, I18nTranslationMap>;
Expand Down
13 changes: 12 additions & 1 deletion packages/shared-domain/src/translations/FormsApiTranslation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>; en?: Record<string, string> };
};

const isGlobalTranslation = (translation: FormsApiTranslation): translation is FormsApiGlobalTranslation =>
!!(translation as FormsApiGlobalTranslation).tag;
Expand All @@ -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,
};

0 comments on commit cb6cf53

Please sign in to comment.