File

libs/sdk/src/lib/journey/config/migrations/footer-copyright/footer-copyright.ts

Extends

Omit<PagesJsonSchema, 'frame'>

Index

Properties

Properties

frame (Optional)
Type Omit<Frame & footer> & literal type
pages
Type PageConfiguration[]
Description

List of all available pages

$schema (Optional)
Type string
Description

The JSON Schema for this file

description (Optional)
Type string
Description

Application Description. Will be used for the meta description tag.

libraries
Type LibraryConfiguration[]
Default value []
Description

Provide a list of all used Building Block Libraries with the package name & version. Unused packages (i.e. not referred to in any 'Building Block Configuration') will be ignored. []

plugins (Optional)
Type PluginConfiguration[]
Default value []
Description

Configuration for plugins []

title (Optional)
Type string
Description

Application Title. Will be used as the website title. If omitted the app name is used.

webcomponent (Optional)
Type WebcomponentConfiguration
Description

Configuration for generating a webcomponent out of this application

workInProgressIndicator (Optional)
Type boolean
Default value false
Description

set to true to show WORK IN PROGRESS text on top of the application. false

import { FooterConfiguration, Frame } from '@allianz/taly-core/schemas';
import { copyComments } from '../shared/jsonc-comments';
import { ApplicationJsonSchema, PagesJsonSchema } from '../../../../model';
import {
  deletePropertyByPath,
  getBaseApplicationPropertyPath,
  getPropertyByPath,
  setPropertyByPath
} from '../shared/base-application-property-path/base-application-property-path';
import { cloneDeepWithJsonComments } from '../shared/utils';

const COPYRIGHT_KEY = 'copyright';
const COPYRIGHT_HOLDER_KEY = 'copyrightHolder';

function removeCopyRightYear(copyrightContent: string, filePath?: string) {
  // ignore copyrightContent without a year (20XX)
  if (!/\b20\d{2}\b/.test(copyrightContent)) {
    return copyrightContent;
  }

  const migratedCopyrightContent = copyrightContent
    .replace(/\b20\d{2}\b/g, '')
    .replace(/\s{2,}/g, ' ')
    .trim();
  console.warn(
    `⚠️ ${filePath}: The copyright string in your journey configuration was migrated from '${copyrightContent}' to '${migratedCopyrightContent}'. TALY will automatically add the copyright symbol and the current year to this string during runtime.
⚠️ TALY did not migrate your translation files! If you have any, please manually check them for any inconsistencies that the above copyright migration might have introduced. The i18n ID for the copyright notice didn't change.
`
  );
  return migratedCopyrightContent;
}

interface FooterConfigurationBeforeTheFooterCopyrightMigration extends FooterConfiguration {
  copyright?: string;
}

export interface PagesJsonSchemaBeforeTheFooterCopyrightMigration
  extends Omit<PagesJsonSchema, 'frame'> {
  frame?: Omit<Frame, 'footer'> & { footer?: FooterConfigurationBeforeTheFooterCopyrightMigration };
}

export function migrateFooterCopyrightInPagesJson(
  pagesConfiguration: PagesJsonSchemaBeforeTheFooterCopyrightMigration,
  filePath?: string
) {
  const copyrightContent = pagesConfiguration?.frame?.footer?.copyright;
  if (!copyrightContent) {
    return pagesConfiguration as PagesJsonSchema;
  }

  const copyrightContentWithoutYear = removeCopyRightYear(copyrightContent, filePath);

  const migratedPagesConfiguration = cloneDeepWithJsonComments(pagesConfiguration);
  // we need this check again to tell typescript the footer exists
  if (!migratedPagesConfiguration?.frame?.footer) {
    return migratedPagesConfiguration;
  }
  migratedPagesConfiguration.frame.footer.copyrightHolder = copyrightContentWithoutYear;

  migratedPagesConfiguration.frame.footer = copyComments(
    migratedPagesConfiguration.frame.footer,
    pagesConfiguration.frame?.footer,
    new Map([[COPYRIGHT_HOLDER_KEY, COPYRIGHT_KEY]])
  );

  delete migratedPagesConfiguration.frame.footer.copyright;
  return migratedPagesConfiguration as PagesJsonSchema;
}

export function migrateFooterCopyrightInApplicationJson(
  applicationJson: ApplicationJsonSchema,
  filePath?: string
) {
  const copyrightPath = getBaseApplicationPropertyPath(
    ['frame', 'footer', COPYRIGHT_KEY],
    applicationJson
  );

  if (!copyrightPath) {
    return applicationJson;
  }
  const footerIndexInPath = copyrightPath?.indexOf('footer');
  const footerPath = [...copyrightPath].slice(0, footerIndexInPath + 1);

  // add refDefault to footer path if footer uses it directly
  if (copyrightPath[footerIndexInPath + 1] === 'refDefault') {
    footerPath.push('refDefault');
  }

  const migratedApplicationJson = cloneDeepWithJsonComments(applicationJson);

  const copyrightContent = getPropertyByPath(applicationJson, copyrightPath);
  const copyrightContentWithoutYear = removeCopyRightYear(copyrightContent, filePath);

  const copyrightHolderPath = [...copyrightPath];
  copyrightHolderPath[copyrightPath.indexOf(COPYRIGHT_KEY)] = COPYRIGHT_HOLDER_KEY;
  setPropertyByPath(migratedApplicationJson, copyrightHolderPath, copyrightContentWithoutYear);

  const footerWithComment = copyComments(
    getPropertyByPath(migratedApplicationJson, footerPath),
    getPropertyByPath(applicationJson, footerPath),
    new Map([[COPYRIGHT_HOLDER_KEY, COPYRIGHT_KEY]])
  );
  setPropertyByPath(migratedApplicationJson, footerPath, footerWithComment);

  const hasCopyrightRefDefault = copyrightPath[copyrightPath.length - 1] === 'refDefault';

  // delete refDefault again from path
  if (hasCopyrightRefDefault) {
    copyrightPath.pop();
    copyrightHolderPath.pop();
  }

  if (hasCopyrightRefDefault) {
    const copyrightRefDefaultWithComments = copyComments(
      getPropertyByPath(migratedApplicationJson, copyrightHolderPath),
      getPropertyByPath(applicationJson, copyrightPath)
    );

    setPropertyByPath(
      migratedApplicationJson,
      copyrightHolderPath,
      copyrightRefDefaultWithComments
    );
  }

  deletePropertyByPath(migratedApplicationJson, copyrightPath);

  return migratedApplicationJson;
}

results matching ""

    No results matching ""