import chalk from 'chalk'; import { HttpMethod, getNewApiHelper } from '../helpers/root-api'; import { clone } from './clone'; import { ROOT_API_DEFAULT_HOST } from '../helpers/constants'; import { runWithSpinner } from '../helpers/spinner'; import { symbols } from '../helpers/symbols'; export const create = async (params: { apiKey: string; productModuleName: string; productModuleKey: string; options: { host?: string; }; }) => { const { apiKey, productModuleName, productModuleKey } = params; const hostPath = params.options.host || ROOT_API_DEFAULT_HOST; const rootApi = getNewApiHelper({ host: hostPath, apiKey, throwResponseErrors: true }); const templateProductModules = await runWithSpinner('Fetching product module templates...', () => rootApi.send({ path: `/template-product-modules` }), ); const findBlankStarterTemplate = await templateProductModules.find( (element: Record) => element.key === 'template_blank_starter', ); if (!findBlankStarterTemplate) { throw new Error('Could not find template_blank_starter. Please contact us on support@rootplatform.com'); } const body = { clone_from_product_module_id: findBlankStarterTemplate.product_module_id, name: productModuleName, key: productModuleKey, }; await runWithSpinner('Creating product module on Root...', () => rootApi.send({ method: HttpMethod.Post, path: `/insurance/product-modules`, body }), ); console.log( symbols.success + chalk.green(`Product module '${productModuleKey}' successfully created on the Root Platform`), ); await clone({ apiKey, moduleKey: productModuleKey, options: { live: false, host: hostPath, }, }); };