All files / src/context/yaml/handlers connections.ts

97.56% Statements 40/41
85.71% Branches 12/14
100% Functions 5/5
100% Lines 40/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 1041x 1x 1x   1x 1x                           41x 41x   41x 38x     3x     5x 2x 2x   2x 1x 1x 1x   1x     4x           1x 6x 6x                   3x         6x   6x   6x   6x               6x 1x 1x 1x 1x 1x   1x 1x 1x   1x     6x         1x         1x  
import path from 'path';
import fs from 'fs-extra';
import { constants } from '../../../tools';
 
import log from '../../../logger';
import {
  isFile,
  sanitize,
  ensureProp,
  convertClientIdToName,
  mapClientID2NameSorted,
} from '../../../utils';
import { YAMLHandler } from '.';
import YAMLContext from '..';
import { Asset, ParsedAsset } from '../../../types';
 
type ParsedConnections = ParsedAsset<'connections', Asset[]>;
 
async function parse(context: YAMLContext): Promise<ParsedConnections> {
  const { connections } = context.assets;
  const connectionsFolder = path.join(context.basePath, constants.CONNECTIONS_DIRECTORY);
 
  if (!connections) {
    return { connections: null };
  }
 
  return {
    connections: [
      ...connections.map((connection) => {
        if (connection.strategy === 'email') {
          ensureProp(connection, 'options.email.body');
          const htmlFileName = path.join(connectionsFolder, connection.options.email.body);
 
          if (!isFile(htmlFileName)) {
            const missingTemplateErrorMessage = `Passwordless email template purportedly located at ${htmlFileName} does not exist for connection. Ensure the existence of this file to proceed with deployment.`;
            log.error(missingTemplateErrorMessage);
            throw new Error(missingTemplateErrorMessage);
          }
          connection.options.email.body = context.loadFile(htmlFileName);
        }
 
        return connection;
      }),
    ],
  };
}
 
const getFormattedOptions = (connection, clients) => {
  try {
    return {
      options: {
        ...connection.options,
        idpinitiated: {
          ...connection.options.idpinitiated,
          client_id: convertClientIdToName(connection.options.idpinitiated.client_id, clients),
        },
      },
    };
  } catch (e) {
    return {};
  }
};
 
async function dump(context: YAMLContext): Promise<ParsedConnections> {
  const { connections, clients } = context.assets;
 
  Iif (!connections) return { connections: null };
 
  return {
    connections: connections.map((connection) => {
      const dumpedConnection = {
        ...connection,
        ...getFormattedOptions(connection, clients),
        ...(connection.enabled_clients && {
          enabled_clients: mapClientID2NameSorted(connection.enabled_clients, clients || []),
        }),
      };
 
      if (dumpedConnection.strategy === 'email') {
        ensureProp(connection, 'options.email.body');
        const connectionsFolder = path.join(context.basePath, constants.CONNECTIONS_DIRECTORY);
        const connectionName = sanitize(dumpedConnection.name);
        const html = dumpedConnection.options.email.body;
        const emailConnectionHtml = path.join(connectionsFolder, `${connectionName}.html`);
 
        log.info(`Writing ${emailConnectionHtml}`);
        fs.ensureDirSync(connectionsFolder);
        fs.writeFileSync(emailConnectionHtml, html);
 
        dumpedConnection.options.email.body = `./${connectionName}.html`;
      }
 
      return dumpedConnection;
    }),
  };
}
 
const connectionsHandler: YAMLHandler<ParsedConnections> = {
  parse,
  dump,
};
 
export default connectionsHandler;