Skip to content

Commit

Permalink
Add village
Browse files Browse the repository at this point in the history
  • Loading branch information
ericboucher committed Sep 22, 2024
1 parent 991f9a9 commit 01bde4e
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 3 deletions.
3 changes: 2 additions & 1 deletion apps/frontend/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,8 @@
"COMMON": {
"column": {
"district": "District",
"commune": "Commune"
"commune": "Commune",
"village": "Villages"
}
}
},
Expand Down
6 changes: 6 additions & 0 deletions apps/frontend/utils/aggregate/aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { countAgg } from './countAgg';
import { countCategoriesAgg } from './countCategoriesAgg';
import { countMultipleChoicesAgg } from './countMultipleChoicesAgg';
import { firstAgg } from './firstAgg';
import { getUniqueValuesSet } from './setAgg';
import { sumAgg } from './sumAgg';

interface IProps {
Expand All @@ -14,6 +15,7 @@ interface IProps {
countKeys?: string[];
countCategoriesKeys?: string[];
countMultipleChoicesKeys?: string[];
setAggKeys?: string[];
}

export const aggregate = ({
Expand All @@ -24,6 +26,7 @@ export const aggregate = ({
countKeys = [],
countCategoriesKeys = [],
countMultipleChoicesKeys = [],
setAggKeys = [],
}: IProps) => {
const groupedData = groupBy(data, groupKey);
const aggregatedData = map(groupedData, (array, keyValue) => {
Expand All @@ -37,13 +40,16 @@ export const aggregate = ({
countMultipleChoicesAgg(key, array),
);

const setAggValues = setAggKeys.map(key => getUniqueValuesSet(key, array));

return assign(
{ [groupKey]: keyValue },
...firstValues,
...sumValues,
...countValues,
...countCategoriesValues,
...countMultipleChoicesValues,
...setAggValues,
) as Record<string, string | number | undefined>;
});

Expand Down
3 changes: 3 additions & 0 deletions apps/frontend/utils/aggregate/generateFloodReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const sumKeys = Object.values(

const countCategoriesKeys = [FloodSpecific.RicePrice];
const countMultipleChoicesKeys = [FloodSpecific.threat];
const setAggKeys = [KoboCommonKeys.village];

export const generateFloodCommuneLevelReport = (
data: Record<string, string | undefined>[],
Expand All @@ -37,6 +38,7 @@ export const generateFloodCommuneLevelReport = (
sumKeys,
countCategoriesKeys,
countMultipleChoicesKeys,
setAggKeys,
});
};

Expand All @@ -57,5 +59,6 @@ export const generateFloodProvinceLevelReport = (
countKeys: provinceLevelReportCountKeys,
countCategoriesKeys,
countMultipleChoicesKeys,
setAggKeys,
});
};
17 changes: 17 additions & 0 deletions apps/frontend/utils/aggregate/setAgg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { compact, flatten } from 'lodash';

/**
* Parses space-separated strings and returns an object with the key and a Set of unique values.
* @param key The key to look for in the group objects
* @param group An array of objects containing the key
* @returns An object with the key and an array of unique values parsed from the space-separated strings
*/
export const getUniqueValuesSet = (
key: string,
group: Record<string, string | undefined>[],
): { [key: string]: string[] | undefined } => {
const allValues = compact(flatten(group.map(x => x[key]?.split(' '))));
const uniqueValues = Array.from(new Set(allValues));

return { [key]: uniqueValues.length === 0 ? undefined : uniqueValues };
};
2 changes: 2 additions & 0 deletions apps/frontend/utils/formatFormToRaw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export const formatFormToRaw = (
province: formValues.region.province[0],
district: formValues.region.district[0],
commune: formValues.region.commune[0],
// TODO - FIXME: This is just a placeholder for the village field
village: 'test',
// TODO - FIXME: This is just a placeholder for the location field
location: formValues.region.commune[0],
entryName: formValues.interviewer,
Expand Down
26 changes: 25 additions & 1 deletion apps/frontend/utils/tableFormatting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ const getLocationCountColumnSetup = (
<FormattedMessage id={`table.${disaster}.column.${params.field}`} />
</Typography>
),
// For villages, we need to count the number of villages in the list
...(field === KoboCommonKeys.village
? {
renderCell: (params: GridRenderCellParams) => {
const villageList = params.value as string[] | undefined;

return villageList ? villageList.length : 0;
},
}
: {}),
});

export const getGroupSetup = (groupId: string, disaster: DisasterType) => ({
Expand All @@ -123,10 +133,20 @@ const addGroup = (
children: GridColumnNode[],
groupParams?: ColumnSetupParams,
) => {
console.log({ children, addChill: groupParams?.additionalChildren });
const group: GridColumnGroup | undefined = groupParams
? {
...getGroupSetup(groupParams.groupId, groupParams.disaster),
children: [...children, ...groupParams.additionalChildren],
children: [
...children,
// Only add KoboCommonKeys.village if not already present
...(children.some(
child => 'field' in child && child.field === KoboCommonKeys.village,
)
? []
: [{ field: KoboCommonKeys.village as string } as GridColumnNode]),
...groupParams.additionalChildren,
],
}
: undefined;

Expand Down Expand Up @@ -207,6 +227,8 @@ export const addCommuneLevelReportLocationColumns = ({
);
},
},
// What to do in detailed commune level report?
getLocationCountColumnSetup(KoboCommonKeys.village as string, 'COMMON', 72),
...columns,
];

Expand Down Expand Up @@ -240,6 +262,7 @@ export const addProvinceLevelReportLocationColumns = ({
getLocationColumnSetup(KoboCommonKeys.province, 200),
getLocationCountColumnSetup(KoboCommonKeys.district, 'COMMON', 72),
getLocationCountColumnSetup(KoboCommonKeys.commune, 'COMMON', 84),
getLocationCountColumnSetup(KoboCommonKeys.village as string, 'COMMON', 72),
...columns,
];

Expand All @@ -249,6 +272,7 @@ export const addProvinceLevelReportLocationColumns = ({
{ field: KoboCommonKeys.province },
{ field: KoboCommonKeys.district },
{ field: KoboCommonKeys.commune },
{ field: KoboCommonKeys.village as string },
],
groupParams,
);
Expand Down
2 changes: 2 additions & 0 deletions packages/interfaces/src/kobo/DroughtDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export class DroughtDto {
@IsString() @Length(2) readonly 'group_yu9nq00/Province'!: string;
@IsString() @Length(4) readonly 'group_yu9nq00/District'!: string;
@IsString() @Length(6) readonly 'group_yu9nq00/Commune'!: string;
// NOTE - This field does not exist in KOBO yet.
@IsString() readonly 'group_yu9nq00/Village'?: string;
@IsDateString() readonly 'group_yu9nq00/Date_Dis': string;
@IsNumberString() readonly 'group_yu9nq00/DisTyp'!: string;
@IsOptional() @IsNumberString() readonly 'group_dg01m69/NumVillAff'?: string;
Expand Down
1 change: 1 addition & 0 deletions packages/interfaces/src/kobo/FloodDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class FloodDto {
@IsString() @Length(2) readonly 'g2/Province'!: string;
@IsString() @Length(4) readonly 'g2/District'!: string;
@IsString() @Length(6) readonly 'g2/Commune'!: string;
@IsString() readonly 'g2/village'?: string;
@IsDateString() readonly 'g2/Date_Dis'!: string;
@IsNumberString() readonly 'g2/DisTyp'!: string;
@IsOptional() @IsNumberString() readonly 'g2/flood_n'?: string;
Expand Down
1 change: 1 addition & 0 deletions packages/interfaces/src/kobo/IncidentDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class IncidentDto {
@IsString() @Length(2) readonly 'G2/Province'!: string;
@IsString() @Length(4) readonly 'G2/District'!: string;
@IsString() @Length(6) readonly 'G2/Commune'!: string;
@IsString() readonly 'G2/Village'?: string;
@IsOptional() @IsNumberString() readonly 'group_tc1fy38/group_pf1pd97/NumVillAff'?: string;
@IsOptional() @IsNumberString() readonly 'group_tc1fy38/group_pf1pd97/NumFamAff'?: string;
@IsOptional() @IsNumberString() readonly 'group_tc1fy38/group_pf1pd97/NumPeoAff'?: string;
Expand Down
6 changes: 5 additions & 1 deletion packages/interfaces/src/kobo/mapping/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { DROUGHT, FLOOD, INCIDENT } from '../constants';
import { floodSpecificKeys } from './flood';

export enum KoboCommonKeys {
province = 'province',
district = 'district',
commune = 'commune',
province = 'province',
village = 'village',
disasterDate = 'disasterDate',
entryName = 'entryName',
phone = 'phone',
Expand All @@ -21,6 +22,7 @@ export const koboKeys = {
[KoboCommonKeys.district]: 'g2/District',
[KoboCommonKeys.commune]: 'g2/Commune',
[KoboCommonKeys.province]: 'g2/Province',
[KoboCommonKeys.village]: 'g2/village',
[KoboCommonKeys.disasterDate]: 'g2/Date_Dis',
[KoboCommonKeys.entryName]: 'g1/q_Enum',
[KoboCommonKeys.phone]: 'g1/q_Phone',
Expand All @@ -35,6 +37,7 @@ export const koboKeys = {
[KoboCommonKeys.district]: 'group_yu9nq00/District',
[KoboCommonKeys.commune]: 'group_yu9nq00/Commune',
[KoboCommonKeys.province]: 'group_yu9nq00/Province',
[KoboCommonKeys.village]: 'group_yu9nq00/Village',
[KoboCommonKeys.disasterDate]: 'group_yu9nq00/Date_Dis',
[KoboCommonKeys.entryName]: 'group_ve4vz14/q_Enum',
[KoboCommonKeys.phone]: 'group_ve4vz14/q_Phone',
Expand All @@ -48,6 +51,7 @@ export const koboKeys = {
[KoboCommonKeys.district]: 'G2/District',
[KoboCommonKeys.commune]: 'G2/Commune',
[KoboCommonKeys.province]: 'G2/Province',
[KoboCommonKeys.village]: 'G2/Village',
[KoboCommonKeys.disasterDate]: 'G2/Date_Dis',
[KoboCommonKeys.entryName]: 'G1/q_Enum',
[KoboCommonKeys.phone]: 'G1/q_Phone',
Expand Down
3 changes: 3 additions & 0 deletions packages/interfaces/src/kobo/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const formatCommonFields = (form: DisasterDtoType) => {
[KoboCommonKeys.province]: form[keys.province],
[KoboCommonKeys.district]: form[keys.district],
[KoboCommonKeys.commune]: form[keys.commune],
[KoboCommonKeys.village]: form[keys.village],
[KoboCommonKeys.disasterDate]: form[keys.disasterDate],
[KoboCommonKeys.disTyp]: form[keys.disTyp],
[KoboCommonKeys.entryName]: form[keys.entryName],
Expand All @@ -71,6 +72,7 @@ export const formatCommonFields = (form: DisasterDtoType) => {
[KoboCommonKeys.province]: form[keys.province],
[KoboCommonKeys.district]: form[keys.district],
[KoboCommonKeys.commune]: form[keys.commune],
[KoboCommonKeys.village]: form[keys.village],
[KoboCommonKeys.disasterDate]: form[keys.disasterDate],
[KoboCommonKeys.disTyp]: form[keys.disTyp],
[KoboCommonKeys.entryName]: form[keys.entryName],
Expand All @@ -90,6 +92,7 @@ export const formatCommonFields = (form: DisasterDtoType) => {
[KoboCommonKeys.province]: form[keys.province],
[KoboCommonKeys.district]: form[keys.district],
[KoboCommonKeys.commune]: form[keys.commune],
[KoboCommonKeys.village]: form[keys.village],
[KoboCommonKeys.disasterDate]: form[keys.disasterDate],
[KoboCommonKeys.disTyp]: form[keys.disTyp],
[KoboCommonKeys.entryName]: form[keys.entryName],
Expand Down

0 comments on commit 01bde4e

Please sign in to comment.