-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
4000 - Dashboard cycle 2029: dynamic metadata (#4017)
* 4000 - Dashboard metadata migration to node ext (#4015) * 4000 - Dashboard metadata migration to node ext * type camel case * update nodeExtType: dataquery -> dashboard * 4000 - Dashboard API (#4016) * 4000 - Initial commit for data query API * 4000 - Move api to Cycle level * remove unused type * 4000 - Dashboard store (#4018) * 4000 - Dashboard store * 4000 - repository: return in correct format * 4000 - Overview: use dashboard store * 4000 - return result * 4000 - Utils for object manipulation (#4020) * 4000 - introduce new object utils mergePartial, getDiffAsPartialObject.ts * 4000 - use lodash.merge * 4000 - getDiffAsPartialObject -> getDiff * 4000 - Migrate dashboard region partial data (pending) (#4021) * 4000 - restore original metadata * 4000 - dashboard: migrate partial metadata for regions * use correct function call * 4000 - dashboard API: merge region data (#4022) * 4000 - Remove unused files (#4027) * 4000 - dashboard: refetch even when existing items (region) * remove unused files * region/country dashboard in store * 4000 - code quality * Fix typo in import
- Loading branch information
Showing
35 changed files
with
380 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { createAsyncThunk } from '@reduxjs/toolkit' | ||
import axios from 'axios' | ||
|
||
import { ApiEndPoint } from 'meta/api/endpoint' | ||
import { AreaCode } from 'meta/area' | ||
import { AssessmentName, CycleName } from 'meta/assessment' | ||
import { DashboardItem } from 'meta/dashboard' | ||
|
||
type Returned = Array<DashboardItem> | ||
|
||
type Props = { | ||
assessmentName: AssessmentName | ||
cycleName: CycleName | ||
countryIso: AreaCode | ||
} | ||
|
||
export const getDashboard = createAsyncThunk<Returned, Props>('metadata/dashboard/get', async (props) => { | ||
const params = { ...props } | ||
const { data } = await axios.get(ApiEndPoint.CycleData.Dashboard.one(), { params }) | ||
return data | ||
}) |
16 changes: 16 additions & 0 deletions
16
src/client/store/metadata/extraReducers/getDashboardReducer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { ActionReducerMapBuilder } from '@reduxjs/toolkit' | ||
import { Objects } from 'utils/objects' | ||
|
||
import { Areas } from 'meta/area' | ||
|
||
import { getDashboard } from 'client/store/metadata/actions/getDashboard' | ||
import { DashboardAreaType, MetadataState } from 'client/store/metadata/state' | ||
|
||
export const getDashboardReducer = (builder: ActionReducerMapBuilder<MetadataState>): void => { | ||
builder.addCase(getDashboard.fulfilled, (state, action) => { | ||
const { assessmentName, cycleName, countryIso } = action.meta.arg | ||
const key = Areas.isISOCountry(countryIso) ? DashboardAreaType.Country : DashboardAreaType.Region | ||
|
||
Objects.setInPath({ obj: state.dashboard, path: [assessmentName, cycleName, key], value: action.payload }) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Areas } from 'meta/area' | ||
import { DashboardItem } from 'meta/dashboard' | ||
|
||
import { MetadataSelectors } from 'client/store/metadata/selectors' | ||
import { useAppSelector } from 'client/store/store' | ||
import { useCountryRouteParams } from 'client/hooks/useRouteParams' | ||
|
||
import { DashboardAreaType } from '../state' | ||
|
||
export const useDashboardItems = (): Array<DashboardItem> => { | ||
const { assessmentName, cycleName, countryIso } = useCountryRouteParams() | ||
const key = Areas.isISOCountry(countryIso) ? DashboardAreaType.Country : DashboardAreaType.Region | ||
return useAppSelector((state) => MetadataSelectors.getDashboard(state, assessmentName, cycleName, key)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useEffect } from 'react' | ||
|
||
import { useAppDispatch } from 'client/store' | ||
import { useCountryRouteParams } from 'client/hooks/useRouteParams' | ||
|
||
import { MetadataActions } from '../slice' | ||
import { useDashboardItems } from './useDashboardItems' | ||
|
||
export const useGetDashboard = () => { | ||
const dispatch = useAppDispatch() | ||
const { assessmentName, cycleName, countryIso } = useCountryRouteParams() | ||
const dashboardItems = useDashboardItems() | ||
|
||
useEffect(() => { | ||
if (!dashboardItems) { | ||
dispatch(MetadataActions.getDashboard({ assessmentName, cycleName, countryIso })) | ||
} | ||
}, [assessmentName, cycleName, countryIso, dispatch, dashboardItems]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
export { useDashboardItems } from './hooks/useDashboardItems' | ||
export { useGetDashboard } from './hooks/useGetDashboard' | ||
export { useGetTableSections } from './hooks/useGetTableSections' | ||
export { usePreviousSection, useSection, useSections } from './hooks/useSections' | ||
export { useTableSections, useTableSectionsCycle } from './hooks/useTableSections' | ||
export { MetadataSelectors } from './selectors' | ||
export { MetadataActions } from './slice' | ||
export type { MetadataState } from './state' | ||
export { DashboardAreaType } from './state' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,28 @@ | ||
import { AssessmentName, CycleName, Section, TableSection } from 'meta/assessment' | ||
import { SectionName } from 'meta/assessment/section' | ||
import { DashboardItem } from 'meta/dashboard' | ||
|
||
export enum DashboardAreaType { | ||
Region = 'region', | ||
Country = 'country', | ||
} | ||
|
||
type DashboardState = Record< | ||
AssessmentName, | ||
Record< | ||
CycleName, | ||
{ [DashboardAreaType.Region]?: Array<DashboardItem>; [DashboardAreaType.Country]?: Array<DashboardItem> } | ||
> | ||
> | ||
|
||
export interface MetadataState { | ||
sections: Record<AssessmentName, Record<CycleName, Array<Section>>> | ||
tableSections: Record<AssessmentName, Record<CycleName, Record<SectionName, Array<TableSection>>>> | ||
dashboard: DashboardState | ||
} | ||
|
||
export const initialState: MetadataState = { | ||
sections: {}, | ||
tableSections: {}, | ||
dashboard: {}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export type { DashboardItem, DashboardTable } from './dashboard' | ||
export type { DashboardBarChart, DashboardItem, DashboardPieChart, DashboardTable } from './dashboard' | ||
export { DashboardItemType } from './dashboard' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Response } from 'express' | ||
|
||
import { CycleRequest } from 'meta/api/request' | ||
|
||
import { AssessmentController } from 'server/controller/assessment' | ||
import { DashboardController } from 'server/controller/cycleData/dashboard' | ||
import Requests from 'server/utils/requests' | ||
|
||
export const getDashboardItems = async (req: CycleRequest, res: Response) => { | ||
try { | ||
const { assessmentName, cycleName, countryIso } = req.query | ||
|
||
const { assessment, cycle } = await AssessmentController.getOneWithCycle({ assessmentName, cycleName }) | ||
|
||
const result = await DashboardController.getManyItems({ assessment, cycle, countryIso }) | ||
Requests.send(res, result) | ||
} catch (e) { | ||
Requests.sendErr(res, e) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Objects } from 'utils/objects' | ||
|
||
import { AreaCode, Areas } from 'meta/area' | ||
import { Assessment, Cycle } from 'meta/assessment' | ||
import { DashboardItem, DashboardItemType } from 'meta/dashboard' | ||
|
||
import { NodeExtRepository } from 'server/repository/assessmentCycle/nodeExt' | ||
|
||
type Props = { | ||
assessment: Assessment | ||
cycle: Cycle | ||
countryIso: AreaCode | ||
} | ||
|
||
export const getManyItems = async (props: Props): Promise<Array<DashboardItem<DashboardItemType>>> => { | ||
const { assessment, cycle, countryIso } = props | ||
const isISOCountry = Areas.isISOCountry(countryIso) | ||
const countryDashboardItems = await NodeExtRepository.getManyDashboardItems({ assessment, cycle }) | ||
if (isISOCountry) return countryDashboardItems | ||
|
||
const regionDashboardItems = await NodeExtRepository.getManyDashboardItems({ assessment, cycle, region: true }) | ||
|
||
return Objects.merge(countryDashboardItems, regionDashboardItems) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { getManyItems } from './getManyItems' | ||
|
||
export const DashboardController = { | ||
getManyItems, | ||
} |
29 changes: 29 additions & 0 deletions
29
src/server/repository/assessmentCycle/nodeExt/getManyDashboardItems.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Assessment, Cycle } from 'meta/assessment' | ||
import { DashboardItem, DashboardItemType } from 'meta/dashboard' | ||
import { NodeExtType } from 'meta/nodeExt' | ||
|
||
import { BaseProtocol, DB, Schemas } from 'server/db' | ||
|
||
type Props = { assessment: Assessment; cycle: Cycle; region?: boolean } | ||
|
||
export const getManyDashboardItems = async ( | ||
props: Props, | ||
client: BaseProtocol = DB | ||
): Promise<Array<DashboardItem<DashboardItemType>>> => { | ||
const { assessment, cycle, region } = props | ||
const schemaCycle = Schemas.getNameCycle(assessment, cycle) | ||
return client.one<Array<DashboardItem<DashboardItemType>>>( | ||
` | ||
select value | ||
from ${schemaCycle}.node_ext | ||
where type = $1 | ||
${ | ||
region | ||
? `and (props->>'region')::boolean = true` | ||
: `and (props->>'region' is null or (props->>'region')::boolean = false)` | ||
} | ||
`, | ||
[NodeExtType.dashboard], | ||
(result) => result.value | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { getManyContacts } from './getManyContacts' | ||
import { getManyDashboardItems } from './getManyDashboardItems' | ||
import { removeContact } from './removeContact' | ||
import { upsert } from './upsert' | ||
|
||
export const NodeExtRepository = { | ||
getManyContacts, | ||
getManyDashboardItems, | ||
removeContact, | ||
upsert, | ||
} |
Oops, something went wrong.