Skip to content

Commit

Permalink
refactor: remove translations parameter from string replacements (#1153)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaschlote authored Sep 3, 2024
1 parent 9c2bdd6 commit 6020225
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 195 deletions.
6 changes: 1 addition & 5 deletions app/flows/flows.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { geldEinklagenVorabcheck } from "app/flows/geldEinklagenVorabcheck";
import { beratungshilfeFormular } from "~/flows/beratungshilfeFormular";
import { fluggastrechtFlow } from "~/flows/fluggastrechteFormular";
import { geldEinklagenFormular } from "~/flows/geldEinklagenFormular";
import type { Translations } from "~/services/cms/index.server";
import type { FlowPageId } from "~/services/cms/schemas";
import type { Config } from "~/services/flow/server/buildFlowController";
import type { FlowTransitionConfig } from "~/services/session.server/flowTransitionValidation.server";
Expand All @@ -20,10 +19,7 @@ export type Flow = {
guards: Guards;
migrationSource?: FlowId;
flowTransitionConfig?: FlowTransitionConfig;
stringReplacements?: (
context: Context,
translations: Translations,
) => Replacements;
stringReplacements?: (context: Context) => Replacements;
};

export const flows = {
Expand Down
107 changes: 0 additions & 107 deletions app/flows/fluggastrechteVorabcheck/__test__/stringReplacaments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ import {
COMPENSATION_VALUE_600,
COMPENSATION_VALUE_400,
COMPENSATION_VALUE_250,
TRANSLATION_ROUTE_COMPENSATION_DESCRIPTION_ABOVE_3500_KM_INSIDE_EU,
TRANSLATION_ROUTE_COMPENSATION_DESCRIPTION_ABOVE_3500_KM_OUTSIDE_EU,
TRANSLATION_ROUTE_COMPENSATION_DESCRIPTION_UNTIL_1500_KM,
TRANSLATION_ROUTE_COMPENSATION_DESCRIPTION_UNTIL_3500_KM,
getCompensantionPaymentString,
getEndAirportName,
getLastDaytFromFourYearsAgoDate,
getRouteCompensationDescription,
getStartAirportName,
hasArbitrationBoardBfJ,
hasArbitrationBoardSoeP,
Expand All @@ -26,17 +21,6 @@ const mockedGetRouteCompensationBetweenAirports = vi.mocked(
getRouteCompensationBetweenAirports,
);

const EXPECTED_TRANSLATION_KEY_RECORDS = {
[TRANSLATION_ROUTE_COMPENSATION_DESCRIPTION_UNTIL_1500_KM]:
"Kurzstrecke (unter 1.500 km)",
[TRANSLATION_ROUTE_COMPENSATION_DESCRIPTION_UNTIL_3500_KM]:
"Mittelstrecke (zwischen 1.500 und 3.000 km)",
[TRANSLATION_ROUTE_COMPENSATION_DESCRIPTION_ABOVE_3500_KM_INSIDE_EU]:
"Langstrecke (über 3.500 km) innerhalb der EU",
[TRANSLATION_ROUTE_COMPENSATION_DESCRIPTION_ABOVE_3500_KM_OUTSIDE_EU]:
"Langstrecke (über 3.500 km), bei der ein Flughafen außerhalb der EU liegt",
};

beforeEach(() => {
vi.clearAllMocks();
});
Expand Down Expand Up @@ -151,97 +135,6 @@ describe("getEndAirportName", () => {
});
});

describe("getRouteCompensationDescription", () => {
it("should return route compensation description until 1500, if the distance is short", () => {
mockedGetRouteCompensationBetweenAirports.mockReturnValue("shortDistance");

const expectedTranslation =
EXPECTED_TRANSLATION_KEY_RECORDS[
TRANSLATION_ROUTE_COMPENSATION_DESCRIPTION_UNTIL_1500_KM
];

const actual = getRouteCompensationDescription(
{},
EXPECTED_TRANSLATION_KEY_RECORDS,
);

expect(actual).toStrictEqual({
routeCompensationDescription: expectedTranslation,
});
});

it("should return route compensation description until 3500, if the distance is middle", () => {
mockedGetRouteCompensationBetweenAirports.mockReturnValue("middleDistance");

const expectedTranslation =
EXPECTED_TRANSLATION_KEY_RECORDS[
TRANSLATION_ROUTE_COMPENSATION_DESCRIPTION_UNTIL_3500_KM
];

const actual = getRouteCompensationDescription(
{},
EXPECTED_TRANSLATION_KEY_RECORDS,
);

expect(actual).toStrictEqual({
routeCompensationDescription: expectedTranslation,
});
});

it("should return return route compensation description above 3500 inside EU, if the distance is above 3500 inside EU", () => {
mockedGetRouteCompensationBetweenAirports.mockReturnValue(
"longDistanceInsideEU",
);

const expectedTranslation =
EXPECTED_TRANSLATION_KEY_RECORDS[
TRANSLATION_ROUTE_COMPENSATION_DESCRIPTION_ABOVE_3500_KM_INSIDE_EU
];

const actual = getRouteCompensationDescription(
{},
EXPECTED_TRANSLATION_KEY_RECORDS,
);

expect(actual).toStrictEqual({
routeCompensationDescription: expectedTranslation,
});
});

it("should return return route compensation description above 3500 outside EU, if the distance is above 3500 outside EU", () => {
mockedGetRouteCompensationBetweenAirports.mockReturnValue(
"longDistanceOutsideEU",
);

const expectedTranslation =
EXPECTED_TRANSLATION_KEY_RECORDS[
TRANSLATION_ROUTE_COMPENSATION_DESCRIPTION_ABOVE_3500_KM_OUTSIDE_EU
];

const actual = getRouteCompensationDescription(
{},
EXPECTED_TRANSLATION_KEY_RECORDS,
);

expect(actual).toStrictEqual({
routeCompensationDescription: expectedTranslation,
});
});

it("should return empty object, if the distance can not be calculated", () => {
mockedGetRouteCompensationBetweenAirports.mockReturnValue(
"notPossibleCalculateDistance",
);

const actual = getRouteCompensationDescription(
{},
EXPECTED_TRANSLATION_KEY_RECORDS,
);

expect(actual).toStrictEqual({});
});
});

describe("hasArbitrationBoardBfJ", () => {
it("should return hasArbitrationBoardBfJ object as true, in case fluggesellschaft is undefined", () => {
const actual = hasArbitrationBoardBfJ({});
Expand Down
8 changes: 1 addition & 7 deletions app/flows/fluggastrechteVorabcheck/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import type { Flow } from "~/flows/flows.server";
import fluggastrechteVorabcheckFlow from "~/flows/fluggastrechteVorabcheck/flow.json";
import { guards as fluggastrechteVorabcheckGuards } from "~/flows/fluggastrechteVorabcheck/guards";
import type { Translations } from "~/services/cms/index.server";
import type { FluggastrechtVorabcheckContext } from "./context";
import {
getCompensantionPaymentString,
getEndAirportName,
getLastDaytFromFourYearsAgoDate,
getRouteCompensationDescription,
getStartAirportName,
hasArbitrationBoardBfJ,
hasArbitrationBoardSoeP,
Expand All @@ -22,15 +20,11 @@ export const fluggastrechteVorabcheck = {
cmsSlug: "vorab-check-pages",
config: fluggastrechteVorabcheckFlow,
guards: fluggastrechteVorabcheckGuards,
stringReplacements: (
context: FluggastrechtVorabcheckContext,
translations: Translations,
) => ({
stringReplacements: (context: FluggastrechtVorabcheckContext) => ({
...getCompensantionPaymentString(context),
flightDateExpiration: getLastDaytFromFourYearsAgoDate(),
...getStartAirportName(context),
...getEndAirportName(context),
...getRouteCompensationDescription(context, translations),
...hasArbitrationBoardBfJ(context),
...hasArbitrationBoardSoeP(context),
...hasCompensationLongDistanceInsideEU(context),
Expand Down
50 changes: 0 additions & 50 deletions app/flows/fluggastrechteVorabcheck/stringReplacements.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import airlines from "data/airlines/data.json";
import { getAirportNameByIataCode } from "~/services/airports/getAirportNameByIataCode";
import { getRouteCompensationBetweenAirports } from "~/services/airports/getRouteCompensationBetweenAirports";
import type { Translations } from "~/services/cms/index.server";
import { toGermanDateFormat, today } from "~/util/date";
import { getTranslationByKey } from "~/util/getTranslationByKey";
import type { FluggastrechtVorabcheckContext } from "./context";

export const COMPENSATION_VALUE_250 = "250";
Expand All @@ -15,15 +13,6 @@ const LAST_MONTH_YEAR = 11; // Date.setMonth starts from 0 to 11, where 11 is De
const ARBITRATION_BOARD_BFJ = "BfJ";
const ARBITRATION_BOARD_SOEP = "söp";

export const TRANSLATION_ROUTE_COMPENSATION_DESCRIPTION_UNTIL_1500_KM =
"route-compensation-description-until-1500-km";
export const TRANSLATION_ROUTE_COMPENSATION_DESCRIPTION_UNTIL_3500_KM =
"route-compensation-description-until-3500-km";
export const TRANSLATION_ROUTE_COMPENSATION_DESCRIPTION_ABOVE_3500_KM_INSIDE_EU =
"route-compensation-description-above-3500-km-inside-eu";
export const TRANSLATION_ROUTE_COMPENSATION_DESCRIPTION_ABOVE_3500_KM_OUTSIDE_EU =
"route-compensation-description-above-3500-km-outside-eu";

export function getCompensantionPaymentString({
startAirport = "",
endAirport = "",
Expand Down Expand Up @@ -130,45 +119,6 @@ export function hasCompensationShortDistance({
return { hasShortDistance: routeCompensation === "shortDistance" };
}

export function getRouteCompensationDescription(
{ startAirport = "", endAirport = "" }: FluggastrechtVorabcheckContext,
translations: Translations,
) {
const routeCompensation = getRouteCompensationBetweenAirports(
startAirport,
endAirport,
);

let translationKey = "";

switch (routeCompensation) {
case "longDistanceInsideEU":
translationKey =
TRANSLATION_ROUTE_COMPENSATION_DESCRIPTION_ABOVE_3500_KM_INSIDE_EU;
break;
case "longDistanceOutsideEU":
translationKey =
TRANSLATION_ROUTE_COMPENSATION_DESCRIPTION_ABOVE_3500_KM_OUTSIDE_EU;
break;
case "middleDistance":
translationKey = TRANSLATION_ROUTE_COMPENSATION_DESCRIPTION_UNTIL_3500_KM;
break;
case "shortDistance":
translationKey = TRANSLATION_ROUTE_COMPENSATION_DESCRIPTION_UNTIL_1500_KM;
break;
default: {
return {};
}
}

return {
routeCompensationDescription: getTranslationByKey(
translationKey,
translations,
),
};
}

function getAirlineByIataCode(iataCode?: string) {
if (typeof iataCode === "undefined" || iataCode.length === 0) {
return undefined;
Expand Down
4 changes: 2 additions & 2 deletions app/routes/shared/formular.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function getInterpolateFlowTranslations(
*/
return interpolateDeep(
flowTranslations,
currentFlow.stringReplacements(migrationData, flowTranslations),
currentFlow.stringReplacements(migrationData),
);
}

Expand Down Expand Up @@ -165,7 +165,7 @@ export const loader = async ({
const cmsContent = interpolateDeep(
structureCmsContent(formPageContent),
"stringReplacements" in currentFlow
? currentFlow.stringReplacements(userDataWithPageData, stringTranslations)
? currentFlow.stringReplacements(userDataWithPageData)
: {},
);

Expand Down
26 changes: 10 additions & 16 deletions app/routes/shared/result.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,20 @@ export const loader = async ({ request, context }: LoaderFunctionArgs) => {
await sessionManager.commitSession(session);
}

const [
resultPageContent,
parentMeta,
amtsgerichtCommon,
defaultStrings,
flowTranslations,
] = await Promise.all([
fetchFlowPage("result-pages", flowId, stepId.replace("ergebnis/", "")),
fetchMeta({
filterValue: pathname.substring(0, pathname.lastIndexOf("/")),
}),
fetchTranslations("amtsgericht"),
fetchTranslations("defaultTranslations"),
fetchTranslations(flowId),
]);
const [resultPageContent, parentMeta, amtsgerichtCommon, defaultStrings] =
await Promise.all([
fetchFlowPage("result-pages", flowId, stepId.replace("ergebnis/", "")),
fetchMeta({
filterValue: pathname.substring(0, pathname.lastIndexOf("/")),
}),
fetchTranslations("amtsgericht"),
fetchTranslations("defaultTranslations"),
]);

const cmsContent = interpolateDeep(
resultPageContent,
"stringReplacements" in currentFlow
? currentFlow.stringReplacements(userData, flowTranslations)
? currentFlow.stringReplacements(userData)
: {},
);

Expand Down
14 changes: 6 additions & 8 deletions app/routes/shared/vorabcheck.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,17 @@ export const loader = async ({
if (!flowController.isReachable(stepId))
return redirectDocument(flowController.getInitial());

const [vorabcheckPage, parentMeta, translations, flowTranslations] =
await Promise.all([
fetchFlowPage("vorab-check-pages", flowId, stepId),
fetchMeta({ filterValue: parentFromParams(pathname, params) }),
fetchTranslations("defaultTranslations"),
fetchTranslations(flowId),
]);
const [vorabcheckPage, parentMeta, translations] = await Promise.all([
fetchFlowPage("vorab-check-pages", flowId, stepId),
fetchMeta({ filterValue: parentFromParams(pathname, params) }),
fetchTranslations("defaultTranslations"),
]);

// Do string replacement in content if necessary
const contentElements = interpolateDeep(
vorabcheckPage.pre_form,
"stringReplacements" in currentFlow
? currentFlow.stringReplacements(userData, flowTranslations)
? currentFlow.stringReplacements(userData)
: undefined,
);

Expand Down

0 comments on commit 6020225

Please sign in to comment.