File

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

Extends

Omit

Index

Properties

Properties

frame (Optional)
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(/\s*\b20\d{2}\b\s*/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 ""