From cea968f4f0b11876f44bbf15776efeb500b3a72d Mon Sep 17 00:00:00 2001 From: VWSCoronaDashboard29 Date: Mon, 18 Dec 2023 17:03:39 +0100 Subject: [PATCH] fix(COR-1865): Throw error if pageMetric not found in data --- .../utils/get-last-insertion-date-of-page.ts | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/packages/app/src/utils/get-last-insertion-date-of-page.ts b/packages/app/src/utils/get-last-insertion-date-of-page.ts index 126952d102..f381205c98 100644 --- a/packages/app/src/utils/get-last-insertion-date-of-page.ts +++ b/packages/app/src/utils/get-last-insertion-date-of-page.ts @@ -6,8 +6,7 @@ function hasLastValue(metric: any): boolean { } function hasValues(metric: any): boolean { - return Array.isArray(metric?.values) && - typeof metric?.values[metric.values.length - 1]?.date_of_insertion_unix !== 'undefined'; + return Array.isArray(metric?.values) && typeof metric?.values[metric.values.length - 1]?.date_of_insertion_unix !== 'undefined'; } function hasInsertionDate(metric: any): boolean { @@ -15,8 +14,7 @@ function hasInsertionDate(metric: any): boolean { } function hasNestedLastValue(metric: any): boolean { - return Array.isArray(metric?.values) && - typeof metric?.values[0]?.last_value?.date_of_insertion_unix !== 'undefined'; + return Array.isArray(metric?.values) && typeof metric?.values[0]?.last_value?.date_of_insertion_unix !== 'undefined'; } // functions for getting values @@ -33,7 +31,7 @@ function getDateFromInsertionDate(metric: any): number { } function getDateFromNestedLastValue(metric: any): number { - return metric?.values.reduce((lastDate :number, innerValue: any) => { + return metric?.values.reduce((lastDate: number, innerValue: any) => { const metricDate = getMetricDate(innerValue); return Math.max(metricDate, lastDate); }, 0); @@ -55,14 +53,21 @@ function getMetricDate(metricOrUnixDate: any): number { return 0; } -export function getLastInsertionDateOfPage( - data: unknown, - pageMetrics: string[] -) { - return pageMetrics.reduce((lastDate, metricProperty) => { +export function getLastInsertionDateOfPage(data: unknown, pageMetrics: string[]) { + const foundMetrics: string[] = pageMetrics.filter((metricProperty) => { + if (get(data, metricProperty) !== undefined) { + return metricProperty; + } + }); + + if (foundMetrics?.length === 0) { + throw new Error(`Pagemetrics not found in data`); + } + + return foundMetrics.reduce((lastDate, metricProperty) => { const metric: any = get(data, metricProperty); const metricDate = getMetricDate(metric); - + return Math.max(metricDate, lastDate); }, 0); -}; +}