From 24d8fc41319279a9058502d5c6f6f24cb53e2434 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mir=C3=B3=20Sorja?= Date: Thu, 3 Oct 2024 13:01:00 +0200 Subject: [PATCH] 3992 - Bulk download: getNDPYear (#3993) --- src/i18n/resources/en/bulkDownload.js | 4 + .../cycleData/getBulkDownload/getNDPYear.ts | 93 +++++++++++++++++++ .../cycleData/getBulkDownload/index.ts | 8 +- 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 src/server/controller/cycleData/getBulkDownload/getNDPYear.ts diff --git a/src/i18n/resources/en/bulkDownload.js b/src/i18n/resources/en/bulkDownload.js index fa72d5c6aa..3777c5ba1e 100644 --- a/src/i18n/resources/en/bulkDownload.js +++ b/src/i18n/resources/en/bulkDownload.js @@ -1,3 +1,7 @@ module.exports = { dateOfExport: 'Date of export', + + NDPYear: { + year: 'Latest NDP year', + }, } diff --git a/src/server/controller/cycleData/getBulkDownload/getNDPYear.ts b/src/server/controller/cycleData/getBulkDownload/getNDPYear.ts new file mode 100644 index 0000000000..24c57477d6 --- /dev/null +++ b/src/server/controller/cycleData/getBulkDownload/getNDPYear.ts @@ -0,0 +1,93 @@ +import { createI18nPromise } from 'i18n/i18nFactory' +import { TFunction } from 'i18next' +import { Promises } from 'utils/promises' + +import { CountryIso } from 'meta/area' +import { Assessment, Cycle } from 'meta/assessment' +import { ODPDataSourceMethod } from 'meta/assessment/originalDataPoint' +import { RecordAssessmentDatas } from 'meta/data' + +import { OriginalDataPointRepository } from 'server/repository/assessmentCycle/originalDataPoint' + +import { climaticDomain } from './climaticDomain' +import { getClimaticValue } from './getClimaticValue' +import { getData } from './getData' +import { Props } from './props' + +const METHODS = Object.values(ODPDataSourceMethod) + +const getMethodKey = (method: ODPDataSourceMethod, t: TFunction) => + method === ODPDataSourceMethod.other ? t('common.other') : t(`nationalDataPoint.dataSourceMethodsOptions.${method}`) + +const getValue = async (props: { + base: Record + assessment: Assessment + cycle: Cycle + countryIso: CountryIso + t: TFunction +}): Promise> => { + const { base, assessment, cycle, countryIso, t } = props + const years = await OriginalDataPointRepository.getReservedYears({ assessment, cycle, countryIso }) + + const yearKey = t('bulkDownload.NDPYear.year') + const year = years.length ? Math.max(...years.map(({ year }) => year)) : null + base[yearKey] = year ? String(year) : null + + const originalDataPoint = year + ? await OriginalDataPointRepository.getOne({ assessment, cycle, countryIso, year: String(year) }) + : null + + METHODS.forEach((method) => { + const key = getMethodKey(method, t) + base[key] = originalDataPoint + ? t(`yesNoTextSelect.${originalDataPoint.dataSourceMethods?.includes(method) ? 'yes' : 'no'}`) + : null + }) + + return base +} + +export const getNDPYear = async (props: Props) => { + const { assessment, cycle, countries } = props + const { t } = await createI18nPromise('en') + + const climaticData = RecordAssessmentDatas.getCycleData({ + assessmentName: assessment.props.name, + cycleName: cycle.name, + data: await climaticDomain(props), + }) + + const tableData = await getData({ + assessment, + cycle, + countries, + tableNames: ['extentOfForest'], + }) + const result: Array> = [] + + await Promises.each(countries, async ({ countryIso, regionCodes }) => { + const base: Record = { + regions: regionCodes.join(';'), + iso3: countryIso, + name: countryIso, + boreal: getClimaticValue('boreal', countryIso, climaticData), + temperate: getClimaticValue('temperate', countryIso, climaticData), + tropical: getClimaticValue('tropical', countryIso, climaticData), + subtropical: getClimaticValue('sub_tropical', countryIso, climaticData), + [`forest area ${cycle.name}`]: RecordAssessmentDatas.getDatum({ + assessmentName: assessment.props.name, + cycleName: cycle.name, + data: tableData, + countryIso, + tableName: 'extentOfForest', + variableName: 'forestArea', + colName: cycle.name, + }), + } + + const value = await getValue({ base, assessment, cycle, countryIso, t }) + result.push(value) + }) + + return result +} diff --git a/src/server/controller/cycleData/getBulkDownload/index.ts b/src/server/controller/cycleData/getBulkDownload/index.ts index 07a500efa2..8523b9972e 100644 --- a/src/server/controller/cycleData/getBulkDownload/index.ts +++ b/src/server/controller/cycleData/getBulkDownload/index.ts @@ -18,6 +18,7 @@ import { RegionRepository } from 'server/repository/assessmentCycle/region' import { entries as annualEntries } from './entries/AnnualData' import { entries as FRAEntries } from './entries/FRAYears' import { entries as intervalEntries } from './entries/Intervals' +import { getNDPYear } from './getNDPYear' const _convertToCSV = (arr: Array>): string => { if (!arr.length) return '' @@ -120,12 +121,13 @@ export const getBulkDownload = async (props: { assessment: Assessment; cycle: Cy getContentVariables({ ...params, fileName: 'FRA_Years', entries: FRAEntries(cycle) }), ]) - const [annual, intervals, fraYears, nwfp, forestPolicy] = await Promise.all([ + const [annual, intervals, fraYears, nwfp, forestPolicy, ndpYear] = await Promise.all([ getContent({ ...params, entries: annualEntries }), getContent({ ...params, entries: intervalEntries(cycle), intervals: true }), getFraYearsData(params), getNWFP(params), getForestPolicy(params), + getNDPYear(params), ]) const promises = [ @@ -168,6 +170,10 @@ export const getBulkDownload = async (props: { assessment: Assessment; cycle: Cy fileName: _getFileName('ForestPolicy'), content: await handleContent(forestPolicy), }, + { + fileName: _getFileName('NDPYear'), + content: await handleContent(ndpYear), + }, ] // Tier data only available for 2025