Skip to content

Commit

Permalink
3992 - Bulk download: getNDPYear (#3993)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorja authored Oct 3, 2024
1 parent 7e7deb0 commit 24d8fc4
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/i18n/resources/en/bulkDownload.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module.exports = {
dateOfExport: 'Date of export',

NDPYear: {
year: 'Latest NDP year',
},
}
93 changes: 93 additions & 0 deletions src/server/controller/cycleData/getBulkDownload/getNDPYear.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>
assessment: Assessment
cycle: Cycle
countryIso: CountryIso
t: TFunction
}): Promise<Record<string, string>> => {
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<Record<string, string>> = []

await Promises.each(countries, async ({ countryIso, regionCodes }) => {
const base: Record<string, string> = {
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
}
8 changes: 7 additions & 1 deletion src/server/controller/cycleData/getBulkDownload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<string, string>>): string => {
if (!arr.length) return ''
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 24d8fc4

Please sign in to comment.