UNPKG

@railzai/railz-visualizations

Version:
318 lines 13.8 kB
/*! * Accounting Data as a Service™ is the solution that makes sense of your business customers' financial data. * Built with Stencil * Copyright (c) FIS. */ import { compareAsc, parseISO } from 'date-fns'; import { isEmpty, isNil } from 'lodash-es'; import Translations from '../config/translations/en.json'; import { getOptionsBarChart } from '../elements/statements-chart/statements-chart.utils'; import { errorLog } from '../services/logger'; import { RAILZ_CHART_LABEL_COLOR, RAILZ_CHART_LEGEND_COLOR, RAILZ_TEXT_COLOR, RVReportFrequency, RVReportTypes, } from '../types'; import { isRequiredReportFrequency } from './utils'; import { checkAccessibility } from './accessibility'; /** * @function getConfiguration: if configuration is a string, convert to an object * Validate that configuration is present, if not, return formatted configuration as undefined * @param configuration: Authentication configuration */ export const getConfiguration = (configuration) => { let formattedConfiguration; if (configuration) { try { if (typeof configuration === 'string') { formattedConfiguration = JSON.parse(configuration); } else { formattedConfiguration = configuration; } if (!(formattedConfiguration === null || formattedConfiguration === void 0 ? void 0 : formattedConfiguration.token)) { errorLog(Translations.RV_TOKEN_NOT_PRESENT); formattedConfiguration = undefined; } } catch (error) { errorLog(Translations.RV_ERROR_PARSING_CONFIGURATION, error); } } else { errorLog(Translations.RV_CONFIGURATION_NOT_PRESENT); } return formattedConfiguration; }; /** * @function getFilter: if filter is a string, convert to an object * @param filter: Filter query */ export const getFilter = (filter) => { let formattedFilter; if (filter) { try { if (typeof filter === 'string') { formattedFilter = JSON.parse(filter); } else { formattedFilter = filter; } } catch (error) { errorLog(Translations.RV_ERROR_PARSING_CONFIGURATION + ' ' + JSON.stringify(error)); } } else { errorLog(Translations.RV_FILTER_NOT_PRESENT); } return formattedFilter; }; export const validateRequiredParams = (filter) => { return (validateBusinessServiceNameParams(filter) && validateDateParams(filter) && validateReportFrequencyParams(filter) && // validateIndustryCodeAndRegionParams(filter) && // validateAccountingMethodParams(filter) && validateReportTypeParams(filter)); }; export const validateIndustryCodeAndRegionParams = (filter) => { const hasConnectionId = !isEmpty(filter === null || filter === void 0 ? void 0 : filter.connectionUuid); if (!hasConnectionId) { errorLog(Translations.RV_ERROR_INVALID_BUSINESS_IDENTIFICATION); return false; } if (isNil(filter.industryCode)) { errorLog(Translations.RV_TAX_BENCHMARKING_REQUIRED_FIELDS); return false; } if (!isNil(filter.region) && isEmpty(filter.region)) { delete filter.region; } return true; }; export const validateBusinessServiceNameParams = (filter) => { const filterPassed = filter; const hasConnectionId = !isEmpty(filterPassed === null || filterPassed === void 0 ? void 0 : filterPassed.connectionUuid); if (!hasConnectionId) { errorLog(Translations.RV_ERROR_INVALID_BUSINESS_IDENTIFICATION); return false; } return true; }; export const validateDateParams = (filter) => { const { startDate, endDate, reportType } = filter; if ([RVReportTypes.BANK_ACCOUNT, RVReportTypes.CREDIT_SCORE].includes(reportType)) { return true; } if (startDate && endDate) { const compare = compareAsc(parseISO(startDate), parseISO(endDate)); if (compare >= 0) { errorLog(Translations.RV_END_DATE_BEFORE_START_DATE); return false; } } else { errorLog(Translations.RV_DATE_REQUIRED); return false; } return true; }; export const validateReportFrequencyParams = (filter) => { if (isRequiredReportFrequency(filter === null || filter === void 0 ? void 0 : filter.reportType) && !Object.values(RVReportFrequency).includes(filter === null || filter === void 0 ? void 0 : filter.reportFrequency)) { errorLog(Translations.RV_ERROR_INVALID_REPORT_FREQUENCY); return false; } return true; }; // export const validateAccountingMethodParams = (filter: RVFilterAll): boolean => { // if ( // isRequiredAccountingMethod(filter?.reportType) && // !Object.values(RVAccountingMethod).includes(filter?.accountingMethod) // ) { // errorLog(Translations.RV_ERROR_INVALID_ACCOUNTING_METHOD); // return false; // } // return true; // }; export const validateReportTypeParams = (filter) => { if (!Object.values(RVReportTypes).includes(filter.reportType)) { errorLog(Translations.RV_ERROR_INVALID_REPORT_TYPE); return false; } return true; }; /** * @function checkAccessibilityFromOptions: * Check if options passed are accessibility friendly * @param options: Whitelabeling options */ export const checkAccessibilityFromOptions = (options) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14; try { if (((_b = (_a = options === null || options === void 0 ? void 0 : options.container) === null || _a === void 0 ? void 0 : _a.style) === null || _b === void 0 ? void 0 : _b.backgroundColor) || ((_d = (_c = options === null || options === void 0 ? void 0 : options.container) === null || _c === void 0 ? void 0 : _c.style) === null || _d === void 0 ? void 0 : _d.background)) { if ((_f = (_e = options === null || options === void 0 ? void 0 : options.title) === null || _e === void 0 ? void 0 : _e.style) === null || _f === void 0 ? void 0 : _f.color) { checkAccessibility(((_h = (_g = options === null || options === void 0 ? void 0 : options.container) === null || _g === void 0 ? void 0 : _g.style) === null || _h === void 0 ? void 0 : _h.backgroundColor) || ((_k = (_j = options === null || options === void 0 ? void 0 : options.container) === null || _j === void 0 ? void 0 : _j.style) === null || _k === void 0 ? void 0 : _k.background), ((_m = (_l = options === null || options === void 0 ? void 0 : options.title) === null || _l === void 0 ? void 0 : _l.style) === null || _m === void 0 ? void 0 : _m.color) || RAILZ_TEXT_COLOR, Translations.RV_ACCESSIBILITY_CONTAINER_TITLE); } checkAccessibility(((_p = (_o = options === null || options === void 0 ? void 0 : options.container) === null || _o === void 0 ? void 0 : _o.style) === null || _p === void 0 ? void 0 : _p.backgroundColor) || ((_r = (_q = options === null || options === void 0 ? void 0 : options.container) === null || _q === void 0 ? void 0 : _q.style) === null || _r === void 0 ? void 0 : _r.background), ((_t = (_s = options === null || options === void 0 ? void 0 : options.loadingIndicator) === null || _s === void 0 ? void 0 : _s.textStyle) === null || _t === void 0 ? void 0 : _t.color) || RAILZ_CHART_LABEL_COLOR, Translations.RV_ACCESSIBILITY_CHART_LOADING_INDICATOR); checkAccessibility(((_v = (_u = options === null || options === void 0 ? void 0 : options.container) === null || _u === void 0 ? void 0 : _u.style) === null || _v === void 0 ? void 0 : _v.backgroundColor) || ((_x = (_w = options === null || options === void 0 ? void 0 : options.container) === null || _w === void 0 ? void 0 : _w.style) === null || _x === void 0 ? void 0 : _x.background), ((_z = (_y = options === null || options === void 0 ? void 0 : options.errorIndicator) === null || _y === void 0 ? void 0 : _y.textStyle) === null || _z === void 0 ? void 0 : _z.color) || RAILZ_CHART_LABEL_COLOR, Translations.RV_ACCESSIBILITY_CHART_ERROR_INDICATOR); } if ((_0 = options === null || options === void 0 ? void 0 : options.chart) === null || _0 === void 0 ? void 0 : _0.backgroundColor) { if ((_3 = (_2 = (_1 = options === null || options === void 0 ? void 0 : options.chart) === null || _1 === void 0 ? void 0 : _1.label) === null || _2 === void 0 ? void 0 : _2.style) === null || _3 === void 0 ? void 0 : _3.color) { checkAccessibility((_4 = options === null || options === void 0 ? void 0 : options.chart) === null || _4 === void 0 ? void 0 : _4.backgroundColor, ((_7 = (_6 = (_5 = options === null || options === void 0 ? void 0 : options.chart) === null || _5 === void 0 ? void 0 : _5.label) === null || _6 === void 0 ? void 0 : _6.style) === null || _7 === void 0 ? void 0 : _7.color) || RAILZ_CHART_LABEL_COLOR, Translations.RV_ACCESSIBILITY_CHART_LABEL); } if ((_10 = (_9 = (_8 = options === null || options === void 0 ? void 0 : options.chart) === null || _8 === void 0 ? void 0 : _8.legend) === null || _9 === void 0 ? void 0 : _9.style) === null || _10 === void 0 ? void 0 : _10.color) { checkAccessibility((_11 = options === null || options === void 0 ? void 0 : options.chart) === null || _11 === void 0 ? void 0 : _11.backgroundColor, ((_14 = (_13 = (_12 = options === null || options === void 0 ? void 0 : options.chart) === null || _12 === void 0 ? void 0 : _12.legend) === null || _13 === void 0 ? void 0 : _13.style) === null || _14 === void 0 ? void 0 : _14.color) || RAILZ_CHART_LEGEND_COLOR, Translations.RV_ACCESSIBILITY_CHART_LEGEND); } } } catch (e) { errorLog(Translations.RV_ACCESSIBILITY_CHECK_FAILED, e); } }; /** * getOptions: if option is a string, convert to an object * and check if options are accessible * @param options: Whitelabeling options * @param filter: Filter query */ export const getOptions = (options) => { let formattedOptions; try { if (options) { try { if (typeof options === 'string') { formattedOptions = JSON.parse(options); } else { formattedOptions = options; } } catch (error) { errorLog(Translations.RV_ERROR_PARSING_OPTIONS + ' ' + JSON.stringify(error)); } } checkAccessibilityFromOptions(formattedOptions); } catch (error) { errorLog(Translations.RV_ERROR_PARSING_OPTIONS + ' ' + JSON.stringify(error)); } return formattedOptions; }; /** * getData: if data is a string, convert to an object */ export const getData = (options) => { let formattedData; try { if (options) { try { if (typeof options === 'string') { formattedData = JSON.parse(options); } else { formattedData = options; } } catch (error) { errorLog(Translations.RV_ERROR_PARSING_DATA + ' ' + JSON.stringify(error)); } } } catch (error) { errorLog(Translations.RV_ERROR_PARSING_DATA + ' ' + JSON.stringify(error)); } return formattedData; }; /** * getBarOptionsStyle: if option is a string, convert to an object * this is used if railz-progress-bar is used directly * @param options: Whitelabeling options */ export const getBarOptionsStyle = (options) => { let formattedOptionsStyle; if (options) { try { if (typeof options === 'string') { formattedOptionsStyle = JSON.parse(options); } else { formattedOptionsStyle = options; } } catch (error) { errorLog(Translations.RV_ERROR_PARSING_CONFIGURATION + ' ' + JSON.stringify(error)); } } else { formattedOptionsStyle = {}; } return formattedOptionsStyle; }; /** * getBankReconciliationOptionsStyle: if option is a string, convert to an object * this is used if railz-bank-reconciliation is used directly * @param options: Whitelabeling options */ export const getBankReconciliationOptionsStyle = (options) => { let formattedOptionsStyle; if (options) { try { if (typeof options === 'string') { formattedOptionsStyle = JSON.parse(options); } else { formattedOptionsStyle = options; } } catch (error) { errorLog(Translations.RV_ERROR_PARSING_CONFIGURATION + ' ' + JSON.stringify(error)); } } else { formattedOptionsStyle = {}; } return formattedOptionsStyle; }; // /** // * @function getContent: if configuration is a string, convert to an object // * Validate that configuration is present, if not, return formatted configuration as undefined // * @param content: Content text/info // * @param filter: To retrieve reportType // */ // export const getContent = ( // content: RVContent | string, // filter?: RVAllFilter, // ): RVContent | never => { // let formattedContent: RVContent; // if (content) { // try { // if (typeof content === 'string') { // formattedContent = JSON.parse(content); // } else { // formattedContent = content; // } // } catch (error) { // errorLog(Translations.RV_ERROR_PARSING_CONTENT + ' ' + error.message); // } // } // if (filter) { // formattedContent.title = formattedContent.title || getTitleByReportType(filter.reportType); // } // return formattedContent; // }; /** * getHighchartsParams: Combine generic stacked bar line chart * options and formatted data based on a given report type * into one option for highcharts usage * @param dataFormatted: formatted highchart data based on report type * @param options: Whitelabeling options */ export const getHighchartsParams = ({ dataFormatted, options, }) => { let containerOptions; try { containerOptions = getOptionsBarChart(Object.assign(Object.assign({}, dataFormatted), { chart: options === null || options === void 0 ? void 0 : options.chart })); } catch (error) { errorLog(Translations.RV_NOT_ABLE_TO_PARSE_REPORT_DATA, error); } return containerOptions; }; //# sourceMappingURL=chart.utils.js.map