Skip to content

Commit

Permalink
Merge pull request #1656 from rodekruis/fix.percent-compact
Browse files Browse the repository at this point in the history
  • Loading branch information
gulfaraz authored Oct 21, 2024
2 parents e5b3f5b + 2c4117e commit 7c9e26a
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 50 deletions.
3 changes: 2 additions & 1 deletion interfaces/IBF-dashboard/src/app/pipes/compact.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export class CompactPipe implements PipeTransform {
}

const style = format === NumberFormat.perc ? 'percent' : 'decimal';
const maximumSignificantDigits = value > 100 ? 2 : 1;
const maximumSignificantDigits =
value > 100 || format === NumberFormat.perc ? 2 : 1;

let min = 0;
let prefix = '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ export class MjmlService {

children.push(this.mailOpening);

children.push(
...getMjmlEventListBody(emailContent, this.helperService.toCompactNumber),
);
children.push(...getMjmlEventListBody(emailContent));

children.push(
this.notificationAction({
Expand Down
28 changes: 2 additions & 26 deletions services/API-service/src/api/notification/email/mjml/body-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
getTextElement,
getTimeFromNow,
getTimezoneDisplay,
getTotalAffected,
getTriangleIcon,
} from '../../helpers/mjml.helper';

Expand All @@ -23,15 +22,12 @@ const getMjmlBodyEvent = ({
firstLeadTimeString,
firstTriggerLeadTimeFromNow,
firstTriggerLeadTimeString,
indicatorLabel,
issuedDate,
nrOfTriggeredAreas,
timeZone,
totalAffected,
triangleIcon,
eapLink,
triggerStatusLabel,
toCompactNumber,
}: {
color: string;
defaultAdminAreaLabel: string;
Expand All @@ -42,15 +38,12 @@ const getMjmlBodyEvent = ({
firstLeadTimeString: string;
firstTriggerLeadTimeFromNow: string;
firstTriggerLeadTimeString: string;
indicatorLabel: string;
issuedDate: string;
nrOfTriggeredAreas: number;
timeZone: string;
totalAffected: number;
triangleIcon: string;
eapLink: string;
triggerStatusLabel: string;
toCompactNumber: (value: number) => string;
}): object => {
const icon = getInlineImage({ src: triangleIcon, size: 16 });

Expand Down Expand Up @@ -82,16 +75,7 @@ const getMjmlBodyEvent = ({

contentContent.push(
`<strong>Expected exposed ${defaultAdminAreaLabel}:</strong> ${nrOfTriggeredAreas} (see list below)`,
),
contentContent.push(
`<strong>${indicatorLabel}:</strong> ${
totalAffected
? `Approximately ${toCompactNumber(
totalAffected,
)} ${indicatorLabel.toLowerCase()}`
: 'Information is unavailable'
}`,
);
);

contentContent.push(
triggerStatusLabel === TriggerStatusLabelEnum.Trigger
Expand All @@ -117,10 +101,7 @@ const getMjmlBodyEvent = ({
});
};

export const getMjmlEventListBody = (
emailContent: ContentEventEmail,
toCompactNumber: (value: number) => string,
): object[] => {
export const getMjmlEventListBody = (emailContent: ContentEventEmail) => {
const eventList = [];

for (const event of emailContent.dataPerEvent) {
Expand All @@ -147,11 +128,6 @@ export const getMjmlEventListBody = (
defaultAdminAreaLabel:
emailContent.defaultAdminAreaLabel.plural.toLocaleLowerCase(),

// Indicator details
indicatorLabel: emailContent.indicatorMetadata.label,
totalAffected: getTotalAffected(event),
toCompactNumber: toCompactNumber,

// EAP details
triangleIcon: getTriangleIcon(
event.eapAlertClass?.key,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { NumberFormat } from '../../../../shared/enums/number-format.enum';
import { IndicatorMetadataEntity } from '../../../metadata/indicator-metadata.entity';
import { AdminAreaLabel } from '../../dto/admin-area-notification-info.dto';
import { ContentEventEmail } from '../../dto/content-trigger-email.dto';
Expand Down Expand Up @@ -30,7 +31,7 @@ const getMjmlEventAdminAreaTable = ({
indicatorMetadata: IndicatorMetadataEntity;
event: NotificationDataPerEventDto;
triangleIcon: string;
toCompactNumber: (value: number) => string;
toCompactNumber: (value: number, format: NumberFormat) => string;
}) => {
const icon = getInlineImage({ src: triangleIcon, size: 16 });

Expand Down Expand Up @@ -68,7 +69,10 @@ const getMjmlEventAdminAreaTable = ({
.filter(({ actionsValue }) => actionsValue)
.map((triggeredArea) => {
return {
exposed: toCompactNumber(triggeredArea.actionsValue),
exposed: toCompactNumber(
triggeredArea.actionsValue,
indicatorMetadata.numberFormatMap,
),
name: `${triggeredArea.name} ${
triggeredArea.nameParent ? `(${triggeredArea.nameParent})` : ''
}`,
Expand Down Expand Up @@ -112,7 +116,7 @@ export const getMjmlAdminAreaDisclaimer = (): object => {

export const getMjmlAdminAreaTableList = (
emailContent: ContentEventEmail,
toCompactNumber: (value: number) => string,
toCompactNumber: (value: number, format: NumberFormat) => string,
): object[] => {
const adminAreaTableList = [];

Expand Down
11 changes: 1 addition & 10 deletions services/API-service/src/api/notification/helpers/mjml.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { format } from 'date-fns';
import { EapAlertClassKeyEnum } from '../../../shared/data.model';
import { LeadTime } from '../../admin-area-dynamic-data/enum/lead-time.enum';
import { CountryTimeZoneMapping } from '../../country/country-time-zone-mapping';
import {
NotificationDataPerEventDto,
TriggerStatusLabelEnum,
} from '../dto/notification-date-per-event.dto';
import { TriggerStatusLabelEnum } from '../dto/notification-date-per-event.dto';

interface AdminArea {
exposed?: string;
Expand Down Expand Up @@ -278,12 +275,6 @@ export const getTimeFromNow = (leadTime: LeadTime) => {
} from now`;
};

export const getTotalAffected = (
event: NotificationDataPerEventDto,
): number | null => {
return event.totalAffectedOfIndicator ?? null;
};

export const getTriangleIcon = (
eapAlertClassKey: EapAlertClassKeyEnum,
triggerStatusLabel: TriggerStatusLabelEnum,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

import { EventSummaryCountry, TriggeredArea } from '../../../shared/data.model';
import { NumberFormat } from '../../../shared/enums/number-format.enum';
import { HelperService } from '../../../shared/helper.service';
import { LeadTime } from '../../admin-area-dynamic-data/enum/lead-time.enum';
import { CountryEntity } from '../../country/country.entity';
Expand Down Expand Up @@ -194,7 +195,11 @@ export class NotificationContentService {
disasterType,
);

data.totalAffectedOfIndicator = this.getTotalAffected(data.triggeredAreas);
const indicatorMetadata = await this.getIndicatorMetadata(disasterType);
data.totalAffectedOfIndicator = this.getTotal(
data.triggeredAreas,
indicatorMetadata.numberFormatMap,
);
data.eapAlertClass = event.disasterSpecificProperties?.eapAlertClass;
return data;
}
Expand Down Expand Up @@ -261,12 +266,22 @@ export class NotificationContentService {
});
}

private getTotalAffected(triggeredAreas: TriggeredArea[]) {
return parseFloat(
triggeredAreas
.reduce((acc, triggeredArea) => acc + triggeredArea.actionsValue, 0)
.toFixed(2),
private getTotal(
triggeredAreas: TriggeredArea[],
numberFormat: NumberFormat,
) {
const total = triggeredAreas.reduce(
(acc, { actionsValue }) => acc + actionsValue,
0,
);

if (numberFormat === NumberFormat.perc) {
// return average for percentage
return total / triggeredAreas.length;
}

// return sum
return total;
}

private async getFirstLeadTimeDate(
Expand Down
3 changes: 2 additions & 1 deletion services/API-service/src/shared/helper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ export class HelperService {
}

const style = format === NumberFormat.perc ? 'percent' : 'decimal';
const maximumSignificantDigits = value > 100 ? 2 : 1;
const maximumSignificantDigits =
value > 100 || format === NumberFormat.perc ? 2 : 1;

let min = 0;
let prefix = '';
Expand Down

0 comments on commit 7c9e26a

Please sign in to comment.